Author Topic: Getting the Midpoint of each Polyline Segment  (Read 985 times)

0 Members and 1 Guest are viewing this topic.

Offline fixo

  • Full Member
  • ***
  • Posts: 135
  • Karma: +4/-0
  • Gender: Male
    • prefered language: C
    • Prog expertise: Good
    • View Profile
Getting the Midpoint of each Polyline Segment
« on: January 10, 2013, 03:08:32 PM »
        Getting the Midpoint of each Polyline Segment
        based on article by Fenton Webb here:
        http://adndevblog.typepad.com/autocad/2013/01/getting-the-midpoint-of-each-polyline-segment-using-objectarx.html
 
Code: [Select]
      [CommandMethod("smp")]
        public void ShowMidPointOnEachSegment()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

            Editor ed = doc.Editor;

            Database db = HostApplicationServices.WorkingDatabase;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTableRecord btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;

                Entity ename;

                PromptEntityOptions peo = new PromptEntityOptions("\nSelect Polyline: ");

                peo.SetRejectMessage("\nMust be a Polyline.");

                peo.AddAllowedClass(typeof(Polyline), false);
               
                PromptEntityResult retval = ed.GetEntity(peo);

                if (retval.Status != PromptStatus.OK)   return;

                Curve curv;

                ObjectId id;

                id = retval.ObjectId;

                ename = tr.GetObject(id, OpenMode.ForWrite) as Entity;

                curv = ename as Curve;
                if (curv == null)
                {
                    ed.WriteMessage("\nCould not cast object as Curve.");

                    return;
                }
                if (!curv.IsWriteEnabled)

                    curv.UpgradeOpen();

                double startParam, endParam;

                // get the start param, usually it starts at 0 or 1

                startParam = curv.StartParam;

                ed.WriteMessage("\nStartParam is: {0:f3}\n", startParam);

                // get the end param, for a polyline it's the total number of

                // vertex's -1

                endParam = curv.EndParam;

                ed.WriteMessage("\nEndParam is: {0:f3}\n", endParam);

                // now loop the parameters, adding 1.0 each iteration

               
                for (double i = startParam; i < endParam; ++i)
                {
                    Point3d pt;

                    pt = curv.GetPointAtParameter(i + 0.5);
                  //  DBPoint dp = new DBPoint(pt);
                    Circle cr = new Circle(pt, Vector3d.ZAxis, 2.5);
                    btr.AppendEntity(cr);
                    tr.AddNewlyCreatedDBObject(cr, true);

                    ed.WriteMessage("\nPoint: {0}, {1}, {2}\n", pt[0], pt[1], pt[2]);

                }
                tr.Commit();
            }
        }
« Last Edit: June 23, 2013, 08:02:08 AM by fixo »