- Autocad Batch Run Script
- Autocad Batch Plot Script
- Autocad Batch Script
- Autocad Batch Plot Script
- Autocad Script Tutorial
AutoCAD Script is a simple and great way to run a batch of commands automatically. You can see how Paul uses the script to automate plotting. You can share a script to help your colleague creating the AutoCAD classic interface. The batch file contains commands that run the script and apply the export profile. Create the batch file in a text editor, such as Notepad. The finished batch file looks like the following example (except for your replacements for file and folder names).
Although each release of AutoCAD translates to bigger and arguably better features one thing has never changed; its malleability. In fact some would argue the malleability of the software as being its best feature. Be it the CUI command, LISP routines, or even VBA and .NET applications. Each of these features is included in the software for one reason – user customization. Even still, LISP and .NET customization can be rather illusive for all but a small minority of the AutoCAD user base.
An Easy Path to Customization
Luckily script customization affords even rookie AutoCAD users a way to automate AutoCAD with endless possibility. While I know the sheer term “Script File” intimidates many, but truth be known, you’ve been writing script files for as long as you have been using AutoCAD. So how is that possible? Well, an AutoCAD script file is really nothing more than a sequence of standard AutoCAD Commands. Thus if you know how to type commands at the command line, you know how to write a script file.
Scripting Commands with Dialog Boxes
Let’s take a look at how we might create a script file which creates a new layer named C-ROAD-TEXT, sets its color to Yellow, and then sets the layer current.
The LAYER command is among a growing number of AutoCAD Commands which have both a dialog box version and a command line version. By default, AutoCAD uses the dialog box version of a command. While this is great for general usage, script files have no way to interact with dialog boxes. For this reason, we must explicitly tell AutoCAD to use the command line version of a command. Generally, this can be done by prefixing the command name with a hyphen (-). Consequently, rather than typing LAYER which opens the Layer Manager Dialog, we will need to type -LAYER. This will suppress the dialog, and present the entire command at the command line.
Using the Command Line Version of a Command
Autocad Batch Run Script
Typing -LAYER at the command line results in the following prompt:
Enter an option [?/Make/Set/New/ON/OFF/Color/Ltype/LWeight/MATerial/Plot/Freeze/Thaw/LOck/Unlock
/stAte]:
Admittedly, navigating the command line version of a command can certainly add a degree of intimidation to this whole process. The important thing to pay attention to here is which letter(s) are capitalized. Looking at the -LAYER command, typing “C” will allow you to set a color, but assigning a material means three characters; MAT. In looking at the command prompt, you’ll notice that Color is represented with just the C capitalized, whereas MATerial has MAT capitalized. Using these abbreviations will share you a few keystrokes, but when in doubt you can always spell out the entire option. Thus rather than typing C, you could type Color in its entirety.
Looking once again at the command prompt, I want to create a layer named C-ROAD-TEXT. The option to create a new layer is New, and so I’ll simply enter N at the command line.
Autocad Batch Plot Script
Enter an option [?/Make/Set/New/ON/OFF/Color/Ltype/LWeight/MATerial/Plot/Freeze/Thaw/LOck/Unlock
/stAte]: N
After specifying the New option, AutoCAD will now prompt me with the following where I will enter my desired layer name. In this case, I’m going to enter C-ROAD-TEXT.
Enter name list for new layer(s): C-ROAD-TEXT
Upon pressing enter I am returned to the base LAYER command, where I can now change the color of my C-ROAD-TEXT layer from the default, White, to Yellow. To do that I’ll enter C, which we now know stands for Color.
Enter an option [?/Make/Set/New/ON/OFF/Color/Ltype/LWeight/MATerial/Plot/Freeze/Thaw/LOck/Unlock
/stAte]: C
Assuming I use one of the standard 255 AutoCAD colors, I can simply enter the desired number. As some may know, for primary colors such as Red, Yellow, Blue, even Cyan, I can either enter the colors number or it’s name.
New color [Truecolor/COlorbook] : Yellow
After specifying my desired color, I must now tell AutoCAD which layer to make yellow. I will of course enter C-ROAD-TEXT at this point.
Enter name list of layer(s) for color 2 (yellow) <0>: C-ROAD-TEXT
Building a Script from a Command Sequence
At this point, I have now assigned the color Yellow to my newly created C-ROAD-TEXT layer. With that, I can simply <Enter> through the command. So what does all of this look like in the form of a script?
Script | Description |
-LAYER | Command Line Version of the LAYER command |
N | Shortcut for New |
C-ROAD-TEXT | My new Layer Name |
C | Shortcut for Color |
Yellow | Could also enter 2 for Yellow |
C-ROAD-TEXT | Layer I want to assign the color Yellow to. |
Blank line representing an <Enter> | |
Blank line representing an <Enter> |
AutoCAD knows to execute this as a script file based on its extension. For this reason, I’ll want to use my favorite text editor (Notepad), do a File > Save As, and be sure to use a .SCR extension.
Naturally, creating the script file is but half the battle. I now need to execute the file in some way. Being a keyboard commando, you can execute a script file by typing SCRIPT or SCR at the command line. The other option is to go to Tools > Run Script. Both options will launch a dialog where I can then browse to the location of my .SCR file.
The most fundamental way of launching a script file is by typing SCRIPT or SCR at the command line. This will launch a dialog, where I can then browse to wherever I have saved my .SCR file.
Related
One of the great benefits of using AutoCAD to create your technical drawings is the ability to adapt AutoCAD to suit your company’s processes. If you can establish a technical drawing process that you perform frequently – you can automate it with AutoCAD. If you’ve ever had to do the same thing with AutoCAD twice – think about how you could automate it so you never have to do it again.
One of the easiest ways to automate a process within AutoCAD is to write a script. In this post, I will demonstrate how to write and develop a script for AutoCAD. In this example we will write a script that will automate a typical plot routine.
What is a script?
In computer programming terms, a script is a programme that will run with no interaction from the user. To AutoCAD, a script file is an ASCII text file that contains a set of command line instructions for AutoCAD to follow – just like an actor reading from a script. AutoCAD script files always have a file extension of ‘.scr’.
Defining a process
Autocad Batch Script
Before we can automate a process, first we have to define it. In this example we will write a script file that will create an A1 PDF plot of a drawing. We will work through the instructions that we would usually need to give AutoCAD to make a plot, and make a note of them so that we can write it up into a script.
Recording the steps
AutoCAD script files cannot interact with dialogue boxes, so we need to work out what we would type at the command line to produce our plot. To this end we will use the command line version of the plot command ‘-PLOT’ (the hyphen suppresses the plot dialogue) and we will manually step through the options to produce our plot.
Tip: If you don’t know the answer to the question AutoCAD is asking you, type ‘?’ at the command line and hit return to see a list of available options. Copy these options out into a text file for later reference. You may have to step through the routine a few times to get all the answers you need.
When we have produced a plot manually, we can open up the text window (hit F2 on your keyboard) to review the steps.
Creating the script file
The next step is to open a new blank notepad file (Please don’t be tempted to use Word or another word processing programme for this!). Copy the commands from the AutoCAD text window, and paste them into your notepad file.
Editing out the Junk
The next step is to edit out what you don’t need.
You don’t need AutoCAD’s command line prompts. You do need your replies. In the image below I have highlighted what can be stripped out. An empty line is equivalent to you pressing ‘Return’ on your keyboard, so leave those empty lines in!
Watch out for character wrapping inherited from the AutoCAD text window. Make sure all the text for each step is on the same line.
You should end up with something that looks like this:
Autocad Batch Plot Script
Don’t forget that you will need an empty line at the end of your script. This is equivalent to pressing ‘Return’ to complete the command.
Testing the script
The next step is to test the script. First, close down the text file and change the file extension to ‘.scr’
Tip: make sure that Windows is showing file extensions before you do this!
Next, simply drag and drop your Script file into AutoCAD to run it.
Debugging
It may take you a couple of goes to get your script to run. Use the text window to see how far your script ran before it stalled. Make the corrections in the .scr file and keep trying until you are happy!
Adding Comments (putting some junk back in)
Autocad Script Tutorial
Now you have a running script file it is a good idea to add a few comments to remind you what you did. You never know, it might be you who has to re-write the script in six months time…
AutoCAD will ignore any line in your script file that is preceded by a semi-colon. My version of the script ended up looking like this:
How could you use AutoCAD Scripting to automate your workflow?
AutoCAD scripts can be used to automate many tasks. You could use a script to add your corporate standard layers or styles to a drawing. You could write a script to update your title block. And of course you could adapt the script we’ve just finished writing to automate many of your plotting tasks.
AutoCAD scripts also work for AutoCAD LT, and you can run scripts on a whole batch of drawings using a windows .bat file or by using the free Scriptpro program from Autodesk labs.
Next time we will look at the differences between scripts and Macros.
Until then – stay lucky.