Jump to content
Tharwat

Vertices of a Polyline

Recommended Posts

Tharwat

Hello.

 

How could I get the Vertices of a Polyline after selecting it.

 

Regards,

Share this post


Link to post
Share on other sites
alanjt

(vlax-get OBJ 'Coordinates)

or

(entget eName) and use everything that has a DXF 10 code.

 

 

EDIT: I didn't realize you said Polyline. If so, forget the entget part (for LWPOlylines). You'll have to entnext through the polyline and take each 10 code.

Share this post


Link to post
Share on other sites
Lee Mac

Quite a few ways to do that:

 

1) Vanilla AutoLISP: Retrieving all the DXF 10 codes:

 

(defun mAssoc ( key lst )
 (foreach x lst
   (if (= key (car x))
     (setq l (cons (cdr x) l))
   )
 )
 (reverse l)
)

(mAssoc 10 (entget <LWPolyline>))

2) Visual LISP: getting the Coordinates Property:

 

(defun LM:lst->2DPoint ( l ) ;; © Lee Mac 2010
 (if l (cons (list (car l) (cadr l)) (LM:lst->2DPoint (cddr l))))
)

(LM:lst->2DPoint (vlax-get <LWPolyline VLA-Object> 'Coordinates))

Of course, these methods apply to an LWPolyline.

 

EDIT: Oops! Didn't see you posted Alan, sorry.

Share this post


Link to post
Share on other sites
rkmcswain

One way...

 


(vlax-safearray->list
 (vlax-variant-value
  (vla-get-Coordinates
   (vlax-ename->vla-object
    ent
   )
 )
)
)

 

 

Results with vary depending on if the selected entity is a LWpolyline or a Polyline.

More details here: http://cadpanacea.com/node/188

Share this post


Link to post
Share on other sites
BlackBox
Hello.

 

How could I get the Vertices of a Polyline after selecting it.

 

Regards,

 

Like so:

 

(defun c:TEST (/ ss coords)
 (vl-load-com)
 (cond
   (*activeDoc*)
   ((setq *activeDoc* (vla-get-activedocument (vlax-get-acad-object)))))
 (prompt "\n  >>  Select Line Object To Display Coordinates: ")
 (if (setq ss (ssget ":S:E" '((0 . "*POLYLINE"))))
   (progn
     (vlax-for x  (setq ss (vla-get-activeselectionset *activeDoc*))
       (setq coords
        (vlax-safearray->list
          (vlax-variant-value
            (vla-get-coordinates x)))))
     (vla-delete ss)
     (terpri)
     (prompt "\n  >>  Coordinates List: \n\t\t\t")
     (princ coords)
     (terpri)))
 (princ)) ;_end defun

 

Hope this helps!

Share this post


Link to post
Share on other sites
Tharwat

Thank you gentlemen.

 

That's really great and marvellous.

 

Regards,

Share this post


Link to post
Share on other sites
alanjt
One way...

 


(vlax-safearray->list
 (vlax-variant-value
  (vla-get-Coordinates
   (vlax-ename->vla-object
    ent
   )
 )
)
)

 

 

Results with vary depending on if the selected entity is a LWpolyline or a Polyline.

More details here: http://cadpanacea.com/node/188

 

Like so:

 

(defun c:TEST (/ ss coords)
 (vl-load-com)
 (cond
   (*activeDoc*)
   ((setq *activeDoc* (vla-get-activedocument (vlax-get-acad-object)))))
 (prompt "\n  >>  Select Line Object To Display Coordinates: ")
 (if (setq ss (ssget ":S:E" '((0 . "*POLYLINE"))))
   (progn
     (vlax-for x  (setq ss (vla-get-activeselectionset *activeDoc*))
       (setq coords
        (vlax-safearray->list
          (vlax-variant-value
            (vla-get-coordinates x)))))
     (vla-delete ss)
     (terpri)
     (prompt "\n  >>  Coordinates List: \n\t\t\t")
     (princ coords)
     (terpri)))
 (princ)) ;_end defun

 

Hope this helps!

 

 

Check out my post to see an alternative to vla-get-coordinats - saves a couple steps.

Share this post


Link to post
Share on other sites
rkmcswain
Check out my post to see an alternative to vla-get-coordinats [(vlax-get OBJ 'Coordinates)]- saves a couple steps.

 

Good reminder - I always forget about that....

Thanks.

Share this post


Link to post
Share on other sites
gile

Hi,

 

Another way, using vlax-curve* functions. Works with all curve polylines types (lw, 2d, 3d) returns WCS coordinates.

 

(defun polyCoords (pl / n l)
 (vl-load-com)
 (setq n (if (vlax-curve-IsClosed pl)
           (fix (vlax-curve-getEndParam pl))
           (1+ (fix (vlax-curve-getEndParam pl)))
         )
 )
 (while (/= 0 n)
   (setq l (cons (vlax-curve-getPointAtParam pl (setq n (1- n))) l))
 )
)

Share this post


Link to post
Share on other sites
fixo

Thanks, Gilles

Personally this likes me more

Regards,

 

Oleg

Share this post


Link to post
Share on other sites
BlackBox
Check out my post to see an alternative to vla-get-coordinats - saves a couple steps.

 

Good reminder - I always forget about that....

Thanks.

 

Agreed... thanks, Alan!

Share this post


Link to post
Share on other sites
sonali
Like so:

 

(defun c:TEST (/ ss coords)
 (vl-load-com)
 (cond
   (*activeDoc*)
   ((setq *activeDoc* (vla-get-activedocument (vlax-get-acad-object)))))
 (prompt "\n  >>  Select Line Object To Display Coordinates: ")
 (if (setq ss (ssget ":S:E" '((0 . "*POLYLINE"))))
   (progn
     (vlax-for x  (setq ss (vla-get-activeselectionset *activeDoc*))
       (setq coords
        (vlax-safearray->list
          (vlax-variant-value
            (vla-get-coordinates x)))))
     (vla-delete ss)
     (terpri)
     (prompt "\n  >>  Coordinates List: \n\t\t\t")
     (princ coords)
     (terpri)))
 (princ)) ;_end defun

 

Hope this helps!

 

Hello sir, the code what you have written is very helpful to me. But, I just wanted t know that, how can I get this data into a csv file in a single row.

Please help me, sir in his regard.

Share this post


Link to post
Share on other sites
BIGAL

Good task to learn lisp, instead of (princ coords) you would do a (write-line coords fo) look up lisp help how to "Open" a file. You will also need to add a bit more so its comma seperated. look up lisp help re "nth" to read value in a list. Also "Repeat" & "length"

Share this post


Link to post
Share on other sites
hanhphuc
Hello sir, the code what you have written is very helpful to me. But, I just wanted t know that, how can I get this data into a csv file in a single row.

Please help me, sir in his regard.

 

single row? X,Y,X,Y,X,Y....etc..?

not separated column X,Y?

 

[color="green"]
(defun c:TEST (/ ss coords [color="black"]fn f[/color] )
;;...
;;...
(terpri)
     (prompt "\n  >>  Coordinates List: \n\t\t\t")
     (princ coords)[/color]

(setq fn  (strcat (getvar "dwgprefix") "SONALI LWPOLY.csv") [color="green"]; <-- output file[/color]
	 f   (open fn "a")) [color="green"]; "a" to append, "w" to overite[/color]
(write-line
      (apply 'strcat
      (mapcar ''((x) (strcat (rtos x 2 3) ",")) [color="green"]; or "\t" for tab[/color]
	      coords
	      )
      )
      f
      )
(if f (close f))
(startapp "notepad" fn) ; <--optional
 
[color="green"]  (terpri)))
 (princ)) ;_end defun[/color]
   

Share this post


Link to post
Share on other sites
pBe

(defun c:TEST (/ ss coords fn f )

.....

(apply 'strcat

(mapcar ''((x) (strcat (rtos x 2 3) ",")) ; or "\t" for tab

coords

)

)

[/code]

 

I like :thumbsup:

Share this post


Link to post
Share on other sites
Lee Mac
(mapcar ''((x) (strcat (rtos x 2 3) ","))

 

I admire the variation, but just be aware that since this construct is equivalent to defining a function using defun-q, you may lose performance:

(defun mapcar-quote-lambda ( lst )
   (mapcar '(lambda ( x ) (+ 2 x)) lst)
)
(defun mapcar-function-lambda ( lst )
   (mapcar (function (lambda ( x ) (+ 2 x))) lst)
)
(defun mapcar-quote-quote ( lst )
   (mapcar ''(( x ) (+ 2 x)) lst)
)
(defun mapcar-defun-q ( lst )
   (mapcar '2+q lst)
)
(defun mapcar-defun ( lst )
   (mapcar '2+ lst)
)
(defun 2+ ( x ) (+ 2 x))
(defun-q 2+q ( x ) (+ 2 x))

_$ (setq lst '(1 2 3 4 5))
(1 2 3 4 5)
_$ (benchmark '((mapcar-quote-lambda lst)(mapcar-function-lambda lst)(mapcar-quote-quote lst)(mapcar-defun-q lst)(mapcar-defun lst)))
Benchmarking ...................Elapsed milliseconds / relative speed for 65536 iteration(s):

   (MAPCAR-QUOTE-LAMBDA LST)........1170 / 2.35 <fastest>
   (MAPCAR-FUNCTION-LAMBDA LST).....1186 / 2.32
   (MAPCAR-DEFUN LST)...............1186 / 2.32
   (MAPCAR-QUOTE-QUOTE LST).........2652 / 1.04
   (MAPCAR-DEFUN-Q LST).............2746 / 1 <slowest>

(Above results for 'uncompiled' code)

Share this post


Link to post
Share on other sites
hanhphuc
I admire the variation, but just be aware that since this construct is equivalent to defining a function using defun-q, you may lose performance

 

Thank you Lee, good advise :thumbsup:

i admit these variation is poor for speed (my signature noted: "lambda not optimized..")

@pBE : It's just for lazy typist :lol:

 

maybe slow due to extra quote? :P

Tools -> Check Text in Editor window

; warning: bad argument: (QUOTE (( ... ) ( ... )))

(MAPCAR (QUOTE (QUOTE ((X) (+ 2 X)))) LST)[color="green"] ; <-- 4 pairs parentheses, ie: extra (quote ) ?[/color]
(MAPCAR (FUNCTION (LAMBDA (X) (+ 2 X))) LST)[color="green"] ; <-- 3 pairs parentheses[/color]

 

another two more for testing

([color="blue"]defun-q-list-set[/color] '2+a  '(( x ) (+ 2 x)))

([color="blue"]set[/color] '2+b '(( x ) (+ 2 x)))

(defun mapcar-defun-qa ( lst )
   (mapcar '2+a lst)
)
(defun mapcar-set ( lst )
   (mapcar '2+b lst)
)

 

defun-q-list-set the slowest

   (MAPCAR-DEFUN LST)...............1172 / 2.13 <fastest>
   (MAPCAR-QUOTE-LAMBDA LST)........1187 / 2.11
   (MAPCAR-FUNCTION-LAMBDA LST).....1250 / 2
   ([color="red"]MAPCAR-SET[/color] LST).................2375 / 1.05
   (MAPCAR-QUOTE-QUOTE LST).........2422 / 1.03
   (MAPCAR-DEFUN-Q LST).............2469 / 1.01
   ([color="red"]MAPCAR-DEFUN-QA[/color] LST)............2500 / 1 <slowest>

my surprise set is just a bit faster then double quote :o

Share this post


Link to post
Share on other sites
Lee Mac

Of course, there is also the incredibly slow:

(mapcar (quote (eval (defun-q foo ( x ) (+ 2 x)))) lst)
(mapcar (quote (eval (defun   foo ( x ) (+ 2 x)))) lst)

Benchmarking ...................Elapsed milliseconds / relative speed for 65536 iteration(s):

   (MAPCAR-DEFUN LST)..................1170 / 3.47 <fastest>
   (MAPCAR-QUOTE-LAMBDA LST)...........1201 / 3.38
   (MAPCAR-FUNCTION-LAMBDA LST)........1217 / 3.33
   (MAPCAR-QUOTE-QUOTE LST)............2745 / 1.48
   (MAPCAR-DEFUN-QA LST)...............2761 / 1.47
   (MAPCAR-SET LST)....................2761 / 1.47
   (MAPCAR-DEFUN-Q LST)................2808 / 1.44
   (MAPCAR-QUOTE-EVAL-DEFUN LST).......3073 / 1.32
   (MAPCAR-QUOTE-EVAL-DEFUN-Q LST).....4056 / 1 <slowest>

Share this post


Link to post
Share on other sites
pBe

@pBE : It's just for lazy typist :lol:

 

Yes, I know. i just like the variation since i never thought of coding it that way. :) Nice still.

Share this post


Link to post
Share on other sites
sonali
(defun c:TEST (/ ss coords)
 (vl-load-com)
 (cond
   (*activeDoc*)
   ((setq *activeDoc* (vla-get-activedocument (vlax-get-acad-object)))))
 (prompt "\n  >>  Select Line Object To Display Coordinates: ")
 (if (setq ss (ssget ":S:E" '((0 . "*POLYLINE"))))
   (progn
     (vlax-for x  (setq ss (vla-get-activeselectionset *activeDoc*))
       (setq coords
        (vlax-safearray->list
          (vlax-variant-value
            (vla-get-coordinates x)))
)
[color="red"][color="red"](setq n -1)
(setq lll (length coords))
(repeat lll
  
  (setq n (+ 1 n))
  (setq cox1(nth n coords))
  (setq co1(rtos cox1 2 2))
 (setq f(open "D:/circle.csv" "a"))
  (write-line co1 f)
 (close f)
[/color]	 )     
[/color]

)
     (vla-delete ss)
     (terpri)
     (prompt "\n  >>  Coordinates List: \n\t\t\t")
     (princ coords)
     (terpri)))
 (princ)) ;_end defun

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...