.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Adding a Vertex to a closed Polyline

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
vinayan
747 Views, 2 Replies

Adding a Vertex to a closed Polyline

I have a method for inserting a vertex to a polyline. But this does not work for closed polylines for the case shown below. The code fails if the point is on the last edge of the polyline. Can someone look into it?

 

 

public void AddVertexOnPolyline(Point3d addPoint,Polyline editPolyline)
        {
            Document acDoc = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument;
            Editor pEditor = acDoc.Editor;
            int chk = 1;
            try
            {
                for (int i = 0; (i <= (editPolyline.NumberOfVertices - 1)); i++)
                {
                    double dist1 = editPolyline.GetDistAtPoint(addPoint);
                    double dist2 = editPolyline.GetDistAtPoint(editPolyline.GetPoint3dAt(i));

                    if ((editPolyline.GetDistAtPoint(addPoint) < (editPolyline.GetDistAtPoint(editPolyline.GetPoint3dAt(i)))) && chk != 0)
                    {
                        Point2d pnt2 = new Point2d(addPoint.X, addPoint.Y);
                        editPolyline.AddVertexAt(i, pnt2, 0, 0, 0);
                        chk = 0;
                        break;
                    }
                }
            }
            catch (System.Exception ex)
            {
                throw;
            }
        }

 

2 REPLIES 2
Message 2 of 3
Jeff_M
in reply to: vinayan

This works for me. Note that I chose a slightly different method of obtaining the index to add it to.

 

        public void AddVertexOnPolyline(Point3d addPoint, Polyline editPolyline)
        {
            try
            {
                Point3d ptOnPline = editPolyline.GetClosestPointTo(addPoint, false);
                double parm = editPolyline.GetParameterAtPoint(ptOnPline);
                Point2d pnt2 = new Point2d(addPoint.X, addPoint.Y);
                editPolyline.AddVertexAt((int)System.Math.Floor(parm) + 1, pnt2, 0, 0, 0);
            }
            catch (System.Exception ex)
            {
                throw;
            }
        }

 

Jeff_M, also a frequent Swamper
EESignature
Message 3 of 3
vinayan
in reply to: vinayan

Jeff,

 

That worked out fine!

thanks a lot

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost