Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Pick a polyline and create a list of its coordinates

15 REPLIES 15
SOLVED
Reply
Message 1 of 16
richie_hodgson
12870 Views, 15 Replies

Pick a polyline and create a list of its coordinates

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.

Richie
15 REPLIES 15
Message 2 of 16
Anonymous
in reply to: richie_hodgson

(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)
)

 

Message 3 of 16

So I asked if there was a way to create a list without outputting to a file, I am not seeing that happening here
Richie
Message 4 of 16
pbejse
in reply to: richie_hodgson

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)))))

Message 5 of 16

Definitely a lwpolyline. By the look of above (setq pts (cdr (assoc.......) will get me what I want
Richie
Message 6 of 16

I am going to do some tinkering with that list and would like to push the results back into the selected somehow, I am going to do some reading but is this a simple thing
Richie
Message 7 of 16
pbejse
in reply to: richie_hodgson


@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)
  )

 

Message 8 of 16
Anonymous
in reply to: pbejse

sorry for bad english. Lisp records the coordinates vertex in txt file, the file name coor_pline.txt in the same folder as the dwg file.
Message 9 of 16
braudpat
in reply to: richie_hodgson

 

Hello

 

The routine PTEXPORT does the job ...

http://www.dotsoft.com/freestuff.htm

 

Patrice ( Supporting Troops ) - Autodesk Expert Elite
If you are happy with my answer please mark "Accept as Solution" and if very happy please give me a Kudos (Felicitations) - Thanks

Patrice BRAUD

EESignature


Message 10 of 16


@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.

 

Kent Cooper, AIA
Message 11 of 16
rkmcswain
in reply to: richie_hodgson

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

R.K. McSwain     | CADpanacea | on twitter
Message 12 of 16


@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.

Kent Cooper, AIA
Message 13 of 16
phanaem
in reply to: richie_hodgson


@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)
  )

 

Message 14 of 16
Lee_Mac
in reply to: phanaem

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>"))
Message 15 of 16
phanaem
in reply to: Lee_Mac

You're right Lee. Thanks.

 

Message 16 of 16
doni49
in reply to: Lee_Mac


@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




If a reply solves your issue, please remember to click on "Accept as Solution". This will help other users looking to solve a similar issue. Thank you.


Please do not send a PM asking for assistance. That's what the forums are for. This allows everyone to benefit from the question asked and the answers given.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report