It is very tedious and boring when you need to repeat the same group of operations on many files. To speed up such tasks you can use AutoCAD scripting and a free utility ScriptPro; It allows you to run the same script on many files in a batch mode. It can be installed as a part of Migration Tools for AutoCAD 2004- 2006 or as a separate tool for AutoCAD 2007-2009. After installationy you can find it in the Autodesk Program Group
ScriptPro runs outside of AutoCAD. It starts a session of AutoCAD for every file on the list, runs the script on it and then closes that session of AutoCAD and starts on the next file.
Cleanup tasks are most likely candidates for scripting because similar problems are most likely exist in many files of the same dataset.
If you can do something in AutoCAD using just your keyboard, you can script it. A script is just a text file with a .SCR extension; it tells AutoCAD what to do just as if an operator types the commands at the command line.
Let’s create a script for purging, auditing and repairing a file.
- Open any drawing. Type –purge at the command line. You need to pre-pend “-“ to many AutoCAD commands to force them into the command line mode instead of dialog box. Then just follow the prompts at the command line to purge everything.
- Start Notepad. You can start typing commands as you would enter them in AutoCAD. I’m a bad typist, I so I simply copy my “conversation” with AutoCAD from the command window and paste it into Notepad. Then I delete AutoCAD’s responses and leave only my commands.
- A couple of things that you need to know about scripting syntax: every space and every new line is interpreted like pressing the ENTER key in AutoCAD, so be careful with extra spaces and lines. To make my scripts more readable, I write one command per line, no spaces at the end of the line, and finish my scripts with one empty line.
- Audit checks the file for corruption, and then repairs what it can. It checks the whole file but repairs only the objects in the current space. Most of the time corrupted elements reside in model space. AUDIT also diagnoses loaded XREFs and, if errors are found, may report them as fixed. But it cannot write changes back to XREFs, so next time it will report them again. It is better to repair XREFs first or use RECOVERALL on the files with reported problems
Here is our first script line-by-line. There are no spaces at the end of each line and no empty lines except where noted.
|1||-PURGE ALL * N-PURGE ALL * N||Purges all elements with any name, does not ask for confirmation (repeat at least 2 times)|
|2||-PURGE RE * N||This line will delete all unused registered applications (not available from dialog box interface and)|
|3||TILEMODE 1||Switches to model space|
|4||AUDIT Y||Audits and fixes corrupted elements|
|5||TILEMODE 0||Switches to layout|
|6||AUDIT Y||Audits and fixes corrupted elements|
|7||QSAVE||Saves the changes|
|Blank line (if prefer, you can use a space after QSAVE instead of empty line)|
Now save the text file with .scr extension and run the script on an AutoCAD file to test it (Tools->Run Script).
To use a script in batch mode on multiple files, I added QSAVE at the end. If I do not, the script will still run, but results will not be saved.
In the several following posts we will look at the scripts for various tasks
- Use short scripts as “building blocks” for longer scripts. This way you will need to open and close you are going to process. Check if ScriptPro has timed out on any files after the processing is completed. the file only once. Huge timesaver.
- Check for the unexpected. You may encounter locked layers, corrupted files that cannot be repaired by AUDIT, proxy elements for which you do not have object enablers, etc. To verify that your scripts actually worked, examine the log file after batch processing.
- Check your old scripts when/if using with newer version of AutoCAD. Commands can change.
Before starting batch processing, make sure that no one is working on or planning to open any files