PDA

View Full Version : Renaming All Blocks



wilsons3774183
2025-04-16, 01:24 AM
Hi,

I am having a bit of trouble with a lisp routine in AutoCAD 2025. I am trying to rename all blocks within a drawing to have a suffix of "- Existing". The issue is it is also changing the external reference name to have the same suffix. This is being done as part of a cleaning up process of older projects.

The ultimate goal is to have all blocks to have a suffix of "- Existing" while leaving the external reference names as they are (if this is possible)
If there is someone who could help me it would be much appreciated as my LISP knowledge is very basic.

The routine is:


(defun c:CB2E (/ acdoc blocks blkdef oldname newname ss blkref ent)
(vl-load-com)

;; Get the active AutoCAD document and block collection
(setq acdoc (vla-get-ActiveDocument (vlax-get-acad-object))
blocks (vla-get-Blocks acdoc)
)

;; Loop through all block definitions
(vlax-for blkdef blocks
(setq oldname (vla-get-Name blkdef)) ; Get current block name

;; Ensure we are not renaming anonymous or already renamed blocks
(if (and (not (wcmatch oldname "`**")) ; Skip anonymous blocks
(not (vl-string-search " - Existing" oldname))) ; Skip already renamed
(progn
;; Create the new block name
(setq newname (strcat oldname " - Existing"))

;; Rename the block definition
(vla-put-Name blkdef newname)

;; Update all block references to use the new block name
(setq ss (ssget "_X" (list (cons 2 oldname)))) ; Select blocks with old name
(if ss
(progn
(foreach ent (vl-remove-if 'null (mapcar 'cadr (ssnamex ss)))
(setq blkref (vlax-ename->vla-object ent))
(vla-put-Name blkref newname) ; Assign new block name to reference
)
)
)
)
)
)

(princ "\nAll blocks have been renamed with the suffix ' - Existing'.")
(princ)
)

PaulLi_apa
2025-04-16, 04:59 AM
Here's a hint...when you're looping through the block definitions you can check for xrefs like this:

(vlax-for blkdef blocks
(setq oldname (vla-get-Name blkdef))
(if (or (eq :vlax-true (vla-get-isxref blkdef))(wcmatch oldname "*|*"))(alert (strcat "Xref is: " oldname))(alert (strcat "Block is: " oldname)))
)

wilsons3774183
2025-04-16, 11:12 PM
Thank you PaulLi_apa, that put me on the right track.

Appreciate the help