An Introduction to Script Writing

Scripts are very different to AutoLISP programs - a Script is merely a list of AutoCAD commands to be executed one after the other, as if the user were typing them at the command-line.

In an AutoCAD Script, a space or new-line is interpreted as the user pressing Enter, hence, if we wanted to use a script to draw a circle at the origin with a radius of 5, we could use the following in a script file:

Select all _non 0,0,0 5 

The following code represents the above Script line with spaces shown for clarity:


Note the space after the 5 representing the user pressing Enter to submit the entered radius to complete the command (a new blank line could also have been used in the script file for the same result).

At this point, you may be slightly confused by some of the prefixes & command parameters used in the above example, so here is a short explanation of the purpose of each item:

  • _ (underscore)
    Non-localised command prefix: this prefix ensures that the English name of a command is used, irrelevant of the language of the version in which it is invoked. For example, if we have a Script calling the LINE command, by prefixing the command name with an underscore (i.e. _line) the same Script can be used in, say, a French version of AutoCAD, without needing to replace every occurrence of 'line' with 'ligne'.

    Note that this prefix should also be used with command keywords to allow for cases in which the keyword does not use the the same 'key letter' when translated to another language.
  • . (period)
    Non-redefined command prefix: this prefix is used to account for commands that have been redefined, ensuring that the original standard command is used and not a redefined version.
  • non (or 'none')
    This is an Object Snap modifier (similar to end or mid) which instructs the command to ignore any active Object Snap modes for the next point input. The modifier keyword is prefixed with an underscore to account for non-English versions of AutoCAD, as described above.

For English versions of AutoCAD in which no commands have been redefined, the underscore & period prefixes are not absolutely necessary, however, it is good practice to include them to enable compatibility with all versions without any effect on the performance or operation of the Script.

Now its your turn...

Try it for yourself: select the above code (that is, the Script code without the spaces displayed) and copy it into a new Notepad file (or other plain text editor), save the file as filename.scr (the filename may be anything you like, but ensure that the Save As Type panel is set to All Files):

Then, in a blank new AutoCAD drawing type SCRIPT at the command line, and select the saved script file to run it. If you're lucky, you might just see a circle of radius 5 appear at the origin.

Now, this all seems pretty clever so far, but huge amounts of time can be saved if we extend this idea to more than one drawing.

Processing Multiple Drawings

If we issue the OPEN command within our script and specify a filename to open, the script will proceed to open the specified drawing and continue evaluating the remainder of the script file on the newly opened drawing.

When specifying a drawing to open, be sure to enclose it with quotation marks as this accounts for any spaces in the filepath - otherwise these spaces will be interpreted as the user pressing Enter.

Select all "C:\My Folder\Drawing1.dwg" _non 0,0,0 5 _.qsave _.close "C:\My Folder\Drawing2.dwg" _non 0,0,0 5 _.qsave _.close "C:\My Folder\Drawing3.dwg" _non 0,0,0 5 _.qsave _.close 

With spaces displayed:"C:\My Folder\Drawing1.dwg"_.circle_non0,0,05_.qsave_.close"C:\My Folder\Drawing2.dwg"_.circle_non0,0,05_.qsave_.close"C:\My Folder\Drawing3.dwg"_.circle_non0,0,05_.qsave_.close

This script will open Drawing1.dwg, create a circle centered at the origin with a radius of 5, and then proceed to save the drawing and close it before moving onto Drawing2.dwg.

Observe that there is no space after the final close command for the first two lines, as the new-line in the Script file will be interpreted as the user pressing Enter, and will issue this final command before proceeding to open the next drawing.

You can also load & call AutoLISP programs from within scripts, and so, to make the scripting more robust, you might consider creating an AutoLISP program to execute all operations on the drawing, and then merely use the script to open each drawing, load & evaluate the AutoLISP program, then finally save and close the drawing.

Upon viewing the above example, you can probably already see where the next time saving can be made: in the writing of the script. After all, who would want to type all those filenames manually? The Solution: Script Writer.


increase · reset · decrease

Designed & Created by Lee Mac © 2010