![](/uploads/1/2/7/1/127157997/686905031.jpg)
I was inspired by some code sent out by Viru Aithal to write a command that iterates through the vertices of the various types of AutoCAD polyline:
This course gives you a solid understanding of AutoCAD VBA concepts and fundamentals to develop your own applications. You will also learn how to declare an array of doubles that will be used as a storage for the vertices. Draw a LWPolyline and Polyline Objects.
- Optimized (or 'lightweight') 2D polylines, which store an array of 2D vertices
- Old-format (or 'heavyweight') 2D polylines, which contain a sequence of 2D vertex objects
- 3D polylines, which contain a sequence of 3D vertex objects
Polylines that contain vertex objects are containers for the object IDs for the various vertices - you can use foreach to loop through the vertex objects, opening them one-by-one.
For optimized polylines we need to loop using a counter which gets passed as an index to a retrieval function. In this case we use GetPoint2dAt() to get the Point2d at that index, but we might also have used other access functions, depending on our interest:
- GetPoint3dAt()
- GetLineSegmentAt()
- GetLineSegment2dAt()
- GetArcSegmentAt()
- GetArcSegment2dAt()
- GetBulgeAt()
- GetStartWidthAt()
- GetEndWidthAt()
Here's the C# code:
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
namespace IterateObjects
{
publicclassCommands
{
[CommandMethod('LV')]
staticpublicvoid ListVertices()
{
Document doc =
Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
PromptEntityResult per =
ed.GetEntity('Select a polyline');
if (per.Status PromptStatus.OK)
{
Transaction tr =
db.TransactionManager.StartTransaction();
using (tr)
{
DBObject obj =
tr.GetObject(per.ObjectId, OpenMode.ForRead);
// If a 'lightweight' (or optimized) polyline
Polyline lwp = obj asPolyline;
if (lwp != null)
{
// Use a for loop to get each vertex, one by one
int vn = lwp.NumberOfVertices;
for (int i = 0; i < vn; i++)
{
// Could also get the 3D point here
Point2d pt = lwp.GetPoint2dAt(i);
ed.WriteMessage('n' + pt.ToString());
}
}
else
{
// If an old-style, 2D polyline
Polyline2d p2d = obj asPolyline2d;
if (p2d != null)
{
// Use foreach to get each contained vertex
foreach (ObjectId vId in p2d)
![Autocad Autocad](http://what-when-how.com/wp-content/uploads/2012/07/tmp89fa217_thumb.png)
{
Vertex2d v2d =
(Vertex2d)tr.GetObject(
vId,
OpenMode.ForRead
);
ed.WriteMessage(
'n' + v2d.Position.ToString()
);
}
}
else
{
// If an old-style, 3D polyline
![Pagla Pagla](/uploads/1/2/7/1/127157997/301557404.jpg)
Polyline3d p3d = obj asPolyline3d;
if (p3d != null)
{
// Use foreach to get each contained vertex
foreach (ObjectId vId in p3d)
{
PolylineVertex3d v3d =
(PolylineVertex3d)tr.GetObject(
vId,
OpenMode.ForRead
);
ed.WriteMessage(
'n' + v3d.Position.ToString()
);
}
}
}
}
// Committing is cheaper than aborting
tr.Commit();
}
}
}
}
}
Is there a way to select everything within a closed polyline by simplyselecting the polyline?(Please note: This is not the same as the cpolygon or wpolygon. You haveto pick each individual point for that command. I just want to select theline.)Jason gave me the following lisp file (see below), but I don't know Lisp, soI'm trying to translate it to VBA. I can select the polyline with theSelectOnScreen command, and can select every entity within the drawing, butam unsure how to test if the entity is within the boundary.Has this already been done or am I reinventing the wheel?TIA, -KirstenLisp file:(defun C:Test (ssget 'wp' (massoc 10 (entget (car (entsel 'nselect polyline:'))))))(defun massoc (key alist / x nlist)(foreach x alist(if (eq key (car x))(setq nlist (cons (cdr x) nlist))))(reverse nlist))TomD08.03.02 08:24.
Kirsten wrote in messagenews:[email protected]. Is there a way to select everything within a closed polyline by simply selecting the polyline? Has this already been done or am I reinventing the wheel? TIA, -KirstenHave you looked into the SelectionSet.SelectByPolygon method?
It soundslike exactly what you need. If you need help with the point retrieval,there are a couple of web sites with good descriptions on getting LWPoly andPolyline point lists.A word of caution: I did something similar to that with lips a few yearsback, but using the SSGET function of lisp, with the WP option (I think)only picked up objects currently visible on-screen. If the user selected apolyline that went beyond the current zoom scale, objects would bepotentially missed.
I don't know if this is still true, or what wouldeliminate the problem, but something to consider and test for. I just gaveup on it, because I didn't need it very often.THTMinkwitz Design08.03.02 08:45. Hi,To over come the object not on screen issue:After selecting the polyline get its boundingbox and do a zoom centre on themiddle of the bounding box with a scale matching twice the diagonal length.-Laurie ComerfordCADApps'TomD' wrote in messagenews:[email protected]. Kirsten wrote in message news:[email protected].
A word of caution: I did something similar to that with lips a few years back, but using the SSGET function of lisp, with the WP option (I think) only picked up objects currently visible on-screen. If the user selecteda polyline that went beyond the current zoom scale, objects would be potentially missed. I don't know if this is still true, or what would eliminate the problem, but something to consider and test for. I justgave up on it, because I didn't need it very often.Alan McCormack08.03.02 13:24. 'Kirsten' wrote in message news.Here's a snippet of code that I wrote for an appication that does whatyou want (I think). You might be able to use a similar technique:varVertices = objPolyline.CoordinatesintFilterType(0) = 0varFilter(0) = 'TEXT'varFilterType = intFilterTypevarFilterData = varFilterlngSelectMode = acSelectionSetCrossingPolygonSet ssetBFText = ThisDrawing.SelectionSets.Add('BondFingerText')ssetBFText.SelectByPolygonlngSelectMode, varVertices, varFilterType, varFilterDataRob Starz10.03.02 20:58.
I have not tried this but I am curious as to the result if the selectedPolyline has an arc. I could be wrong but from what see it would miss itemsif the fall outside the straight line of the start and end point. Am Iwrong in saying this.What I have been doing is converting the Polyline into a Region then testingall objects that fall within the BoundBox to see if they intersect.
Thisguaranties they are inside even arcs.- -+- Rob Starz Stardsign cad solutions AEC Designer / Consultant / Developer Arch Desktop tools.coming soon e-Training for. programming Arch Desktop learn what you can do.with little input.Minkwitz Design11.03.02 07:56. Hi Rob,That's a good point, something I didn't even take into consideration.Andthe bounding box idea isn't a bad work arouund, except it could have theopposite effect. You could wind up selecting entities outside of thepolyline if they fall within the bounding box. I suppose the only way tohandle it accurately would be to select the set assuming that none ofthe polyline segments are arcs, then go back and test each segment.
Ifit's an arc, you would have to calculate the area it encompasses andthen test for entities that fall within that area but are not part ofthe set, then add them to the set. That could become pretty involved.Out of curiousity, under what circumstances would you want to selectenities that are contained in an existing polyline?-Joshp.s.- sorry Tom, I didn't catch your response to the original postbefore posting my own redundant version.Rob Starz11.03.02 08:15.
Maybe I didn't state myself properly.Select the PlineGet BoundBoxCreate Region from Polylinecreate a selection set of object using BoundBox of Polylinecheck to see if each object intersects with Region.This will easily grab all objects even if there is an arc with a largebulge.- -+- Rob Starz Stardsign cad solutions AEC Designer / Consultant / Developer Arch Desktop tools.coming soon e-Training for. programming Arch Desktop learn what you can do.with little input.Rob Starz11.03.02 08:17. Yes if you just did the BoundBox it would grab all.But I if you checkand see if each object Interfers with the created Region and remove the onesthat don't you will get all objects that fall within the Polyline.Works great on my end.- -+- Rob Starz Stardsign cad solutions AEC Designer / Consultant / Developer Arch Desktop tools.coming soon e-Training for. programming Arch Desktop learn what you can do.with little input.TomD11.03.02 11:57. Minkwitz Design wrote in messagenews:[email protected]. Out of curiousity, under what circumstances would you want to select enities that are contained in an existing polyline?
-JoshFor civil/surveying, that type of thing can be handy quite often. Oneexample would be to select objects within boundary polyline to createsurface models from. p.s.- sorry Tom, I didn't catch your response to the original post before posting my own redundant version.No problem. I'm not the best at conveying the thoughts in my scrambledhead, so most of my posts can't hurt a second perspective, even if it'ssaying basically the same thing.;)Interesting topic (at least to me.)TomD11.03.02 11:55.
![](/uploads/1/2/7/1/127157997/686905031.jpg)