If you are using XREFs for borders, you can use script to replace a current border with a different one. This example assumes that
- the layer G-Xref exists (but if not, you can script making it
- the border needs to be overlayed in the paperspace
- old and new border have the same position in respect to 0,0,0
The following script replaces the XREF border CD_SHEET_E_VERT in paperspace. You may use it to replace XREF borders from a consultant with your company border.
The new border BR08-4851.DWG is overlaid on G-XREF layer and its full path is then converted to relative path (we will talk more about full and relative paths in future posts)
-XREF D CD_SHEET_E_VERT
-LAYER S G-XREF
-XREF O P:\4000\4851\DRAWINGS\REF\ARCH\BR08-4851.DWG
0,0,0 1 1 0
-XREF P “BR08-4851″
In AutoCAD 2008, there is a new cleaning tool: SETBYLAYER (menu: Modify->Change to ByLayer). Before using this command in a script, let’s take a look at its settings. Select it from the menu or type SETBYLAYER from the command line, then type “s” to get to the setbylayer settings dialog box. Here you can easily select which properties will be changed. But dialog boxes cannot be used in scripts, so we will use the SETBYLAYERMODE variable to select the properties that we want to change.
The default value of the SETBYLAYERMODE variable is 127. It is NOT saved in the drawing. The picture on the left shows value corresponding to each property. In addition:
+32 Changes ByBlock to ByLayer
+64 Includes blocks when changing ByBlock to ByLayer
To select several properties, add these values. For example, to change just color and lineweight to ByLayer including all objects in blocks and ByBlock objects, set SETBYLAYERMODE=1+4+32+64=101.
The following script will change only color and lineweight of all objects (including set to ByBlock and nested in the blocks) on unlocked layers in modelspace and layout. Note that there are two spaces between “ALL” and first “Y” and one space between first and second “Y”
SETBYLAYER ALL Y Y
SETBYLAYER ALL Y Y
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.
||-PURGE ALL * N-PURGE ALL * N
||Purges all elements with any name, does not ask for confirmation (repeat at least 2 times)
||-PURGE RE * N
||This line will delete all unused registered applications (not available from dialog box interface and)
||Switches to model space
||Audits and fixes corrupted elements
||Switches to layout
||Audits and fixes corrupted elements
||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
DWG True View 2009 rocks!
It is a free viewer which in addition contains an updated DWGTrueConvert. It can now not only package the files with or without conversion to a different version file format, it can also fix file errors, and purge them! And it will not even use AutoCAD license while doing it.
Download free from http://usa.autodesk.com/adsk/servlet/index?id=6703438%26siteID=123112
Another time saver is RECOVERALL command
(File->Drawing Utilities->Recover drawings and Xrefs). Handy when you do not have time to clean up the whole dataset
I have heard that question one too many times from my children. When they were little I coud just say “because I said so”. But in the production environment where every minute counts this is a legitimate question. Cleanup takes time. You can clean anything, but it’s impossible to clean everything. To establish the scope of your AutoCAD cleanup you first need to determine your goals.
Three major scenarios for cleanups are:
- IN: Cleaning up incoming datasets. The data could be coming from outside sources, old projects, translation from other CAD formats, etc.
- OUT: Cleaning up outgoing datasets. You may need to package your data for outside consultants or restructure it to accommodate client requirements.
- Housekeeping: When data does not need to be reorganized but you need to deal with such problems as file corruption, purging, resetting variables or simply prepare dataset for archiving in your own system.
In all cases your first objective is to maintain graphic and file integrity.
Additionally, your second objective for the IN scenario is to make the data functional in your environment.
- Layer structure from outside data sources may not work well with your plot style table(s).
- Full XREF paths saved in outside data may contain locations that do not exist on your system.
For the OUT scenario, the second objective is portability and liability. In other words, when your dataset is “transplanted” to a different location, the drawings should look exactly the same as they looked on your system.
- Some CAD systems do not have concept of paper space, and the graphics from layouts will be lost.
- If you are using full path for XREFs and keep your drawings on the P: drive in your office, then send them to an office that uses the W: drive, the XREFs and images saved with a full path may not be found.
Housekeeping tasks deal with problems specific to the dataset, and usually would address file corruption, purging, resetting variables and preparing data for archiving.
Before approaching any cleanup task, you need to uncover as much information about the purpose of the cleanup as you can. You can save lots of time by finding out in advance if the incoming data will be updated later. When you expect the data to be updated, make sure to save your tools and settings for processing of subsequent data sets.
Finally, after the cleanup is completed, if the work on the dataset will continue, you would want to make sure that the team working on the dataset is aware of the cleanup process and will not overwrite clean dataset with original versions of the file.
The dataset cleanup tasks can be divided into 3 categories:
- Organizing and cleaning up layers
- Organizing and managing XREFs and other resource files
- Everything else.
In the next post I will go over “everything else”. 🙂 I will show you the tools that are available and will teach you to create your own tools.