I have a little project that I need to get started, has anyone got a code snippet for turning a multi vertex polyline into a list of coordinates rather than out-putting them to a file.
Solved! Go to Solution.
Solved by Kent1Cooper. Go to Solution.
Solved by pbejse. Go to Solution.
(defun c:56 (/ obj f_name f_zm ff ) (vl-load-com) (while (not obj) (if (setq obj (car (entsel "\nPIK POLYLINE"))) (if (not (wcmatch (cdr (assoc 0 (entget obj))) "LWPOLYLINE")) (setq obj nil) ) ) ) (setq f_name (strcat (vla-get-path (vla-get-activedocument (vlax-get-acad-object))) "\\COOR_PLINE.txt")) (if (findfile f_name) (progn (setq f_zm (strcat (vla-get-path (vla-get-activedocument (vlax-get-acad-object))) "\\C_z.txt")) (setq ff (open f_zm "w")) (princ (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 10)) (entget obj))) ff) (princ "\n" ff) (close ff) (vl-file-copy f_zm f_name T) (vl-file-delete f_zm) ) (progn (setq ff (open f_name "w")) (princ (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 10)) (entget obj))) ff) (princ "\n" ff) (close ff) ) ) (princ) )
For clarification. Are you refering to POLYLINE (3dpolyline) AKA Heave Polyline or LWPOLYLINE (lightweight poliline)
copy and paste this smippet onto your command prompt, select the entity in question and tell me what you see
(cdr (assoc 0 (entget (Car (entsel)))))
@richie_hodgson wrote:
Definitely a lwpolyline. By the look of above (setq pts (cdr (assoc.......) will get me what I want
Well not really, there's a couple of ways to retrieve the points. and here's one way
(defun c:pts (/ pl ) (and (setq pl (ssget '((0 . "LWPOLYLINE")))) (foreach itm (setq ptlist (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 10) ) (entget (ssname pl 0)) ) ) ) (print itm) ) ) (princ) )
Hello
The routine PTEXPORT does the job ...
http://www.dotsoft.com/freestuff.htm
Patrice BRAUD
@richie_hodgson wrote:
.... has anyone got a code snippet for turning a multi vertex polyline into a list of coordinates ....
For a lightweight polyline, if you're in [or parallel to] the World Coordinate Systam and you can take a list that does not include Z coordinates:
(mapcar 'cdr
(vl-remove-if-not '(lambda (x) (= (car x) 10))
(entget (car (entsel "\nSelect lightweight Polyline: ")))
)
)
If you need Z coordinates:
(setq
edata (entget (car (entsel "\nSelect lightweight Polyline: ")))
elev (cdr (assoc 38 edata))
)
(mapcar '(lambda (x) (append x (list elev))); add Z coordinate to all
(mapcar 'cdr
(vl-remove-if-not '(lambda (y) (= (car y) 10)) edata)
)
)
Add object-selection controls, etc. Add (vl-load-com) if needed.
I'll throw another variety out there. Should handle all polylines, splines, lines, arcs, & 3dfaces.
http://www.4d-technologies.com/techcenter/pl/pl_plist.lsp
It requires this function also:
http://www.4d-technologies.com/techcenter/list/li_item.lsp
@richie_hodgson wrote:
I am going to do some tinkering with that list and would like to push the results back into the selected somehow, ... is this a simple thing
For that, I would recommend using the 'Coordinates property of the Polyline as a VLA object. That starts out with an un-differentiated list of numbers, X & Y [& Z for other than LW Polylines] coordinate values in a continuous list. You can break that up into points for the vertices, for example this way:
(vl-load-com); if needed
(defun C:CoordList (/ plent plobj coords num pt)
(setq
plent (car (entsel "\nSelect Polyline: "))
plobj (vlax-ename->vla-object plent)
coords (vlax-get plobj 'Coordinates); un-differentiated list of X Y [& Z if applicable] coordinate values
); setq
(setq num (if (= (cdr (assoc 0 (entget plent))) "LWPOLYLINE") 2 3))
; LW Polylines have only X & Y; "heavy" 2D & 3D have X Y & Z
(repeat (/ (length coords) num)
(repeat num ; number of coordinates to separate into a point list
(setq
pt (append pt (list (car coords)))
coords (cdr coords)
)
); repeat
(setq
ptlist (cons pt ptlist); put that point into list of points
pt nil ; reset for next point
); setq
); repeat
(setq ptlist (reverse ptlist)); list of coordinates divided up into point lists
); defun
You can then mess with the values of the points in that list, and when you're done, conglomerate them back into an un-differentiated list of numbers with:
(setq coords (apply 'append ptlist))
Then [and this is the best part], you can just impose that adjusted list back on the Polyline:
(vlax-put plobj 'Coordinates coords)
It's much easier to do that than to try to [for example] interweave (assoc 10) vertex point locations in between segment width and bulge-factor entries in a LWPolyline's entity data list, or work through the separate entity names of the vertices in "heavy" 2D or 3D Polylines. It works for any variety of Polyline, as long as you manipulate point values in the right number of coordinates, i.e. don't do something that would add a Z coordinate into adjusted vertex locations for a LWPolyline, or if your process results in that, strip it off before conglomerating the coordinates list again.
@richie_hodgson wrote:I have a little project that I need to get started, has anyone got a code snippet for turning a multi vertex polyline into a list of coordinates rather than out-putting them to a file.
This will return a list of point in WCS. Note that for a closed polyline the start and the end point are the same and both are returned in the result list.
Works for light, heavy or 3D polylines.
(defun pline_points (e / i r) (repeat (1+ (setq i (fix (vlax-curve-getendparam e)))) (setq r (cons (vlax-curve-getpointatparam e i) r) i (1- i) ) ) r )
And here is a test function. Works on any UCS.
(defun c:test ( / ss) (if (setq ss (ssget ":E:S" '((0 . "*POLYLINE") (-4 . "<NOT") (-4 . "<OR") (-4 . "&=") (70 . 2);Curve-fit (-4 . "&=") (70 . 4);Spline-fit (-4 . "&=") (70 . 16);3D polygon mesh (-4 . "&=") (70 . 32);polygon mesh (-4 . "&=") (70 . 64);polyface mesh (-4 . "OR>") (-4 . "NOT>") ) ) ) (foreach p (pline_points (ssname ss 0)) (entmake (list '(0 . "POINT") (cons 10 p) ) ) ) ) (princ) )
FWIW, I believe this:
'((0 . "*POLYLINE") (-4 . "<NOT") (-4 . "<OR") (-4 . "&=") (70 . 2);Curve-fit (-4 . "&=") (70 . 4);Spline-fit (-4 . "&=") (70 . 16);3D polygon mesh (-4 . "&=") (70 . 32);polygon mesh (-4 . "&=") (70 . 64);polyface mesh (-4 . "OR>") (-4 . "NOT>") ) )
Could be written:
'((0 . "POLYLINE") (-4 . "<NOT") (-4 . "&") (70 . 118) (-4 . "NOT>"))
@Lee_Mac wrote:
FWIW, I believe this:
@phanaem wrote:'((0 . "*POLYLINE") (-4 . "<NOT") (-4 . "<OR") (-4 . "&=") (70 . 2);Curve-fit (-4 . "&=") (70 . 4);Spline-fit (-4 . "&=") (70 . 16);3D polygon mesh (-4 . "&=") (70 . 32);polygon mesh (-4 . "&=") (70 . 64);polyface mesh (-4 . "OR>") (-4 . "NOT>") ) )
Could be written:
'((0 . "POLYLINE") (-4 . "<NOT") (-4 . "&") (70 . 118) (-4 . "NOT>"))
While it's a little shorter (less code), it makes it less obvious as to exactly what it's doing. The first version explicity tells what is being excluded. WIth the second version, you'd have to have understand binary to know what it's doing.
Don Ireland
Engineering Design Technician
Can't find what you're looking for? Ask the community or share your knowledge.