select all instances of a specific block

select all instances of a specific block

demus72
Collaborator Collaborator
798 Views
5 Replies
Message 1 of 6

select all instances of a specific block

demus72
Collaborator
Collaborator

I found a lisp some years ago that prompts the user to pick a block and it selects all instances of the block wherever they exist in the drawing. This allows the user to select different visibility states, change common attributes, or even delete them all. Unfortunately, I don't know the source of the original author to give proper credit.

 

(defun C:SelBlkEv ( / blk SS nSS i e )
(setvar 'errno 0)
(while (/= 52 (getvar 'errno))
(setq blk (car (entsel "\nSelect block to filter <exit>: ")))
(cond
((= 7 (getvar 'errno)) (princ) (setvar 'errno 0))
((and blk (/= "INSERT" (cdr (assoc 0 (entget blk))))) (princ))
(blk
(setq blk (vla-get-EffectiveName (vlax-ename->vla-object blk)))
(setq SS (ssget "_X" (list (cons 0 "INSERT"))))
(setq nSS (ssadd))
(repeat (setq i (sslength SS))
(setq e (ssname SS (setq i (1- i))))
(and (eq blk (vla-get-EffectiveName (vlax-ename->vla-object e)))
(ssadd e nSS)
)
)
(sssetfirst nil nSS)
(setvar 'errno 52)
)
)
)
(princ)
)

 

I could have sworn it worked when I first used it years ago, but it isn't functioning the way I'd like, which is to delete all instances of the selected block in all locations, model or any layouts. I'm not a coder and am hoping someone can identify the issue or maybe point me to an alternative lisp to accomplish this?

 

Thanks!

0 Likes
Accepted solutions (1)
799 Views
5 Replies
Replies (5)
Message 2 of 6

MrJSmith
Advocate
Advocate
Accepted solution

The normal ACAD delete command does not work across layouts; you have to use the VLA version. https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/deleting-selection-set-across-multip...

 

For your current code, you could modify it like so to delete all of a selected block.

 

 

(defun C:SelBlkEvDel ( / blk SS nSS i e )
	(setvar 'errno 0)
	(while (/= 52 (getvar 'errno))
		(setq blk (car (entsel "\nSelect block to delete all of <exit>: ")))
		(cond
			((= 7 (getvar 'errno)) (princ) (setvar 'errno 0))
			((and blk (/= "INSERT" (cdr (assoc 0 (entget blk))))) (princ))
			(blk
				(setq blk (vla-get-EffectiveName (vlax-ename->vla-object blk)))
				(setq SS (ssget "_X" (list (cons 0 "INSERT"))))
				(setq nSS (ssadd))
				(repeat (setq i (sslength SS))
					(setq e (ssname SS (setq i (1- i))))
					(and (eq blk (vla-get-EffectiveName (vlax-ename->vla-object e)))
						(ssadd e nSS)
					)
				)
				(if nSS 
					(foreach x (mapcar 'cadr (ssnamex nSS))(vla-delete (vlax-ename->vla-object x)))
				)
				(setvar 'errno 52)
			)
		)
	)
	(princ)
)

 

 

0 Likes
Message 3 of 6

ryanatkins49056
Contributor
Contributor

Another option you have is to use the built in QSELECT command. Either QSELECT at the command line or right click an empty space (don't have anything selected) and then choose Quick Select.

0 Likes
Message 4 of 6

paullimapa
Mentor
Mentor

also possible to use entdel function as a test replace vla-del:

(foreach x (mapcar 'cadr (ssnamex nSS))(vla-delete (vlax-ename->vla-object x)))

with entdel:

(foreach x (mapcar 'cadr (ssnamex nSS))(entdel x))

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

MrJSmith
Advocate
Advocate

@paullimapa Entdel does not work in AutoCAD for deleting objects across layouts.

Message 6 of 6

paullimapa
Mentor
Mentor

I find this post now...thanks for the correction....strange that it only works on the last layout...


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos