Author Topic: Polyline overlap  (Read 2439 times)

0 Members and 1 Guest are viewing this topic.

hojjat

  • Mosquito
  • Posts: 11
Polyline overlap
« on: September 17, 2023, 12:55:28 PM »
Using C# 7.3, Visual Studio 2022 and Autocad 2013:

Is there any function to compare two polylines for area overlap?
I want to compare two polylines and detect area overlapping.
Polyline segments can be LineSegment2d or CircularArc2d.
My purpose is a function that returns false if they are only tangent or non-overlapping, and returns true if they have overlap with a specific tolerance.

Thanks

gile

  • Water Moccasin
  • Posts: 2492
  • Marseille, France
Re: Polyline overlap
« Reply #1 on: September 17, 2023, 01:47:43 PM »
Hi,

You can create regions from the polyline and check if the intersection boolean operation generates an area greater than 0.
Code - C#: [Select]
  1.         public static double OverlapArea(Polyline pline1, Polyline pline2)
  2.         {
  3.             using (var region1 = CreateRegion(pline1))
  4.             using (var region2 = CreateRegion(pline2))
  5.             {
  6.                 if (region1 != null && region2 != null)
  7.                 {
  8.                     region1.BooleanOperation(BooleanOperationType.BoolIntersect, region2);
  9.                     return region1.Area;
  10.                 }
  11.                 return 0.0;
  12.             }
  13.         }
  14.  
  15.         private static Region CreateRegion(Polyline pline)
  16.         {
  17.             var curves = new DBObjectCollection { pline };
  18.             var regions =  Region.CreateFromCurves(curves);
  19.             switch (regions.Count)
  20.             {
  21.                 case 0:
  22.                     return null;
  23.                 case 1:
  24.                     return (Region)regions[0];
  25.                 default:
  26.                     foreach (DBObject dBObject in regions) dBObject.Dispose();
  27.                     return null;
  28.             }
  29.         }

A testing command:
Code - C#: [Select]
  1.         [CommandMethod("TEST")]
  2.         public static void Test()
  3.         {
  4.             var doc = AcAp.DocumentManager.MdiActiveDocument;
  5.             var db = doc.Database;
  6.             var ed = doc.Editor;
  7.  
  8.             var options = new PromptEntityOptions("\nSelect first polyline: ");
  9.             options.SetRejectMessage("\nSelected object is not a Polyline.");
  10.             options.AddAllowedClass(typeof(Polyline), true);
  11.             var result = ed.GetEntity(options);
  12.             if (result.Status != PromptStatus.OK)
  13.                 return;
  14.             var id1 = result.ObjectId;
  15.  
  16.             options.Message = "\nSelect second polyline: ";
  17.             result = ed.GetEntity(options);
  18.             if (result.Status != PromptStatus.OK)
  19.                 return;
  20.             var id2 = result.ObjectId;
  21.  
  22.             using (var tr = db.TransactionManager.StartTransaction())
  23.             {
  24.                 var pline1 = (Polyline)tr.GetObject(id1, OpenMode.ForRead);
  25.                 var pline2 = (Polyline)tr.GetObject(id2, OpenMode.ForRead);
  26.                 double area = OverlapArea(pline1, pline2);
  27.                 if (0.0 < area)
  28.                     ed.WriteMessage($"\nPolylines overlap ({area:0.00} square units).");
  29.                 else
  30.                     ed.WriteMessage("\nPolylines do not overlap.");
  31.                 tr.Commit();
  32.             }
  33.         }
Speaking English as a French Frog

hojjat

  • Mosquito
  • Posts: 11
Re: Polyline overlap
« Reply #2 on: September 17, 2023, 02:22:58 PM »
Thank you very much Gile, It works properly.
« Last Edit: September 17, 2023, 02:29:14 PM by hojjat »