© 2011 Autodesk
Quick bio …
Mechanical engineer turned computer geek Private consultant since 1991AutoLISP programmer since the beginningFocusing on CAD standardization, customization and managementCadalyst Magazine contributing editor 14 year AU speaker
© 2011 Autodesk
Some fundamentalsLISP – LISt ProcessorShould you still use LISP?Everything is in lists
© 2011 Autodesk
Should we still use AutoLISP? Yes!AutoLISP is still very valid!A HUGE base of LISP code existsLISP shareware is abundantLISP does many things very easily as compared to VB/VBAIt is great for controlling AutoCAD configurations You don’t have to know much to get results …
© 2011 Autodesk
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?
© 2011 Autodesk
What do they do, where they live … They load on startup of AutoCADThey 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 …
© 2011 Autodesk
So what should I do … Use ACADDOC.LSP to get startedYou create your own ACADDOC.LSP It loads with every new drawingPut in in the SUPPORT folder and start hacking awayIf you mess up too bad, just delete!
© 2011 Autodesk
Create the file … Use Notepad – not Word!Use (prompt “\nACADDOC.LSP loaded.”) as text
© 2011 Autodesk
Alternately … You can use APPLOAD to load filesYou can use STARTUP SUITE to load at each start
© 2011 Autodesk
Syntax Basics Lists and ArgumentsRules of AutoLISPAccessing the command lineSpecial characters
© 2011 Autodesk
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?
© 2011 Autodesk
The Command Line How would you work normally?Draw a line between two user points
Type in LINE to start the line commandClick POINT for first point locationClick POINT for second point locationType in ENTER to halt the command
In AutoLISP: (command “line” pause pause “”)PAUSE instruction waits for the userThe “” is equal to hitting the ENTER key. Why? Because that’s they way it is …
© 2011 Autodesk
Some RulesFor 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 parensLike 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)
© 2011 Autodesk
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 …
© 2011 Autodesk
Lists and embedded lists illustrated Standard AutoLISP Result8 + 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
© 2011 Autodesk
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?
© 2011 Autodesk
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” )
© 2011 Autodesk
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 …
© 2011 Autodesk
Call outside programs …To invoke a browser do this:
(command “browser” “file goes here”)
To invoke an app:
(startapp “C:\folder\progname.exe”)
© 2011 Autodesk
User functions Speed for the userLower support for youA win-win scenarioLet’s put everything we’ve learned into action to build some functions.
© 2011 Autodesk
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)
)
© 2011 Autodesk
Fillet Zero FunctionFillet Zero(defun c:fz () (setvar “filletrad” 0.0)(command “.fillet” pause pause)(princ)
)
* What have I not done in this function?
© 2011 Autodesk
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!
© 2011 Autodesk
Auto Purge FunctionAuto Purge(defun c:atp () (command “-purge” “a” “*” “n” “.qsave”)(princ)
)
(defun c:atp () (command “-purge” “b” “*” “n” “.qsave”)(princ)
)
© 2011 Autodesk
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.
© 2011 Autodesk
The DOT form … Invoke commands like this: .LINENote the dot “.” character?This allows you to invoke a command whether it has been undefined or not!This is our little secret right ...
© 2011 Autodesk
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.
© 2011 Autodesk
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 …
© 2011 Autodesk
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!
© 2011 Autodesk
What Does This Do?(command “.undefine” “QSAVE”)
(defun c:qsave () (command “-purge” “b” “*” “n”)(command “.qsave”)(princ)
)
© 2011 Autodesk
Alerting the user … You can send a message to the user like this:
(alert “Message goes here”)
© 2011 Autodesk
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
© 2011 Autodesk
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 …
© 2011 Autodesk
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 …
© 2011 Autodesk
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 MOVEWhy do we have a “” in there …
© 2011 Autodesk
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)
)
© 2011 Autodesk
Adding in NumbersObtain numbers:
GETREAL – Real numbersGETINT – Integer numbers
How can we work with object sets now?
© 2011 Autodesk
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)
)
© 2011 Autodesk
More Bits and Bytes Loading programsLoading CUIx filesCentralized loadingVLIDE environmentCompiling
© 2011 Autodesk
Load a CUI like this …
(command “menu” “c:\\path\\cuiname.cuix”)
Note: Not MENULOAD but MENU …
© 2011 Autodesk
Compile your code … Use the VLIDE environment like this:
(vlisp-compile ‘st “c:\\test\\myprog.lsp”)
You’ll get MYPROG.FAS as a result …
© 2011 Autodesk
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!
© 2011 Autodesk
Centralizing AutoLISP CodeCAD 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!
© 2011 Autodesk
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 driveNote the \\ where a \ would normally be …
© 2011 Autodesk
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 driveAgain, note the \\ where a \ would normally be …
© 2011 Autodesk
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. © 2011 Autodesk, Inc. All rights reserved.