There are several methods of replacing a block:maybe you can use design center CTRL+2
BLOCKREPLACE: does not replace blocks when they are inside another block
-INSERT old=new: works on nested blocks, but does end up with the old blockname. Renaming globally is not possible because there could have been old blocks in the drawing that should not be renamed.
LISP PROGRAMS: As far as I know all lisp programs work with a selection and will not replace nested blocks.
Any suggestions?
-INSERT old=new: works on nested blocks, but does end up with the old blockname. Renaming globally is not possible because there could have been old blocks in the drawing that should not be renamed.Woabow, globally changing all block references from 'old' to 'new' would be equivalent to this. Can you clarify what you want to do?
(defun _updateblocks (old new)
(if (tblobjname "block" new)
(vlax-for b (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object)))
(vlax-for bb b
(if (and (= (vla-get-objectname bb) "AcDbBlockReference")
(wcmatch (strcase (vla-get-name bb)) (strcase old))
(print (vla-get-name bb))
)
(vl-catch-all-apply 'vla-put-name (list bb new))
)
)
)
)
)
(_updateblocks "old" "new")
I think this should do what you want. The new block definition has to exist in the drawing though.wow . very interesting way to replace block :)Code: [Select](defun _updateblocks (old new)
(if (tblobjname "block" new)
(vlax-for b (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object)))
(vlax-for bb b
(if (and (= (vla-get-objectname bb) "AcDbBlockReference")
(wcmatch (strcase (vla-get-name bb)) (strcase old))
(print (vla-get-name bb))
)
(vl-catch-all-apply 'vla-put-name (list bb new))
)
)
)
)
)
(_updateblocks "old" "new")
Nice explanation Lee, very helpful.
I think this should do what you want. The new block definition has to exist in the drawing though.wow . very interesting way to replace block :)Code: [Select]...
maybe vla-get-EffectiveName to replace dynamic also
kruuger
(defun _updateblocks (old new)
(if (tblobjname "block" new)
(vlax-for b (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object)))
(vlax-for bb b
(if (and (= (vla-get-objectname bb) "AcDbBlockReference")
(wcmatch (strcase (vlax-get bb
(if (vlax-property-available-p bb 'effectivename)
'effectivename
'name
)
)
)
(strcase old)
)
(print (vla-get-name bb))
)
(vl-catch-all-apply 'vla-put-name (list bb new))
)
)
)
)
)
;; (_updateblocks "old" "new")
;D
@ronjonp: No need to check if the effectivename property is available.
I will update my code (thank you kruuger).
What about for older versions of CAD ?That is a good point. When were dynamic blocks introduced?
;;; ======================================================================
;;; BKG_ReplaceBlock: Change the referenced name of inserts.
;;; ======================================================================
;;; Author: Roy Klein Gebbinck (www.b-k-g.nl)
;;; Made for: http://www.theswamp.org/index.php?topic=45490.0
;;; Version: 20131011.
;;; History: 20131011: Added xref and recursion check.
;;; 20131011: Added support for AcDbMInsertBlock.
;;; 20131011: Using effective name (thanks kruuger and ronjonp).
;;; 20131010: First version.
;;; ======================================================================
What about for older versions of CAD ?That is a good point. When were dynamic blocks introduced?
works nicely :) may come in handy somedayCode: [Select];;; ======================================================================
;;; BKG_ReplaceBlock: Change the referenced name of inserts.
;;; ======================================================================
;;; Author: Roy Klein Gebbinck (www.b-k-g.nl)
;;; Made for: http://www.theswamp.org/index.php?topic=45490.0
;;; Version: 20131011.
;;; History: 20131011: Added xref and recursion check.
;;; 20131011: Added support for AcDbMInsertBlock.
;;; 20131011: Using effective name (thanks kruuger and ronjonp).
;;; 20131010: First version.
;;; ======================================================================
... how to attsync existing/inserted block ?
;;; ======================================================================
;;; BKG_ReplaceBlock: Change the referenced name of inserts.
;;; ======================================================================
;;; Author: Roy Klein Gebbinck (www.b-k-g.nl)
;;; Made for: http://www.theswamp.org/index.php?topic=45490.0
;;; Version: 20131013.
;;; History: 20131013: If the new block has no editable attributes,
;;; existing attribute references are deleted.
;;; Else there is an optional call to the Attsync
;;; command.
;;; 20131011: Added xref and recursion check.
;;; 20131011: Added support for AcDbMInsertBlock.
;;; 20131011: Using effective name (thanks kruuger and ronjonp).
;;; 20131010: First version.
;;; ======================================================================