cadalyst
Management

CAD Manager Programming Topics, Part 2: AutoLISP

23 Sep, 2008 By: Robert Green

Learn how to program your CAD tools, and you'll never have to wait for someone else to program a solution for you.


In the last edition of the CAD Manager's Newsletter, I began a series on programming methodologies I think are useful for CAD managers. I'm an advocate of knowing how to program because it has always allowed me to get quick results at the job site (and has kept me employed, I might add) over the years. My logic is that if you know how to program your CAD tools, you'll never have to wait for Autodesk, SolidWorks, Parametric Technologies, or Bentley to program a solution for you because you'll be able to do it yourself.

In this edition, I'll give you a very quick guide on the most commonly used CAD management programming tool, AutoLISP. AutoLISP is still very popular because so much AutoCAD is in use and because AutoLISP's long history means a lot of programs are already written. The Visual Basic and .NET programmers may scoff at knowing AutoLISP, but I've found my knowledge of the language continues to serve me well. Here goes.

What Do You Need To Know?
There's so much to learn in AutoLISP that you could spend years learning it, but the basics for CAD managers lie in the following topics:

  • controlling AutoCAD's startup
  • removing commands from AutoCAD's vocabulary
  • adding commands to AutoCAD's vocabulary
  • redefining commands in AutoCAD's vocabulary

If you know these tricks, you can revise and extend AutoCAD's command set to work any way you like and even make all the AutoCAD machines on your network pull from the same startup files to gain managerial consistency.

Interested? Sure you are. And even if you know AutoLISP basics already, you may learn a few tricks about managing machines around the network that will assist you.

Controlling the Startup
In order for AutoCAD to start up and load your custom AutoLISP functions, you have to make sure to create a certain file (ACADDOC.LSP) in a certain location (one of your support folders) so that AutoCAD will read the file on startup. This method for loading in AutoLISP instructions predates the StartUp Suite control in AutoCAD's Appload command and is actually preferred because no prior setting of the StartUp Suite file is required.

Simply create a file called ACADDOC.LSP in a text editing application such as Notepad (see Figure 1), and be sure to save the file into a folder that is set in your AutoCAD profile as shown in Figure 2.

figure
Figure 1. 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.

figure
Figure 2: 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).

Removing Commands
Now the fun begins as you remove commands from AutoCAD's command vocabulary. Ever wish your users would quit messing with the Sphere command or that they never could use 3dpoly (feel free to substitute in your own trouble-maker commands)? Of course you have at some point.

So instead of telling users not to use a command and hoping they'll comply, you can simply turn off the offending commands using the AutoLISP statement for UNDEFINING commands like this:

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

These instructions must be typed into your ACADDOC.LSP file and saved so that the next time AutoCAD starts, it will turn the offending commands (Sphere and 3dpoly) off. It'll be as though AutoCAD doesn't know these commands. If your users attempt to use the commands, they'll receive an "unknown command" error. How easy was that?

What you actually did with each line of code was tell AutoCAD that an AutoLISP expression was being input (that's what the opening and closing parentheses mean) and that you'd like to access AutoCAD's Command line (that's what command references) and that you'd like to instruct AutoCAD to use the undefine command to remove the given commands from the command set.

Adding Commands
OK, so now it gets a bit trickier, but you'll be using the same techniques you learned for removing commands to make the creative leap. To illustrate, I'll give you an example problem.

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 0.95x to give a slight dead space around the contents of the drawing.

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. To perform this task, you will use this AutoCAD command sequence:

  • Zoom <enter>
  • All <enter>
  • Zoom <enter>
  • 0.95x <enter>

OK, lets program it, shall we? Here's how the syntax will look:

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

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 blank set of parentheses).

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

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

(princ)

Trust me on this one. The last line of any well-written AutoCAD command function is always princ because it makes sure all temporary memory is flushed from the Command line and that the function exits gracefully. Again, trust me.

)

The final parenthesis simply balances the very first parenthesis (you did notice that the left and right parens didn't balance in the first line, right?) and closes out the function.

ACADDOC.LSP Now
At this point your ACADDOC.LSP file looks like this:

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

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

Now make sure to save your changes and 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.

A New Level of Control
Based on this little bit of AutoLISP, you now have the ability to make AutoCAD start up with a command set of your choosing. You can delete commands and build your own commands to make AutoCAD run as you want it to run.

All you have to do now is redefine standard AutoCAD commands to customize them and you'll be able to totally control AutoCAD's command set! The good news is that redefining commands is only slightly more complex than what you've looked at so far, so you shouldn't have too much trouble understanding it.

Summing Up
In the next edition of the CAD Manager's Newsletter, I'll conclude our look at using AutoLISP to control AutoCAD-based CAD tools by redefining AutoCAD commands and finishing off our custom ACADDOC.LSP file. I'll also point you toward some AutoLISP reference materials you can use to learn AutoLISP should you choose to do so. Until then.


About the Author: Robert Green

Robert Green

More News and Resources from Cadalyst Partners

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!