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());
}

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”>

Leave a Reply to aubelecCancel reply