How to detect if a Polyline is self-intersecting?

By Philippe Leefsma

Here is a request from one of our ADN developers:

I would like to detect if a certain polyline is self-intersecting and if so I would like to compute those intersection points. How to achieve this using the .Net API?

Solution

The following C# sample prompts the user for selecting an existing Polyline entity and then computes its self-intersecting points, if any:

[CommandMethod("SelfIntersectPline")]

public static void SelfIntersectPline()

{

    Document doc = Application.DocumentManager.MdiActiveDocument;

    Database db = doc.Database;

    Editor ed = doc.Editor;

 

    PromptEntityOptions peo = new PromptEntityOptions(

        "\nSelect Polyline: ");

 

    peo.SetRejectMessage("\nMust be a Polyline…");

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

 

    PromptEntityResult per = ed.GetEntity(peo);

 

    if (per.Status != PromptStatus.OK) ret
urn
;

 

    using (Transaction Tx =

        db.TransactionManager.StartTransaction())

    {

        Polyline polyline = per.ObjectId.GetObject(

            OpenMode.ForRead)

                as Polyline;

 

        DBObjectCollection entities = new DBObjectCollection();

        polyline.Explode(entities);

 

        for (int i = 0; i < entities.Count; ++i)

        {

            for (int j = i + 1; j < entities.Count; ++j)

            {

                Curve curve1 = entities[i] as Curve;

                Curve curve2 = entities[j] as Curve;

 

                Point3dCollection points = new Point3dCollection();

                curve1.IntersectWith(

                    curve2,

                    Intersect.OnBothOperands,

                    points,

                    IntPtr.Zero,

                    IntPtr.Zero);

 

                foreach (Point3d point in points)

                {

                    // Make a check to skip the start/end points

                    // since they are connected vertices

                    if (point == curve1.StartPoint ||

                        point == curve1.EndPoint)

                    {

                        if (point == curve2.StartPoint ||

                            point == curve2.EndPoint)

                        {

                            // If two consecutive segments, then skip

                            if (j == i + 1)

                            {

                                continue;

                            }

                        }

                    }

 

                    ed.WriteMessage(

                        "\n – Intersection point: " +

                        point.ToString());

                }

  &#1
60;        
}

 

            // Need to be disposed explicitely

            // since entities are not DB resident

            entities[i].Dispose();

        }

    }

}

<

p style=”line-height: normal;margin: 0in 0in 0pt” class=”MsoNormal”> 


Comments

3 responses to “How to detect if a Polyline is self-intersecting?”

  1. Hi,
    While inspecting namespaces i found an interesting class named CurveCurveIntersector3d in Geometry namespace. It provides functionality to find intersection of two curves. Even better it says if you give the same curve for both operands then you can get if the curve is self intersecting or not.

  2. HI thanks for this but . i found a little error :
    this the correct code :
    If (point = curve1.StartPoint Or _
    point = curve1.EndPoint) Then
    If (point = curve2.StartPoint Or _
    point = curve2.EndPoint) Then
    Continue For
    End If
    End If
    ‘// If two consecutive segments, then skip
    If (j = i + 1) Then
    Continue For
    End If

  3. methodman Avatar
    methodman

    For c#:
    if (point == pl1.StartPoint ||
    point == pl1.EndPoint)
    {
    if (point == pl2.StartPoint ||
    point == pl2.EndPoint)
    {
    continue;
    }
    }
    // If two consecutive segments, then skip
    if (j == i + 1)
    {
    continue;
    }

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading