Table of Contents
What is Scripting in AutoCAD?
Scripting is another method of automating tasks in AutoCAD, without the aid of programming languages like VBA or AutoLISP. We can create and manipulate AutoCAD objects, Open and Plot multiple drawings using Script files.
What is an AutoCAD Script File?
An AutoCAD script file is nothing more than a sequence of standard AutoCAD Commands. So, if you know how to type commands at the AutoCAD command prompt, you can easily create script files.
Why Script file?
Script files are small, lightweight, easy to use. Most importantly scripts save a great amount of time by cutting down the time required for manual labor.
How to create an AutoCAD Script File?
Any text editor can be used to create an AutoCAD script file. The commonly used text editor for this purpose is Notepad. AutoCAD script files use the extension .scr.
Examples
Script file to create ‘n’ number of AutoCAD lines
First of all, I will create two AutoCAD lines using the command prompt of AutoCAD.
The first line starts at (500,500) and ends at (600,550). The second line starts at (700,450) and ends at (575, 630).
Like I mentioned in the introduction part, a script file is a series of AutoCAD commands. So, to create the same objects using a Script file, we just need to ‘type in the above command sequence into a Text file’, ‘save it as a Script file’ and ‘load it into AutoCAD’.
So, let us copy the command sequence from the command prompt and paste it into a text file.
Now, we have to remove everything except the inputs used at the command prompt to create the lines. The portions to remove are highlighted in pink color.
Following is what remain after cleaning and these two statements are enough to create 2 AutoCAD lines.
Note that there is a space character after the Y co-ordinate of every point.
A blank space in an AutoCAD script is equal to pressing the ‘Enter key’ or the ‘Space bar’.
Save this text file as Test.scr. ‘Test’ becomes the name of the script file and .scr is the extension of AutoCAD script file.
To load this script file into another AutoCAD drawing,
Open the drawing > Type in SCRIPT in the command bar and press the Enter Key
‘Select Script File’ dialog will be activated. Select the file called Test.scr and click on Open.
Great! We just created two AutoCAD lines using an AutoCAD Script file.
The beauty of this method is that we can create hundreds or thousands of AutoCAD lines or any other AutoCAD objects using a single script file.
Try the following the script yourself which is meant to create 12 AutoCAD lines.
LINE 496.24,1692.94 2193.58,2522.55 LINE 625.22,1862.98 1951.11,2481.33 LINE 3065.47,930.31 3065.47,2326.74 LINE 3385.34,930.31 3065.47,930.31 LINE 3385.34,2326.74 3385.34,930.31 LINE 3065.47,2326.74 3385.34,2326.74 LINE 1069.1,1105.74 1069.1,1765.31 LINE 1848.12,1105.74 1069.1,1105.74 LINE 1848.12,1765.31 1848.12,1105.74 LINE 1069.1,1765.31 1848.12,1765.31 LINE 2668.42,1265.48 1729.46,2064.18 LINE 1729.46,2064.18 2668.42,2064.18
Useful commands related to Scripting
DELAY
DELAY command is used to specify the duration of a pause while executing the script.
The line DELAY 1000 in the script delays the execution of the next command for about one second. The longest delay we can use is 32767.
RSCRIPT
RSCRIPT command can be used when you want a script to repeat multiple times.
Things to note while creating AutoCAD scripts
Dialog boxes
AutoCAD commands that are followed by a dialog box will cause the script to stop in between. To avoid that, use a hyphen (-) before that particular command.
For example, LAYER command in AutoCAD prompts a dialog box called LAYER PROPERTIES MANAGER.
But when we use the statement ‘-LAYER‘, dialog box called LAYER PROPERTIES MANAGER will be suppressed and all options related to the LAYER command will be displayed in the command prompt.
Comments in AutoCAD Scripts
AutoCAD Scripts can contain descriptive text called ‘comment’. Comments can be used to add any kind of information regarding the script.
In AutoCAD Scripting, lines that begin with a semicolon (;) are considered as comments and will be ignored while processing the script.
The following is an AutoCAD script which will create 4 circles and 3 lines. The lines which begin with a semi colon are comments which I added as information for my colleagues.
As you can see in the command prompt, the first 2 lines which start with a semi colon are ignored and the Script started executing from the third line CIRCLE 950,550,195
It is a good practice to add comments if you intend to share the script with your co-workers. But avoid adding comment for the sake of it or commenting the obvious.
Command Aliases
AutoCAD command aliases may not be the same for all versions of AutoCAD. So, it is better to avoid using command aliases in script files.
Naming convention
For the script to execute properly, File Names, File Paths and Layer Names that contain spaces should be enclosed in double quotes. For example, to open the drawing called Route Markings-R0 at E:\Smart City Project\Reports and Drawings\ of my P.C, the statement should be:
OPEN "E:\Smart City Project\Reports and Drawings\Route Markings-R0"
Course on AutoCAD Scripting
I have created a comprehensive course on AutoCAD Scripting which is designed for all kind of AutoCAD users (Beginner to Advanced) who want to learn this advanced drafting method and save time spent for repetitive processes.
This course is divided into 6 sections
- How to create AutoCAD objects like Line and Circe
- How to use of Microsoft Excel or any other spreadsheet program to create Script files
- How to create layers using script file and change the color of AutoCAD objects
- How to create thousands of Text objects using Script file (with practical Examples)
- How to import points into AutoCAD drawings using Script file (with practical Examples)
- Simple Animations in AutoCAD, using Script file.
Check my UDEMY course AutoCAD Scripting