[Original link to AutoCAD tips blog]
The Express Tool called “Replace Block” or command line = BLOCKREPLACE <enter> has been mentioned [HERE] and found on the ribbon (as seen below)
This tool is helpful for replacing blocks globally (through out the entire drawing) but what if you need to simply replace a few selected blocks?…
Alan T. has made an awesome LISP routine called BRE.lsp to accomplish just that.
Here’s how:
- BRE <enter>
- Select the block that you want to replace the other selected blocks.
- Select the blocks that you want replaced
- <enter> when finished
~enjoy
(defun c:BRE (/ *error* blk f ss temp) ;; Replace multiple instances of selected blocks (can be different) with selected block ;; Size and Rotation will be taken from original block and original will be deleted ;; Required subroutines: AT:GetSel ;; Alan J. Thompson, 02.09.10 ;; Found at: http://www.cadtutor.net/forum/showthread.php?48458-Replace-Selected-Block-Or-Blocks-With-Another-Block (vl-load-com) (defun *error* (msg) (and f *AcadDoc* (vla-endundomark *AcadDoc*)) (if (and msg (not (wcmatch (strcase msg) "*BREAK*,*CANCEL*,*QUIT*,"))) (princ (strcat "\nError: " msg)) ) ) (if (and (AT:GetSel entsel "\nSelect replacement block: " (lambda (x / e) (if (and (eq "INSERT" (cdr (assoc 0 (setq e (entget (car x)))))) (/= 4 (logand (cdr (assoc 70 (tblsearch "BLOCK" (cdr (assoc 2 e))))) 4)) (/= 4 (logand (cdr (assoc 70 (entget (tblobjname "LAYER" (cdr (assoc 8 e)))))) 4)) ) (setq blk (vlax-ename->vla-object (car x))) ) ) ) (princ "\nSelect blocks to be repalced: ") (setq ss (ssget "_:L" '((0 . "INSERT")))) ) (progn (setq f (not (vla-startundomark (cond (*AcadDoc*) ((setq *AcadDoc* (vla-get-activedocument (vlax-get-acad-object)))) ) ) ) ) (vlax-for x (setq ss (vla-get-activeselectionset *AcadDoc*)) (setq temp (vla-copy blk)) (mapcar (function (lambda (p) (vl-catch-all-apply (function vlax-put-property) (list temp p (vlax-get-property x p)) ) ) ) '(Insertionpoint Rotation XEffectiveScaleFactor YEffectiveScaleFactor ZEffectiveScaleFactor ) ) (vla-delete x) ) (vla-delete ss) (*error* nil) ) ) (princ) ) (defun AT:GetSel (meth msg fnc / ent good) ;; meth - selection method (entsel, nentsel, nentselp) ;; msg - message to display (nil for default) ;; fnc - optional function to apply to selected object ;; Ex: (AT:GetSel entsel "\nSelect arc: " (lambda (x) (eq (cdr (assoc 0 (entget (car x)))) "ARC"))) ;; Alan J. Thompson, 05.25.10 (setvar 'errno 0) (while (not good) (setq ent (meth (cond (msg) ("\nSelect object: ") ) ) ) (cond ((vl-consp ent) (setq good (cond ((or (not fnc) (fnc ent)) ent) ((prompt "\nInvalid object!")) ) ) ) ((eq (type ent) 'STR) (setq good ent)) ((setq good (eq 52 (getvar 'errno))) nil) ((eq 7 (getvar 'errno)) (setq good (prompt "\nMissed, try again."))) ) ) )
What an excellent tool, thank you, i have been looking for a way to do this for a while.
This looks like a pretty useful tool. Unfortunately I have the AutoCad 2013 which does not have access to express tools. Is there another way I can block-replace or can I download Express Tools anywhere?
The LISP routine at the bottom of the post should work just fine if you have full AutoCAD. Do you happen to have AutoCAD LT? this will not have the Express tools and it will not use LISP routines as well.
If you have full AutoCAD, the following link shows how to load them.
https://autocadtips.wordpress.com/2012/04/16/load-express-tools-for-2013/
~Greg
How can you use scale and rotation of the new block?
Hi Allen,
Thanks SO much for your help. I have now got this routine working for me. It does scale as you say but that shouldn’t matter for what I want it for. HOURS saved!
J ;0)
You are very welcome! I was just sharing what another Caddie figured out! I am glad the routine was useful for you! It definitely saved me a ton of time.. NO I in TEAM!!! I am always happy to help the CAD community which as always given me so much assistance!
Is there a way to keep the attributes just like if I were to replace a block globally? With this routine I lose all attributes and have to retype everything in.
Here is a link to a routine that is free called RIblock.vlx. I tried to find an open-source lisp version for you but I cannot find one. This one works great and does what you need.
http://www.cadstudio.cz/en/download.asp?file=RIblock
The file format “.vlx” is a compiled lisp code. You still load it the same way as a lisp routine.
I usually don’t touch compiled code because people have been abusing its hidden nature, but the programs found at http://www.cadstudio.cz are quality routines.
~Greg
Hi, I uploaded BRE.lsp successfully but when I try to use it I get this message:
; error: bad character read (octal): 0
I have full version of AutoCAD 2014.
Would you know what am I doing wrong?
Thanks,
Natalija
I saved mine in notepad first. The loaded it with the Visual Lisp editor then I created the lisp file from that. Did you copy/add everything to the lisp file. Even all of the parentheses?
Then loaded it
Yes it works perfectly!!! You are the greatest! Autodesks needs to add this to the product!!!
I used this lisp in one of my drawings and it worked great. I then tried to use it in another drawing and now getting this error:
Command: bre
Error: no function definition: AT:GETSEL
Command:
Please let me know if you can help because this is great and I want to be able to use this lisp.
Try adding the following to the bottom of your code:
Looks like that did the trick.
Thank you,
Kristie
Please read the post below. This routine works great..
I found a lisp routine that works like a charm.. its called BRE. Enjoy!
(defun c:BRE (/ *error* blk f ss temp)
;; Replace multiple instances of selected blocks (can be different) with selected block
;; Size and Rotation will be taken from original block and original will be deleted
;; Required subroutines: AT:GetSel
;; Alan J. Thompson, 02.09.10
;; Found at: http://www.cadtutor.net/forum/showthread.php?48458-Replace-Selected-Block-Or-Blocks-With-Another-Block
(vl-load-com)
(defun *error* (msg)
(and f *AcadDoc* (vla-endundomark *AcadDoc*))
(if (and msg (not (wcmatch (strcase msg) “*BREAK*,*CANCEL*,*QUIT*,”)))
(princ (strcat “\nError: ” msg))
)
)
(if
(and
(AT:GetSel
entsel
“\nSelect replacement block: ”
(lambda (x / e)
(if
(and
(eq “INSERT” (cdr (assoc 0 (setq e (entget (car x))))))
(/= 4 (logand (cdr (assoc 70 (tblsearch “BLOCK” (cdr (assoc 2 e))))) 4))
(/= 4 (logand (cdr (assoc 70 (entget (tblobjname “LAYER” (cdr (assoc 8 e)))))) 4))
)
(setq blk (vlax-ename->vla-object (car x)))
)
)
)
(princ “\nSelect blocks to be repalced: “)
(setq ss (ssget “_:L” ‘((0 . “INSERT”))))
)
(progn
(setq f (not (vla-startundomark
(cond (*AcadDoc*)
((setq *AcadDoc* (vla-get-activedocument (vlax-get-acad-object))))
)
)
)
)
(vlax-for x (setq ss (vla-get-activeselectionset *AcadDoc*))
(setq temp (vla-copy blk))
(mapcar (function (lambda (p)
(vl-catch-all-apply
(function vlax-put-property)
(list temp p (vlax-get-property x p))
)
)
)
‘(Insertionpoint Rotation XEffectiveScaleFactor YEffectiveScaleFactor
ZEffectiveScaleFactor
)
)
(vla-delete x)
)
(vla-delete ss)
(*error* nil)
)
)
(princ)
)
(defun AT:GetSel (meth msg fnc / ent good)
;; meth – selection method (entsel, nentsel, nentselp)
;; msg – message to display (nil for default)
;; fnc – optional function to apply to selected object
;; Ex: (AT:GetSel entsel “\nSelect arc: ” (lambda (x) (eq (cdr (assoc 0 (entget (car x)))) “ARC”)))
;; Alan J. Thompson, 05.25.10
(setvar ‘errno 0)
(while (not good)
(setq ent (meth (cond (msg)
(“\nSelect object: “)
)
)
)
(cond
((vl-consp ent)
(setq good (cond ((or (not fnc) (fnc ent)) ent)
((prompt “\nInvalid object!”))
)
)
)
((eq (type ent) ‘STR) (setq good ent))
((setq good (eq 52 (getvar ‘errno))) nil)
((eq 7 (getvar ‘errno)) (setq good (prompt “\nMissed, try again.”)))
)
)
)
What an awesome tool! Saved me MANY hours of tedious manual work. Thanks so much!
Awesome – that’s why I love scripts and Lisps so much. They make tedious tasks simple.
~Greg
Do you have anything updated for Mac? I get the message “; error: vl-load-com not supported on “Mac OS X Version 10.6 (x86_64)””
This would be a life saver if I could get this to work. I’ve tried loading and using it but when I select the blocks I want to replace they just disappear?? What am I doing wrong?
Thanks
Justine
Please use the code from the following link. It has been updated by Alan and when I tested it in ACAD 2015 it worked fine. One issue that I experienced was when a block that was previously scaled when I inserted it, when I ran the BRE.lsp it scaled up as well. So I am wondering if the blocks that you are tring to replace have been scaled…
All credit goes to Alan Thompson for putting together great routines – Thanks Alan
http://www.cadtutor.net/forum/showthread.php?48458-Replace-Selected-Block-Or-Blocks-With-Another-Block&p=359585&viewfull=1#post359585
I use the BRE in combination with the Filter command. I have a cad file that has been exported from Revit. So all the blocks have a similar prefix but varying information in the block name. Filter allows me to isolate all the similar blocks and the I use BRE to change them to a single block/name.. If you have any issues/questions please do not hesitate to ask
Ar great. Yes this is similar to what I’m doing. I work in Illustrator to start with and then bring the artwork into CAD to detail up for construction. When you bring Symbols (Illustrator’s version of Blocks) in from Illustrator CAD annoyingly gives each symbol a separate definition Block 1, Block 2 etc. I’m using BRE to select all these separate definitions of essentially the same block and redefine them as one block/one name.
I’ll certainly get in contact if I need any more help, thanks…you’ve been great.
J
I used this BRE.lsp and it works fairly well. I am impressed with the functionality. I do have two small requests that may be beneficial to everyone. When replacing the block, it seems that a new object is created because any dynamic fields (tables or labels) that I had pointing to the original block are broken when I use the BRE command. Also, attribute data is lost when replacing with new block.
Would you please consider rewriting this LISP to allow for maintaining the same entity/object name and also preserve attribute data from the old block? Thank you for considering my request.
Try the following Lisp
BRE is a Great Command! Saved me tons of time changing and replace large numbers of blocks. I use it in conjunction with the filter command. Especially when working with Revit exported files.
I tried BRE.lsp and didn’t have luck but SWAPBLK.lsp works. Only thing that is not that great is that replacing block has to be named as one word:
(defun c:SWAPBLK (/ SELECTIONSET COUNT NBLK BLOCK ELIST)
(princ “\n SWAPBLK”)
(setvar “cmdecho” 0)
(setvar “osmode” 0)
(prompt “\nSelect blocks to be swapped to another:”)
(setq SELECTIONSET (ssget) COUNT 0)
(setq NBLK (getstring “\nName of swapping block:”))
(repeat (sslength SELECTIONSET)
(setq BLOCK (ssname SELECTIONSET COUNT))
(setq ELIST (entget BLOCK))
(setq ELIST (subst (cons 2 NBLK)(assoc 2 ELIST) ELIST))
(entmod ELIST)
(entupd BLOCK)
(setq COUNT (+ COUNT 1))
)
(princ)
)
(princ “\nSWAPBLK loaded.”)
(princ)
BRE works great except all of the blocks are -90degrres out, what am i doing wrong?
Thank you Alan T. Awesome tool…excellent job
how can i keep the blocks in the same scale
I think the lsp file worked, however, rather than replacing my block, it deleted the existing block… how do I change this? I am brand new to Auto Lisp.
Here are some others to check out instead of the one posted on my site.
http://caddhelp.blogspot.com/2007/12/autocad-lisp-freebie-swap-block.html
http://www.cadtutor.net/forum/showthread.php?26011-Block-Swap-AutoLisp
i have learned something new today thank you
does any have this for Mac?