Loop on selection set in AUTOLISP

Loop on selection set in AUTOLISP

prakash.powergrid
Participant Participant
9,073 Views
5 Replies
Message 1 of 6

Loop on selection set in AUTOLISP

prakash.powergrid
Participant
Participant

I am trying to write a programme for selecting all polylines in a layer and make their start and end width as 0.  The below command  works  with individual selection of polylines with 4 no. of vertices.

(command "PEDIT" pause "E" "W" "0" "0.0075" "N" "N" "W" "0.0075" "0" "X" "")))

 

However, instead of individual selection, i need a programme which selects all polylines in a layer and makes start and end vertices as 0.

I used following function for selection of all polylines in autocad drawing

(setq ss (ssget "_x" '((0 . "*POLYLINE") (8 . "Layername"))))

 

But i am struggling to pass each object from selection set to PEDIT command in a loop. Can someone provide me the programme/guidance.

 

Thanks in advance

 

 

 

 

 

0 Likes
Accepted solutions (2)
9,074 Views
5 Replies
Replies (5)
Message 2 of 6

_gile
Consultant
Consultant
Accepted solution

Hi,

 

Typically, we use  the ssname function with an  incremented (or decremented) index in a loop.

 

With a (while ...) loop

(if (setq ss (ssget "_x" '((0 . "*POLYLINE") (8 . "Layername"))))
  (progn
    (setq i 0)
    (while (setq ent (ssname ss i))
      (command "_.PEDIT" ent "_E" "_W" 0 0.0075 "_N" "_N" "_W" 0.0075 0 "_X" "")
      (setq i (1+ i)
      )
    )
  )
)

With a (repeat ...) loop

(if (setq ss (ssget "_x" '((0 . "*POLYLINE") (8 . "Layername"))))
  (repeat (setq i (sslength ss))
    (setq ent (ssname ss (setq i (1- i))))
    (command "_.PEDIT" ent "_E" "_W" 0 0.0075 "_N" "_N" "_W" 0.0075 0 "_X" "")
  )
)


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 6

prakash.powergrid
Participant
Participant

Your solution worked perfectly. Thank you so much.

 

But i have another problem. I am trying fix poly line starting and ending width based on number of vertices it has. I have gathered following function for counting number of vertices of selected polyline, which is working perfectly

(defun c:nvert ()
(vl-load-com)
(setq Pl_Ent (car (entsel "\nSelect polyline: ")))
(setq VL_Obj (vlax-ename->vla-object Pl_Ent))
(setq endParam (vlax-curve-getEndParam Vl_Obj))
(setq NumVert (+ 1 endParam))
(princ (strcat "\n" (rtos NumVert 2 0) " vertices"))
(princ)
)

 

However, i am trying to call this function from my main function and take action accordingly. The following code has been used for this 

 

(Defun c:width_mgr (/ ss i ent novertex)
(if (setq ss (ssget "_x" '((0 . "*POLYLINE") (8 . "Test"))))
(repeat (setq i (sslength ss))
(setq ent (ssname ss (setq i (1- i))))
(setq novertex (nvert ent))
(cond ((= novertex 4)
(command "_.PEDIT" ent "_E" "_W" "0" "0.0075" "_N" "_N" "_W" "0.0075" "0" "_X" ""))
((= novertex 5)
(command "_.PEDIT" ent "_E" "_W" "0" "0.0075" "_N" "_N" "_N" "_W" "0.0075" "0" "_X" ""))
((= novertex 6)
(command "_.PEDIT" ent "_E" "_W" "0" "0.0075" "_N" "_N" "_N" "_N" "_W" "0.0075" "0" "_X" ""))
((= novertex 7)
(command "_.PEDIT" ent "_E" "_W" "0" "0.0075" "_N" "_N" "_N" "_N" "_N" "_W" "0.0075" "0" "_X" ""))
(t ((princ (strcat "\n" (rtos novertex 2 0) " vertices"))))
)
)
)
)

(Defun nvert (ent)
(vl-load-com)
(setq Pl_Ent (car ent))
(setq VL_Obj (vlax-ename->vla-object Pl_Ent))
(setq endParam (vlax-curve-getEndParam Vl_Obj))
(setq NumVert (+ 1 endParam))
)

But it is not working. The error is given below :
"; error: bad argument type: consp <Entity name: 7ff63e95ab60>"

 

I think some problem with auguments passing from main function,  but i could not figure out the problem. Can you please guide me again.

 

Thanks in advance.

 

 

0 Likes
Message 4 of 6

doaiena
Collaborator
Collaborator
Accepted solution

I think this code will do the job for you. 

(vl-load-com)
(Defun c:width_mgr (/ ss i ent lastSeg obj)

(if (setq ss (ssget "_X" '((0 . "*POLYLINE") (8 . "Test"))))
(repeat (setq i (sslength ss))
(setq ent (ssname ss (setq i (1- i))))

(if (> (setq lastSeg (- (cdr (assoc 90 (entget ent))) 2)) 0)
(progn
(setq obj (vlax-ename->vla-object ent))
(vla-setwidth obj 0 0 0.0075)
(vla-setwidth obj lastSeg 0.0075 0)
))
);repeat
)
);defun
Message 5 of 6

prakash.powergrid
Participant
Participant

Thank you.. Worked perfectly

0 Likes
Message 6 of 6

Kent1Cooper
Consultant
Consultant

@prakash.powergrid wrote:

.....  The below command  works  with individual selection of polylines with 4 no. of vertices.

(command "PEDIT" pause "E" "W" "0" "0.0075" "N" "N" "W" "0.0075" "0" "X" "")))

 

However, instead of individual selection, i need a programme which selects all polylines in a layer and makes start and end vertices as 0.

I used following function for selection of all polylines in autocad drawing

(setq ss (ssget "_x" '((0 . "*POLYLINE") (8 . "Layername"))))

 

But i am struggling to pass each object from selection set to PEDIT command in a loop. ....


A typical way to do that, in simplest terms and if all Polylines on that Layer are in the current space and have 4 vertices:

(if (setq ss (ssget "_x" '((0 . "*POLYLINE") (8 . "Layername"))))

  (repeat (setq n (sslength ss))

    (command "PEDIT" (ssname ss (setq n (1- n))) "E" "W" "0" "0.0075" "N" "N" "W" "0.0075" "0" "X" "")

  ); repeat

); if

 

It actually counts down from the end  of the selection set, because it takes a little less code than starting from the beginning, but presumably the order doesn't matter to your end result.

 

It will have trouble if any qualifying Polylines are in different layouts, but if that's possible, it could made to either "find" only those in the current Layout, or switch to the Layout each is in to process it.

Kent Cooper, AIA
0 Likes