polyline simplification

polyline simplification

mttlp
Enthusiast Enthusiast
1,665 Views
4 Replies
Message 1 of 5

polyline simplification

mttlp
Enthusiast
Enthusiast

hi;

simplification of multi-point drawn polyline

How do you remove the vertex from a polyline?

 

 

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
namespace IterateObjects
{
  public class Commands
  {
    [CommandMethod("PLN")]
    static public void ListVertices()
    {
      Document doc  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 = b.TransactionManager.StartTransaction();
        using (tr)
        {

          DBObject obj = tr.GetObject(per.ObjectId, OpenMode.ForRead);
          Polyline lwp = obj as Polyline;
          if (lwp != null)
          {
            int vn = lwp.NumberOfVertices;
            for (int i = 0; i < vn; i++)
            {
              Point2d pt = lwp.GetPoint2dAt(i);
            }
			// delete vertex
			//How do you remove the vertex from a polyline?
          }
          tr.Commit();
        }
      }
    }
  }
}

 

 

 

 

polyline simplification.png

0 Likes
Accepted solutions (3)
1,666 Views
4 Replies
Replies (4)
Message 2 of 5

_gile
Consultant
Consultant
Accepted solution

Hi,

You can use the Polyline.RemoveVertexAt() method.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 5

mttlp
Enthusiast
Enthusiast

hi gile thank you

couldn't find the vertex between you can help me

0 Likes
Message 4 of 5

_gile
Consultant
Consultant
Accepted solution

You can try something like this:

        [CommandMethod("TEST")]
        public static void Test()
        {
            var doc = AcAp.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;
            var options = new PromptEntityOptions("\nSelect Polyline: ");
            options.SetRejectMessage("\nSelected entity is not a Polyline.");
            options.AddAllowedClass(typeof(Polyline), true);
            var result = ed.GetEntity(options);
            if (result.Status != PromptStatus.OK) 
                return;
            using (var tr = db.TransactionManager.StartTransaction())
            {
                Vector2d? vector = null;
                var pline = (Polyline)tr.GetObject(result.ObjectId, OpenMode.ForWrite);
                for (int i = 0; i < pline.NumberOfVertices; i++)
                {
                    var segmentType = pline.GetSegmentType(i);
                    if (segmentType == SegmentType.Coincident)
                    {
                        pline.RemoveVertexAt(i--);
                    }
                    else if (segmentType == SegmentType.Line)
                    {
                        var direction = pline.GetLineSegment2dAt(i).Direction;
                        if (vector.HasValue && vector.Value.IsCodirectionalTo(direction))
                        {
                            pline.RemoveVertexAt(i--);
                        }
                        else
                        {
                            vector = direction;
                        }
                    }
                    else
                    {
                        vector = null;
                    }
                }
                tr.Commit();
            }
        }

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 5 of 5

mttlp
Enthusiast
Enthusiast
Accepted solution

thank you so much Gile

0 Likes