Testing whether a point is on an AutoCAD polyline using .NET

Occasionally I come across a topic that I'm sure I've addressed in a previous post, but for the life of me I can't track it down. The code in a recent response by Balaji Ramamoorthy, from the DevTech India team, fits firmly into this category: it shows how to iterate through a polyline's segments, testing whether a point is on the polyline.

I did some refactoring of Balaji's code to extract it into a helper function (although not an extension method, which people who use them may prefer), but otherwise this C# code is basically his.

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Geometry;

using Autodesk.AutoCAD.Runtime;

 

namespace PolylineTesting

{

  public class Commands

  {

    [CommandMethod("POP")]

    public void PointOnPolyline()

    {

      Document doc =

        Application.DocumentManager.MdiActiveDocument;

      Database db = doc.Database;

      Editor ed = doc.Editor;

 

      PromptEntityOptions peo =

        new PromptEntityOptions("\nSelect a polyline");

      peo.SetRejectMessage("Please select a polyline");

      peo.AddAllowedClass(typeof(Polyline), true);

 

      PromptEntityResult per = ed.GetEntity(peo);

      if (per.Status != PromptStatus.OK)

        return;

 

      PromptPointResult ppr = ed.GetPoint("\nSelect a point");

      if (ppr.Status != PromptStatus.OK)

        return;

 

      Transaction tr = db.TransactionManager.StartTransaction();

      using (tr)

      {

        Polyline polyline =

          tr.GetObject(per.ObjectId, OpenMode.ForRead) as Polyline;

     
  
if (polyline != null)

        {

          bool isOn = IsPointOnPolyline(polyline, ppr.Value);

          ed.WriteMessage(

            "\nSelected point is {0}on the polyline.",

            isOn ? "" : "not "

          );

        }

        tr.Commit();

      }

    }

 

    private bool IsPointOnPolyline(Polyline pl, Point3d pt)

    {

      bool isOn = false;

      for (int i = 0; i < pl.NumberOfVertices; i++)

      {

        Curve3d seg = null;

 

        SegmentType segType = pl.GetSegmentType(i);

        if (segType == SegmentType.Arc)

          seg = pl.GetArcSegmentAt(i);

        else if (segType == SegmentType.Line)

          seg = pl.GetLineSegmentAt(i);

 

        if (seg != null)

        {

          isOn = seg.IsOn(pt);

          if (isOn)

            break;

        }

      }

      return isOn;

    }

  }

}

When you run the POP command and select a polyline and a point, you'll see a message on the command-line indicating whether the latter is on the former.

Update

An improved approach to this has been shown in this post.

7 responses to “Testing whether a point is on an AutoCAD polyline using .NET”

  1. Thanks for sharing!

    I've added it to my toolkit.

  2. Given a selection within an in-place editor, how do you set the font?... Not the font on the style... the font on the selection within the editor... or, how do you get the index number of a specific named font through AutoCAD?

  3. Your question is somewhat off-topic (and this isn't a forum for support).

    Please post your question to ADN, if you're a member, or otherwise the appropriate online discussion forum.

    Kean

  4. Hello Kean!

    I hope my question is on topic.

    I have a polyline and I want to find each point from the polyline from 10 to 10 units from the startpoint. I could use your function, but I don't think is very good in my situation. Any ideas?

  5. As judges often seem to say in courtroom dramas: "I'll allow it." 🙂

    Having said that, I'm not sure I understand the question.

    What do you mean by "10 to 10 units from the startpoint"?

    And what points do you need - vertices or points on the polyline?

    Kean

  6. Hi Kean,

    is there a way to check the point is inside the polyline and disregard the edges of the polyline?

  7. Kean Walmsley Avatar

    The ADN team should have a solution posted to the ADN DevBlog that shows how to test whether a point is inside a polyline (it's a bit complicated, from what I remember). If it isn't there, please ask them for it.

    Kean

Leave a Reply to Peter Cancel reply

Your email address will not be published. Required fields are marked *