Insert vertex to polyline

Insert vertex to polyline

giskumar
Enthusiast Enthusiast
3,801 Views
8 Replies
Message 1 of 9

Insert vertex to polyline

giskumar
Enthusiast
Enthusiast

Hi all,

 

I am new to .net environment in cad. I am using autocad 2006 with dotnet 2005.

I have written the following code to insert a new vertex at selected location on polyline.

But it is getting error.

Please help me how to work out this.

 

[CommandMethod("iv")]
        public static void addplyver()
        {
            // Get the current document and database
            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            Editor ed = acDoc.Editor;
            int chk = 1;


            PromptEntityOptions ets = new PromptEntityOptions("\nSelect entity to Insert vertex");
            PromptEntityResult ets1 = ed.GetEntity(ets);

            if (ets1.Status == PromptStatus.OK)
            {
                Transaction tr = acCurDb.TransactionManager.StartTransaction();
                using (tr)
                {
                    DBObject obj = tr.GetObject(ets1.ObjectId, OpenMode.ForWrite);

                    Polyline lws = obj as Polyline;
                    if (lws != null)
                    {
                        Point3d pnt1 = ed.Snap("Nea", ets1.PickedPoint);
                        Plane pl = new Plane(pnt1, lws.Normal);
                       
                        for (int i = 0; (i <= (lws.NumberOfVertices - 1)); i++)
                        {
                            if ((lws.GetDistAtPoint(pnt1) < (lws.GetDistAtPoint(lws.GetPoint3dAt(i)))) && chk != 0)
                            {
                                lws.AddVertexAt(i, pnt1.Convert2d(pl), 0, 0, 0);
                                chk = 0;
                            }
                        }


                    }
                    tr.Commit();
                }
            }
        }

 

 

 

Thanks in advance,

Kumar.

0 Likes
3,802 Views
8 Replies
Replies (8)
Message 2 of 9

Anonymous
Not applicable

Hi,

 

The way I have always added a vertex to a polyline is to return an array of doubles for the coordinates with polyline.coordinates, then create an array with 3 more elements (one for X,Y and Z obviously), push the data in the array to make space for your vertex at the point in the polyline you want, then input the new coordinate. Then I'd just go polyline.coordinates = newlistofcoordinates.

 

Hope this helps,

0 Likes
Message 3 of 9

giskumar
Enthusiast
Enthusiast

HI,

 

I tried to use polyline.coordinates but i think this is not available for the autocad 2006 version.

Do i need to add any librearies to get polyline.coordinates function?????????

 

The following process some what solved my problem but not completely.

 

I tried, instead of converting 3d point to 2d, i have created a new point using

 

New point2d(point3d.X,point3d.Y);

 

Now i am able to insert the point at selected location on polyline. Still my requirement is not completed.

 

When i tried to insert a point which is far away from the polyline again i am getting error.

 

any help will be appriciated......

 

 

Thanks,

Kumar.

0 Likes
Message 4 of 9

Anonymous
Not applicable

 


@giskumar wrote:

But it is getting error.


What error are you getting?

 

0 Likes
Message 5 of 9

giskumar
Enthusiast
Enthusiast

HI,

 

This is the error i am getting for the following code

 

2394iF772DDE005DE5F63

 

 

        [CommandMethod("iv")]
        public static void addplyver()
        {
            // Get the current document and database
            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            Editor ed = acDoc.Editor;
            int chk = 1;


            PromptEntityOptions ets = new PromptEntityOptions("\nSelect entity to Insert vertex");
            PromptEntityResult ets1 = ed.GetEntity(ets);

            

            if (ets1.Status == PromptStatus.OK)
            {
                Transaction tr = acCurDb.TransactionManager.StartTransaction();
                using (tr)
                {
                    DBObject obj = tr.GetObject(ets1.ObjectId, OpenMode.ForWrite);

                    Polyline lws = obj as Polyline;
                    if (lws != null)
                    {
                        PromptPointOptions tpt = new PromptPointOptions("\nPick New vertex location");
                        PromptPointResult tpt1 = ed.GetPoint(tpt);
                        
                        Point3d pnt = tpt1.Value;
                        Point3d pnt1 = ed.Snap("Nea", ets1.PickedPoint);
                        Point2d pnt2 = new Point2d(pnt.X, pnt.Y);                                                
                        for (int i = 0; (i <= (lws.NumberOfVertices - 1)); i++)
                        {
                            if ((lws.GetDistAtPoint(pnt1) < (lws.GetDistAtPoint(lws.GetPoint3dAt(i)))) && chk != 0)
                            {
                                double px = pnt2.X;
                                double py = pnt2.Y;
                                ed.WriteMessage(px.ToString() + "," + (py.ToString()));
                                lws.AddVertexAt(i, pnt2, 0, 0, 0);
                                chk = 0;

                            }
                        }
                     
                    }
                    tr.Commit();
                }
            }
        }

 

 

If i replace the red color code as follows it is working fine.

 

                        //PromptPointOptions tpt = new PromptPointOptions("\nPick New vertex location");
                        //PromptPointResult tpt1 = ed.GetPoint(tpt);
                        
                        //Point3d pnt = tpt1.Value;
                        Point3d pnt1 = ed.Snap("Nea", ets1.PickedPoint);
                        Point2d pnt2 = new Point2d(pnt1.X, pnt1.Y);

 

Thanks,

 

Kumar.

 

0 Likes
Message 6 of 9

Anonymous
Not applicable

You had to press button Details in this message window in order to locate line of code throwing exception.

0 Likes
Message 7 of 9

giskumar
Enthusiast
Enthusiast

HI,

 

These are the error details

 

 

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> Autodesk.AutoCAD.Runtime.Exception: eInvalidInput
   at Autodesk.AutoCAD.Runtime.Interop.ThrowExceptionForErrorStatus(Int32 errorStatus)
   at Autodesk.AutoCAD.DatabaseServices.Curve.GetDistAtPoint(Point3d point)
   at ClassLibrary2.Class1.addplyver() in D:\sds\ClassLibrary2\ClassLibrary2\Class1.cs:line 107
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
   at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at AcMgCommandClass.Invoke(AcMgCommandClass* , gcroot<System::Reflection::MethodInfo __gc \*>* mi)
   at Autodesk.AutoCAD.Runtime.SEHExceptionFilter.InvokeWorker()
   at Autodesk.AutoCAD.Runtime.ExceptionFilter.Invoke()


Thanks,

Kumar. 

0 Likes
Message 8 of 9

Anonymous
Not applicable

Now you understand that error is in method 

Autodesk.AutoCAD.DatabaseServices.Curve.GetDistAtPoint(Point3d point)

If point is not on curve GetDistAtPoint throwing exception. You have to getting point on curve with metod GetClosestPointTo which return point on curve.

0 Likes
Message 9 of 9

giskumar
Enthusiast
Enthusiast

Hi,

 

Thanks for the reply.

 

I solved this problem by breaking the for loop which iterating through the points, after adding the new vertex to the polyline.

 

Now it is working well.

 

This is the final solution to insert a vertex in a polyline

 

[CommandMethod("iv")]
        public static void addplyver()
        {
            // Get the current document and database
            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            Editor ed = acDoc.Editor;
            int chk = 1;


            PromptEntityOptions ets = new PromptEntityOptions("\nSelect entity to Insert vertex");
            PromptEntityResult ets1 = ed.GetEntity(ets);

            

            if (ets1.Status == PromptStatus.OK)
            {
                Transaction tr = acCurDb.TransactionManager.StartTransaction();
                using (tr)
                {
                    DBObject obj = tr.GetObject(ets1.ObjectId, OpenMode.ForWrite);

                    Polyline lws = obj as Polyline;
                    if (lws != null)
                    {
                        Point3d pnt1 = lws.GetClosestPointTo(ets1.PickedPoint, false);
                        PromptPointOptions tpt = new PromptPointOptions("\nPick New vertex location");
                                                                    
                        for (int i = 0; (i <= (lws.NumberOfVertices - 1)); i++)
                        {
                            if ((lws.GetDistAtPoint(pnt1) < (lws.GetDistAtPoint(lws.GetPoint3dAt(i)))) && chk != 0)
                            {
                                PromptPointResult tpt1 = ed.GetPoint(tpt);
                                Point3d pnt = tpt1.Value;
                                Point2d pnt2 = new Point2d(pnt.X, pnt.Y);
                                double px = pnt2.X;
                                double py = pnt2.Y;
                                ed.WriteMessage(px.ToString() + "," + (py.ToString()));
                                lws.AddVertexAt(i, pnt2, 0, 0, 0);
                                chk = 0;
                                break;                            }
                        }
                     
                    }
                    tr.Commit();
                }
            }
        }

 

Thanks,

Kumar.

0 Likes