70002446 Customizing polyline Break

Article 70002446
Type HowTo
Product Engine
Version 10
Date Added 11/30/2023 12:00:00 AM
Fixed 10.1005.0.4 (11/30/2023 12:00:00 AM)
Submitted by Daniele Scarabottolo

Summary

I need a custom polyline Break that I will decide which polyline segment that is produced, I will keep or not

Solution

Try a code like:

               
               vdFigure fig;
                gPoint pt;
                doc.Prompt("select polyline");
                doc.ActionUtility.getUserEntity(out fig, out pt);
                doc.Prompt(null);
                vdPolyline pl = fig as vdPolyline;
                if (pl == null) return;
                if (pl.VertexList.Count < 2) return;
                doc.Prompt("select first break point 1");
                gPoint p1 = doc.ActionUtility.getUserPoint() as gPoint;
                doc.Prompt(null);
                if (p1 == null) return;
                doc.Prompt("select second break point 2");
                gPoint p2 = doc.ActionUtility.getUserPoint() as gPoint;
                doc.Prompt(null);
                if (p2 == null) return;
                if (pl.SPlineFlag != VdConstSplineFlag.SFlagSTANDARD) return;//splines are not supported

                //Get The closest point to passed p1 belonging to the curve.
                gPoint _p1nea = pl.getClosestPointTo(p1);
                //Get The closest point to passed p2 belonging to the curve.
                gPoint _p2nea = pl.getClosestPointTo(p2);
                //failed if no closest found
                if (_p1nea == null || _p2nea == null) return ;
                gPoint _p1 = _p1nea;
                gPoint _p2 = _p2nea;
                //find the distance of each point (p1,p2) from the start of the polyline and put them so the p1 to be nearest to the start of pline
                double param1, param2, sparam, eparam;
                try
                {
                    param1 = pl.getParamAtPoint(_p1);
                    param2 = pl.getParamAtPoint(_p2);
                    sparam = pl.getStartParam();
                    eparam = pl.getEndParam();
                }
                catch//an error occurred
                {
                    return ;
                }
                if (param1 > param2)
                {
                  
                    double tmp = param1;
                    param1 = param2;
                    param2 = tmp;
                }

                //insert the passed break points as new vertexes in the pline vertex list and get the inserted indexes
                _p1 = pl.getPointAtParam(param1);
                _p2 = pl.getPointAtParam(param2);

                vdPolyline clonepl = pl.Clone(null) as vdPolyline;
                bool success = clonepl.InsertPointAtParam(param1);
                if (!success) return;
                success = clonepl.InsertPointAtParam(param2);
                if (!success) return;
                Vertexes verts = clonepl.VertexList.Clone() as Vertexes;
                if (clonepl.Flag == VdConstPlineFlag.PlFlagCLOSE) verts.makeClosed();
                


                int index1 = verts.FindVertexPoint(_p1);
                int index2 = verts.FindVertexPoint(_p2);

                if (index1 == -1 || index2 == -1) return;


                //we broke the pline vertexes into three pieces
                
                Vertexes verts1 = new Vertexes();//from start to the first break point
                Vertexes verts2 = new Vertexes();//from first break point to the second one
                Vertexes verts3 = new Vertexes();//from the second break point to the end of polyline
                int i = 0;
                for (i = 0; i <= index1; i++)
                {
                    verts1.Add(new Vertex(verts[i]));
                }
                for (i = index1; i <= index2; i++)
                {
                    verts2.Add(new Vertex(verts[i]));
                }
                for (i = index2; i < verts.Count; i++)
                {
                    verts3.Add(new Vertex(verts[i]));
                }

                //we get finally 2 pieces that vertexes break by 2 selected points
                Vertexes vertsA, vertsB;
                vertsA = Vertexes.Join(verts1, verts3);
                vertsB = verts2;
               
                vertsA.RemoveEqualPoints();
                if (vertsA.Count == 1) vertsA.RemoveAll();
                vertsB.RemoveEqualPoints();
                if (vertsB.Count == 1) vertsB.RemoveAll();


                //create 2 new polylines one for each piece and add it to the document model entities to see the result on the screen
                doc.UndoHistory.StoreUndoGroup(true);
                
                if (vertsA.Count > 0)
                {
                    //just some methods may be useful if you want to keep or not the generated polyline
                    //one for return the length of the vertexes
                    //and one that returns the side of the polyline (left or right) relative to the selected points direction

                    double lenA = vertsA.Length();
                    bool isvertsALeft = Globals.isLeft(_p1nea, _p2nea, vertsA[1]) > 0;//else it is on right side
                    vdPolyline pla = pl.Clone(null) as vdPolyline;
                    pla.Flag = VdConstPlineFlag.PlFlagOPEN;
                    pla.VertexList = vertsA;
                    pla.PenColor = new vdColor(Color.Yellow);
                    doc.Model.Entities.AddItem(pla);
                }
                if (vertsB.Count > 0)
                {
                    //just some methods may be useful if you want to keep or not the generated polyline
                    //one for return the length of the vertexes
                    //and one that returns the side of the polyline (left or right) relative to the selected points direction
                    double lenB = vertsB.Length();
                    bool isvertsBLeft = Globals.isLeft(_p1nea, _p2nea, vertsB[1]) > 0;//else it is on right side
                    vdPolyline plb = pl.Clone(null) as vdPolyline;
                    plb.Flag = VdConstPlineFlag.PlFlagOPEN;
                    plb.VertexList = vertsB;
                    plb.PenColor = new vdColor(Color.Blue);
                    doc.Model.Entities.AddItem(plb);
                }
                doc.UndoHistory.StoreUndoGroup(false);
                doc.Redraw(true);

Send comments on this topic.