Assuming that there are two similar blocks Block1 and Block2 and you want to replace all instances of Block1 with Block2 retaining its scale and rotation, and that blocks have no attributes this may work. Just fill the list of block names to be changed. Test on file copy.
(defun c:replace_duplicate_block_definitions ( / block_replace_names ss i ent)
(defun *error* (msg)
(if (not (wcmatch (strcase msg) "*BREAK*,*CANCEL*,*EXIT*"))
(progn
(princ (strcat "\nOops an Error : ( " msg " ) occurred."))
)
)
(if (and adoc) (vla-endundomark adoc))
(setvar 'cmdecho 1)
(princ)
)
(setq adoc (vla-get-activedocument (vlax-get-acad-object)))
(vla-endundomark adoc)
(vla-startundomark adoc)
;Edit this list of block names to be changed
;Add as many (existing_block_name wanted_block_name) Names are case sensitive
(setq block_replace_names
'(
("Block2" "Block1")
)
)
(setvar 'cmdecho 0)
(foreach e block_replace_names
(setq ss (ssget "x" (list (cons 0 "INSERT") (cons 2 (car e)))))
(setq i -1)
(cond
((and ss)
(while (< (setq i (1+ i)) (sslength ss))
(setq ent (entget (ssname ss i)))
(setq ent (subst (cons 2 (cadr e)) (assoc 2 ent) ent))
(entmod ent)
)
)
)
)
(setvar 'cmdecho 1)
(vla-endundomark adoc)
(princ)
)
You can use function getblocks to collect in a list names of all blocks in file
(defun c:getblocks (/ adoc name lst)
(setq adoc (vla-get-activedocument (vlax-get-acad-object)))
(vlax-for blk (vla-get-blocks adoc)
;; Exclude model and paper spaces, xref and anonymus blocks
(if (and (equal (vla-get-IsLayout blk) :vlax-false)
(equal (vla-get-IsXref blk) :vlax-false)
(/= (substr (vla-get-Name blk) 1 1) "*"))
(setq lst (cons (vla-get-Name blk) lst))
)
)
lst
)
Miljenko Hatlak

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.