Programming for CAD Managers, Part 3

12 May, 2010 By: Robert Green

Learn how to create command shortcuts with this simple introduction to AutoLISP.

In the last two editions of the CAD Manager's Newsletter, I discussed the topic of programming for CAD managers and provided you with skills-assessment criteria and a couple of video-based tutorials about customization. If you haven't had a chance to review the previous installments, I suggest you do so now to gain the proper context.

In this final installment, I'll walk through the all steps of a simple project to introduce you to AutoLISP programming. As you'll see below, AutoLISP is a great way for anyone using an AutoCAD-based program to get started in his or her programming quest. Here goes.

Why AutoLISP?

I know I'm going to receive queries on this subject, so let me provide my answers to the following questions before I dive into the example.

  •  Why AutoLISP instead of .NET (Visual Basic)?
  • AutoLISP is specific to AutoCAD — what about other languages?

AutoLISP is a great place to start learning programming for the following reasons:

  • It doesn't require any expensive compilers, so it is cheap to use.
  • It is specific to AutoCAD-based products, but there are a great many AutoCAD users out there — more than any other single system — so AutoLISP has become the common denominator of CAD programming.
  • Each individual program that uses .NET customization has its own API (application programming interface) specifications, so it would be impossible to cover them all here anyway.

Simply put, AutoLISP is a cheap, common, and relatively easy way to start learning about programming. And even if you already know .NET or other programming languages, knowing AutoLISP can always come in handy.

Introductory AutoLISP Project

To illustrate AutoLISP's power, I'll show you how to control AutoCAD's startup assuming a network architecture, then allow you to create your own command shortcuts.

Here are the steps we'll perform during this exercise:

  1. Create an ACADDOC.LSP file on the workstation(s)
  2. Create a STARTUP.LSP file on the server
  3. Remove AutoCAD commands in the STARTUP.LSP file
  4. Add AutoCAD commands in the STARTUP.LSP file
  5. Redefine AutoCAD commands in the STARTUP.LSP file.

I'll provide you with a brief description of each step, plus a link to a video where you can watch me complete the exercise if you'd like some extra help.


In order for AutoCAD to startup and load your custom AutoLISP functions, you must create an ACADDOC.LSP file in the support folder so AutoCAD will read it upon startup. Create the ACADDOC.LSP file in a text editing application such as Notepad, and be sure to save the file into a folder that is set in your AutoCAD support variable.

The code in the file will be a load instruction, so the user's machine knows to look to the server directory for the STARTUP.LSP file, then load the file if it is found.

   (if (findfile "z:\\autolisp\\startup.lsp")
    (load "z:\\autolisp\\startup.lsp")


The architecture of what we're doing can be seen in this diagram:

All CAD stations have access to the network drive/path Z:\AutoLISP, where the AutoLISP custom STARTUP.LSP file will reside.


Next, we'll create a STARTUP.LSP file in the network folder Z:\AutoLISP (or another directory of your choosing), so each CAD station will read the master startup file from the network.

From now on, when you need to make changes to your AutoLISP programming, you will only need to change the STARTUP.LSP on the server — all the CAD stations will load the changes the next time they start! Three words: centralized file support.

Remove Commands

Now we'll use the STARTUP.LSP file to remove some commands we don't want users to mess with. Instead of instructing users not to use a command and hoping they'll comply, we'll simply turn off the offending commands using the AutoLISP statement for undefining commands:

   (command "undefine" "sphere")
   (command "undefine" "3dpoly")

Simply add these lines of code to your STARTUP.LSP file, and your users will no longer have the Sphere or 3DPoly commands. It really is that simple.

Add Commands

Let's say you'd like to create a new command in AutoCAD called ZM (meaning Zoom Max) that will execute a zoom all, then zoom out to 95% magnification to give a slight dead space around the contents of the drawing. In order to program this command, you'll need to know the following:

  • The command name will be ZM (that is, the user will type ZM to activate it).
  • The command name ZM must not be already defined in AutoCAD.
  • The AutoCAD command sequence to perform this task would be:
        Zoom <Enter>
        All <Enter>
        Zoom <Enter>
        0.95x <Enter>

Here's how the program syntax will look:
   (defun c:ZM ()
    (command "zoom" "all" "zoom" "0.95x")

But what does the code mean? Let's take it line by line:

   (defun c:ZM ()

Creates a command (that's the "c:" part) called ZM with no other variables used in the routine (that explains the empty set of parentheses).

   (command "zoom" "all" "zoom" "0.95x")

Here we access the Command line (that's the "command" part) and type in "zoom" "all" "zoom" "0.95x" just as we would if we were using AutoCAD manually. Note that the pairs of quotation marks act like an Enter key.


The last line of any well-written AutoCAD command function is always Princ, because it ensures that all temporary memory is flushed from the Command line and that the function exits gracefully.


The final parenthesis simply balances the very first parenthesis (you did notice that there was an unequal number of left and right parens in the first line, right?), and closes out the function.

At this point your STARTUP.LSP file looks like this:

   (command ".undefine" "sphere")
   (command ".undefine" "3dpoly")

   (defun c:ZM ()
    (command ".zoom" "all" ".zoom" "0.95x")

Now make sure to save your changes, then restart AutoCAD to load the file. If everything was done correctly, you should no longer be able to use the Sphere or 3DPoly commands, and the new ZM command should be active.

Redefine Commands

Let's say you want your users to purge all the old blocks out of their files, but they never seem to do so. How do you deal with this problem?

AutoLISP allows you to undefine a command and add a new command, but what if you combined the two operations? You could undefine the normal QSave command, then redefine it to include some Purge logic to clean out the blocks every time your users save their work.

Here are the commands you'll use:

QSave. This command allows users to save drawing changes without changing the name of the file. In the case of a new drawing, the QSave command will prompt for the file name the first time, but not afterward.

-Purge. The Command line version of the Purge command allows us to automate the purging process.

Here's the method we'll follow for the project:

  •  Turn off (Undefine) the default QSave command.
  • Define (Defun) our own QSave command to include –Purge instructions for purging out blocks.
  • Defun (Defun) our utility commands as needed.

Let's get to work.

The Undefine Code

First, we need to add the Undefine instructions to the ACADDOC.LSP file:

    (command ".undefine" "qsave")

Now that QSave is undefined, we can make our own QSave command:

   (defun c:QSAVE ()
    (command ".-purge" "b" "*" "n")
    (command ".qsave")

(Notice that I had to use .QSave with the dot in front of it, because the original QSave command had been undefined.) Now every time AutoCAD starts, the QSave command will use your command instructions rather than the default AutoCAD QSave command.

(Note: Are you wondering how I got the Purge code line in the program? I did it by going through the process manually in AutoCAD and writing down the steps.)

At this point, your STARTUP.LSP file looks like this:

   (command "undefine" "sphere")
   (command "undefine" "3dpoly")
   (command "undefine" "qsave")

   (defun c:ZM ()
    (command ".zoom" "all" ".zoom" "0.95x")

   (defun c:QSAVE ()
    (command ".-purge" "b" "*" "n")
    (command ".qsave")

Programming in Action

I've broken the example down step by step in video form, so you can see how I do the programming and test the results.

Summing Up

I hope you've become enthused about programming during this series in the CAD Manager's Newsletter, and have taken advantage of the video tutorials to get started. I know the coverage has been quick and that I've thrown you in the deep end of the pool to start learning, but if you give it an honest effort you'll find it isn't nearly as hard as you might think.

If you have any questions, please feel free to ask me. You'll find my e-mail link at


About the Author: Robert Green

Robert Green

AutoCAD Tips!

Lynn Allen

In her easy-to-follow, friendly style, long-time Cadalyst contributing editor and Autodesk Technical Evangelist Lynn Allen guides you through a new feature or time-saving trick in every episode of her popular AutoCAD video tips. Subscribe to the free Cadalyst Video Picks newsletter and we'll notify you every time a new video tip is published. All exclusively from Cadalyst!

Follow Lynn on TwitterFollow Lynn on Twitter

Which device do you typically use to read content?
A desktop computer / tower workstation
A tablet
A smartphone
A laptop or mobile workstation
I regularly use both a desktop computer and a smartphone for this purpose
I regularly use another combination of devices for this purpose
I prefer to print out articles from the website and read them on paper
Submit Vote

Download Cadalyst, Fall 2015

Download Cadalyst Magazine Special Edition