Converting a Polyline2d to Polyline

By Virupaksha Aithal

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);
             }
         }
     }
 }
}

Comments

7 responses to “Converting a Polyline2d to Polyline”

  1. Artvegas Avatar
    Artvegas

    What about using the OpenCloseTransaction class instead?
    using (OpenCloseTransaction tr = db.TransactionManager.StartOpenCloseTransaction())
    {
    Polyline2d poly2d = (Polyline2d)tr.GetObject(res.ObjectId, OpenMode.ForWrite);
    poly.ConvertFrom(poly2d, true);
    tr.Commit();
    }
    This is how Kean did it a while back:
    http://through-the-interface.typepad.com/through_the_interface/2010/07/shortening-a-set-of-autocad-lines-using-net.html

  2. Artvegas Avatar
    Artvegas

    Actually after a little more experimenting I found that the OpenCloseTransaction causes some other issues with the conversion. In particular the converted polyline couldn’t be selected, so I’ll go with what you have here.

  3. Artvegas Avatar
    Artvegas

    OpenCloseTransaction is now working for me. You just have to make sure you add the newly created polylines to the transaction using AddNewlyCreatedDBObject(). At the end of the day I get the same result.

  4. Thanks. your comments will help anyone how wants to use OpenCloseTransaction.
    -Viru

  5. Antti Karanta Avatar
    Antti Karanta

    The given code works if the polyline2d is of Poly2dType.SimplePoly or Poly2dType.FitCurvePoly. However, it fails with eNotApplicable if the type is Poly2dType.CubicSplinePoly (and I would imagine the same would happen with Poly2dType.QuadSplinePoly). From ConvertFrom docs: “entity must point to a SimplePoly or FitCurvePoly type of Polyline2d”.
    Any ideas on how to convert CubicSplinePoly or QuadSplinePoly to Polylines? Is it possible?

  6. Hi Viru, I am getting a eInvalidInput at the poly.ConvertFrom line. I am supplying it a 2D Polyline, with true flag as shown in your example. I also copied your code into a separate module to see if it works, but it fails wit the same error. Any Idea why or how I can do this?

  7. Well, My Bad. The object had Xdata.. Precisely what you advised!

Leave a Reply to Shinu MathewCancel reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading