.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Getting co-ordinates of intervals for a polyline divided with a preset length

4 REPLIES 4
Reply
Message 1 of 5
CodeBug
528 Views, 4 Replies

Getting co-ordinates of intervals for a polyline divided with a preset length

Hi all,

 

I have a an angular polyline (not horizontal or vertical). I want to equally divide the polyline with a specified length and get all seperation co-ordinates at each interval. The line remains as polyline, I need to get only the co-ordinates of all the possible divisions for a given length.

I need a C#.Net function for this. Kindly help in achieving this.

 

Thanks and regards

4 REPLIES 4
Message 2 of 5
norman.yuan
in reply to: CodeBug

Since you know the length of the interval (assuming the length is counted along the polyline, which may not be straight with each interval), then you can easily get the coordinates with

 

Polyline.GetPointAtDist(double)

 

 

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 5
CodeBug
in reply to: norman.yuan

Thanks for the pointer norman.yuan.

Will check with that method and come back.

Message 4 of 5
Hallex
in reply to: CodeBug

Try this code for the test, you can expand the functionality

for other object types, currently it's working for lwplines,

3dplines and splines

 

       [CommandMethod("diem")]

        public void testDivideMethod()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;
            Matrix3d ucs = ed.CurrentUserCoordinateSystem;
            Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("pdmode", 34);
            Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("pdsize", 2.0);
            Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("osmode", 0);
            Transaction tr = doc.TransactionManager.StartOpenCloseTransaction();
            try
            {
                using (tr)
                {
                   
                    PromptEntityOptions peo = new PromptEntityOptions("\nSelect an Curve >>");
                    peo.SetRejectMessage("\nPlease select LWPOLYLINE, 3DPOLY or SPLINE only >>");
                    peo.AddAllowedClass(typeof(Polyline), false);
                    peo.AddAllowedClass(typeof(Spline), false);
                    peo.AddAllowedClass(typeof(Polyline3d), false);
                    PromptEntityResult res;
                    res = ed.GetEntity(peo);
                    if (res.Status != PromptStatus.OK)
                        return;
                    Entity ent = (Entity)tr.GetObject(res.ObjectId, OpenMode.ForRead);
                    if (ent == null)
                        return;
                Curve curv     = (Curve)ent as Curve;
                if (curv == null)
                    {
                        ed.WriteMessage("\nError: selected entity is not a Curve");
                        return;
                    }
                Plane plan = curv.GetPlane();
                    int num = 36;
                    double delta =curv.GetDistanceAtParameter (curv.EndParam - curv.StartParam) / (num + 1);
             
                   double step = delta;
                    Point3dCollection pts = new Point3dCollection();
                    
                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);

                        for (int n = 0; n <= num + 1; n++)
                        {
                           Point3d cp = curv.GetPointAtDist(step * n);

                        pts.Add(cp.TransformBy(ucs));
                        
                    }
                    if (pts == null) return;
                    // a dummy test to see result
                    foreach (Point3d pp in pts)
                    {

                        DBPoint pt = new DBPoint(curv.GetClosestPointTo(pp, false));
                        pt.SetDatabaseDefaults();
                        pt.ColorIndex = 1;
                        btr.AppendEntity(pt);
                        tr.AddNewlyCreatedDBObject(pt, true);
                    }

                    tr.Commit();
                }
            }
            catch (Autodesk.AutoCAD.Runtime.Exception ex)
            {
                Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage
                    (ex.ToString());
                return;
            }
            finally
            {
                Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("osmode", 8);
                Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nCheck result\n");
            }
        }

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 5 of 5
CodeBug
in reply to: Hallex

hi Hallex,

 

Thanks for your code. Currently, I have put the testing activity on hold for a few days.
Will get back once I start again.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost