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 firstname.lastname@example.org