Author Topic: Trouble implementing a similar function to trim  (Read 430 times)

0 Members and 1 Guest are viewing this topic.

themethodman

  • Mosquito
  • Posts: 9
Trouble implementing a similar function to trim
« on: November 23, 2020, 07:20:46 PM »
For a single polyline entity of multiple segments, I'm looking to create a gap at each intersection. Ideally need to be able to specify the gap distance.

simple example:



more complex example:



Basic code outline:

1. Get polyline entity from editor
2. Explode polyline into individual polylines
3. test each polyline against each other for intersections (intersections must be 'genuine', eg. can't be between 2 consecutive segments)
4. create temporary circle at each intersection of specific radius
5. for each polyline, "trim" the contents inside the circle *(having difficulty at this point onwards)*, thereby removing the intersection
6. delete the temporary circles
7. for each existing polyline, where possible if they can be joined back up, do so

Biggest problem I'm facing is trimming each of the polylines at the circle intersections - I'm just not sure how to put this in code  :crazy2:

Eventually I would like to be able to incorporate arcs into the polylines.

My code so far (up to point 4):
 

Code: [Select]
        [CommandMethod("SelfIntersectPline")]

        public static void SelfIntersectPline()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            PromptEntityOptions peo = new PromptEntityOptions(
                "\nSelect Curve: ");
            peo.SetRejectMessage("\nMust be a Curve...");

            PromptEntityResult per = ed.GetEntity(peo);
            if (per.Status != PromptStatus.OK) return;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTableRecord ms = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;

                //get curve and explode into entities, store curves in entities dbobjectcollection
                Curve curve = per.ObjectId.GetObject(OpenMode.ForRead) as Curve;
                DBObjectCollection origDBObjects = new DBObjectCollection();
                curve.Explode(origDBObjects);

                //erase original curve
                var originalCurve = (Entity)tr.GetObject(curve.ObjectId, OpenMode.ForWrite);
                originalCurve.Erase();

                //create temp dbcollection for circle entities
                DBObjectCollection circleDBObjects = new DBObjectCollection();

                //convert exploded curve entities to polylines
                for (int i = origDBObjects.Count - 1; i >= 0; i--)
                {
                    Curve curveObj = origDBObjects[i] as Curve;
                    origDBObjects.Add(ConvertToPolyline(curveObj));
                    origDBObjects.RemoveAt(i);
                }

                //iterate through each polyline within entities collection
                for (int i = 0; i < origDBObjects.Count; ++i)
                {
                    for (int j = i + 1; j < origDBObjects.Count; ++j)
                    {
                        Polyline pl1 = origDBObjects[i] as Polyline;
                        Polyline pl2 = origDBObjects[j] as Polyline;
                        Point3dCollection points = new Point3dCollection();

                        //test for intersection(s)
                        pl1.IntersectWith(pl2, Intersect.OnBothOperands, points, IntPtr.Zero, IntPtr.Zero);

                        //validation for intersections
                        foreach (Point3d point in points)
                        {
                            // Skip the start/end points since they are connected vertices
                            if (point == pl1.StartPoint ||
                                point == pl1.EndPoint)
                            {
                                if (point == pl2.StartPoint ||
                                    point == pl2.EndPoint)
                                {
                                    // If two consecutive segments, then skip
                                    if (j == i + 1)
                                    {
                                        continue;
                                    }
                                }
                            }

                            //if genuine intersection detected, create temporary circle of specified radius at intersection
                            foreach (Point3d pt3d in points)
                            {
                                Circle c = new Circle();
                                c.Center = pt3d;
                                c.Radius = 0.5;
                                circleDBObjects.Add(c);
                            }
                        }
                    }
                }
« Last Edit: November 24, 2020, 05:41:58 AM by themethodman »

nobody

  • Swamp Rat
  • Posts: 853
  • .net stuff
Re: Trouble implementing a similar function to trim
« Reply #1 on: November 24, 2020, 12:58:12 PM »
Perhaps a more direct approach would be use GETSPLITCURVES using your intersection point to split them and then reducing the lengths of the ends of the split curves.

Split Curves Example
https://forums.autodesk.com/t5/net/trim-function/td-p/7770714

Changing Lengths (almost the last line in this example)
http://docs.autodesk.com/ACD/2010/ENU/AutoCAD%20.NET%20Developer%27s%20Guide/index.html?url=WS1a9193826455f5ff2566ffd511ff6f8c7ca-3e52.htm,topicNumber=d0e25580

WILL HATCH

  • Bull Frog
  • Posts: 450

themethodman

  • Mosquito
  • Posts: 9
Re: Trouble implementing a similar function to trim
« Reply #3 on: November 25, 2020, 12:08:26 AM »
Similar. Looking for it to work with self intersecting polylines.

I've made a little bit of progress since my original post, might have some code up later if all goes well.


themethodman

  • Mosquito
  • Posts: 9
Re: Trouble implementing a similar function to trim
« Reply #4 on: November 25, 2020, 04:03:00 AM »
All sorted.

Biggest hurdle was working out that the intercept point3dcollection should be sorted in distance-from-the-StartPoint order before plugging it into GetSplitCurves. :whistling: