AutoLISP Productivity Power Robert Green CAD-Manager.com
Quick bio Mechanical engineer turned computer geek Private consultant since 1991 AutoLISP programmer since the beginning Focusing on CAD standardization, customization and management Cadalyst Magazine contributing editor 14 year AU speaker rgreen@cad-manager.com
Some fundamentals LISP LISt Processor Should you still use LISP? Everything is in lists
Should we still use AutoLISP? Yes! AutoLISP is still very valid! A HUGE base of LISP code exists LISP shareware is abundant LISP does many things very easily as compared to VB/VBA It is great for controlling AutoCAD configurations You don t have to know much to get results
Key files and why they matter LSP Load in order What to mess with What not to!
All those wacky file names ACAD20XX.LSP (system file XX is version) ACAD.LSP (This is your file) ACAD20XXDOC.LSP (system file - XX is version) ACADDOC.LSP (This is your file) CUINAME.MNL (loads with CUI) So what does it all mean?
What do they do, where they live They load on startup of AutoCAD They load in a certain order (listed on previous slide) Some have code in them and some don t (ACADDOC.LSP and ACAD.LSP don t as an example) They reside in the SUPPORT folder
So what should I do Use ACADDOC.LSP to get started You create your own ACADDOC.LSP It loads with every new drawing Put in in the SUPPORT folder and start hacking away If you mess up too bad, just delete!
Make sure it works Create ACADDOC.LSP Load from network? Verify operation
Find the support folder Use OPTIONS to find the folders
Add a network folder Use OPTIONS to add it
Create the file Use Notepad not Word! Use (prompt \nacaddoc.lsp loaded. ) as text
Save the file To the SUPPORT folder Use ACADDOC.LSP as the name
Alternately You can use APPLOAD to load files You can use STARTUP SUITE to load at each start
Syntax Basics Lists and Arguments Rules of AutoLISP Accessing the command line Special characters
Lists and Arguments (+ 20 30) Here the + is a FUNCTION and the two numbers are ARGUMENTS (command line 0,0 1,1 ) Here COMMAND is the function, all others are ARGUMENTS (getvar dimscale ) Which is the function? The argument?
The Command Line How would you work normally? Draw a line between two user points Type in LINE to start the line command Click POINT for first point location Click POINT for second point location Type in ENTER to halt the command In AutoLISP: (command line pause pause ) PAUSE instruction waits for the user The is equal to hitting the ENTER key. Why? Because that s they way it is
Some Rules For every ( there is an equal and opposite ) Like this: (setq A 3.0) Not like this: (setq A 3.0)) or (setq A 3.0 Same goes for quote marks! Like this: (setq name Robert Green ) Not like this: (setq name Robert Green) (setq name Robert Green ) For every function there is precisely one set of parens Like this: (+ 3 5) Not like this: ((+ 3 5)) When formatting numbers always avoid invalid dotted pairs Like this: (setq A 0.5) Not like this: (setq A.5)
What s Going On Here? (command viewres y 5000 ) (command -color BYLAYER ) (command -linetype set BYLAYER ) (command menu menuname.cuix ) (command viewres y pause) That s not so bad intuitive actually
Syntax Basics Named variables System variables Data types
Lists and embedded lists illustrated Standard AutoLISP Result 8 + 4 (+ 8 4) 12 8 + 2.3 (+ 8 2.3) 10.3 5 4 (/ 5 4) 1 (8 + 4) * 3 (* (+ 8 4) 3) 36 (3.0 * 5.0) 4.0 (/ (* 3.0 5.0) 4.0) 3.75 ((4 + 4) * 3) 2 (/ (* (+ 4 4) 3) 2) 12
SETQ (SET equal) To create the variable MYNAME and set it with your proper name as a value do this: (setq MYNAME Robert Green ) To store a system variable for DIMSCALE do this: (setq OLD_DIMSCALE (getvar dimscale )) Which uses embedded lists?
SETVAR (SET VARiable) To set the system variable for the fillet radius to 0.25 do this: (setvar filletrad 0.25) To set the system variable for expert status do this: (setvar expert 5) To set the system variable for dimension style do this: (setvar dimstyle am_ansi )
Storing variables and data types (setq VALUE 12.7) real number (setq VALUE 1) integer number (setq VALUE Happy ) string variable (setvar filletrad 0.25) real (setvar expert 5) Integer (setvar dimstyle am_ansi ) - string Make sure you input the data the variable needs
Call outside programs To invoke a browser do this: (command browser file goes here ) To invoke an app: (startapp C:\folder\progname.exe )
User functions Speed for the user Lower support for you A win-win scenario Let s put everything we ve learned into action to build some functions.
User Function Examples (defun C:ZA () (command.zoom a ) (princ) )
User Function Examples (defun C:ZA () (command.zoom a ) (princ) )
User Function Examples (defun C:VR () (command viewres y 5000 ) ) * Note that I left out PRINC? (defun C:BL () (command -color BYLAYER ) (command -linetype set BYLAYER ) (princ) )
Fillet Zero Function Fillet Zero (defun c:fz () (setvar filletrad 0.0) (command.fillet pause pause) (princ) ) * What have I not done in this function?
Improved Fillet Zero (defun c:fz () (setq old_filletrad (getvar filletrad )) (setvar filletrad 0.0) (command.fillet pause pause) (setvar filletrad old_filletrad) (princ) ) * Note how we store and recall the FILLETRAD so the function puts things back the way they were!
Auto Purge Function Auto Purge (defun c:atp () (command -purge a * n.qsave ) (princ) ) (defun c:atp () (command -purge b * n.qsave ) (princ) )
More Bits and Bytes Undefine Dot form Redefine Alerts CMDECHO
Undefining (command.undefine LINE ) (command.undefine TORUS ) Don t want them messing with a command? Just undefine it Now you can SUBTRACT from the AutoCAD Command set in your ACADDOC.LSP file.
The DOT form Invoke commands like this:.line Note the dot. character? This allows you to invoke a command whether it has been undefined or not! This is our little secret right...
Redefining (command.redefine LINE ) (command.redefine TORUS ) Want to be sure that a command is active? Just redefine it Now you can UNSUBTRACT from the AutoCAD Command set with ease.
Undefining revisited What if your users find out about REDEFINE and start REDEFINING your UNDEFINES? Just undefine the redefine like this: (command.undefine REDEFINE ) That ll stop users from redefining
Redefining You can undefine a command and redefine it like this: (command.undefine TORUS ) (defun C:TORUS () (alert Don t use that command! ) (princ) ) Now you do whatever you want!
What Does This Do? (command.undefine QSAVE ) (defun c:qsave () (command -purge b * n ) (command.qsave ) (princ) )
Alerting the user You can send a message to the user like this: (alert Message goes here )
Command echo (CMDECHO) Run in STEALTH mode like this: (defun C:BL () (setvar cmdecho 0) (command -color BYLAYER ) (command -linetype set BYLAYER ) (setvar cmdecho 1) (princ) ) * SETVAR is on or off so no need to store it s value
Points and Sets How do points work? How can we work with object sets?
Get a set like this (setq user_set (ssget \nselect objects for movement: )) You now have a set stored in a variable Note the \n prompting to the user
Get some points like this (setq from_point (getpoint \nselect point to move objects FROM: )) (setq to_point (getpoint \nselect point to move objects TO: )) You now have two 3D points stored in variables Note the \n prompting to the user
Now take action like this (command.move user_set from_point to_point) You are now moving the set from the first point to the second point The user doesn t have to interact with MOVE Why do we have a in there
What would this do (defun c:mm () (setq user_set (ssget \nselect objects for movement: )) (setq from_point (getpoint \nselect point to move objects FROM: )) (setq to_point (getpoint \nselect point to move objects TO: )) (command.move user_set from_point to_point) (princ) )
Adding in Numbers Obtain numbers: GETREAL Real numbers GETINT Integer numbers How can we work with object sets now?
What would this do (defun c:mr () (setq user_set (ssget \nselect objects for move/rotate: )) (setq rotate_angle (getreal \ninput rotation angle: )) (setq from_point (getpoint \nselect point to move objects FROM: )) (setq to_point (getpoint \nselect point to move objects TO: )) (command.move user_set from_point to_point) (command.rotate user_set to_point rotate_angle) (princ) )
More Bits and Bytes Loading programs Loading CUIx files Centralized loading VLIDE environment Compiling
Load a CUI like this (command menu c:\\path\\cuiname.cuix ) Note: Not MENULOAD but MENU
VLIDE environment You can write code in the VLIDE window like this:
Compile your code Use the VLIDE environment like this: (vlisp-compile st c:\\test\\myprog.lsp ) You ll get MYPROG.FAS as a result
Load compiled code Use a LOAD statement like this: (load c:\\test\\myprog.fas ) Now your LSP code is secure! Be sure not to lose your LSP file though!
Centralizing AutoLISP Code CAD Managers rejoice! Keep you code in a single network location and load it every time users start up. Need to change code? Just change the master copy on the network!
Put this in the ACADDOC.LSP file (if (findfile "x:\\autolisp\\utils1.lsp") (load "x:\\autolisp\\utils1.lsp")) ) Network loaded programs are the way to go for convenience. This assumes a network X drive Note the \\ where a \ would normally be
Put this in the ACADDOC.LSP file (if (findfile "x:\\autolisp\\utils1.fas") (load "x:\\autolisp\\utils1.fas")) ) Network loaded compiled programs are the way to go for security. This assumes a network X drive Again, note the \\ where a \ would normally be
AutoLISP Productivity Power Robert Green CAD-Manager.com
Autodesk, AutoCAD* [*if/when mentioned in the pertinent material, followed by an alphabetical list of all other trademarks mentioned in the material] are registered trademarks or trademarks of Autodesk, Inc., and/or its subsidiaries and/or affiliates in the USA and/or other countries. All other brand names, product names, or trademarks belong to their respective holders. Autodesk reserves the right to alter product and services offerings, and specifications and pricing at any time without notice, and is not responsible for typographical or graphical errors that may appear in this document., Inc. All rights reserved.