You should first understand that Visual Lisp began its life as an add-on product and has wound up as a native AutoCAD function with the AutoCAD 2000 release. This means that Visual Lisp support is not standard for AutoCAD R14 platforms so you should be careful not to build Visual Lisp commands into your Lisp routines if R14 support is an issue.
Having exposed Visual Lisp’s only major restriction I can now summarize some of the enhanced features Visual Lisp brings to the table:
Color coding of syntax – Correctly spelled Lisp functions will show as blue while incorrectly spelled functions will remain black, as an example. The color coded environment makes it much easier to detect errors than working with a standard text editor.
Parenthesis matching – A great feature for newer Autolisp programmers (and handy for those of us who’ve been at it a while) that alerts you to mismatched parentheses and quote marks. This feature combined with the color coding mentioned above can save hours on each project.
Compilers – Visual Lisp supports compiling LSP code to a secured “fast load” FAS file format. By compiling your code you’ll discourage user tampering, protect your programming code, and your routines will even load faster due to the compression (roughly 60%) over standard LSP file types.
Reactors – Much like Visual Basic, Visual Lisp reactors have the capability to trigger programs based on what the user is doing at the moment. A useful example would be to trigger a certain procedure when a user saves a drawing to set layers and variables correctly. In Autolisp the only way to accomplish this task would be to redefine the SAVE, SAVEAS and QUIT functions (relatively messy) but in Visual Lisp the VLR reactor commands allow you to seamlessly detect what the user is doing and REACT to it.
Let’s Get Rolling
Start up AutoCAD and type in VLISP or VLIDE at the command line and wait for the Visual Lisp window to activate. You’ll now be able to start a new file using the File menu’s New File command. You may now start typing Autolisp code into the window. For our purposes we’ll start with the following:
(setq variable1 28.4)
Note how the components of the syntax are colored? The color coding will help you differentiate functions from variables. Now type in the following intentionally incorrect syntax and see what happens:
(seta variable1 28.4)
Note that the SETA will not be colored blue but rather black. This is your clue that the SETA is not being recognized as a function. See how that works? You may now feel free to mistype functions and see how the colors change to assist you with debugging.
Save the Results
Now you’ll simply use the File menu’s SAVEAS command to save your programming work in an LSP file format. Please note where the LSP file is stored because its location will be important in just a moment!
After saving the LSP file you can compile your programming code to the FAS file format by using the Visual Lisp Console (you’ll see it in the Visual Lisp for AutoCAD window) to execute the VLISP-COMPILE command as follows:
(vlisp-compile ‘st “filename.lsp”)
The ‘ST parameter denotes the standard compile build method which creates a FAS type file.
The FILENAME.LSP is the input file you wish to have compiled to FILENAME.FAS
I should point out that there are other options you can use with the VLISP-COMPILE function that I’ve not mentioned. The easiest way to see all the options are to use the AutoCAD Developer’s Help and search on VLISP-COMPILE for all the syntax options. I think you’ll find that the form of the command I’ve covered is useful in 99% of the cases you’ll likely need though.
Hint: As an extension of the above topic why not fire up the Visual Lisp VLIDE and simply compile your old LSP files to FAS files to gain the speed and security advantages that Visual Lisp has to offer?
Load It Up!
Now that you have compiled FAS files you may need to modify other Autolisp routines or menus to load the newly compiled formats. The rule of thumb is to look for LOAD instructions like this one:
and change them to:
All the same rules regarding paths and file loading are exactly the same for Visual Lisp FAS files as they are for LSP so your biggest challenge will be searching your existing code and substituting FAS for LSP.
Programming with Reactors
In the last issue of the newsletter I promised we’d have a look at Visual Lisp reactors in this issue. Simply put, Visual Lisp reactors are a set of functions that understand what you are doing within the AutoCAD session and then react by running instructions you program, thus the name reactors.
Getting reactors running requires a few overhead steps which we must go over before we actually try to program something. Those steps are listed here:
Load the necessary reactor code from the ACADDOC.LSP or the ACAD.LSP file by inserting the following line into the code:
Note that this code must execute BEFORE any other functions having to do with reactors. Also note that this only works with AutoCAD 2000 or later, not with AutoCAD R14.
Define your particular commands using names that indicate what the functions will do. I offer the following two examples, one will run just prior to saving while the other will run just after saving:
(defun DoThisAfterSave (Caller CmdSet)
(alert “You just saved the drawing!”)
(defun DoThisBeforeSave (Caller CmdSet)
(alert “You are about to save the drawing!”)
Note the syntax for the input arguments to the functions, CALLER and CMDSET which cannot be omitted. In both these cases the only result of the function will be an ALERT box. We’ll substitute in some more useful command syntax in a while.
Now create your own reactor set as follows using the VLR-DWG-REACTOR function as shown:
Note the “:” characters before the VLR functions which are very easy to miss. Note that the instruction for each reactor is simply the one of the functions you defined in step 2. There are additional drawing reactors available besides the two I’ve illustrated, those being :vlr-beginClose, :vlr-databaseConstructed, :vlr-databaseToBeDestroyed, vlr-beginDwgOpen, :vlr-endDwgOpen and :vlr-dwgFileOpened.
Last, define removal tools to unload your reactors and/or all reactors should you want to return AutoCAD to its default state. Simply use the following code segments to accomplish both removal tasks:
Note that it the CLEAR_MY_REACTORS function that the MyReactor1 argument must agree with the reactor function you created in step 3.
Using the sample code from the steps above create an ACADDOC.LSP file and place it in your support directory. Now restart AutoCAD so that the code will be loaded into AutoCAD upon startup. There will be no evidence that you’ve actually created the reactors if everything goes well, however, if you have any typos or errors in your ACADDOC.LSP file you will see error codes at AutoCAD’s command line.
Assuming no errors you may now draw a couple of objects in the drawing and proceed to save your work. After you specify a name for the drawing you should receive an alert that:
“You are about to save the drawing!”
after actual saving you’ll receive another alert that:
“You just saved the drawing!”
These alert messages are proof that your custom reactors are functioning. We may now proceed to add some code that performs more useful functions that simply alerting you.
Beefing up the Code
Let’s say you defined one of your reactor functions like this:
This function would automatically zoom to extents before any drawing was saved!
It doesn’t take a whole lot of imagination to see what you can do with reactors does it? I bet you’re already thinking of what you can accomplish, right?
Besides the vlr-dwg-reactor you may want to investigate the vlr-dxf-reactor and the vlr-editor-reactor for functions that allow you to control dxf file writes/reads and to eavesdrop on the editing session, respectively. These reactor elements are very powerful and far easier to use than command definitions in conventional Autolisp.
You may even find that customization using reactors makes moving away from the tried and true R14 platform worth it if nothing else has. I encourage you to explore these reactor elements, you’ll find it worthwhile.
March 28th, 2007 |
Comments are closed.
Robert has provided CAD management consulting, programming and training services for clients throughout the United States and Canada since 1991. Reach Robert at email@example.com