How to split with intersections between two Polylines ?

How to split with intersections between two Polylines ?

anhtrung
Advocate Advocate
2,999 Views
7 Replies
Message 1 of 8

How to split with intersections between two Polylines ?

anhtrung
Advocate
Advocate

Hi everyone,

 

I have a question that is have two polylines, and it's intersection with together (you can see below image with intersection points 1,2,3,4,5,6). I'm using with C.# to automatic how to split with this intersection can create polylines as right image. File attached to check.

Help me please with using C.# to do that. Thanks.

anhtrung_0-1587195976954.png

 

 

 

 

0 Likes
Accepted solutions (2)
3,000 Views
7 Replies
Replies (7)
Message 2 of 8

_gile
Consultant
Consultant
Accepted solution

Hi,

 

Here's an example which should work on polylines with linear segments.

 

        [CommandMethod("PLINESPLIT")]
        public static void SplitPolylines()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;

            var options = new PromptEntityOptions("\nSelect first polyline: ");
            options.SetRejectMessage("\nMust be a polyline.");
            options.AddAllowedClass(typeof(Polyline), true);
            var result = ed.GetEntity(options);
            if (result.Status != PromptStatus.OK)
                return;
            var id1 = result.ObjectId;

            options.Message = "\nSelect second polyline: ";
            result = ed.GetEntity(options);
            if (result.Status != PromptStatus.OK)
                return;
            var id2 = result.ObjectId;

            using (var tr = db.TransactionManager.StartTransaction())
            {
                var pline1 = (Polyline)tr.GetObject(id1, OpenMode.ForWrite);
                var pline2 = (Polyline)tr.GetObject(id2, OpenMode.ForWrite);
                var plane = new Plane(Point3d.Origin, pline1.Normal);
                var points = new Point3dCollection();
                pline1.IntersectWith(pline2, Intersect.OnBothOperands, points, IntPtr.Zero, IntPtr.Zero);
                AddVertices(pline1, points);
                AddVertices(pline2, points);
                var curveSegments = new DBObjectCollection();
                pline1.Explode(curveSegments);
                pline2.Explode(curveSegments);
                var regions = Region.CreateFromCurves(curveSegments);
                foreach (Entity entity in curveSegments)
                {
                    entity.Dispose();
                }
                var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                foreach (Region region in regions.Cast<Region>().OrderByDescending(r => r.Area).Skip(1))
                {
                    var pline = RegionToPolyline(region);
                    region.Dispose();
                    curSpace.AppendEntity(pline);
                    tr.AddNewlyCreatedDBObject(pline, true);
                }
                pline1.Erase();
                pline2.Erase();
                tr.Commit();
            }
        }

        static void AddVertices(Polyline pline, Point3dCollection points)
        {
            var points3d = points
                    .Cast<Point3d>()
                    .Select(p => pline.GetClosestPointTo(p, false))
                    .Select(p => new { Point = p, Index = (int)Math.Floor(pline.GetParameterAtPoint(p)) })
                    .OrderByDescending(p => p.Index)
                    .ToArray();
            var plane = new Plane(Point3d.Origin, pline.Normal);
            var points2d = points3d.Select(p => p.Point.Convert2d(plane)).ToArray();
            for (int i = 0; i < points3d.Length; i++)
            {
                pline.AddVertexAt(points3d[i].Index + 1, points2d[i], 0.0, 0.0, 0.0);
            }
        }

        static Polyline RegionToPolyline(Region region)
        {
            var normal = region.Normal;
            var plane = new Plane(Point3d.Origin, normal);
            var pline = new Polyline();
            var entitySet = new DBObjectCollection();
            region.Explode(entitySet);
            var edges = entitySet.Cast<Line>().ToList();
            pline.Elevation = edges[0].StartPoint.TransformBy(Matrix3d.WorldToPlane(plane)).Z;
            int i = 0;
            pline.AddVertexAt(i++, edges[0].StartPoint.Convert2d(plane), 0.0, 0.0, 0.0);
            var endPt = edges[0].EndPoint;
            edges.RemoveAt(0);
            while (edges.Count > 0)
            {
                var edge = edges[0];
                if (endPt.IsEqualTo(edge.StartPoint))
                {
                    pline.AddVertexAt(i++, edge.StartPoint.Convert2d(plane), 0.0, 0.0, 0.0);
                    endPt = edge.EndPoint;
                }
                else if (endPt.IsEqualTo(edge.EndPoint))
                {
                    pline.AddVertexAt(i++, edge.EndPoint.Convert2d(plane), 0.0, 0.0, 0.0);
                    endPt = edge.StartPoint;
                }
                else
                {
                    edges.Add(edges[0]);
                }
                edges.RemoveAt(0);
            }
            pline.Closed = true;
            pline.Normal = normal;
            return pline;
        }

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 8

_gile
Consultant
Consultant
Accepted solution

This version works with arcs.

_gile_0-1587288877286.png

        [CommandMethod("SPLITPLINES")]
        public static void SplitPolylines()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;

            var options = new PromptEntityOptions("\nSelect first polyline: ");
            options.SetRejectMessage("\nMust be a polyline.");
            options.AddAllowedClass(typeof(Polyline), true);
            var result = ed.GetEntity(options);
            if (result.Status != PromptStatus.OK)
                return;
            var id1 = result.ObjectId;

            options.Message = "\nSelect second polyline: ";
            result = ed.GetEntity(options);
            if (result.Status != PromptStatus.OK)
                return;
            var id2 = result.ObjectId;

            using (var tr = db.TransactionManager.StartTransaction())
            {
                var pline1 = (Polyline)tr.GetObject(id1, OpenMode.ForWrite);
                var pline2 = (Polyline)tr.GetObject(id2, OpenMode.ForWrite);
                var plane = new Plane(Point3d.Origin, pline1.Normal);
                var points = new Point3dCollection();
                pline1.IntersectWith(pline2, Intersect.OnBothOperands, points, IntPtr.Zero, IntPtr.Zero);
                var curveSegments = SplitPline(pline1, points);
                foreach (DBObject obj in SplitPline(pline2, points))
                {
                    curveSegments.Add(obj);
                }
                var regions = Region.CreateFromCurves(curveSegments);
                foreach (Entity entity in curveSegments)
                {
                    entity.Dispose();
                }
                var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                foreach (Region region in regions.Cast<Region>().OrderByDescending(r => r.Area).Skip(1))
                {
                    var pline = RegionToPolyline(region);
                    region.Dispose();
                    curSpace.AppendEntity(pline);
                    tr.AddNewlyCreatedDBObject(pline, true);
                }
                pline1.Erase();
                pline2.Erase();
                tr.Commit();
            }
        }

        static DBObjectCollection SplitPline (Polyline pline, Point3dCollection points)
        {
            var pts = new Point3dCollection(
                points
                .Cast<Point3d>()
                .OrderBy(p => pline.GetParameterAtPoint(p))
                .ToArray());
            return pline.GetSplitCurves(pts);
        }

        static Polyline RegionToPolyline(Region region)
        {
            var normal = region.Normal;
            var plane = new Plane(Point3d.Origin, normal);
            var pline = new Polyline();
            var entitySet = new DBObjectCollection();
            region.Explode(entitySet);
            var edges = entitySet.Cast<Curve>().ToList();
            pline.Elevation = edges[0].StartPoint.TransformBy(Matrix3d.WorldToPlane(plane)).Z;
            int i = 0;
            double bulge = 0.0;
            if (edges[0] is Arc)
            {
                bulge = GetBulge((Arc)edges[0], plane);
            }
            pline.AddVertexAt(i++, edges[0].StartPoint.Convert2d(plane), bulge, 0.0, 0.0);
            var endPt = edges[0].EndPoint;
            edges.RemoveAt(0);
            while (edges.Count > 0)
            {
                var edge = edges[0];
                if (endPt.IsEqualTo(edge.StartPoint))
                {
                    bulge = 0.0;
                    if (edges[0] is Arc)
                    {
                        bulge = GetBulge((Arc)edges[0], plane);
                    }
                    pline.AddVertexAt(i++, edge.StartPoint.Convert2d(plane), bulge, 0.0, 0.0);
                    endPt = edge.EndPoint;
                }
                else if (endPt.IsEqualTo(edge.EndPoint))
                {
                    bulge = 0.0;
                    if (edges[0] is Arc)
                    {
                        bulge = -GetBulge((Arc)edges[0], plane);
                    }
                    pline.AddVertexAt(i++, edge.EndPoint.Convert2d(plane), bulge, 0.0, 0.0);
                    endPt = edge.StartPoint;
                }
                else
                {
                    edges.Add(edges[0]);
                }
                edges.RemoveAt(0);
            }
            pline.Closed = true;
            pline.Normal = normal;
            return pline;
        }

        static double GetBulge(Arc arc, Plane plane)
        {
            double bulge;
            double angle = (2.0 * Math.PI + arc.EndAngle - arc.StartAngle) % (2.0 * Math.PI);
            bulge = Math.Tan(angle / 4.0);
            if (arc.Normal.TransformBy(Matrix3d.WorldToPlane(plane)).Z < 0.0)
                bulge = -bulge;
            return bulge;
        }

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 4 of 8

anhtrung
Advocate
Advocate

I have more a question, that is i want to split only at intersect location between two polylines. See image below.

left image have 2 polylines and have 4 intersection, right image is result i want to do that. File attached.

2020-04-20_16-40-19.png

 

0 Likes
Message 5 of 8

_gile
Consultant
Consultant

You should be able to do it on your own by taking inspiration from the above codes.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 6 of 8

anhtrung
Advocate
Advocate

Thanks Sir, i created success, The first i create polyline with start and end point between segment pline2, and then create region between pline1 with new polyline.

 

 

0 Likes
Message 7 of 8

anhtrung
Advocate
Advocate

@_gile hi Sir, i have modify code to with situation, but a lot of other situation not work. So help me please write a code to do that, please. Thanks

 

0 Likes
Message 8 of 8

_gile
Consultant
Consultant

Nobody will be able to help you while you do not provide an exhaustive and non-equivoque description of what you want to achieve. You have to define the criterias of your choices in each situation. Despite someones say, there no intelligency in computing, just binary choices: if... then... else...



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes