Author Topic: How to IntersectWith() for multiple selections  (Read 2222 times)

0 Members and 1 Guest are viewing this topic.

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
How to IntersectWith() for multiple selections
« on: April 01, 2015, 04:52:35 PM »
I don't know why I can't wrap my head around this but I need to find the intersections of multiple selected polylines crossing each other and get their coordinates.  Anyone have anything they'd be willing to share that might help? If not no biggy.  Thanks!

ronjonp

  • Needs a day job
  • Posts: 7527
Re: How to IntersectWith() for multiple selections
« Reply #1 on: April 01, 2015, 05:24:56 PM »
Here's a quick one .. does not include self intersecting polylines.
Code - Auto/Visual Lisp: [Select]
  1. (defun _intersections (/ e out p ss x)
  2.   (if (setq ss (ssget "_x" (list (cons 0 "*LINE,ARC,CIRCLE,ELLIPSE"))))
  3.     (progn (setq ss (mapcar 'vlax-ename->vla-object (mapcar 'cadr (ssnamex ss))))
  4.            (while (setq e (car ss))
  5.              (if (setq p
  6.                         (apply 'append
  7.                                (vl-remove 'nil
  8.                                           (mapcar '(lambda (x) (vlax-invoke e "intersectwith" x acextendnone))
  9.                                                   (setq ss (cdr ss))
  10.                                           )
  11.                                )
  12.                         )
  13.                  )
  14.                (while p (setq out (cons (list (car p) (cadr p) (caddr p)) out)) (setq p (cdddr p)))
  15.              )
  16.            )
  17.     )
  18.   )
  19.   out
  20. )
  21. (_intersections)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
Re: How to IntersectWith() for multiple selections
« Reply #2 on: April 01, 2015, 05:37:12 PM »
If only i was using lisp :( 


ronjonp

  • Needs a day job
  • Posts: 7527
Re: How to IntersectWith() for multiple selections
« Reply #3 on: April 01, 2015, 06:54:32 PM »
If only i was using lisp :(


DOH! Wrong forum :)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Jeff_M

  • King Gator
  • Posts: 4094
  • C3D user & customizer
Re: How to IntersectWith() for multiple selections
« Reply #4 on: April 01, 2015, 07:49:18 PM »
May be better ways of doing this, but it seems to have worked for me when needed.
Code - C#: [Select]
  1.         [CommandMethod("PolyIntersect")]
  2.         public void polyintersects()
  3.         {
  4.             Document doc = Application.DocumentManager.MdiActiveDocument;
  5.             Editor ed  = doc.Editor;
  6.             Database db = doc.Database;
  7.             PromptSelectionOptions ssOpts = new PromptSelectionOptions();
  8.             ssOpts.MessageForAdding = "Select polylines:";
  9.             ssOpts.MessageForRemoval = "...Select polylines to remove from selection:";
  10.             ssOpts.RejectObjectsOnLockedLayers = true;
  11.             TypedValue[] tv = { new TypedValue(0, "LWPOLYLINE") };
  12.             SelectionFilter filter = new SelectionFilter(tv);
  13.             PromptSelectionResult ssRes = ed.GetSelection(ssOpts, filter);
  14.             if (ssRes.Status != PromptStatus.OK)
  15.                 return;
  16.             using (Transaction tr = db.TransactionManager.StartTransaction())
  17.             {
  18.                 Point3dCollection interPoints = getintersectingpoints(ssRes.Value.GetObjectIds());
  19.                 //do what you need with the points here
  20.                 ed.WriteMessage("\nFound {0} instersections", interPoints.Count.ToString());
  21.                 tr.Commit();
  22.             }
  23.         }
  24.  
  25.         private Point3dCollection getintersectingpoints(ObjectId[] idArray)
  26.         {
  27.             List<ObjectId> ids = idArray.ToList<ObjectId>();          
  28.             List<ObjectId> idstocompare = idArray.ToList<ObjectId>();
  29.             idstocompare.RemoveAt(0);
  30.             Point3dCollection intersectpoints = new Point3dCollection();
  31.             for (int i = 0; i < ids.Count - 1; i++)
  32.             {
  33.                 Polyline poly = (Polyline)ids[i].GetObject(OpenMode.ForRead);
  34.                 foreach (ObjectId id in idstocompare)
  35.                 {
  36.                     Polyline poly2 = (Polyline)id.GetObject(OpenMode.ForRead);
  37.                     Point3dCollection points = new Point3dCollection();
  38.                     poly.IntersectWith(poly2, Intersect.ExtendArgument, points, new IntPtr(0), new IntPtr(0));
  39.                     foreach (Point3d pt in points)
  40.                     {
  41.                         if (!intersectpoints.Contains(pt))
  42.                             intersectpoints.Add(pt);
  43.                     }
  44.                 }
  45.                 idstocompare.RemoveAt(0);
  46.             }
  47.             return intersectpoints;
  48.         }
  49.     }
  50.  

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
Re: How to IntersectWith() for multiple selections
« Reply #5 on: April 02, 2015, 01:58:00 PM »
Thank you jeff :)  /kowtow