Programming Topics, Part 3: AutoLISP
7 Oct, 2008 By: Robert GreenMore 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.
The UNDEFINE Code
First, add the UNDEFINE instructions to the ACADDOC.LSP file like this:
(command "undefine" "saveas")
(command "undefine" "qsave")
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")
(princ)
)
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.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")
(princ)
)
(defun c:ZM ()
(command "zoom" "all" "zoom" "0.95x")
(princ)
)
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.
For Mold Designers! Cadalyst has an area of our site focused on technologies and resources specific to the mold design professional. Sponsored by Siemens NX. Visit the Equipped Mold Designer here!
For Architects! Cadalyst has an area of our site focused on technologies and resources specific to the building design professional. Sponsored by HP. Visit the Equipped Architect here!