.NET

.NET

Reply
Valued Contributor
dynamicscope
Posts: 95
Registered: ‎04-04-2012
Message 1 of 2 (617 Views)

Divide(Measure) a Polyline with a certain interval

617 Views, 1 Replies
07-16-2012 10:43 PM

Is it possible to divide a polyline into multiple segments with a certain interval?

I am looking for something similar to "Measure" command.

But I want to do this programmatically.

 

SendStringToExcute("measure ");

 

quite does not help.

I need the point objects created by "measure' commnad in order to draw a new polyine with them.

 

Of course, "SendStringToExcute" does not return such object collection.

 

Does anyone know how to solve this problem?

If you could provide me with C# (or .NET ObjectARX 2010) code snippet, it would be appreciated.

 

Thank you very much.

 

Regards,

 

Jake

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

Re: Divide(Measure) a Polyline with a certain interval

07-16-2012 11:37 PM in reply to: dynamicscope

Try this code

 

        //      FH
        [CommandMethod("measp")]
        public static void testMeasure()
        {

            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

            Database db = doc.Database;

            Editor ed = doc.Editor;

            Matrix3d ucs = ed.CurrentUserCoordinateSystem;

            object osm = Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("osmode");

            Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("osmode", 512);

            double fuzz = (double)Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("dimtxt");

            if (fuzz == 0) fuzz = 0.01;// change fuzz to your needs (say 0.1 or so)
            //-------------------------------------------------------------------
            PromptPointOptions ppo = new PromptPointOptions("\nPick a point near to the start of curve: ");

            PromptPointResult ppr = default(PromptPointResult);

            ppr = ed.GetPoint(ppo);

            if (ppr.Status != PromptStatus.OK)
            {
                ed.WriteMessage("\nwrong point specification!");

                return;
            }
            Point3d p = ppr.Value;
            //-------------------------------------------------------------------
            Vector3d vd = new Vector3d(fuzz, fuzz, 0);
            Point3d pMin = p - vd;
            Point3d pMax = p + vd;

            TypedValue[] tvs = new TypedValue[] { new TypedValue(0, "lwpolyline") };

            Point3dCollection points = new Point3dCollection();

            points.Add(pMin);

            points.Add(pMax);

            SelectionFilter sf = new SelectionFilter(tvs);

            PromptSelectionResult sres = ed.SelectFence(points, sf);

            if (sres.Status != PromptStatus.OK)
            {
                ed.WriteMessage("\nWrong selection!");

                return;
            }

            if (sres.Value.Count == 0)
            {
                ed.WriteMessage("\nNothing selected!");

                return;
            }

            //-------------------------------------------------------------------

            Transaction tr = db.TransactionManager.StartTransaction();

            using (tr)
            {
                try
                {

                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);

                    ObjectId eid = sres.Value.GetObjectIds()[0];

                    Entity ent = tr.GetObject(eid, OpenMode.ForRead) as Entity;
                    // cast entity as Polyline
                    Polyline pline = ent as Polyline;

                    if (pline != null)
                    {
                        ed.WriteMessage("\n{0},{1}", pline.StartPoint.X, pline.StartPoint.Y);
                    }

                    Point3d px = pline.GetClosestPointTo(pMin, false);

                    double leng = pline.Length;

                    bool endclick = false;

                    if ((pline.GetDistAtPoint(px) > leng / 2))
                    {
                        endclick = true;
                    }

                    double theight = db.Textsize;

                    if (theight == 0) theight = db.Dimtxt;

                    double step = 50.0;// <---  change the step to your needs*************************************************

                    int num = (int)(leng / step);

                    double ang;

                    int n = 1;

                    Plane plan = pline.GetPlane();

                    Line ln = null;

                    DBText txt = null;

                    Vector3d vect = new Vector3d();

                    Vector3d vec = new Vector3d();

                    Point3d ptxt = new Point3d();

                    Point3d ppt = new Point3d();

                    Point3d pptx = new Point3d();

                    if (!endclick)
                    {
                        for (n = 0; n < num + 1; n++)
                        {
                            ptxt = pline.GetPointAtDist(step * n);

                            vec = pline.GetFirstDerivative(ptxt).GetPerpendicularVector();

                            ppt = (ptxt + vec * theight * 8).TransformBy(ucs);

                            pptx = (ptxt + vec * theight * 5).TransformBy(ucs);

                            ln = new Line(ptxt, ppt);

                            btr.AppendEntity(ln);

                            tr.AddNewlyCreatedDBObject(ln, true);

                            vect = ln.GetFirstDerivative(ppt).GetPerpendicularVector();

                            txt = new DBText();

                            txt.Position = pptx;

                            txt.TextString = string.Format("{0:f2}", n * step);// <---  change precision to your needs*************************************************

                            txt.HorizontalMode = TextHorizontalMode.TextCenter;

                            txt.VerticalMode = TextVerticalMode.TextBottom;

                            txt.AlignmentPoint = pptx;

                            ang = ln.Angle + Math.PI;

                            if ((ang > Math.PI / 2) || (ang < Math.PI * 1.5))

                                ang = ang + Math.PI;
                            txt.Rotation = ang;

                            btr.AppendEntity(txt);

                            tr.AddNewlyCreatedDBObject(txt, true);

                        }

                        ptxt = pline.EndPoint;

                        vec = pline.GetFirstDerivative(ptxt).GetPerpendicularVector();

                        ppt = (ptxt + vec * theight * 8).TransformBy(ucs);

                        pptx = (ptxt + vec * theight * 5).TransformBy(ucs);

                        ln = new Line(ptxt, ppt);

                        btr.AppendEntity(ln);

                        tr.AddNewlyCreatedDBObject(ln, true);

                        vect = ln.GetFirstDerivative(ppt).GetPerpendicularVector();

                        txt = new DBText();

                        txt.Position = pptx;

                        txt.TextString = string.Format("{0:f2}", leng);// <---  change precision to your needs*************************************************

                        txt.HorizontalMode = TextHorizontalMode.TextCenter;

                        txt.VerticalMode = TextVerticalMode.TextBottom;

                        txt.AlignmentPoint = pptx;

                        ang = ln.Angle + Math.PI;

                        if ((ang > Math.PI / 2) || (ang < Math.PI * 1.5))

                            ang = ang + Math.PI;

                        txt.Rotation = ang;

                        btr.AppendEntity(txt);

                        tr.AddNewlyCreatedDBObject(txt, true);
                    }
                    else
                    {
                        //---------------------------------------------------
                        for (n = num; n >= 0; n--)
                        {
                            ptxt = pline.GetPointAtDist(step * n);

                            vec = pline.GetFirstDerivative(ptxt).GetPerpendicularVector();

                            ppt = (ptxt + vec * theight * 8).TransformBy(ucs);

                            pptx = (ptxt + vec * theight * 5).TransformBy(ucs);

                            ln = new Line(ptxt, ppt);

                            btr.AppendEntity(ln);

                            tr.AddNewlyCreatedDBObject(ln, true);

                            vect = ln.GetFirstDerivative(ppt).GetPerpendicularVector();

                            txt = new DBText();

                            txt.Position = pptx;

                            txt.TextString = string.Format("{0:f2}", n * step);// <---  change precision to your needs*************************************************

                            txt.HorizontalMode = TextHorizontalMode.TextCenter;

                            txt.VerticalMode = TextVerticalMode.TextBottom;

                            txt.AlignmentPoint = pptx;

                            ang = ln.Angle + Math.PI;

                            if ((ang > Math.PI / 2) || (ang < Math.PI * 1.5))

                                ang = ang + Math.PI;

                            txt.Rotation = ang;

                            btr.AppendEntity(txt);

                            tr.AddNewlyCreatedDBObject(txt, true);

                        }

                        ptxt = pline.EndPoint;

                        vec = pline.GetFirstDerivative(ptxt).GetPerpendicularVector();

                        ppt = (ptxt + vec * theight * 8).TransformBy(ucs);

                        pptx = (ptxt + vec * theight * 5).TransformBy(ucs);

                        ln = new Line(ptxt, ppt);

                        btr.AppendEntity(ln);

                        tr.AddNewlyCreatedDBObject(ln, true);

                        vect = ln.GetFirstDerivative(ppt).GetPerpendicularVector();

                        txt = new DBText();

                        txt.Position = pptx;

                        txt.TextString = string.Format("{0:f2}", leng);// <---  change precision to your needs*************************************************

                        txt.HorizontalMode = TextHorizontalMode.TextCenter;

                        txt.VerticalMode = TextVerticalMode.TextBottom;

                        txt.AlignmentPoint = pptx;

                        ang = ln.Angle + Math.PI;

                        if ((ang > Math.PI / 2) || (ang < Math.PI * 1.5))

                            ang = ang + Math.PI;

                        txt.Rotation = ang;

                        btr.AppendEntity(txt);

                        tr.AddNewlyCreatedDBObject(txt, true);

                    }

                    db.TransactionManager.QueueForGraphicsFlush();

                    tr.Commit();
                }
                catch (Autodesk.AutoCAD.Runtime.Exception ex)
                {
                    MessageBox.Show(string.Format("Error:\n{0}\nTrace:\n{1}", ex.Message, ex.StackTrace));
                }
                finally
                {
                    Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("osmode", osm);
                }
            }

 

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
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 or visit the Installation and Licensing Forum to get help installing your software.