For converting Polyline2d to Polyline, you can use Polyline.ConvertFrom API. But this API throws the eInvalidContext exception when the second argument for ConvertFrom() is set to true, inside the transaction. The true argument will handover the same object Id's to the new entity. This is fine except for a rule that AcDbObject::handOverTo() (defined in ObjectARX) cannot be used inside of a transaction. So, to solve issue, use Open and Close technology as shown in below code.
[CommandMethod("TestConvertPoly")]
public void testConvertPoly()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
// select the entity
PromptEntityResult res = ed.GetEntity("Select PolyLine: ");
// if ok
if (res.Status == PromptStatus.OK)
{
// use the using keyword so that the objects auto-dispose
//(close) at the end of the brace
// open for write otherwise ConvertFrom will fail
using (Entity ent =
(Entity)res.ObjectId.Open(OpenMode.ForRead))
{
// if it's a polyline2d
if (ent is Polyline2d)
{
Polyline2d poly2d = (Polyline2d)ent;
poly2d.UpgradeOpen();
// again use the using keyword to ensure auto-closing
using (Autodesk.AutoCAD.DatabaseServices.Polyline poly
= new Autodesk.AutoCAD.DatabaseServices.Polyline())
{
poly.ConvertFrom(poly2d, true);
}
}
}
}
}