Bulk Editing Drawings

Introduction to batch editing drawings

There are good reasons to bulk edit drawings from time to time. To mention a few:

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.

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

The script way

The process

Preparing the process

Your data

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:

C:\tmpdata\process
C:\somewhereelse\backup

Software used

Making the files

Making "run.bat"

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.

Making scripts

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:

Making "code.scr"

Example 1

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:

filedia 0

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 e

Zoom Extents makes sure that the extents, nothing more or less, get exported. BricsCAD exports SVG that is visible on the screen.

export

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")

filedia 0

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!

Example 2

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"))

Example 3

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"))

Making "job.scr"

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

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.

Additional tips

Removing bak files

Always be careful not to get more than you want with the del command! From the command prompt...

Windows:

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:

The directory is contaminated with other files

treemapping

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.

Doctor's analogy

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.

Final Thoughts

Bulk Editing Drawings (laatst bewerkt op 2018-10-20 06:56:11 door WiebeVanDerWorp)