1. Mastering AutoLISP in 80
MinutesInstructor: Lynn Allen
Course Summary:
AutoLISP has been around for a long time and has always separated the AutoCAD
green thumbs from the 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 programming using AutoCAD's native graphical language.
The class is designed for intermediate-level AutoCAD users who have never
programmed in AutoLISP before.
2. Objectives
• To lay a firm foundation of the basics of Visual
Lisp.
• Prepare you to write your own Visual Lisp
routines
• Start you down the path to official AutoCAD
Gurudom ( or “Nerdom”)
• Teach you some quick and dirty basics of Visual
Lisp (don’t look too close!).
• Discover new ways to torture your coworkers!
3. Hold on - we have a lot of information to
cover in 80 minutes!
11. Arguments
• Arguments are the values you pass to a
function
(+ 5 6)
+ is the function
5 and 6 are the arguments
(setq x “Autodesk”)
Setq is the function
X and “Autodesk” are the
arguments
12. The Golden Rules of Visual Lisp
• For every open paren, you must have a
closed paren
Example: (setq x (+ a b))
• For every open double quote, you must
have a closed double quote.
Example: (prompt “How are you?”)
13. The Key to unlocking complicated
LISP routines:
Visual Lisp works from the Inside Out
(+ 5 (* 4 3))
is equal to
(4 * 3) + 5
(- (+ 5 2) (* 6 (- 7 6)))
is equal to
(5 + 2) - (6 * (7 - 6))
7 - (6 * 1)
15. Some popular Data Types:
• Real Numbers 1.5
• Integers 5
• Strings “LINE”
• Lists (8 . “DIM”)
• Subrs (or functions) SETQ
16. 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 is incorrect 0.5 is correct
Dotted pair: (0 . “CIRCLE”)
error: misplaced dot on input
17. (/ 7 2) => 3
(/ 7 2.0) => 3.5
(+ 1 2 3 4 5 6. ) => 21.0
(+ 1 .5) => invalid dotted pair
(+ 1 0.5) => 1.5
One real number changes the entire pot!
21. 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)
22. Variable Names
• Alpha-numeric
• May not contain spaces
• should not replace existing preset values
such as T or pi
Note: A variable that hasn’t been set is
equal to nil
23. Using Visual Lisp variables in
AutoCAD
(setq X 1 Y 2)
Command: !X
returns 1
Command: circle
3P/2P/TTR/<Center point>:
Diameter/<Radius>:!Y
24. Ways to ruin your Visual Lisp life
(setq + -)
(setq * /)
(setq pi 2.5)
Visual Lisp will let you abuse
yourself. . .
25. Using AutoCAD commands
in Visual Lisp (the good stuff!)
Using the COMMAND function, you can
access the AutoCAD commands
Example:
(command “QSAVE”)
(command “TRIM”)
(command “ZOOM” “P”)
(command “LAYER”)
26. By default, Visual Lisp doesn’t
display dialog boxes
Visual Lisp displays the command line interface
for commands.
To force the dialog box use:
(initdia)
Before the command:
(initdia)
(command “layer”)
29. Creating your own AutoCAD
Commands
(DEFUN)
DEFUN binds a set of expressions to a
variable.
(DEFUN C:ZAP ( )
Command: zap
30. • DEFUN
is the function
• C:
indicates the function will be an
AutoCAD command
• ( )
indicates no local variables and no
arguments (we’ll get to that another time!)
Anatomy of DEFUN
33. Loading Visual Lisp routines
• APPLOAD - used to load one or more
Visual Lisp routines
• (load “short”)
34. Opening a dialog to a specific tab
(command “+dialogname” X)
(command “+options” 7)
will open the Options dialog to tab #8
(command “+customize” 0)
35. What’s wrong with this picture?
(defun c:door
(“insert” “door” pause 1 1 45)
)
(defun c:fun ())
(prompt “are we having fun yet?)
)
40. Just for fun!
ALERT
ALERT sends an ALERT box to the
screen with the indicated text
Example:
(ALERT “Formatting the hard drive”)
41. ACAD.LSP or
ACADDOC.LSP
Automatic Visual Lisp Loading
ACAD.LSP or
ACADDOC.LSP
Automatic Visual Lisp Loading
• Put frequently used Visual Lisp routines.
• Undefine those AutoCAD commands you
want to automatically replace with Visual
Lisp routines.
• Place partial menu loading instructions
44. Undefine and RedefineUndefine and Redefine
Permits undefining and redefining the
internal AutoCAD commands
Note: AutoCAD commands can always be
executed with a leading period.
45. S::STARTUP
a special section of ACAD.LSP
S::STARTUP
a special section of ACAD.LSP
(defun C:LINE ( )
(prompt “Shouldn’t you be using Polylines?”)
(command “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.
46. Ways to torture your coworkers:Ways to torture your coworkers:
ACAD.LSP
(defun c:qsave ( )
(command “undo” “b” “y”)
(command “.qsave” “.qsave”)
(defun s::startup ()
(command “undefine” “save”)
(command “undefine” “qsave”)
(command “undefine” “saveas”)
)
47. one more means of torture:
(defun c:zoom ( )
(command “erase” “L” ““)
(command “.zoom”)
(princ)
)
(defun c:redo ( )
(prompt “You goofed - deal with it!”)
)
(defun c:undo ( )
(alert “Get it right the first time!”)
49. Other evil things to put in
s::startup!
S::STARTUP
(setvar “angbase” “180”)
OR
(setvar “snapang” 0.000001)
OR
(command “vpoint” “0,0,-1”)
(command “ucsicon” “off”)
50. What’s wrong with this picture?What’s wrong with this picture?
(1 + 1)
(* 5 .25)
(/ 9 2)
(setq x (+ 1 2)
(defun d:dimwit
(command “text” .5 90 pause)
(alert “hit cancel to exit dialog box”)
(defun s:startup ( )