Looking for a Lisp to replace a block with another version of the same block in several drawings (Batch)

Looking for a Lisp to replace a block with another version of the same block in several drawings (Batch)

jesusrperezd
Enthusiast Enthusiast
2,611 Views
41 Replies
Message 1 of 42

Looking for a Lisp to replace a block with another version of the same block in several drawings (Batch)

jesusrperezd
Enthusiast
Enthusiast

Hi everyone, I have looked for something similar here but have not succeeded.

 

I have several drawings in a folder that I need to open one by one, delete a block, purge the block, and finally paste the new block version.

 

The new block version is in another dwg, which I copied with the base point 0,0. The pasting is in the same coordinate, 0,0. All the blocks (the new version) and the old to be replaced have the same name and are located in the layout at 0,0 (not in the model). I do need to run the purge command after deleting so the block is pated with the new changes.

 

I'm wondering if any of you have an LISP file that can help me automate this process.

 

The inputs could be the Block name, the route of the new block version, and the folder route with the other drawings where the block will be replaced. Another option could be that the lisp is executed over the drawings opened in the current session, and the new block is taken from the active drawing where the command is executed.

0 Likes
Accepted solutions (1)
2,612 Views
41 Replies
Replies (41)
Message 2 of 42

pkenewell6347
Advocate
Advocate

Have you tried the Express Tools "-BLOCKREPLACE" command with a script?

0 Likes
Message 3 of 42

ec-cad
Collaborator
Collaborator

A couple of questions:

Does the original block have attributes, and if so, do you want to perserve those values ?

A lisp can be made to swap out the 'old' block with a 'new' block by selecting the 'old'

blockname (foreach Layout), and checking the insert point, and scale factor. Then,

if you have the replacement block wblocked somewhere, it can be inserted in the same location

and scale factor. The question is, if the old block has attributes or not.

Or, updating the 'same' blockname can be done with a simple (command "_-insert" Blockname= ... type block update.

Could you post a sample drawing before / after ?

ECCAD

 

0 Likes
Message 4 of 42

pendean
Community Legend
Community Legend
To do what you want easily without fighting AutoCAD's built-in nature, your "bad" DWG file needs to be inserted in your "good" DWG file, then you SAVEAS to put it back where it came from (maybe add an identifier so you still have that original).

Is that an option?

NOTE: LISP stops working when you start/open a different DWG file. So you need to use a script processor to open/close all of your DWG files and run your LISP inside each one. Do you have one you like to use?
0 Likes
Message 5 of 42

DGCSCAD
Collaborator
Collaborator

I pieced this together with some code I had and some from Lee Mac.

 

Instructions:

  1. Most importantly, back up the directory first, as a fail-safe.
  2. Change the block name where I've commented.
  3. Load/run this from a new unsaved drawing, or one that doesn't reside in the directory to be processed.
  4. Clean-up. There will be a scrfile.scr file located in the selected directory that will need to be deleted after running the function.

 

Lightly tested.

 

 

AutoCad 2018 (full)
Win 11 Pro
0 Likes
Message 6 of 42

jesusrperezd
Enthusiast
Enthusiast
Hi, pkenewell6347. No, I haven't tried it. So, wil the script run over all the drawings i select? Thanks for your time!
0 Likes
Message 7 of 42

ec-cad
Collaborator
Collaborator

I think this line:

(setq scrline (strcat "open" " " n2 " " "(load\"batcher\")" " " "batcher_insert" " " "qsave" " " "close"))

Should have (c:batcher_insert) .. maybe.

ECCAD

Message 8 of 42

pkenewell6347
Advocate
Advocate

@jesusrperezd wrote:
Hi, pkenewell6347. No, I haven't tried it. So, wil the script run over all the drawings i select? Thanks for your time!

Nevermind on that. Both blocks need to be in the drawing and named differently with BLOCKREPLACE.

0 Likes
Message 9 of 42

DGCSCAD
Collaborator
Collaborator

@ec-cad wrote:

I think this line:

(setq scrline (strcat "open" " " n2 " " "(load\"batcher\")" " " "batcher_insert" " " "qsave" " " "close"))

Should have (c:batcher_insert) .. maybe.

ECCAD


It's not inside a function, so normal command call. The scrline variable is a string that is setting the script.

AutoCad 2018 (full)
Win 11 Pro
0 Likes
Message 10 of 42

jesusrperezd
Enthusiast
Enthusiast
Hi ec-cad. The original block has attributes, as well as the new one, but I don't care about losing the values loaded on the attributes. I have a routine that I can run so all the attributes will be filled again quickly.
The insert point is the same for both (0,0). The scale factor is 1. The dwg files only have a layout

Thanks for your time!
0 Likes
Message 11 of 42

jesusrperezd
Enthusiast
Enthusiast

Thanks for doing this.

I opened the file and saw the line:

 

(setq blk_ss (ssget "_X" '((0 . "INSERT") (2 . "YourBlockName"))));;;<-----Replace YourBlockName with your actual block name. Use \\ if a path is needed, i.e.: C:\\YourPath\\YourBlockName

 

My new block is named 'TMP'  (it is always the same name).  I understand that If the new block is in the active drawing, I just write "TMP"  ("YourBlockName"), if the block is in another route  I write the route, let's say "C:\\Users\JP\Desktop\tes lisp\xxf.dwg\TMP" .

 

Did I understand this well?

 

Another question, I saw 2 execution commands Batcher and Batcher_insert, I suppose I have to execute only the Batcher, is that correct?  I'm trying to find a step-by-step process. 

 

thanks again!

 

 

0 Likes
Message 12 of 42

DGCSCAD
Collaborator
Collaborator

@jesusrperezd wrote:

Thanks for doing this.

I opened the file and saw the line:

 

(setq blk_ss (ssget "_X" '((0 . "INSERT") (2 . "YourBlockName"))));;;<-----Replace YourBlockName with your actual block name. Use \\ if a path is needed, i.e.: C:\\YourPath\\YourBlockName

 

My new block is named 'TMP'  (it is always the same name).  I understand that If the new block is in the active drawing, I just write "TMP"  ("YourBlockName"), if the block is in another route  I write the route, let's say "C:\\Users\JP\Desktop\tes lisp\xxf.dwg\TMP" .

 

Did I understand this well?

 

Another question, I saw 2 execution commands Batcher and Batcher_insert, I suppose I have to execute only the Batcher, is that correct?  I'm trying to find a step-by-step process. 

 

thanks again!

 

 


You are on the right track. Assuming you have your folder of drawings already backed-up:

 

Step-by-Step Instructions:

  1. Download the new version of Batcher_Block.lsp (I've revised per your block name and new location).
  2. Make sure to move the new Batcher_Block.lsp to a support/trusted path.
  3. Drag/drop the new Batcher_Block.lsp onto a new unsaved drawing.
  4. Type: Batcher
  5. Locate the directory where the drawings to be changed reside when prompted.
  6. Clean-up step from above (Message #5, Step #4).
AutoCad 2018 (full)
Win 11 Pro
0 Likes
Message 13 of 42

DGCSCAD
Collaborator
Collaborator

@jesusrperezd wrote:

... if the block is in another route  I write the route, let's say "C:\\Users\JP\Desktop\tes lisp\xxf.dwg\TMP" !

 

 


Also, the quoted text in red above doesn't seem like a valid directory. You'll need to change that line to the real valid directory, but use double backslashes for each folder level, as in:

 

C:\\Users\\JP\\tes lisp\\BLOCKNAMEHERE

 

...where BLOCKNAMEHERE is the block name TMP.

AutoCad 2018 (full)
Win 11 Pro
0 Likes
Message 14 of 42

DGCSCAD
Collaborator
Collaborator

@jesusrperezd wrote:
Hi ec-cad. The original block has attributes, as well as the new one, but I don't care about losing the values loaded on the attributes. I have a routine that I can run so all the attributes will be filled again quickly.
The insert point is the same for both (0,0). The scale factor is 1. The dwg files only have a layout

Thanks for your time!

Just saw this pertinent information. That'll throw a monkey wrench into the gears. The insert command call will need to have the attributes taken into consideration.

AutoCad 2018 (full)
Win 11 Pro
0 Likes
Message 15 of 42

jesusrperezd
Enthusiast
Enthusiast

Im sorry I got an error. It happens after it opens the first drawing.

 

. Please check the message I got:

 

Enter name of drawing to open <C:\Users\JP\Desktop\tes lisp\241206\N2P13947.01.dwg>: "C:\Users\JP\Desktop\tes lisp\241206\N2P13947.01.dwg"
Opening an AutoCAD 2018 format file.
Regenerating layout.
Regenerating model.


AutoCAD menu utilities loaded.
Command:

Autodesk DWG. This file is a TrustedDWG last saved by an Autodesk application or Autodesk licensed application.

Command:

Command:
Command: (load"batcher") ; error: LOAD failed: "batcher"

Command: batcher_insert Unknown command "BATCHER_INSERT". Press F1 for help.

0 Likes
Message 16 of 42

ec-cad
Collaborator
Collaborator

Yes, it's in a script. BUT, the script must load "batcher_block", not "batcher", and

to call the Command function inside batcher_block, you will have to do "(C:batcher_insert)",

So the whole line =:

(setq scrline (strcat "open" " " n2 " " "(load\"batcher_block\")" " " "(C:batcher_insert)" " " "qsave" " " "close"))

To avoid the Attributes, just before the (command "_-insert" ...... line - add (setvar 'attreq 0), as in:

(foreach n ins_list
(setvar 'attreq 0)
(command "-insert" "YourBlockName" n "" "" "")

The lisp file should be placed in a support path for input.

And, the block insert may need a formal 'path/path/blockname as well.

ECCAD

Message 17 of 42

DGCSCAD
Collaborator
Collaborator

Yes, ec-cad has the correct command call for (load\"batcher_block\"). My apologies, it was an oversight on my part.

 

The command call for (c:bacher_insert) can be called either way. It will be called at the command line on each drawing, so it can be called using it's localized command call name, or in parenthesis.

 

For the ATTREQ, I'd set that before the foreach, then reset it back to what it was after the insert(s).

 

...and yes, Batcher_Block.lsp needs to be located on a support/trusted path. Lots of little things need to be in place, but when they are it'll get you the results you're after. I've been using variations of this batch routine for a long time, so I tend to glaze over the important stuff that I have already set.

 

I can get to this tomorrow with an updated lisp file if noone else posts up a solution.

 

@ec-cadThank you for the revisions! Was speed posting.

AutoCad 2018 (full)
Win 11 Pro
0 Likes
Message 18 of 42

DGCSCAD
Collaborator
Collaborator

@jesusrperezd 

Also, if you could post the actual file location/folder of the TMP block, we can hard code that and make sure it's correct.

AutoCad 2018 (full)
Win 11 Pro
0 Likes
Message 19 of 42

paullimapa
Mentor
Mentor

If you already have the new block saved out into a folder location then would it not be as simple as using the built-in command of Insert blockname=pathofdwg.

For example if the block inside the current dwg is named "My Block" and the newer version is saved under C:\Autodesk\My Block.dwg, then the following lisp code should replace the old with the new whether in Model or Layout:

(command "_.INSERT" "My Block=C:\\Autodesk\\My Block" \E)

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 20 of 42

DGCSCAD
Collaborator
Collaborator

@paullimapa wrote:

If you already have the new block saved out into a folder location then would it not be as simple as using the built-in command of Insert blockname=pathofdwg.

For example if the block inside the current dwg is named "My Block" and the newer version is saved under C:\Autodesk\My Block.dwg, then the following lisp code should replace the old with the new whether in Model or Layout:

 

(command "_.INSERT" "My Block=C:\\Autodesk\\My Block" \E)

 

 


I had no idea. Thats amazing. What is the \E switch for?

AutoCad 2018 (full)
Win 11 Pro
0 Likes