.NET

Reply
Valued Contributor
dynamicscope
Posts: 95
Registered: ‎04-04-2012
Message 1 of 4 (12,033 Views)
Accepted Solution

How to iterate through Polyline3d vertices with "for" loop?

12033 Views, 3 Replies
02-05-2013 01:00 AM

I know I can use the following...

 

...
Polyline3d pl3d = acEnt as Polyline3d;
foreach (ObjectId id in pl3d)
{
    PolylineVertex3d plv3d = tr.GetObject(id, OpenMode.ForRead) as PolylineVertex3d;
    Point3d p3d = plv3d.Position;
    ...
}

 

But I need to work with "for" loop.

It seems "pl3d[i]" does not work.

 

Is there other ways to iterate through Polyline3d vertices?

using System;
using System.Linq;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
[assembly: CommandClass(typeof(Rivilis.CurveUtils))]
namespace Rivilis
{
public class CurveUtils
{
[CommandMethod("ListPoly3D", CommandFlags.Modal)]
public static void ListPoly3D()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
PromptEntityOptions prCurv = new PromptEntityOptions("\nSelect Polyline3d: ");
prCurv.SetRejectMessage("not a Polyline3d");
prCurv.AddAllowedClass(typeof(Polyline3d), false);
PromptEntityResult resCurv = ed.GetEntity(prCurv);
if (resCurv.Status != PromptStatus.OK) return;
using (Transaction tr = doc.TransactionManager.StartTransaction()) {
Polyline3d pl3d = tr.GetObject(resCurv.ObjectId, OpenMode.ForRead) as Polyline3d;
ObjectId[] verts = pl3d.Cast<ObjectId>().ToArray();
ed.WriteMessage("\nNumber of vertexes: {0}", verts.Length);
for (int i = 0; i < verts.Length; i++) {
PolylineVertex3d vt = tr.GetObject(verts[i], OpenMode.ForRead) as PolylineVertex3d;
if (vt != null) {
ed.WriteMessage("\n[{0}] = {1}", i, vt.Position.ToString());
}
}
tr.Commit();
}
}
}
}

 

Valued Contributor
dynamicscope
Posts: 95
Registered: ‎04-04-2012
Message 2 of 4 (12,029 Views)

Re: How to iterate through Polyline3d vertices with "for" loop?

02-05-2013 01:14 AM in reply to: dynamicscope

Hmm....

 

Looks like the following will do the job.

 

Polyline3d pline3d = ent as Polyline3d;
for (double i = 0; i < pline3d.EndParam; i = i + 1 )
{
    Point3d p3d = pline3d.GetPointAtParameter(i);
}

 Is that correct?

Moderator
Alexander.Rivilis
Posts: 1,446
Registered: ‎04-09-2008
Message 3 of 4 (12,026 Views)

Re: How to iterate through Polyline3d vertices with "for" loop?

02-05-2013 01:39 AM in reply to: dynamicscope
using System;
using System.Linq;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;

[assembly: CommandClass(typeof(Rivilis.CurveUtils))]

namespace Rivilis
{
  public class CurveUtils
  {
    [CommandMethod("ListPoly3D", CommandFlags.Modal)]
    public static void ListPoly3D()
    {
      Document doc = Application.DocumentManager.MdiActiveDocument;
      Editor ed = doc.Editor;
      PromptEntityOptions prCurv = new PromptEntityOptions("\nSelect Polyline3d: ");
      prCurv.SetRejectMessage("not a Polyline3d");
      prCurv.AddAllowedClass(typeof(Polyline3d), false);
      PromptEntityResult resCurv = ed.GetEntity(prCurv);
      if (resCurv.Status != PromptStatus.OK) return;

      using (Transaction tr = doc.TransactionManager.StartTransaction()) {
        Polyline3d pl3d = tr.GetObject(resCurv.ObjectId, OpenMode.ForRead) as Polyline3d;
        ObjectId[] verts = pl3d.Cast<ObjectId>().ToArray();
        ed.WriteMessage("\nNumber of vertexes: {0}", verts.Length);
        for (int i = 0; i < verts.Length; i++) {
          PolylineVertex3d vt = tr.GetObject(verts[i], OpenMode.ForRead) as PolylineVertex3d;
          if (vt != null) {
            ed.WriteMessage("\n[{0}] = {1}", i, vt.Position.ToString());
          }
        }
        tr.Commit();
      }
    }
  }
}

 


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Moderator
Alexander.Rivilis
Posts: 1,446
Registered: ‎04-09-2008
Message 4 of 4 (12,018 Views)

Re: How to iterate through Polyline3d vertices with "for" loop?

02-05-2013 01:57 AM in reply to: dynamicscope

dynamicscope wrote:
Is that correct?

It is look like you skip last vertex...


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are You Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!