.NET

Reply
Contributor
vinayan
Posts: 13
Registered: ‎05-17-2009
Message 1 of 3 (408 Views)
Accepted Solution

Adding a Vertex to a closed Polyline

408 Views, 2 Replies
08-13-2013 04:30 AM

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

 

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

 

*Expert Elite*
Jeff_M
Posts: 4,521
Registered: ‎07-22-2003
Message 2 of 3 (363 Views)

Re: Adding a Vertex to a closed Polyline

08-15-2013 01:59 PM 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
Contributor
vinayan
Posts: 13
Registered: ‎05-17-2009
Message 3 of 3 (313 Views)

Re: Adding a Vertex to a closed Polyline

08-20-2013 03:43 AM in reply to: vinayan

Jeff,

 

That worked out fine!

thanks a lot

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.