Programming Topics, Part 3: AutoLISP

8 Oct, 2008 By: Robert Green

More tips on using AutoLISP codes to customize and control your CAD operations.

In the previous edition of the CAD Manager's Newsletter, I continued my series on programming methodologies by exploring some AutoLISP concepts that are useful for CAD managers. I've been bowled over with e-mail responses to this series, many asking for more AutoLISP.

In this installment, I'll show you how to use the UNDEFINE and DEFUN methods I've already discussed to redefine existing AutoCAD (or AutoCAD vertical products) commands to your liking. First, I'll set up an example problem, and then I'll show you how to make it happen. Here goes.

The Problem Statement
A CAD manager wants to redefine the way that users save files. To this end, she wants to disable the SAVEAS command so that users can't change the name of files. Further, she wants to make sure that users always purge the dead blocks out of their drawings every time they save. Finally, she wants to add new commands that are useful into the file.

Here are some things the CAD manager needs to know:

  • SAVEAS. The actual command in AutoCAD that allows users to rename an existing drawing.

  • QSAVE. The command that 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 afterwards.

  • -PURGE. The command line version of the PURGE command that allows users to automate the purging process.

  • ACADDOC.LSP. The LISP file, where we'll create the programming code, must be located in a support folder (per the Options command).

The Approach
To make our example scenario work, we'll need to do the following:

  • turn off (UNDEFINE) the SAVEAS command
  • 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.

First, add the UNDEFINE instructions to the ACADDOC.LSP file like this:

(command "undefine" "saveas")
(command "undefine" "qsave")

Now every time AutoCAD starts, these two commands will be removed from AutoCAD's command vocabulary. If a user tries to type in or activate these commands in any way, they'll simply receive an "Unknown Command" message at AutoCAD's command line.

Click for larger image
Be sure the name of the file is ACADDOC.LSP and that you save the file in a support directory that is set in AutoCAD's profile. Note that I've placed a single line in the file with a semicolon in the first space to denote a comment. (Click image for a larger version)

Note: Simply create a file called ACADDOC.LSP in a text-editing application like NOTEPAD (see above), and be sure to save the file into a folder that is set in your AutoCAD profile, as shown below.

Support file paths are shown in the Files tab of the Options command. Note that you're not restricted to just plain AutoCAD because these AutoLISP techniques work in an AutoCAD-based platform (like AutoCAD P&ID shown above).

The DEFUN Code
Now add code to your ACADDOC.LSP file to create a new QSAVE command using DEFUN like this:

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

Every time AutoCAD starts, the SAVEAS command will not be available, but the QSAVE command will use your command instructions rather than the default AutoCAD QSAVE command.

The -PURGE Code
So how did I get this code line in the program?

(command "-purge" "b" "*" "n")

I got it by going through the process manually in AutoCAD, and writing down the steps, as you see below.

Click for larger image
(Click image for a larger version)

The .QSAVE Code (Dot Form)
Finally, what does this line in the ACADDOC.LSP file do?

(command ".qsave")

Further, why the dot "." character before the command? Since we've undefined the standard version of QSAVE already, we have to call the old version of the QSAVE by issuing the command in what I like to call "dot form" -- that is with a dot in front of the command name. It turns out that placing a dot in front of the name can access any undefined command. This is good programming form to follow.

Adding More Commands
Now add code to your ACADDOC.LSP file to create other utility commands like this:

(command "undefine" "saveas")
(command "undefine" "qsave")
(defun c:qsave ()
(command "-purge" "b" "*" "n")
(command ".qsave")
(defun c:ZM ()
(command "zoom" "all" "zoom" "0.95x")

Now every time AutoCAD starts, we'll get our new ZM command, as well as all the other undefined and redefined functions.

Summing Up
Now you know how to control AutoCAD's command vocabulary every time AutoCAD starts! Have fun experimenting, and see how you like the approach.

In the next edition of the CAD Manager's Newsletter, I'll finish off our CAD manager's look at AutoLISP by showing you how to control many machines over a network using the ACADDOC.LSP file. I'll also point out some AutoLISP reference materials you can use to further your usage of AutoLISP. Until then.

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