REPLACE MULTIPLE BLOCKS AT ONCE

REPLACE MULTIPLE BLOCKS AT ONCE

venturini.anthony
Advocate Advocate
387 Views
5 Replies
Message 1 of 6

REPLACE MULTIPLE BLOCKS AT ONCE

venturini.anthony
Advocate
Advocate

I'm looking to replace all plant blocks in the drawing with alternate blocks saved on my server. 

 

Example: I have blocks jsb, hhc and jhh already inserted into the drawing. I'd like to automatically replace those blocks with blocks saved in a folder on my server with corresponding names jsb, hhc and jhh. These new blocks will have an image instead of linework.

 

Is this possible to do?

0 Likes
388 Views
5 Replies
Replies (5)
Message 2 of 6

Kent1Cooper
Consultant
Consultant

The command-line [no dialog box] hyphen-prefixed -INSERT command, using this format:

-INSERT BlockName=

with the equals sign immediately following the Block name but with nothing following that, goes looking for an external drawing file with the same as the Block, and assuming it finds one, asks you whether you want to replace the current Block definition with what's in that drawing.

 

AutoLisp [assuming you're not running Acad LT ] can run that operation on all of the Block names collectively in one shot.  If those "blocks saved in a folder" are drawing files, and the folder they're in is in your Support File Search Path list so AutoCAD will be able to find them, try putting the code line below in at the Command: line [followed by Enter].  The hyphen prefix is not needed inside a (command) function -- the command-line version is always used there [for those commands that have one].  The (command) function with no arguments at the end cancels the Insert command as it's asking for an insertion point for placing a new one, but after the new definition has been imported so the existing ones will change.

 

(foreach blk '("jsb" "hhc" "jhh") (command "_.insert" (strcat blk "=") "_yes") (command))

Kent Cooper, AIA
0 Likes
Message 3 of 6

nrz13
Advisor
Advisor

Yes, it's possible. First use the RENAME command to rename jsb, hhc, and jhh to jsb_old, hhc_old, and jhh_old.  Then copy your new blocks in.  Then use BLOCKREPLACE to replace jsb_old, hhc_old, and jhh_old with jsb, hhc, and jhh respectively.


Work:  AutoCAD 2022.1.3, Windows 10 Pro v22H2 64-bit, Intel Core i7-8700K, 32GB RAM, Samsung 960 Pro SSD, AMD Radeon Pro WX 5100, 3 Dell Monitors (3840x2160)
Home: AutoCAD 2022.1.3, Windows 10 Pro v22H2 64-bit, Intel Core i7-11700, 64GB RAM, Samsung 980 Pro SSD, NVIDIA Quadro P2200, Dell Monitor (3840x2160)
Message 4 of 6

3wood
Advisor
Advisor

You can try BLOCKUPDATE, add it into your Startup Suite, then add following lines into your acad.lsp or acaddoc.lsp to replace the blocks in the drawing automatically whenever opening a drawing. Replace "C:\\My Block Library" with your block library directory.

(defun s::startup ()
(BLOCKUPDATE "C:\\My Block Library" (list "jsb" "hhc" "jhh")) 
)

 

 

 

Message 5 of 6

Kent1Cooper
Consultant
Consultant

@3wood wrote:

... to replace the blocks in the drawing automatically whenever opening a drawing. ....


To do that, you may as well just XREF them, rather than Insert them as Blocks.

Kent Cooper, AIA
Message 6 of 6

ronjonp
Advisor
Advisor

This is what I use. It assumes that your new blocks are located within the file support paths.

 

(defun c:ub (/ file out)
  (vlax-for x (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object)))
    (if	(and (= 0 (vlax-get x 'isxref) (vlax-get x 'islayout))
	     (not (wcmatch (strcase (vla-get-name x)) "*|*"))
	)
      (setq out (cons (vla-get-name x) out))
    )
  )
  (setvar 'cmdecho 0)
  (foreach x out
    (if	(setq file (findfile (strcat x ".dwg")))
      (progn (command ".-INSERT" (strcat x "=" file) nil)
	     (and (ssget "_X" (list '(0 . "INSERT") (cons 2 x) '(66 . 1)))
		  (command "._Attsync" "Name" x)
	     )
      )
    )
  )
  (setvar 'cmdecho 1)
  (princ)
)