Programming Topics, Part 4: AutoLISP21 Oct, 2008 By: Robert Green
Set up customized AutoLISP code files for all your users without having to work at individual computers.
In the last several editions of the CAD Manager's Newsletter, I've been leading you through a methodology for using AutoLISP to control your AutoCAD-based applications. Along the way you've learned to undefine commands, add new ones, and redefine commands to perform custom functions.
This time I'll finish up my CAD manager's look at AutoLISP by showing you how to control many machines over a network using the ACADDOC.LSP file in tandem with a network-based LISP file. I'll also point you toward some AutoLISP reference materials you can use to further your usage of AutoLISP should you choose to do so. For the purposes of our discussion, I will assume you've read the last few installments. Here goes.
As you'll recall from prior lessons, the ACADDOC.LSP file is automatically loaded every time AutoCAD starts up. Therefore, the ACADDOC.LSP file becomes a very handy place to put your custom AutoLISP code. The downside is that the ACADDOC.LSP file is unique to the user's machine and therefore resides on the C drive somewhere. And since the file resides on the C drive, the CAD manager has to visit the user's machine to make any changes to it.
So the CAD manager finds himself in a situation where he has a method to control users' machines, yet he'll have to go to the machine over and over to make changes. Hardly a productive way for CAD managers to spend their time.
ACADDOC.LSP as a Pointer
To get around our ACADDOC.LSP problem, we'll use a trick to stabilize the file so you will be able to customize the AutoLISP environment without going to the user's computer. Interested? Here's how it works.
Let's assume you have remote computers running AutoCAD, connected to a network, which has a Z drive, and which is viewable by all users. Further, let's assume that a directory called Z:\AutoLISP has been created to hold all our custom code.
Schematically the setup looks like this:
All CAD stations have access to the network drive/path Z:\AutoLISP, where the AutoLISP custom files will reside.
The ACADDOC.LSP Code
Now we'll place the ACADDOC.LSP file in the user's AutoCAD support directory (for AutoCAD 2009 it would be C:\Program File\AutoCAD 2009\Support) and include the following code to point to the server like this:
(if (findfile "z:\\autolisp\\startup.lsp")
Note a few things about this syntax:
- IF and FINDFILE -- This line instructs AutoCAD to look for the target file and IF it finds the file, to load it in. Using this logic will prevent "File not found" errors on AutoCAD startup.
- LOAD -- This function instructs AutoCAD to load in another AutoLISP file from the path/file name specified in the quote marks.
- " " -- The quote marks contain the fully qualified drive/path/file for the target file.
- \\ -- Since a single \ character is interpreted as an escape sequence, you need to use \\ to denote a file path delimiter. Alternately you could use / characters if you prefer, but I've always used \\ just to stay consistent with normal path statements.
Now every time AutoCAD starts, it will read the user's ACADDOC.LSP file and simply point to a network-maintained STARTUP.LSP that will contain your custom code. Now you'll never need to go visit the user's machine again!
The STARTUP.LSP Code
Now we turn our attention to the STARTUP.LSP file on the server and the code we'll use to control AutoCAD's startup behavior. Here is some example code based on the last issue of the newsletter:
(command "undefine" "saveas")
(command "undefine" "qsave")
(defun c:qsave ()
(command "-purge" "b" "*" "n")
(defun c:ZM ()
(command "zoom" "all" "zoom" "0.95x")
This code will undefine the SAVEAS and QSAVE commands, then redefine the QSAVE command to include automatic block purging. The ZM function creates a neat utility zoom command that zooms to all and then creates a 5% blank buffer space around the drawing using a 0.95x zoom.
Now every time your user starts AutoCAD, these functions will be invoked automatically! It really is that easy.
So what happens when we want to add more functions to our user's AutoLISP environments? We'll simply edit the STARTUP.LSP file at the server and everyone will be updated! A few words of warning and some recommendations, though, to make sure the update goes well:
If you make a syntax mistake: If you put any incorrect syntax in the STARTUP.LSP file, you've created an error on every AutoCAD machine in the company. Ouch.
To prevent mistakes: Create a local copy of STARTUP.LSP on your C drive in a directory like C:\LISP, and then make your edits in the local copy. To test the file, simply load it manually by typing (load "c:\\lisp\\startup.lsp") on the command line and checking that everything works.
To update the network file: After verifying that your locally edited file works, simply copy it back to the network drive (Z:\AutoLISP in this example), and now all users will be updated.
Now you can make edits locally and update everyone via a single network file delivering custom functions to your users with greatly reduced hassle for the CAD manager. Mission accomplished.
Now you have a solid framework for controlling how your AutoCAD-based products start up and operate, and you can do so using single-point network control. I've been using this approach for a long time and have always had good success with it, so I know you can use it at your company.
In the next edition of CAD Manager's Newsletter, I'll compile some information from the 2008 CAD Manager's survey and provide links to the complete survey and analysis. Until then.