Search code examples
c#3dautocadpolylinevertex

Changing the z-coordinate of one of vertex in Polyline3d


Please somebody help. Why it does not work? I have Polyline3d in AutoCAD and the target coordinations to move one of vertex on it. But is does not work. No error, all is clear. But vertex does not move to target pozition. Where am i mistake?


private void MoveOneVertex(Polyline3d poly3d, Point3d old_pozition, Point3d new_pozition)
        {
            Document doc = AuAcApSerApp.DocumentManager.MdiActiveDocument;
            Database db  = doc.Database;
            Editor   edt = doc.Editor;           

            using (var trans = db.TransactionManager.StartTransaction())
            {
                try
                {
                    //Sorting through all vertexes of polyline
                    ObjectId[] verts = poly3d.Cast<ObjectId>().ToArray();

                    for (int i = 0; i < verts.Length; i++)
                    {
                        PolylineVertex3d vertex = trans.GetObject(verts[i], OpenMode.ForWrite) as PolylineVertex3d;

                        if (vertex.Position == old_pozition)
                        {                           
                            vertex.Position = new_pozition; //moving vertex to new pozition                                                     
                        } 
                    }

                    trans.Commit();
                }
                catch (System.Exception ex)
                {
                    edt.WriteMessage("\nError encountered " + ex.Message);
                    trans.Abort();
                }
            }           
        }


Solution

  • I suppose condition if (vertex.Position == old_pozition) is a problem.

    try this:

    double PRECISION = 0.000001; // or other precision as You need
    if ( vertex.Position.DistanceTo(old_pozition) < PRECISION)
    

    You passed the polyline as a parameter but is the polyline open for writing somewhere before?

    Polyline3d poly3d = tr.GetObject(res.ObjectId , OpenMode.ForWrite) as Polyline3d;