Introduction to AutoCAD Script Files

by Donnie Gladfelter on March 2, 2008

in AutoCAD,AutoCAD 2006,AutoCAD 2007,AutoCAD 2008,AutoCAD 2009

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.

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 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.

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.

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 to 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 to 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.

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

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. 

  • Les

    Donnie,

    I’ve been playing with script files using the tabledit command. I can populate cells of the AutoCAD table, but I am trying to figure out how to issue a command to make the text bold.
    At a command line in Autocad, the typing of CTRL+B sets the bold off and on…but how can that be written in script?

    Thanx

    • http://www.thecadgeek.com Donnie Gladfelter

      Les – I’m not sure how to issue a Ctrl + B via script. Something you may look into is using a “Bold” text style, assigning it to the text rather than trying to issue a Ctrl + B command through script.

  • Les

    Donnie,

    I’ve been playing with script files using the tabledit command. I can populate cells of the AutoCAD table, but I am trying to figure out how to issue a command to make the text bold.
    At a command line in Autocad, the typing of CTRL+B sets the bold off and on…but how can that be written in script?

    Thanx

    • http://thecadgeek.com/ Donnie Gladfelter

      Les – I’m not sure how to issue a Ctrl + B via script. Something you may look into is using a “Bold” text style, assigning it to the text rather than trying to issue a Ctrl + B command through script.

  • Andrés

    Hello,
    First of all, thank you for your wonderful blog and the tips (and time) you put in it.
    I have a little question to which I haven’t been able to find the answer, and I’d like to know if you could help me.
    The thing is that I want to execute some lisp script in hundreds of drawings, but in order to save some time I’d like to completely avoid AutoCAD from actually “drawing” the file o the screen.
    I’ve taken a look to ScriptPro but it doesn’t seem to have this feature.
    Have you had any experience alike? or known of some way to do it?

    So far I’ve tried putting this into the script:
    (setvar “layoutregenctl” 2)
    (setvar “regenmode” 0)
    But it still gets drawed when opening the file (off course changing to and from model space afterwards is much faster).

    Thanks in advance for *any* answer you may have (even if it is a simple “I don’t really know”).
    Cheers.

    • http://www.thecadgeek.com Donnie Gladfelter

      @Andrés – I think the underlying problem is with the batch processor, not your script. Your batch processor (ScriptPro) is what opens your drawing (not the script file itself). For this reason, I suspect your DWG is being REGENed (drawn) before your script has a chance to run. Consequently I really don’t know of a way to get around this behavior.
      I’m not sure if ScriptPro (or perhaps another batch processor) has the ability to “Partial Open” a drawing. Partial Open is an option from the AutoCAD open dialog, and doesn’t make AutoCAD load as much of your drawing.

  • Andrés

    Hello,
    First of all, thank you for your wonderful blog and the tips (and time) you put in it.
    I have a little question to which I haven’t been able to find the answer, and I’d like to know if you could help me.
    The thing is that I want to execute some lisp script in hundreds of drawings, but in order to save some time I’d like to completely avoid AutoCAD from actually “drawing” the file o the screen.
    I’ve taken a look to ScriptPro but it doesn’t seem to have this feature.
    Have you had any experience alike? or known of some way to do it?

    So far I’ve tried putting this into the script:
    (setvar “layoutregenctl” 2)
    (setvar “regenmode” 0)
    But it still gets drawed when opening the file (off course changing to and from model space afterwards is much faster).

    Thanks in advance for *any* answer you may have (even if it is a simple “I don’t really know”).
    Cheers.

    • http://thecadgeek.com/ Donnie Gladfelter

      @Andrés – I think the underlying problem is with the batch processor, not your script. Your batch processor (ScriptPro) is what opens your drawing (not the script file itself). For this reason, I suspect your DWG is being REGENed (drawn) before your script has a chance to run. Consequently I really don’t know of a way to get around this behavior.
      I’m not sure if ScriptPro (or perhaps another batch processor) has the ability to “Partial Open” a drawing. Partial Open is an option from the AutoCAD open dialog, and doesn’t make AutoCAD load as much of your drawing.

  • http://www.specwriter.com Tom Barry

    Once you have a script written, how can you have it automatically invoked from the command line? I want to have a program open an AutoCAD drawing, run the specified script(s), and then close the drawing.

    Can this be easily done in VBA?

    • http://www.thecadgeek.com Donnie Gladfelter

      @Tom – If all you want to do is open a drawing, run a script, and then close the drawing, a free utility from Autodesk named ScriptPro is probably your ticket. It does just that, lets you run a script to a batch of DWG’s. Just be sure to add a save command at the end of your script.
      ScriptPro: http://usa.autodesk.com/adsk/servlet/item?siteID=123112&id=4091678&linkID=9240618

      If you really want something available at the command line. I’d probably encase it in a simple LISP. Maybe something like:
      (defun c:myscript ()
      (command ) Insert list of commands into a command parameter
      )

  • http://www.specwriter.com/ Tom Barry

    Once you have a script written, how can you have it automatically invoked from the command line? I want to have a program open an AutoCAD drawing, run the specified script(s), and then close the drawing.

    Can this be easily done in VBA?

    • http://thecadgeek.com/ Donnie Gladfelter

      @Tom – If all you want to do is open a drawing, run a script, and then close the drawing, a free utility from Autodesk named ScriptPro is probably your ticket. It does just that, lets you run a script to a batch of DWG’s. Just be sure to add a save command at the end of your script.
      ScriptPro: http://usa.autodesk.com/adsk/servlet/item?siteID=123112&id=4091678&linkID=9240618

      If you really want something available at the command line. I’d probably encase it in a simple LISP. Maybe something like:
      (defun c:myscript ()
      (command ) Insert list of commands into a command parameter
      )

  • sief badawy

    hey Donnie
    i have a problem ending the polyline command in the script please help me in that

  • sief badawy

    hey Donnie
    i have a problem ending the polyline command in the script please help me in that

  • Eric Louria

    Thanks Donnie I will look into that batch program.

    • JAWAD

      HI
      THIZ IS JAWAD CAN U HELP ME ON SCRIPTS HOW TO MAKE IT

      • http://www.thecadgeek.com Donnie Gladfelter

        JWAD – A script file is nothing but a TXT file saved with a SCR extension. So rather than saving it as script.txt, you would save it as script.scr. As discussed above, each line of your text file will correspond to a ling (enter) on the AutoCAD command line. Whatever you would enter at the command line, is the same thing you would enter inside your text (SCR) file.See the table above for an example of this. My SCR fill would only contain the left column.

  • Eric Louria

    Thanks Donnie I will look into that batch program.

    • JAWAD

      HI
      THIZ IS JAWAD CAN U HELP ME ON SCRIPTS HOW TO MAKE IT

      • http://thecadgeek.com/ Donnie Gladfelter

        JWAD – A script file is nothing but a TXT file saved with a SCR extension. So rather than saving it as script.txt, you would save it as script.scr. As discussed above, each line of your text file will correspond to a ling (enter) on the AutoCAD command line. Whatever you would enter at the command line, is the same thing you would enter inside your text (SCR) file.See the table above for an example of this. My SCR fill would only contain the left column.

Previous post:

Next post: