Author Topic: Emulating AutoCAD's DIVIDE command  (Read 3173 times)

0 Members and 1 Guest are viewing this topic.

jlatimer11

  • Mosquito
  • Posts: 13
Emulating AutoCAD's DIVIDE command
« on: January 17, 2013, 03:22:35 PM »
Hello all.

I've done a search all over and can't find the solution for something that is probably very simple, but I can't wrap my head around it.

I am simply looking to take a line and divide it and get point3d list of each division, much akin to the nodes being placed on a line when the regular divide command is run.

Any suggestions?

n.yuan

  • Bull Frog
  • Posts: 348
Re: Emulating AutoCAD's DIVIDE command
« Reply #1 on: January 17, 2013, 03:58:22 PM »
Assume your target for dividing is entity of Curve type (line, polyline, arc, circle). So, you know the curve's length. You divide the length by the number of segement, you get distance (along the curve) of each segment. Then you call

Curve.GetPointAtDist(double distance)

to get a Point3d along the curve.

Once you get all the points (Point3d), then it is up to you to actually emulate the Divide command (draw DBPoint at each point, or draw something like block at the points), or do something with the group of Point3ds.

jlatimer11

  • Mosquito
  • Posts: 13
Re: Emulating AutoCAD's DIVIDE command
« Reply #2 on: January 17, 2013, 04:01:35 PM »
Assume your target for dividing is entity of Curve type (line, polyline, arc, circle). So, you know the curve's length. You divide the length by the number of segement, you get distance (along the curve) of each segment. Then you call

Curve.GetPointAtDist(double distance)

to get a Point3d along the curve.

Once you get all the points (Point3d), then it is up to you to actually emulate the Divide command (draw DBPoint at each point, or draw something like block at the points), or do something with the group of Point3ds.

Works perfectly. Thanks!

zoltan

  • Guest
Re: Emulating AutoCAD's DIVIDE command
« Reply #3 on: January 18, 2013, 08:34:03 AM »
Another way to do this could be to get a Curve3d by calling Curve.GetGeCurve() and then use the Curve3d.GetSamplePoints(int).  This will return an array of PointOnCurve3d.

fixo

  • Guest
Re: Emulating AutoCAD's DIVIDE command
« Reply #4 on: January 18, 2013, 04:31:35 PM »
My 2 cents
 
Code: [Select]
      [CommandMethod("die")]

        public void divideTestCommand()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;
            Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("pdmode", 96);
            Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("pdsize", 2);
            Transaction tr = doc.TransactionManager.StartOpenCloseTransaction();
            try
            {
                using (tr)
                {
                    Curve pline = null;

                    PromptEntityOptions peo = new PromptEntityOptions("\nSelect an Curve >>");
                    peo.SetRejectMessage("\nSelect Curve only >>");
                    peo.AddAllowedClass(typeof(Curve), 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;
                    pline = (Curve)ent as Curve;
                    if (pline == null)
                    {
                        ed.WriteMessage("\nError: selected entity is not a Curve");
                        return;
                    }
                    int num = 36;
                    double delta = (pline.EndParam - pline.StartParam) / (num + 1);
                    Point3dCollection pts = new Point3dCollection();
                    Point3d cp;
                    for (double par = pline.StartParam; par <= pline.EndParam; par += delta)
                    {
                        cp = pline.GetClosestPointTo(pline.GetPointAtParameter(par), false);
                        pts.Add(cp);
                    }
                    if (pts == null) return;
                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                    foreach (Point3d ptcur in pts)
                    {

                        DBPoint pt = new DBPoint(ptcur);
                        pt.SetDatabaseDefaults();
                        pt.ColorIndex = 5;
                        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.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nPokey\n");
            }
        }
« Last Edit: January 18, 2013, 06:07:21 PM by fixo »

owenwengerd

  • Bull Frog
  • Posts: 451

fixo

  • Guest
Re: Emulating AutoCAD's DIVIDE command
« Reply #6 on: January 18, 2013, 05:46:24 PM »
Well, agreed I often forget about Closestpoint issue
Kind regards,

owenwengerd

  • Bull Frog
  • Posts: 451
Re: Emulating AutoCAD's DIVIDE command
« Reply #7 on: January 18, 2013, 10:06:37 PM »
Well, agreed I often forget about Closestpoint issue

Did you test your code with a QuirkyPolyline?