Sunday, September 5, 2010
Home   |   Search   |   About AUGI   |   My AUGI   |   Join Now
<< < > >>

Customization Corner - March 2004

Last month, I covered in general some of the ways to customize AutoCAD. Although not specifically called out, many of the ways are available for both AutoCAD and AutoCAD LT users. Over the next couple articles, I am going to focus on how to automate repetitive tasks using script files. Hopefully, you will recognize that this form of customization, while basic, can be very beneficial.

Have you ever gotten drawings from a client that don't use the same settings you do? Or maybe you've needed to control and create different layer schemes within your company for different disciplines. I have often used scripts to perform drawing clean-up tasks. These are all perfect examples of how you can use script files. Below, I show an example of how you can use a script to restore or change settings, via system variables.

cmdecho 1 [enter]
blipmode off [enter]
lunits 4 [enter]
cmddia 1 [enter]
filedia 1 [enter]
[enter]

The above example sets the Command Echo (cmdecho) back on, Blip mode (blipmode) off, Linear Units (lunits) to Architectural, Command Dialog (cmddia) on and File Dialogs (filedia) on. There are many other system variables that are stored in the drawing as well that you might want to change. Maybe you want to set the current Text Style or the default radius for the Fillet command. These all can be set in a script file. For a complete list of system variables, type Setvar at the command prompt, then type a ?.

So What Exactly are Script Files?
The ability to use Scripts for automating AutoCAD has been around for some time. A Script file is an ASCII text file that contains a series of AutoCAD commands and can be created in any basic text editor like Notepad. The files must be saved and created with .SCR as the file extension.

There are some things that you must know as far as disadvantages and advantages before you can get the most out of using script files.

Disadvantages or Limitations
- Can't accept user input by any means.
- Can't call up dialog boxes during execution.
- Only one can be loaded at a time into AutoCAD, a script can't be called from inside a script.
- No samples are shipped with AutoCAD to show how to use them efficiently or create them.

Advantages or Workarounds
- Automate simple tasks or sequences fast and easy with minimal updates needed from release to release.
- Can be created by virtually everyone.
- The easiest form of customizing AutoCAD because it works just like typing commands in at the command line.
- Script files can be used transparently (with some limitations).
- Most dialog commands have command line (non-dialog) equivalents; prefix the command name with a hyphen. (Ex. -LAYER is the command line version of LAYER)
- You can use AutoLISP expressions in scripts if you now how to use LISP. This allows for the building of much more interesting scripts at times.

Note: Like most customization in AutoCAD you can add comments. Comments allow you to place remarks about what the file is used for or what a certain line in the script is doing. This allows someone else to understand and maintain the file. It is always a good idea to add comments to a file like when it was last revised, the purpose and who revised/created it. Placing semi-colon(s) ";" at the beginning of any line will turn that line into a comment. This can also be useful for forcing the script to "skip" certain lines while you are testing a certain part.

Related Commands
SCRIPT – Loads and runs a script file from a dialog box
-SCRIPT – Loads and runs a script file from a command line prompt
RSCRIPT – Will repeat the last script file
RESUME – Starts up a script file after it has been paused by the end user
DELAY – Pauses the script based on the milliseconds specified. 1000ms = 1 sec. Maximum delay is 32767 which is equal to over 32 seconds

Keyboard Keys with Special Use When Running Scripts
Backspace – Pauses a script file
ESCape – Stop the execution of a script file

Format and Structure of a Script File

  1. Open Notepad by clicking on Start >> All Programs (or Programs) >> Accessories >> Notepad.
  2. With Notepad open click on the File pull down and select Save As. Enter the filename of Limits.scr. You are well on your way to creating your first Script file.
  3. Now with the file ready, type the text shown below. Again, the [enter] will not appear in your script.
  4.   
    ;; Created 2/24/04 by Lee Ambrosius 
    Limits 0,0 24,24 [enter]
    Zoom Extents [enter]
    Line 0,0 3,3 [enter]
    [enter]
    redraw [enter]
    [enter]
    

    Note: The use of spaces and blank lines can make or break a script. Notice the blank line after the "Line 0,0 3,3" line. After you pick a point, AutoCAD keeps asking for "Next point." The blank line is the same as hitting Enter to finish the command. Remember, a space is the same as an Enter, so if there is a space at the end of a line, AutoCAD performs an Enter. For example, if the line "Limits 0,0 24,24" had a space at the end, as in "Limits 0,0 24,24", then the script would perform an extra Enter, which would repeat the Limits command, thus throwing off the rest of the script. Sometimes, rather than mess with spaces, it is easier to use a new line for each command and option.

    To be safe, you could do the Limits like this:

    Limits [enter]
    0,0 [enter]
    24,24 [enter]
    [enter]
    

    It may be handy to open your script in Word and toggle on the Show/hide feature (the icon that looks like a backwards "P". This will show each space in your script graphically.

  5. Save your script file and go into AutoCAD. From the Tools pull-down select Run Script. Locate the Limits.scr file by using the Select Script File dialog box, and then select Open to run the file.

The above example sets the limits of the drawing, performs a zoom extents and draws a line from coordinates 0,0 to 3,3. After drawing the objects it is always a good idea to perform a redraw to get clear the screen of any blips that might be there.

Although this example might not be all that powerful, it demonstrates some of what scripts can do things with different types of commands. Even the simplest of things can end up taking time out of and away from the day. So, if it is something that you are doing over and over again, it might be a good idea to consider creating a script for that task.

One other thing: you can have AutoCAD automatically execute a specified script when it opens. This is great for controlling the AutoCAD environment. You can have AutoCAD automatically set your preferred object snaps, a preferred dim style, do a zoom extents, and much more. The only trick to this is modifying the AutoCAD icon on your desktop. Right click on it, go to properties, and look at the Target edit box. It will say something like "C:\Program files\AutoCAD 200x\acad.exe." All you'll need to do is add a "/b[script name]" at the end of the target. So, if you have a script file named, MyStartup.scr, in one of your standard AutoCAD support folders, your target string would look like this:

"C:\Program files\AutoCAD 200x\acad.exe" /b MyStartup

This article covered the basics of creating and using scripts. In the next article I will cover how make scripts even more power with a tool called ScriptPro. ScriptPro is a utility that can be downloaded from the Autodesk website free of charge. This utility allows you to run script files across many drawings at once in a batch process. This can be very powerful to change Layer colors, linetypes, add a plot stamp to your drawing placed on a specific layer and many other things. Along with showing how to use scripts in a batch tool, I will list many of the commands that can be used to access commands that normally use dialog boxes.

Submitted by Lee Ambrosius of HyperPics.


<< < > >>