70002266 How to divide a closed polyline into 2 polylines

Article 70002266
Type HowTo
Product Engine
Version 10
Date Added 10/4/2022 12:00:00 AM
Fixed 10.1002.0.3 (10/4/2022 12:00:00 AM)
Submitted by bandiroli

Summary

How to divide a closed polyline into 2 polylines based on line that intersect it.

Solution

You can try this code example:

            
                //Select a closed polyline first
                vdFigure fig;
                gPoint pt;
                doc.Prompt("select polyline to divide");
                doc.ActionUtility.getUserEntity(out fig, out pt);
                doc.Prompt(null);
                vdPolyline pl = fig as vdPolyline;
                if (pl == null) return;

                //Select a line that intersect the polyline at least in 2 points
                doc.Prompt("select cutting line");
                doc.ActionUtility.getUserEntity(out fig, out pt);
                doc.Prompt(null);
                vdLine line = fig as vdLine;
                if (line == null) return;

                //get the first and last intersections to divide the polyline in to 2 sub polylines
                gPoints intpoints = new gPoints();
                pl.IntersectWith(line, VdConstInters.VdIntOnBothOperands, intpoints);
                if (intpoints.Count < 2) return;
                gPoint i1 = intpoints[0];
                gPoint i2 = intpoints.Last();

                //create a temporary polyline clone of the source polyline
                vdPolyline tmppline = pl.Clone(null)as vdPolyline;

                //insert the intersection points into a new vertexes to the temporary polyline vertexlist
                tmppline.InsertPointAtParam(pl.getParamAtPoint(i1));
                tmppline.InsertPointAtParam(pl.getParamAtPoint(i2));

                //get the indexes of the new added vertexes
                int p1 = tmppline.VertexList.FindVertexPoint(i1);
                int p2 = tmppline.VertexList.FindVertexPoint(i2);

                //find 3 sub vertexes 
                //1.From start to the first intersection
                //2.from first intersection to second one
                //3.from the second intersection to the last vertex
                Vertexes v1 = new Vertexes();
                Vertexes v2 = new Vertexes();
                Vertexes v3 = new Vertexes();
                for (int i = 0; i <= p1; i++) v1.Add(tmppline.VertexList[i].Clone());
                for (int i = p1; i <= p2; i++) v2.Add(tmppline.VertexList[i].Clone());
                for (int i = p2; i < tmppline.VertexList.Count; i++) v3.Add(tmppline.VertexList[i].Clone());

                //append the tail with the head bounds that are outside the segment between 2 intersections
                v3.AddRange(v1);

                //v2 is the vertexes of the one sub polyline
                //v3 is the vertexes of the second sub polyline

                //ensure the last closed segment for both vertex lists to be a line
                v3.Last().Bulge = 0.0;
                v2.Last().Bulge = 0.0;

                //create the new polylines and add them to the model entities in order to see them graphically
                vdPolyline npl1 = new vdPolyline(doc, v2) {Flag = VdConstPlineFlag.PlFlagCLOSE, HatchProperties = new vdHatchProperties(VdConstFill.VdFillModeSolid) { FillColor = new vdColor(Color.Red) } };
                vdPolyline npl2 = new vdPolyline(doc, v3) { Flag = VdConstPlineFlag.PlFlagCLOSE, HatchProperties = new vdHatchProperties(VdConstFill.VdFillModeSolid) { FillColor = new vdColor(Color.Blue) } };
              
                doc.Model.Entities.AddItem(npl1);
                doc.Model.Entities.AddItem(npl2);
                doc.Redraw(true);

Send comments on this topic.