Segment of a Polyline

Segment of a Polyline

naveenkuma.mukku
Participant Participant
381 Views
2 Replies
Message 1 of 3

Segment of a Polyline

naveenkuma.mukku
Participant
Participant

Can you please let me know how to get the segment of a polyline between the two points i derived.  I tried using GetSplitCurves() but couldn't get precise segment.

 

Thank you

0 Likes
Accepted solutions (1)
382 Views
2 Replies
Replies (2)
Message 2 of 3

norman.yuan
Mentor
Mentor

If you looked at the Polyline class (in Object Browser window of Visual Studio), you would have found a bunch of methods: GetXxxxx(int). These methods are the one you can use to find out information on each segment of a Polyline.

 

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 3

Miralkong
Advocate
Advocate
Accepted solution

After using GetSplitCurves(), you could test if any segment intersects with both cutting point to get the exact segment.

 

[CommandMethod("splitpoly")]
        public void Test()
        {
            Document acDoc = AcadApp.DocumentManager.MdiActiveDocument;
            Database acDb = acDoc.Database;
            Editor ed = acDoc.Editor;

            using(Transaction acTrans = acDoc.TransactionManager.StartTransaction())
            {
                // Get blocktable and blocltablerecord
                BlockTable acBlkTbl = acTrans.GetObject(acDb.BlockTableId,OpenMode.ForRead) as BlockTable;                
                BlockTableRecord acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],OpenMode.ForWrite) as BlockTableRecord;

                // Get polyline
                PromptEntityResult plRet = ed.GetEntity("Please pick a polyline\n");
                if (plRet.Status != PromptStatus.OK)
                {
                    return;
                }
                Polyline pl = acTrans.GetObject(plRet.ObjectId, OpenMode.ForWrite) as Polyline;
                if(pl == null)
                {
                    return;
                }

                // Get cutting point
                PromptPointResult pt1Ret = ed.GetPoint("Please pick the first split point on previous selected polyline.\n");
                if (pt1Ret.Status != PromptStatus.OK)
                {
                    return;
                }
                Point3d pt1 = pt1Ret.Value;

                PromptPointResult pt2Ret = ed.GetPoint("Please pick the second split point on previous selected polyline.\n");
                if (pt2Ret.Status != PromptStatus.OK)
                {
                    return;
                }
                Point3d pt2 = pt2Ret.Value;

                // Get cutting point collection
                Point3dCollection ptColl = new Point3dCollection();
                ptColl.Add(pt1);
                ptColl.Add(pt2);

                // Split the polyline with point collection
                DBObjectCollection dbColl = pl.GetSplitCurves(ptColl);

                // If any segment of the polyline intersects with both two cutting points, add it to database.
                foreach (DBObject obj in dbColl)
                {
                    Polyline subPl = obj as Polyline;
                    Point3d startPt = subPl.StartPoint;
                    Point3d endPt = subPl.EndPoint;
                    
                    if((pt1 == startPt && pt2 == endPt) || (pt1 == endPt && pt2 == startPt))
                    {
                        subPl.ConstantWidth = 2;
                        acBlkTblRec.AppendEntity(subPl);
                        acTrans.AddNewlyCreatedDBObject(subPl,true);
                    }                   
                }
                acTrans.Commit();
            }
        }