Select Layer in Steps with AutoLisp

Select Layer in Steps with AutoLisp

Emi_Riv
Contributor Contributor
466 Views
4 Replies
Message 1 of 5

Select Layer in Steps with AutoLisp

Emi_Riv
Contributor
Contributor

Hiho

 

I would like to use the following Lisp code to select all objects on a layer with one click. When I run the Lisp again, I want to select another layer, which will be added to the already selected objects.

Unfortunately, my Lisp doesn't work as expected, either with 'ssgetfirst' or when 'ssget' returns 'nil':

 

(defun c:test ()
(setq CurSel (ssget)) ; Aktuelle Auswahl speichern

(setq TargAus (car (entsel "\nSelect object on layer to select: ")))
(setq TargLay (assoc 8 (entget TargAus)))

(setq LaySel (ssget "_X" (list TargLay)))

(setq numObjs (sslength LaySel))

(setq i 0)
(while (< i numObjs)
(setq obj (ssname LaySel i))
(setq CurSel (ssadd obj CurSel))
(setq i (1+ i))
)

(sssetfirst nil CurSel)

(princ)
)

---

The same i want make with blocks/dym-blocks.

 

Whats wrong with the lisp, can someone help me?

 

Warm regards

Emanuel

0 Likes
Accepted solutions (2)
467 Views
4 Replies
Replies (4)
Message 2 of 5

ВeekeeCZ
Consultant
Consultant
Accepted solution

Perhaps like this.

 

(defun c:test ( / func)
  (setq CurSel (ssget "_I")) ; Aktuelle Auswahl speichern
  
  (setq TargAus (car (entsel "\nSelect object on layer to select: ")))
  
  (if CurSel (sssetfirst nil)
    (setq CurSel (ssadd)))

  (setq func (if (ssmemb TargAus CurSel) ssdel ssadd))
  
  (setq TargLay (assoc 8 (entget TargAus)))
  
  (setq LaySel (ssget "_X" (list TargLay)))
  
  (setq numObjs (sslength LaySel))
  
  (setq i 0)
  (while (< i numObjs)
    (setq obj (ssname LaySel i))
    (setq CurSel (func obj CurSel))
    (setq i (1+ i))
    )
  
  (sssetfirst nil CurSel)
  
  (princ)
  )

 

Message 3 of 5

Emi_Riv
Contributor
Contributor

Not perhaps, but exactly what I need. Now I see my mistake. Thank you very much. I would also like to apply the function for dynamic blocks. It doesn't work with assoc 2; I think that would be somewhat complicated?

0 Likes
Message 4 of 5

ВeekeeCZ
Consultant
Consultant
Accepted solution

Well, you need to understand how dynblocks works. Code 2 is right, but if you change any of his dynproperty the ACAD creates a copy of the block definition and names it anonymously starting with *U. Then the affiliation to the original block is given by the so-called effective name.

 

Here's a very close example. If you combine this routine of mine with that one of yours and got what you want.

 

(vl-load-com)

(defun c:SelectBlocks* ( / :getBlockName s n i e) (defun :getBlockName (obj) (if (= (type obj) 'ENAME) (setq obj (vlax-ename->vla-object obj))) (if (vlax-property-available-p obj 'EffectiveName) (vla-get-EffectiveName obj) (vla-get-Name obj))) (or (and (setq s (ssget "_I" '((0 . "INSERT")))) (sssetfirst nil)) (and (setq n (getstring "\nSpecify blockname w/wildcards : ")) (/= n "")) (setq s (car (entsel "\nPick a block: ")))) (if s (setq n (lisped (:getBlockName (setq s (if (= 'ENAME (type s)) s (ssname s 0))))))) (if s (sssetfirst nil)) (if (setq s (ssget "_X" (list (cons 0 "INSERT") (cons 2 (strcat n ",`*U*")) (cons 410 (getvar 'ctab))))) (repeat (setq i (sslength s)) (setq e (ssname s (setq i (1- i)))) (if (not (wcmatch (strcase (:getBlockName e)) (strcase n))) (ssdel e s)))) (if (and s (> (sslength s) 0)) (progn (sssetfirst nil s) (princ (strcat "\n>> " (itoa (sslength s)) " blocks found.")))) (princ) )

 

Message 5 of 5

Emi_Riv
Contributor
Contributor

Great, that helps me a lot. With that, I can tinker around a bit. Many thanks for the explanation!😃

0 Likes