In this Article...
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
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:
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)
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.
Hello I am really new on scripting in cad, but I just want to do something really simple which is Rotate my model a certain angle I always get the same error invalid point, and I was wondering why???
Here you are my script
_AI_SELALL
ROTATE
0,0
100
RSCRIPT
Thank you very much for your support
Hello, im trying to do a CAD script which goes into a frame and edits the attributes of my choosing.
Like if i have a line of “date” which i want to 2014 how do i set that up in a script?
Im trying to work out how the coordinates work with the script.
When im trying to run the script ive wrote:
zoom E
ATTEDIT
Y
DATUMN
C 97.502662,11.175299,0.000000
V
r
140613
_
Thats basicly my script,
and it replys
“Command: zoom
Specify corner of window, enter a scale factor (nX or nXP), or
[All/Center/Dynamic/Extents/Previous/Scale/Window/Object] : E
Command: ATTEDIT
Edit attributes one at a time? [Yes/No] : Y
Enter block name specification :
Enter attribute tag specification : DATUMN
Enter attribute value specification :
Select Attributes: C
Specify first corner: 97.502662,11.175299,0.000000
Specify opposite corner: V
Invalid window specification.”
and in autocad prompt it says “specify first corner”?
What am i doing wrong!
It looks like you need to put the ‘E’ fo extents on it’s own line…
Paul
you can contact with me yildyusuf@gmail.com
Anybody please help me to find out GEAR FACTORY
What's the difference between writing scripts in notepad and using the visual basic interface?
In this case a Script is an AutoCAD .scr file. This is a completely separate thing to VBA – which came a lot later!
Paul
Hello Mr. Paul Munford,
Thanks for this page. I have a question If you answer I'll be thankful to you. I want to change text style "Style2" but style2 have .75 text height I want to change text height to 1.5 for all the drawings, for this what script should I use. Change text style and height for all drawings. I'll use ScriptPro for this but don't have desired script yet.
Thanks.
Follow the advice above to work out what your script should be ;)
many many thanks to you sir….i really learned something today…and it helped me a lot for my regular boring repetitive jos….
That’s great MJ :D
Okay, I'm buying the book. If I win it I'll have one for home and office. If I don't win it I'll buy another one. Paul, in 1900 and 93 I took a vocational college course in AutoCadd, ( that was V-10 before windows when it ran in DOS). We started learning script files to short cut common commands and it was awesome!
Thank you for the intel.
Tony
No problem Tony. What sort of scripts do you think that you will write?
Hi Anonymoose,
Windows .bat files may seem a bit 'old hat', but they get the job done :D Check out this Autodesk University session from Widom Associates
'Changing hundreds of CAD Drawings in a hurry': http://www.widom-assoc.com/html/au_2005.html …
Direct link to the PDF: http://www.widom-assoc.com/AU-CP12-3L.pdf
Enjoy!
Thanks, will give it a look!
"
run scripts on a whole batch of drawings using a windows .bat file" – I've never heard of using scripts in this way – can you please explain this technique?
Thanks for the posts, by the way – very informative.