Jump to content

Distance and azimuth between vertices of a polyline in c#


rodrigo_gcmsoft

Recommended Posts

I have a routine in C # that lists all the vertices of a polyline. I wonder how the distance between each vertex and what angle to the y-axis of the drawing. Anyone have any ideas?

 

 

CODE:

[CommandMethod("listaVertices")]
       static public void ListVertices()
       {
           Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
           Editor ed = doc.Editor;
           Database db = doc.Database;
           PromptEntityResult per = ed.GetEntity("Select a polyline");
           if (per.Status == PromptStatus.OK)
           {
               Transaction tr = db.TransactionManager.StartTransaction();
               using (tr)
               {
                   DBObject obj = tr.GetObject(per.ObjectId, OpenMode.ForRead);
                   // If a "lightweight" (or optimized) polyline
                   Polyline lwp = obj as Polyline;
                   if (lwp != null)
                   {
                       // Use a for loop to get each vertex, one by one
                       int vn = lwp.NumberOfVertices;
                       for (int i = 0; i < vn; i++)
                       {
                           // Could also get the 3D point here
                           Point2d pt = lwp.GetPoint2dAt(i);
                           ed.WriteMessage("\n" + pt.ToString());
                       }
                   }
                   else
                   {
                       // If an old-style, 2D polyline
                       Polyline2d p2d = obj as Polyline2d;
                       if (p2d != null)
                       {
                           // Use foreach to get each contained vertex
                           foreach (ObjectId vId in p2d)
                           {
                               Vertex2d v2d = (Vertex2d)tr.GetObject(vId, OpenMode.ForRead);
                               ed.WriteMessage("\n" + v2d.Position.ToString());
                           }
                       }
                       else
                       {
                           // If an old-style, 3D polyline
                           Polyline3d p3d = obj as Polyline3d;
                           if (p3d != null)
                           {
                               // Use foreach to get each contained vertex
                               foreach (ObjectId vId in p3d)
                               {
                                   PolylineVertex3d v3d = (PolylineVertex3d)tr.GetObject(vId, OpenMode.ForRead);
                                   ed.WriteMessage("\n" + v3d.Position.ToString());
                               }
                           }
                       }
                   }
                   // Committing is cheaper than aborting
                   tr.Commit();
               }
           }
       }

 

Thanks!

Rodrigo

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...