Tipniques: Scripts & ScriptPro
Autodesk defines an external script as “a series of AutoCAD commands in a text file that can be used to carry out a task.” This simple definition contains an unrealized amount of power. Anything that can be performed by typing commands into your command line can be accomplished with a script.
One of the joys we experience in the course of a project is the repetitive task. Perhaps you are faced with a mundane layer control issue accomplished by a consistent layer adjustment. Maybe it is a maintenance issue, requiring you to purge registered applications, turn off wipeout frames, or set PDMODE to 0. It may even be a pre-submittal, last-minute fix to all files for a client. I have seen CAD designers burn half a day adjusting their files individually and at times erroneously. How do you ensure the changes are made consistently to each file? Will the person you assign to these tasks complete a clean adjustment to all of the required elements? How can you perform a repetitive task quicker, with better accuracy and consistency?
You use a script.
Consider some of the common command strings you perform on your files. If the strings are consistent or repetitive, you could create a simple script to automate your task. If you need to perform your task across a group of CAD drawings, you can use a batch-processing tool to run the script consistently across your files. In the course of this article, we will explore the basic process of creating an external script and applying it to multiple files by using a batch-processing tool.
Creating a Basic Script
Let us begin by creating a simple, generic script applicable to an independent file or batch routine.
The first step is the creation of an ASCII file with a .scr extension.
To do this, right-click on your desktop and select NEW->TEXT DOCUMENT. Then rename the text document to have a .scr extension.
Figure 1: Creating a ASCII File
Figure 2: Renaming the script
For our purposes, I named it Example_Script.scr. After your file is created, open it for editing. By default, it should open into Windows Notepad editor.
When I create a script for a particular project I will start with a new session of AutoCAD opened into said projects template, or into a copy of one of my active sheets. It is also very helpful to have your command line turned on. If you prefer to work with no command line, you can hit F2 at any time to view your command history. Although AutoCAD 2012 will aid you with some auto-complete functions, basic knowledge of key-in commands is extraordinarily helpful when creating scripts.
Let’s assume we need to ensure all of the deliverable CAD files in a large project need to be set to Paper Space, have the Wipeout Frames turned off, and have PDMode set to 0. With your CAD file and script editor (notepad) open, let’s begin to build our script. First, type in the command strings into the command line and observe what happens.
Figure 3: Checking the Commands
This is a pretty straight-forward script. There are not a lot of sub-menu options involved in these command strings. That said, if we were developing a more complicated script, we would want to test, refine, and re-test each command until the script works perfectly.
Now that we have observed how to initialize the commands and select the proper variables from the sub-menus by command line, we will begin transposing the information from AutoCAD to our script file. For each keyboard entry you made in the command line when building this string of commands, mirror that entry in your notepad file. Keep in mind that whenever you need to press ‘enter’ on your command line, you should press ‘enter’ within your script file. Semi-colons do not work properly in external scripts. When complete, your script file should look like Figure 4. Notice the extra ‘ENTER’ after QSAVE. This will execute the QSAVE command. Without it, your script will stall. Be sure to save your .scr file when you are done inputting the commands.
Figure 4: My completed script
Now you must test your new script. Reset the values within your CAD file by utilizing the UNDO command or by manually adjusting your drawings settings. When ready, with no active commands initiated within AutoCAD, drag and drop the script file thumbnail from Windows Explorer (or your desktop) to your active AutoCAD session. This will initialize the _SCRIPT command and run the script you dragged and dropped. If the script ran correctly, then you are done. If not, you will need to refine your ASCII file and retest the script. Testing is essential on advanced scripts or on scripts you intend to batch process. Some of the commands in AutoCAD just don’t quite execute the same from a script as they do from a command line. It can be very annoying to run a script to fix a problem created by another script. Therefore testing the script at this stage can save you a serious headache.
Applying Scripted Changes to Multiple Drawing Files
At this point your script should be built, saved, and fully tested. It is now time to apply the script to multiple files using a batch-processing program. In this example, I will demonstrate basic scripting using AutoDesk’s ScriptPro utility. There are other batch processing programs available that can run on a 64-bit operating systems, such as ScriptPro 2.0 for 64-bit found in Autodesk Labs or the freeware plug-in AutoScript from www.cadig.com. The basic ScriptPro found in Autodesk’s website will not run on all versions of windows. Happily most batch processing programs will work essentially the same. If you cannot install the program I am using for this demonstration, you should be able to install one of the other plug-ins I mentioned.
Step 1: Launch the version of AutoCAD associated with your script.
ScriptPro will fully open and fully close AutoCAD for each and every file you add to your batch routine. It determines the AutoCAD version, profile, and workspace based upon what you most recently used. So prior to running ScriptPro, be sure you have set the version and profile by launching the workspace you wish to use. After you have ensured you are using the correct AutoCAD environment, launch ScriptPro.
Step 2: Select your script.
The first step is to identify the script you wish to batch process. ScriptPro give you two standard choices; copy-paste a path, or ‘Browse’ to the .scr file’s location. Either way, you need to tell the program where it can find your desired script.
Figure 5: Selecting Your Script
Figure 6: Selecting Your Drawings
Figure 7: Saving Project Log
Step 3: Select the drawings you want to modify
To select the files you wish to add to your batch, press the little AutoCAD R14-style button. This will bring up a dialog enabling you to find and select the drawings you want to modify. After highlighting the drawings, select the double-arrows “ >> “ to include them in the “files to apply script to:” window. If all the files are not contained within one directory, you can navigate to multiple directories to add additional drawings. When you have selected all of the drawings you want to modify, select the ‘OK’ button.
Figure 8: ScriptPro Running Dialog
Step 4: Execute the batch
After you have your script and CAD file selected, you can execute the batch routine by pressing the “Run this project” button. Don’t be surprised if a little box may pops up asking for a location to store the project log file. This log file will contain valuable information should your batch process fail or your script produce an unexpected result.
Your script should now be running. Batch processing programs behave differently. Some will run everything in your current session of AutoCAD, some wont. As I mentioned earlier, the basic ScriptPro program will open and close a session of AutoCAD for each CAD file. This will make it difficult to perform alternate tasks on the workstation running ScriptPro. If you are running a batch on a large project, you may want to use a resource computer … or get some coffee. A dialog box will display the status of your script. If you notice an error, you can prematurely end the script by selecting “Stop processing” within this dialog box.
Figure 9: Project Completed Dialog
You will receive a notification when the project is complete that will allow you to view the project log file. If your batch routine ran smoothly, you are done. If not, no worries. We have the ability to adjust, modify, and re-run the routine.
Figure 10: Resetting Status
Step 5: Reviewing and resetting
If you need to run the project again, you will have to reset the status on your drawings. You can do this by selecting the drawings you wish to re-run, right-clicking, and choosing ‘Reset status’ from the menu. You can also use this menu to add, remove, or skip specific drawing files.
Figure 11: Project Settings Dialog
If you find your drawings are timing out prior to executing the full script, you can extend the timeout period by selecting
Options->Project Settings. This will bring up the Project Settings Dialog box.
The default timeout is 45 seconds. I typically have no issue with this length of time unless I am running an audit script on a large file, or my CAD file contains several large references. If you are experiencing timeout issues, this variable can be modified for your project to increase the duration ScriptPro spends on a single CAD file. If you set this number too high, your script automation may become very time-consuming should you have an error occur in one or more files.
That’s it! You should have all the knowledge you need to build and run a script on multiple drawing files. Automating some of the more repetitive tasks is not only fun, but can significantly increase the accuracy of your work while boosting productivity. By recognizing when an automated script can be used and developing an innovative method of applying the script to your project, you may find ways to save vital time.
Reuben J. Stone is a Senior Bridge Design Technician for David Evans and Associates, Inc. in Salem, Oregon. He has over 10 years of experience working in Civil Structural Design, Light Rail, and CAD Project Management.