(DEFUN C:DELETEBLOCK (/) (ALERT "WARNING! - THIS COMMAND DELETES ALL OCCURENCES OF BLOCK SELECTED") (DEFUN *ERROR* (MSGE) (PRINC "ERROR: ") (SETQ MSGE (PRINC "\nFUNCTION CANCELLED")) (TERPRI)) (SETQ ENTL (CDR (ASSOC 2 (ENTGET (CAR (ENTSEL "\nSELECT BLOCK TO DELETE: ")))))) (SETQ SETL (SSGET "X" (LIST (CONS 2 ENTL)))) (COMMAND "ERASE" SETL "") (SETQ NAME (STRCAT "\nALL BLOCKS NAMED " ENTL " HAVE NOW BEEN DELETED")) (PRINC NAME) (PRINC) )
a quick mod
(defun c:deleteblock (/ *error* msge effnme ent entl setl e)
(vl-load-com)
(alert
"warning! - this command deletes all occurences of block selected")
(defun *error* (msge)
(princ "error: ")
(setq msge (princ "\nfunction cancelled"))
(terpri))
(setq effnme (lambda (x)
(vla-get-effectivename
(if (eq (type x) 'ENAME)
(vlax-ename->vla-object x)
x))))
(princ "\nselect block to delete: ")
(if (setq ent (ssget "_:S" '((0 . "INSERT"))))
(progn
(setq entl (effnme (ssname ent 0)))
(ssget "x" (list (cons 2 (strcat entl ",`*U*"))))
(vlax-for
itm (vla-get-ActiveSelectionSet
(vla-get-ActiveDocument
(vlax-get-acad-object)))
(if (eq (effnme itm) entl)
(vl-catch-all-apply
'vla-delete
(list itm))))
(princ (strcat "\nall blocks named "
entl
" have now been deleted")))
(princ "\nNo Block Selected:"))
(princ)
)
HTH
Not sure if 6 years is too long between comments, but I modified the previously mentioned code. It's about as simple as they come, but hey IT WORKS. For my example, I was removing a block called "ISSUESTAMP".
(defun C:IFBLOCKEXISTSDELETE (/) (if (setq ss (ssget "x" (list (cons 0 "INSERT") (cons 2 "ISSUESTAMP")))) (command "erase" ss "") ) (command ".purge" "b" "ISSUESTAMP" "n") )
@neil.merris wrote:
.... It's about as simple as they come, but hey IT WORKS. ....
... as long as the Block named is not a dynamic one. And you could make it even simpler:
(defun C:IFBLOCKEXISTSDELETE (/ ss) (if (setq ss (ssget "x" '((2 . "ISSUESTAMP")))) (command
"erase" ss "" ".purge" "b" "ISSUESTAMP" "n"
) )
)
Presumably there won't be any things other than Insertions with a 2-code entry that could possibly be of the same name, which is why you can omit the "INSERT" entity-type entry. And since there's no evaluation of anything required inside the filter list, it can be a "quoted" list instead of using the (list) function outright. And more than one command can be included in a (command) function. But now I'm realizing that if there are none Inserted but the Block definition exists in the drawing, presumably you'd still want to Purge it, so maybe that ought to be pulled out into its own operation, not part of the 'then' expression in that (if) function.
[I did add localizing of the one variable. And personally I would shorten the command name, too -- that's a mouthful!]