Mastering AutoLISP in 80 Minutes Instructor: Lynn Allen Course Summary: AutoLISP has been around for a long time and has always separated the AutoCAD green thumbs from the gurus gurus. This course begins by debunking some popular rumors and explores the amount of AutoLISP code used in CAD-dependent industries today. AutoLISP is more powerful, it’s free and it provides users with the ability to create new AutoCAD commands in minutes. This class helps seasoned AutoCAD users enter the world of customization and p programming g g using g AutoCAD's native g graphical p language. g g The class is designed for intermediate-level AutoCAD users who have never programmed in AutoLISP before.
You have come to the right place if... if • You know nothing or very little about Visual Lisp • You Y wantt tto write it your own Vi Visuall Lisp Li routines ti but b t have no idea where to begin. • You would like to have better control over your AutoCAD environment • You tried to “Walk down the Garden Path” but landed in a ditch! • You do not have a programming background
Objectives • To lay a firm foundation of the basics of Visual Lisp. Li • Prepare you to write your own Visual Lisp routines ti • Start you down the path to official AutoCAD G d Gurudom ( or “N “Nerdom”) d ”) • Teach you some quick and dirty basics of Visual Li (don’t Lisp (d ’t look l k too t close!). l !) • Discover new ways to torture your coworkers!
Hold on - we have a lot of information to cover in 80 minutes!
First and Foremost! Don’tt let Visual Lisp intimidate you! Don
What does LISP stand for? LISt Processor (not Lost In Stupid Parentheses!)
The Basics • • • •
Lists Functions Arguments Golden Rules of AutoLISP
Wh t iis a LIST? What Anything inside of parentheses Examples of LISTS: (a b c) (setq x 1) (princ)
What is a FUNCTION? (or subr) The ACTION you want Visual Lisp to do!
In Visual Lisp the function ALWAYS go first!!! Visual Lisp uses Prefix notation Example:
(+ 1 2) (- 5 3) (inters A B C D) (setq x 3)
Visual Lisp as a Calculator INFIX Notation (1 + 1) (3 * 4) (6 / 2)) PREFIX Notation (+ 1 1) ((* 3 4) (/ 6 2)
Arguments • Arguments are the values you pass to a f function ti (+ 5 6) + is the function g 5 and 6 are the arguments (setq x “Autodesk”) Autodesk ) Setq is the function X and “Autodesk” Autodesk are the arguments
The Golden Rules of Visual Lisp • For every open paren, you must have a closed l d paren Example: p ((setq q x (+ ( a b)) )) • For every open double quote, you must have a closed double quote. quote Example: (prompt “How are you?”)
The Key to unlocking complicated LISP routines: ti Visual Lisp p works from the Inside Out (+ 5 (* 4 3))
is equal to ( * 3) + 5 (4 (- (+ 5 2) (* 6 (- 7 6)))
i equall to is t ( + 2)) - ((6 * ((7 - 6)) (5 )) 7 - (6 * 1)
Quiz Time! Q
(* 4 (/ (+ 6 3) 3)) 12 (+ (* (- 5 2) (/ 15 3)) 6) 21 (/ (* (( 11 9) (+ ( 25 5)) (* 3 2)) 10
Some p popular p Data Types: yp • • • • •
Real Numbers Integers Strings Lists Subrs (or functions)
1.5 5 “LINE” (8 . “DIM”) SETQ
Real Numbers and Integers • Real Numbers have decimal points Example: 1.3 5.0 • Integers do not! Example: 25
11
• Real Numbers must have a leading zero. .5 5 is incorrect 0 5 is correct 0.5
D tt d pair: Dotted i (0 . “CIRCLE”) error: misplaced dot on input
(/ (/ (+ (+ (+
7 7 1 1 1
2) => 3 2.0) => 3.5 2 3 4 5 6. ) => 21.0 .5) 5) => invalid dotted pair 0.5) => 1.5
One real number changes the entire pot!
Basic Arithmetic Functions (for you math-heads): + = addition / = division (sqrt x) ( (expt t x y)) ((abs x)) (float x)
* = multiplication - = subtraction (sin ang) ( (cos ang)) ((log g x)) (fix x)
(atan x)
btw... btw
Angles are measured in radians! (not degrees) and you’ll need to remember that. that
St i Strings Usually Text (literals) Always double double-quoted quoted Spaces accepted
E Examples: l
““autodesk” t d k” “line” line “1.25”
Setting Variables (SETQ) (SETQ X 1) SETQ is the function X is the variable name 1 is the value Setting several variables at once: (SETQ A 1 B 2 C 3)
Variable Names • Alpha-numeric • May not contain spaces • should not replace existing preset values such h as T or pii
Note: A variable that hasn hasn’tt been set is equal to nil
Using Visual Lisp variables in AutoCAD (setq X 1 Y 2) Command: !X returns 1 C Command: d circle i l 3P/2P/TTR/ Center point>: 3P/2P/TTR/
:!Y
Ways to ruin your Visual Lisp life
( (setq q + -)) (setq * /) ( (setq pii 2.5)) Visual Lisp will let you abuse yourself. . .
Using AutoCAD commands in Visual Lisp (the good stuff!) Using the COMMAND function, you can access the th AutoCAD A t CAD commands d
Example: a pe (command (command (command (command
“QSAVE”) “TRIM”) “ZOOM” ZOOM “P”) P ) “LAYER”)
By default, Visual Lisp doesn’t display dialog boxes Visuall Lisp Vi Li displays di l the th command d line li interface i t f for commands. To force the dialog box use: (initdia) Before the command: (initdia) (command “layer”)
pause
allow ll for f user input i t
(command)
cancel
“”
enter
(Command “ZOOM” “A”) (C (Command d “ERASE” “L” ““) (Command “INSERT” INSERT “DESK” DESK pause 1 1 pause) ((Command “LINE” A B C “C”)) (Command “TEXT” pause “.5” 0 “Visual Lisp”) (Command “LAYER” “S” pause ““) (Command)
Creating your own AutoCAD Commands (DEFUN)
DEFUN binds a set of expressions to a variable. (DEFUN C C:ZAP ZAP ( ) Command: d zap
Anatomy ato y o of DEFUN U • DEFUN i the is th function f ti • C: indicates the function will be an AutoCAD command •() indicates no local variables and no arguments (we (we’llll get to that another
time!)
DEFUN examples p (DEFUN C:ZA C ZA ( ) (Command “ZOOM” “A”) ) (DEFUN C:SQ ( ) ( (Command d ““POLYGON”” 4 “E” “ ” pause pause)) ) (DEFUN C:ZAP ( ) (Command “erase” “all” ““) )
(defun c:ls ( ) SHORT.LSP (command “layer” “la e ” “M” pa pause se ““) ) (defun c:ZO ( ) (command “ZOOM” “O”) ) ((defun c:ttr ( ) (command “circle” “ttr” pause pause pause) ) (defun c:Jellydonut ( ) (command “donut” donut “0” 0 pause ) )
Loading Visual Lisp routines • APPLOAD - used to load one or more Visual Lisp routines • ((load “short”))
Opening a dialog to a specific tab (command “+dialogname” X) (command “+options” 7) will open the Options dialog to tab #8 (command “+customize” 0)
What’s wrong g with this p picture? (defun c:door ((“insert” insert “door” door pause 1 1 45) ) (defun c:fun ()) (prompt “are are we having fun yet?) )
PPurge.LSP g
(Defun c:ppurge ( ) (command “purge” “all” “*” “N”) )
Let’s create a command that breaks an object in the same spot twice (defun c:crack ()
Clean up py your ACT! • PRINC (get rid of the nils!)
PPurge.LSP g
(Defun c:ppurge ( ) (command “purge” “all” “*” “N”) (princ) )
Blind the user!
(he doesn’t really want to know what’s h t’ going i on. ..))
(SETVAR “CMDECHO” 0)
but don don’tt forget to turn it back on! (SETVAR “CMDECHO” 1)
PPurge.LSP g
(Defun c:ppurge ( ) (setvar “cmdecho” 0) (command “purge” purge “all” all “*” * “N”) N ) (setvar “cmdecho” 1) (princ) )
Talk to your user with PROMPT (defun c:clean ( ) (setvar “cmdecho” 0) (prompt “this this command will pick up all of your objects and clean the drawing file…please wait”) (command “erase” erase “all” all “”)) (prompt “\nthere..that’s better”) ( t (setvar ““cmdecho” d h ” 1) (princ) )
SSGET-Getting SSGET Getting a selection set
(a bit tougher…hang on!)
Used to grab a group of objects. (setq ss1 (ssget)) Select objects: (ssget “x”) grabs all the objects in the drawing
Using SSGET (defun c:cm ( ) (setq ss1 (ssget)) (command “copy” ss1 “” “m”) )
Let s create a command that does a Let’s full circle, rotated objects Polar Array (defun c:polar ()
Just for fun!
ALERT ALERT sends an ALERT box to the screen with the indicated text Example: (ALERT “Formatting Formatting the hard drive drive”))
ACAD LSP or ACADDOC ACAD.LSP ACADDOC.LSP LSP Automatic Visual Lisp Loading
• Put frequently used Visual Lisp routines routines. • Undefine those AutoCAD commands you y want to automatically replace with Visual Lisp routines. routines • Place partial menu loading instructions
ACAD.LSP
(defun c:ZA ( ) (command “Zoom” “All”) (princ)) (defun c:DT ( ) ((setvar “clayer” y “TEXT”)) (command “Dtext”) (princ)) (defun c:bolt ( ) (command “insert” insert “bolt” bolt pause pause pause) (princ))
Automatic loading LISP files ACAD.LSP ACADDOC LSP ACADDOC.LSP ACAD.MNL ------------ACAD200X LSP ACAD200X.LSP ACAD200XDOC.LSP
2 4 5 1 3
Undefine and Redefine Permits undefining and redefining the internal AutoCAD commands
Note: AutoCAD commands can always be executed with a leading period period.
S::STARTUP
a special i l section ti off ACAD.LSP ACAD LSP
(defun C:LINE ( ) (prompt “Shouldn’t you be using Polylines?”) (command “PLINE”)) PLINE )) (defun S::STARTUP ( ) (command “undefine” “line”) ) Note: s::startup is the last file to be loaded before control is handed over to the user.
Ways to torture your coworkers: ACAD.LSP (defun c:qsave ( ) (command “undo” “b” “y”) (command “.qsave qsave” “.qsave qsave”)) (defun s::startup () (command “undefine” undefine “save”) save ) (command “undefine” “qsave”) (command “undefine” undefine “saveas”) saveas ) )
one more means of torture: (defun c:zoom ( ) (command “erase” “L” ““) (command “.zoom”) (princ) ) (defun c:redo ( ) (prompt (p p “You goofed g - deal with it!”)) ) (defun (d u c:undo u do ( ) (alert “Get it right the first time!”)
(defun (d f c:regen () (setvar “cmdecho” 0) (command “donut” 0 300000000000 “10,10”) ((command “regen”) g ) (command “cmdecho” 1) ) (defun s::startup ( ) (command “undefine” undefine “zoom”) zoom ) (command “undefine” “undo”) ( (command d “undefine” “ d fi ” “redo”) “ d ”) (command “undefine” “regen”) )
Other evil things to put in s::startup! S::STARTUP (setvar “angbase” angbase “180”) 180 ) OR (setvar “snapang” 0.000001) OR (command “vpoint” “0,0,-1”) (command “ucsicon” “off”)
What’s wrong g with this p picture? (1 + 1) (* 5 .25) (/ 9 2) (setq x (+ 1 2) (d f d:dimwit (defun dd (command “text” .5 90 pause) (alert “hit cancel to exit dialog box”) ((defun s:startup p()
Review • • • • • • • • •
LISP stands for. . . List Function Command Co a d pause “” DEFUN (princ) (d)
• • • •
undefine Acad lsp Acad.lsp s::startup string
Lynn Allen l nn allen@a todesk com [email protected] Lynn’s Blog www.autodesk.com/blog