Select blocks in all layouts

Select blocks in all layouts

mruPRQUJ
Advocate Advocate
2,093 Views
19 Replies
Message 1 of 20

Select blocks in all layouts

mruPRQUJ
Advocate
Advocate

Hi there,

 

This lisp below is from Lee MAC. I was attached here as well. It selects blocks based on block name, tag and attribute in the current layout. Could you please make some modification to select the blocks in all layouts in the same file? thank you very much in advance. 🙂

 

;; Block Selection - Lee Mac
;; Selects all blocks in the current layout with a given block name or which contain a specified attribute tag and/or value.

(defun c:bsel ( / att atx blk cnt ent enx flg idx sel str tag )
(setq blk (strcase (getstring t "\nSpecify block name <any>: "))
tag (strcase (getstring "\nSpecify attribute tag <any>: "))
str (strcase (getstring t (strcat "\nSpecify attribute value" (if (= "" tag blk) ": " " <any>: "))))
)
(if (not (= "" str tag blk))
(if
(and
(setq sel
(ssget "_X"
(append
'((000 . "INSERT"))
(if (not (= "" tag str)) '((066 . 1)))
(if (/= "" blk) (list (cons 2 (strcat "`*U*," blk))))
(if (= 1 (getvar 'cvport))
(list (cons 410 (getvar 'ctab)))
'((410 . "Model"))
)
)
)
)
(progn
(repeat (setq idx (sslength sel))
(setq ent (ssname sel (setq idx (1- idx)))
enx (entget ent)
)
(cond
( (not (or (= "" blk) (wcmatch (strcase (LM:name->effectivename (cdr (assoc 2 enx)))) blk)))
(ssdel ent sel)
)
( (member (cdr (assoc 66 enx)) '(nil 0)))
( (progn
(setq att (entnext ent)
atx (entget att)
flg nil
)
(while
(and (= "ATTRIB" (cdr (assoc 0 atx)))
(not
(and
(or (= "" str) (wcmatch (strcase (cdr (assoc 1 atx))) str))
(or (= "" tag) (wcmatch (strcase (cdr (assoc 2 atx))) tag))
)
)
)
(setq att (entnext att)
atx (entget att)
)
)
(= "SEQEND" (cdr (assoc 0 atx)))
)
(ssdel ent sel)
)
)
)
(< 0 (setq cnt (sslength sel)))
)
)
(progn
(princ (strcat "\n" (itoa cnt) " block" (if (= 1 cnt) "" "s") " found."))
(sssetfirst nil sel)
)
(princ "\nNo blocks found.")
)
)
(princ)
)

;; Block Name -> Effective Block Name - Lee Mac
;; blk - [str] Block name

(defun LM:name->effectivename ( blk / rep )
(if
(and (wcmatch blk "`**")
(setq rep
(cdadr
(assoc -3
(entget
(cdr (assoc 330 (entget (tblobjname "block" blk))))
'("acdbblockrepbtag")
)
)
)
)
(setq rep (handent (cdr (assoc 1005 rep))))
)
(cdr (assoc 2 (entget rep)))
blk
)
)

(princ)

0 Likes
Accepted solutions (3)
2,094 Views
19 Replies
Replies (19)
Message 2 of 20

marko_ribar
Advisor
Advisor
Accepted solution

Remove this lines from the code...

(if (= 1 (getvar 'cvport))
(list (cons 410 (getvar 'ctab)))
'((410 . "Model"))
)

Marko Ribar, d.i.a. (graduated engineer of architecture)
Message 3 of 20

mruPRQUJ
Advocate
Advocate

It did not work. Please see the info below,

mruPRQUJ_0-1727974301449.png

 

Please see attached DWG for your reference. The block name is 

mruPRQUJ_0-1727974570287.png

It was located in three layouts.

 

thanks.

0 Likes
Message 4 of 20

paullimapa
Mentor
Mentor
Accepted solution

You probably modified the code incorrectly.

I followed @marko_ribar instructions and works for me:

;                            (if (= 1 (getvar 'cvport))
;                                (list (cons 410 (getvar 'ctab)))
;                               '((410 . "Model"))
;                            )

paullimapa_0-1727980967828.png

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 5 of 20

ec-cad
Collaborator
Collaborator

Found those 3 blocks with the 'layout' stuff commented out.

Are you sure you put in the correct blockname ? I first thought the

"I", s were "1", s  did not find those.

Here's what I got:

Command: BSEL

Specify block name <any>: BCH-X-BORD-REVI-TRIA

Specify attribute tag <any>:

Specify attribute value <any>:

3 blocks found.

 

ECCAD

Message 6 of 20

ec-cad
Collaborator
Collaborator

Paulli,

Didn't see your post, was testing .. a few minutes late.

Same result. Sorry for barging in..

ECCAD

Message 7 of 20

paullimapa
Mentor
Mentor

no problem..the more the merrier...


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 8 of 20

Sea-Haven
Mentor
Mentor

For me 

(setq blk (strcase (getstring t  "\nSpecify block name <any>: "))
          tag (strcase (getstring "\nSpecify attribute tag <any>: "))
          str (strcase (getstring t (strcat "\nSpecify attribute value" (if (= "" tag blk) ": " " <any>: "))))
    )

 

I use nentsel and pick the attribute this returns tag name, it also returns a selection point, using this point and (ssget pt) it selects the block so can get block name. 

(setq tag (nentsel "\nPick an attribute "))
(setq pt (cadr tag))
(setq tagname (cdr (assoc 2 (entget (car tag)))))
(setq ss (ssget pt '((0 . "INSERT"))))
(setq bname (cdr (assoc 2 (entget (ssname ss 0)))))
Message 9 of 20

mruPRQUJ
Advocate
Advocate

Hi there,

 

In the beginning, I made some mistakes. But I downloaded the lisp which you attached here, it did not work for me. The block only in the current layout was selected. I restarted my laptop as well, it did not work.  Could you please provide some advice to me? many thanks. 🙂

0 Likes
Message 10 of 20

paullimapa
Mentor
Mentor

Make sure you overwrote the previous bsel.lsp with the revised one.

Open the same dwg that you posted here.

Then repeat the following at the command prompt:

Command: (load"bsel")
Command: BSEL
Specify block name <any>: BCH-X-BORD-REVI-TRIA
Specify attribute tag <any>:
Specify attribute value <any>:

The command prompt should return:

3 blocks found.

Command:

Do a screen capture of your command prompt again and share that here.

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 11 of 20

mruPRQUJ
Advocate
Advocate

Hi there,

 

The lisp is good, I made some mistakes. 

Is it possible to add an option to pick a block from the screen? can all the selection be deleted? Many thanks. 🙂

 

 

 

0 Likes
Message 12 of 20

paullimapa
Mentor
Mentor
Accepted solution
Message 13 of 20

ec-cad
Collaborator
Collaborator

Since you may have changed the code, best to upload what you have now.

From there, we can update it to allow for a 'Pick' of a Block.

 

ECCAD

Message 14 of 20

mruPRQUJ
Advocate
Advocate

Thank you!

0 Likes
Message 15 of 20

mruPRQUJ
Advocate
Advocate

thanks a lot!

0 Likes
Message 16 of 20

mruPRQUJ
Advocate
Advocate

Hi there,

 

Please see the lisp below, just pick blocks, no delete. thank you!

 

;; Block Selection - Lee Mac
;; Selects all blocks in the current layout with a given block name or which contain a specified attribute tag and/or value.
; OP:
; https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/select-blocks-in-all-layouts/m-p/130...

(defun c:bsel ( / att atx blk cnt ent enx flg idx sel str tag )
(setq blk (strcase (getstring t "\nSpecify block name <any>: "))
tag (strcase (getstring "\nSpecify attribute tag <any>: "))
str (strcase (getstring t (strcat "\nSpecify attribute value" (if (= "" tag blk) ": " " <any>: "))))
)
(if (not (= "" str tag blk))
(if
(and
(setq sel
(ssget "_X"
(append
'((000 . "INSERT"))
(if (not (= "" tag str)) '((066 . 1)))
(if (/= "" blk) (list (cons 2 (strcat "`*U*," blk))))
; (if (= 1 (getvar 'cvport))
; (list (cons 410 (getvar 'ctab)))
; '((410 . "Model"))
; )
)
)
)
(progn
(repeat (setq idx (sslength sel))
(setq ent (ssname sel (setq idx (1- idx)))
enx (entget ent)
)
(cond
( (not (or (= "" blk) (wcmatch (strcase (LM:name->effectivename (cdr (assoc 2 enx)))) blk)))
(ssdel ent sel)
)
( (member (cdr (assoc 66 enx)) '(nil 0)))
( (progn
(setq att (entnext ent)
atx (entget att)
flg nil
)
(while
(and (= "ATTRIB" (cdr (assoc 0 atx)))
(not
(and
(or (= "" str) (wcmatch (strcase (cdr (assoc 1 atx))) str))
(or (= "" tag) (wcmatch (strcase (cdr (assoc 2 atx))) tag))
)
)
)
(setq att (entnext att)
atx (entget att)
)
)
(= "SEQEND" (cdr (assoc 0 atx)))
)
(ssdel ent sel)
)
)
)
(< 0 (setq cnt (sslength sel)))
)
)
(progn
(princ (strcat "\n" (itoa cnt) " block" (if (= 1 cnt) "" "s") " found."))
(sssetfirst nil sel)
)
(princ "\nNo blocks found.")
)
)
(princ)
)

;; Block Name -> Effective Block Name - Lee Mac
;; blk - [str] Block name

(defun LM:name->effectivename ( blk / rep )
(if
(and (wcmatch blk "`**")
(setq rep
(cdadr
(assoc -3
(entget
(cdr (assoc 330 (entget (tblobjname "block" blk))))
'("acdbblockrepbtag")
)
)
)
)
(setq rep (handent (cdr (assoc 1005 rep))))
)
(cdr (assoc 2 (entget rep)))
blk
)
)

(princ)

0 Likes
Message 17 of 20

paullimapa
Mentor
Mentor

you're welcome...cheers!!!


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 18 of 20

mruPRQUJ
Advocate
Advocate

Hi there,

 

Could you please confirm if replace the first part with the second part? It did not work. I may make some mistakes.

first part

(setq blk (strcase (getstring t "\nSpecify block name <any>: "))
tag (strcase (getstring "\nSpecify attribute tag <any>: "))
str (strcase (getstring t (strcat "\nSpecify attribute value" (if (= "" tag blk) ": " " <any>: "))))
)

 

second part

(setq tag (nentsel "\nPick an attribute "))
(setq pt (cadr tag))
(setq tagname (cdr (assoc 2 (entget (car tag)))))
(setq ss (ssget pt '((0 . "INSERT"))))
(setq bname (cdr (assoc 2 (entget (ssname ss 0)))))

 

Thank you.

 

0 Likes
Message 19 of 20

mruPRQUJ
Advocate
Advocate

Hi there,

 

Please see the lisp below, just pick blocks no delete. thank you!

 

;; Block Selection - Lee Mac
;; Selects all blocks in the current layout with a given block name or which contain a specified attribute tag and/or value.
; OP:
; https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/select-blocks-in-all-layouts/m-p/130...

(defun c:bsel ( / att atx blk cnt ent enx flg idx sel str tag )
(setq blk (strcase (getstring t "\nSpecify block name <any>: "))
tag (strcase (getstring "\nSpecify attribute tag <any>: "))
str (strcase (getstring t (strcat "\nSpecify attribute value" (if (= "" tag blk) ": " " <any>: "))))
)
(if (not (= "" str tag blk))
(if
(and
(setq sel
(ssget "_X"
(append
'((000 . "INSERT"))
(if (not (= "" tag str)) '((066 . 1)))
(if (/= "" blk) (list (cons 2 (strcat "`*U*," blk))))
; (if (= 1 (getvar 'cvport))
; (list (cons 410 (getvar 'ctab)))
; '((410 . "Model"))
; )
)
)
)
(progn
(repeat (setq idx (sslength sel))
(setq ent (ssname sel (setq idx (1- idx)))
enx (entget ent)
)
(cond
( (not (or (= "" blk) (wcmatch (strcase (LM:name->effectivename (cdr (assoc 2 enx)))) blk)))
(ssdel ent sel)
)
( (member (cdr (assoc 66 enx)) '(nil 0)))
( (progn
(setq att (entnext ent)
atx (entget att)
flg nil
)
(while
(and (= "ATTRIB" (cdr (assoc 0 atx)))
(not
(and
(or (= "" str) (wcmatch (strcase (cdr (assoc 1 atx))) str))
(or (= "" tag) (wcmatch (strcase (cdr (assoc 2 atx))) tag))
)
)
)
(setq att (entnext att)
atx (entget att)
)
)
(= "SEQEND" (cdr (assoc 0 atx)))
)
(ssdel ent sel)
)
)
)
(< 0 (setq cnt (sslength sel)))
)
)
(progn
(princ (strcat "\n" (itoa cnt) " block" (if (= 1 cnt) "" "s") " found."))
(sssetfirst nil sel)
)
(princ "\nNo blocks found.")
)
)
(princ)
)

;; Block Name -> Effective Block Name - Lee Mac
;; blk - [str] Block name

(defun LM:name->effectivename ( blk / rep )
(if
(and (wcmatch blk "`**")
(setq rep
(cdadr
(assoc -3
(entget
(cdr (assoc 330 (entget (tblobjname "block" blk))))
'("acdbblockrepbtag")
)
)
)
)
(setq rep (handent (cdr (assoc 1005 rep))))
)
(cdr (assoc 2 (entget rep)))
blk
)
)

(princ)

0 Likes
Message 20 of 20

mruPRQUJ
Advocate
Advocate

Cheers! thanks again.

0 Likes