.NET

Reply
Contributor
CodeBug
Posts: 18
Registered: ‎07-17-2012
Message 1 of 5 (235 Views)

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

235 Views, 4 Replies
06-10-2013 05:38 AM

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

*Expert Elite*
norman.yuan
Posts: 961
Registered: ‎04-27-2009
Message 2 of 5 (224 Views)

Re: Getting co-ordinates of intervals for a polyline divided with a preset lengt

06-10-2013 06:38 AM 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)

 

 

Contributor
CodeBug
Posts: 18
Registered: ‎07-17-2012
Message 3 of 5 (195 Views)

Re: Getting co-ordinates of intervals for a polyline divided with a preset lengt

06-10-2013 10:43 PM in reply to: norman.yuan

Thanks for the pointer norman.yuan.

Will check with that method and come back.

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 4 of 5 (190 Views)

Re: Getting co-ordinates of intervals for a polyline divided with a preset lengt

06-11-2013 12:57 AM 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
Contributor
CodeBug
Posts: 18
Registered: ‎07-17-2012
Message 5 of 5 (140 Views)

Re: Getting co-ordinates of intervals for a polyline divided with a preset lengt

07-01-2013 11:36 PM 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.

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community