Adjust the code

Adjust the code

adaptacad
Advocate Advocate
917 Views
7 Replies
Message 1 of 8

Adjust the code

adaptacad
Advocate
Advocate

How to adjust the code to work on multiple polylines:

 

(defun ERR (s)
  (if (/= s "Function cancelled\n")
    (if	(= s "quit / exit abort")
      (princ)
      (alert (strcat "    >> No Polyline Selected << \n"))
    )
  )
  (setq *error* olderr)
  (princ)
)

(defun c:vert ()

  (setq	olderr	*error*
	*error*	err
  )

  (setq pl (entget (car (entsel "Select polyline :"))))

  ;;To get the vertax numbers :
  (if (setq vert (cdr (assoc 90 pl)))
    (progn
      (princ "\nNumbers of Vertices : ")
      (princ vert)
    )
    (princ "\nThis is not a Polyline")
  )

  (princ)
)

(princ "\nType VERT to run lisp")
0 Likes
Accepted solutions (2)
918 Views
7 Replies
Replies (7)
Message 2 of 8

Kent1Cooper
Consultant
Consultant

Not difficult [and it can be rather simpler than that], but:

 

How would you want them reported?  Do you want to pick them one at a time, but as many as you want in one running of the command?  Or if you want to select a bunch at once, do you want just a series of reports at the Command line that all say "Number of Vertices: XX" separately for each, with no differentiation?  Or does each number need to be related to its Polyline somehow [such as listing entity names along with numbers, or a number in Text at each one, or something]?  Or ... ?

Kent Cooper, AIA
0 Likes
Message 3 of 8

adaptacad
Advocate
Advocate

@Kent1Cooper

Simply select multiple polylines and 3d polylines and this will return the "Number of Vertices: XX" of all.

0 Likes
Message 4 of 8

doaiena
Collaborator
Collaborator
Accepted solution

Test this code and tell me, if it's what you are looking for, or it's something else that you want.

(defun c:test ( / ss ctr obj verts)

(if (setq ss (ssget '((0 . "LWPOLYLINE,POLYLINE"))))
(progn

(setq ctr 0
verts 0
)
(repeat (sslength ss)

(setq obj (vlax-ename->vla-object (ssname ss ctr)))
(if (equal (vla-get-objectName obj) "AcDbPolyline")
(setq verts (+ verts (/ (length (vlax-safearray->list (variant-value (vla-get-coordinates obj)))) 2)))
(setq verts (+ verts (/ (length (vlax-safearray->list (variant-value (vla-get-coordinates obj)))) 3)))
);if 2dpoly
(setq ctr (1+ ctr))
);repeat

(princ (strcat "The number of vertices for all objects is: " (rtos verts 2 0)))
));if ss
(princ)
);defun

 PS: Sorry i made a mistake in the code. Make sure you copy this code. I  have edited my post because i had forgotten to increment the counter. It's fixed now.

Message 5 of 8

adaptacad
Advocate
Advocate

@doaienaalmost
works great if you select only one object.
but I would like the result to be the sum of all.

0 Likes
Message 6 of 8

doaiena
Collaborator
Collaborator

Try the code from my previous post now. I had a mistake, but it's fixed now and should work as intended.

0 Likes
Message 7 of 8

Kent1Cooper
Consultant
Consultant
Accepted solution

@adaptacad wrote:

....

Simply select multiple polylines and 3d polylines and this will return the "Number of Vertices: XX" of all.


 

One  grand total, then....  Here's a slightly shorter way, using an approach that doesn't need to distinguish between "lightweight" and "heavy" Polylines:

 

(vl-load-com); if needed
(defun C:PLVQ (/ ss v n pl); = PolyLine Vertices Quantity (if (setq ss (ssget '((0 . "*POLYLINE")))) (progn ; then (setq v 0) (repeat (setq n (sslength ss)) (setq verts (+ v (fix (vlax-curve-getEndParam (setq pl (ssname ss (setq n (1- n)))))) (if (vlax-curve-isClosed pl) 0 1) ); + ); setq ); repeat (prompt (strcat "\nTotal vertices in all Polylines = " (itoa verts) ".")) ); progn ); if (princ) ); defun
Kent Cooper, AIA
Message 8 of 8

adaptacad
Advocate
Advocate

@doaiena@Kent1CooperExactly!!
thank you so much!!

0 Likes