- Introduction to batch editing drawings
- Choose a way to edit many drawings
- Batch and script files in short
- Preparing the process
- Making the files
- Additional tips
- Final Thoughts
Introduction to batch editing drawings
There are good reasons to bulk edit drawings from time to time. To mention a few:
- Audit and purge ill drawings, many organizations neglect data integrity until drawings are kaput.
- Replace titles with updated organization information.
Export SVG's, PDF's, bitmaps, see Bulk_Editing_Scalable_Vector_Graphics for steps after this step.
With this information, you can do it on a regular base for projects as a drawing aid.
There are multiple ways to achieve this. Choosing one is based on both the CAD environment and the needs. This page tries to be a guide.
Examples are done in a Windows environment, but in Linux and OS-X the approach is similar. Focus is on BricsCAD but AutoCAD is mentioned as well.
- There are multiple ways to batch edit drawings. Choose what fits best. Every method is summarized by instructing the CAD program to repeat this cycle:
- Step 1: Read a drawing file like with OPEN or RECOVER.
- Step 2: Do some things like cleaning, changing things, saving or exporting the drawing.
- Step 3: Secure your changes by exporting or saving.
- Step 4: Close the drawing file like with CLOSE or QUIT.
- The ways to accomplish this:
- The script way: Feed one session of the CAD program one long set of instructions for all things to be done.
- The batch way: Use a batch file to let one instance of the CAD program handle only one drawing. After finishing, let the operating system start the next instance of the program with the next drawing and so on.
- Multiple sessions: Utilize strong points of both the script and batch way. For example, find damaged drawings in round 1 the batch way and if everything is cleaned up, do round 2 the script way.
Then there are third party tools, trying to make life easier. For example, BKG_BATCH is a script based application for processing BricsCAD files.
Choose a way to edit many drawings
A batch file starts the CAD program, processes one drawing and closes the program, then starts the program for the next drawing and so on. It is not a very efficient way because of the extra time to start and stop the program for every drawing. A script running in only one CAD session overcomes this. Unfortunately this script solution is not as robust as a batch solution. Consider a drawing with a corrupted drawing database. It can result in a "FATAL ERROR" or program instability. When using a batch file, the session is lost but the next session starts with a fresh and stable instance of the CAD program. Just make a note of the faulty file name and resume the batch. Using the script way, the CAD instance is not refreshed and after a crash, the script needs updating by removing successful drawing edits - and the faulty separated. Despite the extra processing time, a batch solution is better in general. But with a proper drawing set, a long script is very tempting. And there are tools that can help you with a script. A batch file is easier to build than an script file. But when time is important, the script tempts again. If you're using BricsCAD, a start, process and stop cycle is very fast. A batch file running acad.exe is actually not an option at all any more, considering the slow start and stop.
The choice is yours. Third party solution are not discussed here.
Batch and script files in short
The batch way
- Make and run a batch file with a script doing things.
- The batch executes multiple time this command:
The script way
- Run a script from the CAD program command line. In order to avoid confusion with the script "code.scr" for the batch file, it is called "job.scr".
- The script itself is build manually with a spreadsheet and pasted to a script file.
- A SCRIPT command is given in the CAD program and "job.scr" is entered.
- Put a copy of your drawings in a folder.
- Create a script file "code.scr" with CAD commands.
- Create a batch file "run.bat", OR create a script file "job.scr".
- Put "code.scr" and "run.bat" or "job.scr" in the same folder as the folder with copies of the drawings.
- cd to the folder and run "run.bat" from the DOS command line, OR start the CAD program, issue a SCRIPT command and enter "job.scr".
Preparing the process
First create a working directory for processing and a backup directory with the same drawings. In case you ruin the files during testing...
Always work with copies of your files, even if you know what you are doing. Let's assume they are in:
- Notepad++ (or another proper editor)
Optional LibreOffice Calc (or another spreadsheet program) for "job.scr"
- BricsCAD (or AutoCAD or other LISP compliant CAD packages), off course!
Making the files
Open an editor and save a text file as "run.bat". Paste and edit some of the following...
A line to run looks like:
"C:\Program Files\Bricsys\BricsCAD V17 en_US\bricscad.exe" "C:\tmpdata\process\mydwg.dwg" /b "C:\tmpdata\code.scr"
A batch file that processes each drawing with a "For" statement is explained here. In DOS, "For /?" shows much options, should you need it.
It works for OS-X and Linux too, take a look at the [[Bulk_Editing_Scalable_Vector_Graphics]] for an example. However, a BASH file with a "For" statement does not handle files with spaces properly, a construction with "find" and "xargs" is better.
If code.scr, run.bat and the folder containing all drawings are in the same directory and are run from there, run.bat contains:
for /R %%f in (*.dwg) do ( "C:\Program Files\Bricsys\BricsCAD V17 en_US\bricscad.exe" "%%f" /b "c:\tmpdata\code.scr" )
The /R takes care for recursive behaviour.
Following the post of Kean Walmsley, for AutoCAD it should be something like:
for /R %%f in (*.dwg) do ( "C:\Program Files\Autodesk\AutoCAD 2013 - English\accoreconsole.exe" /i "%%f" /s "c:\tmpdata\code.scr" )
A complication can be that the output (DWG, SVG, ...) must also be recursive. To solve this, in the CAD script you can overwrite your copied and processed drawings with QSAVE and QUIT, for example.
Soon your CAD program starts opening your drawing and tries to run a CAD script. There are some things you need to know about scripts:
- Every end of a line is an enter.
- Every space you type is an enter.
Sometimes you give multiple enters to accept defaults or for an empty return like in LINE <enter> <enter>.
- Ending lines with spaces can be very confusing, an empty line for an extra enter means clearer and and less error prone code on this page.
- The last line with code should be ended with... off course, an enter.
In Notepad++ you see a number in front of each line. The last line of code does have this number without code. Just enter or backspace at the end of the script. This is important because an extra <enter> means repeating the last command used.
- Testing of your script, prior to running the batch, is important. A way to do this is opening a test drawing and giving a SCRIPT command that does not close the drawing, so you can see what happens (F2).
- Example 1 and 2 deal with a script that quits the CAD program and therefore are for the batch way.
- Example 3 deals with a construction for "job.scr".
So far about syntax, next are the commands. Open an editor and save a text file as "code.scr". You can paste commands on the CAD command line to see what they do. They are explained below:
This example exports SVG-files from an existing drawing set.
filedia 0 zoom e export (strcat "c:\\tmpdata\\svg\\" (substr (getvar "dwgname") 1 (- (strlen (getvar "dwgname")) 4)) ".svg") filedia 0 (if (> (getvar "dbmod") 0) (command "_quit" "_yes") (command "_quit"))
Save your file as C:\tmpdata\code.scr and you are ready to start. One advice, just start with a batch of a few drawings or do the whole bunch and kill with task-manager in case of problems.
About the commands in the script:
This turns off dialogue boxes, scripts don't like dialogues, just command line input. At the very end after all is finished, put this registry setting on again, issue FILEDIA 1.
Zoom Extents makes sure that the extents, nothing more or less, get exported. BricsCAD exports SVG that is visible on the screen.
Export command, next line is file name...
(strcat "c:\\tmpdata\\svg\\" (substr (getvar "dwgname") 1 (- (strlen (getvar "dwgname")) 4)) ".svg")
This one is a bit harder to get, it is LISP. The nice thing is that you can paste it complete or partial on the CAD command line and see what it does. Paste only a part starting with an opening bracket and make sure the amount of opening brackets match the closing brackets. It goes way to deep to explain this all, but getvar with dwgname returns the name of the opened file, strlen the string length (number of characters) of it, substr returns the name minus (-) the last four characters (.dwg) and strcat concatenates the directory file name and extension.
In this example, every SVG is placed in one folder. That might be a problem if you have drawings with the same name. You can also put the SVG's in the same folder as the DWG's. In that case, the line becomes a bit different:
(strcat (getvar "dwgprefix") (substr (getvar "dwgname") 1 (- (strlen (getvar "dwgname")) 4)) ".svg")
EXPORT apparently turns it back on to 1 so we have to turn it off again.
(if (> (getvar "dbmod") 0) (command "_quit" "_yes") (command "_quit"))
Close BricsCAD in order to go to the next line of the batch file. DBMOD starts with value 0 in a just opened (or new) drawing. As soon as you draw a line, or alter the drawing in another way like zooming in, DBMOD changes to a higher number. If DBMOD is more than 0, and you give a command QUIT, the question is asked "Do you want to save changes?" You enter "No". If DBMOD is 0, the question is not asked. So this line deals with that in a way that the program with one drawing opened will close.
After finishing everything, set filedia at 1, most users panic otherwise!
This example is a bit more complex. Top down, -PURGE is performed to do a clean script before inserting "dummy.dwg". "Dummy" contains two text styles that need another font file. To be sure these styles are available in the drawing, "dummy" is inserted. The -STYLE command binds "DejaVuSansCondensed-Bold.ttf". This is done twice for each text style. The last -STYLE command leaves nl_sign active, current, so it cannot be purged, even if it is not in use. TEXTSTYLE makes style "standard" active, problem solved and all styles not in use can be purged. "Dummy" did its job and can be deleted by ERASE L(ast). Next ZOOM, -PURGE and AUDIT are performed before exporting as SVG as explained before. A SAVEAS is performed putting a copy in the "result" directory. Since FILEDIA is a registry setting, not a drawing setting, it is set to 1 and the drawing is not changed by doing so. Therefore a QUIT without "No" should be enough to close the CAD program. However it is saver to follow the construction in Example 1.
Yes, the extra empty lines are not a typos, they are the extra, empty returns needed to close the -layer command and or give the needed enters to accept defaults.
About the ._ construction: If someone in your organization changed command names, the dot takes care to execute the original command. If your version is not English, the _ makes sure the English command is executed. The minus sign launches a command line version of a command - if available.
filedia 0 ._-layer _m 0 ._audit _y ._-purge _a * _n -insert c:\tmpdata\dummy.dwg 0.0,0.0 1.0 1.0 0.0 ._-style nl_sign_inv DejaVuSansCondensed-Bold.ttf ._-style nl_sign DejaVuSansCondensed-Bold.ttf ._textstyle standard ._erase _l ._zoom _e ._-purge ._a * ._n ._audit ._export (strcat "c:\\tmpdata\\svg\\" (substr (getvar "dwgname") 1 (- (strlen (getvar "dwgname")) 4)) ".svg") filedia 0 ._saveas 2013 (strcat "c:\\tmpdata\\result\\" (getvar "dwgname")) filedia 1 (> (getvar "dbmod") 0) (command "._quit" "._yes") (command "._quit"))
Examples 1 and 2 end in quitting the program. That is something you don't want when running one long script "job.scr" instead of the "run.bat" batch file. The drawing needs to be closed instead of ending the program.
The quit solution is:
(> (getvar "dbmod") 0) (command "._quit" "._yes") (command "_quit"))
The close solution is just a bit different:
(if (> (getvar "dbmod") 0) (command "._close" "._no") (command "._close"))
This is the fastest way but certainly not the finest. If your screen freezes, don't assume the process is crashed. To be complete is the reason to specify the steps here. These next steps mean hard manual labour but processing times are much faster. This is for creating a script "job.scr" with everything in it, running only one CAD session.
However, there are situations where you want an alternative script for certain drawings and this approach with a spreadsheet offers much flexibility, in particular if you do smart things in the sheet like conditional testing.
Making a file list
Open a DOS-prompt:
cd \tmpdata\process dir /o/n/b/s *.dwg > ..\files.txt
A list with files is created here: C:\tmpdata\files.txt.
Creating a command list
- Open this file in Notepad++.
- Select all and copy to clipboard.
- Open a new Calc sheet and paste the lines in column B
- In C1 "._OPEN " is entered, with a trailing space.
In D1 " COMMAND1 COMMAND2 (if (> (getvar "dbmod") 0) (command "._close" "._no") (command "._close"))", with a leading space. Consider putting your commands in a LISP file in order to keep the script readable and manageable.
In A1 "=C1&B1&D1", this is a script line.
So A1 contains:
._OPEN Drawing1 COMMAND1 COMMAND2 (if (> (getvar "dbmod") 0) (command "._close" "._no") (command "._close"))
Your first line of the batch file is ready in A1.
- To automatic fill the rest with formulas and values, you select the top cell A1. Next hold Shift and double click the little cross in the bottom right corner of that cell A1. Repeat to create columns C and D.
Copy filled cells in column A to clipboard, select A1, then Ctrl+Shift+ArrowDown and finally Ctrl+c.
- Start a new file with Notepad++ and paste values: Ctrl+v.
- Save this file as C:\tmpdata\job.scr
- Open an empty drawing, do FILEDIA 0, SCRIPT C:\tmpdata\job.scr and enjoy.
Removing bak files
Always be careful not to get more than you want with the del command! From the command prompt...
cd c:\to\your\folder del /s /q /f *.bak
Unix, first run without -delete:
cd /to/your/folder find . -name "*.bak" -type f -delete
My file is corrupt and I really don't know what to do
Your file is corrupted and you don't know what to do... In order:
- AUDIT and PURGE
- RECOVER the drawing.
- -WBLOCK and open the new drawing.
- Copy (Ctrl+Shift+C) with base 0,0 and paste on 0,0 in new drawing and save under same name.
- Make one new layout and delete old layouts
- Try DXFOUT (R12) and OPEN (dxf filetype)
- Edit DXF in Notepad++, in case of "error on line".
- Save under old format like rel. 12 and open again.
- Multiple EXPLODE and AUDIT, PURGE , AUDIT...
- If some blocks cannot be purged, paste LISP to the command line like the next two lines:
(setq ss (ssget "X" '((0 . "INSERT"))))
- If some blocks cannot be purged, paste LISP to the command line like the next two lines:
- Whatever else comes up in your mind....
The directory is contaminated with other files
Windirstat (Windows) or K4dirstat (Linux) is a nice tool to analyse the tree of drawings. Odd files sizes are directly visible. File types have their own colour so just click on a file that differs to see what it is and delete it or, if there are a lot splintered, do it on the command line like in "Removing bak files": First "cd c:\to\your\folder" and then "del /s /q /f *.jpeg" in case of JPEG files. Visit https://windirstat.net/, highly recommended!
Speeding up the process
If you have enough CPU cores available, you can split your drawing set and run multiple batch files simultaneously. I could double the speed with two batch files running. Further increasing the amount of batch files running caused instabilities without speed gain.
You are the doctor, your drawings are patients. Most patients are sane, but the ill ones can make your life hell. Stay calm, don't get frustrated, this article and the net are your friends. About the ill patients: Many of them have overweight, so you PURGE them. Simple discomforts are easy to cure with an AUDIT, some won't talk to you and need a RECOVER and in the mean time they blow up the hospital, crash the program. After recovering these patients, some are still ill, so you WBLOCK and or DXFOUT them. After EXPLODE therapy some surgery is needed. Most patients can be SAVEd, but accept the fact that some can not be cured, despite your efforts they finally die while some rests of them remain, or worse, only memories remain. In that case, you, the doctor, should accompany a birth with the NEW procedure.
- With large drawing sets there are always unexpected things, like erasing the last entity... in an empty drawing.
- Consider to write statistics to a log file (in LISP open, write-line, close), like "strcat" "getvars", separated with semicolons so you can analyse it directly in a spreadsheet afterwards. Variables to consider: INSBASE, INSUNITS, DWGPREFIX, DWGNAME, EXTMIN, EXTMAX and the calculated delta x and delta y
- With lots of crappy drawings, filter them out after the first batch, put them apart, repair, do a batch on these and if healthy put them back in the tree. Consider a final batch with all drawings that should run flawlessly.