Professional Documents
Culture Documents
Mastering Autolisp in 80 Minutes: Instructor: Lynn Allen
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. 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.
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!
Hold on - we have a lot of information to
cover in 80 minutes!
First and Foremost!
Don’t let Visual Lisp intimidate you!
What does LISP stand for?
LISt Processor
(not Lost In Stupid
Parentheses!)
The Basics
• Lists
• Functions
• Arguments
• Golden Rules of AutoLISP
What is a LIST?
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!!!
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
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
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?”)
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)
Quiz Time!
(* 4 (/ (+ 6 3) 3))
12
(+ (* (- 5 2) (/ 15 3)) 6)
21
(/ (* (- 11 9) (+ 25 5)) (* 3 2))
10
Some popular Data Types:
• Integers do not!
Example: 25 11
Examples: “autodesk”
“line”
“1.25”
Setting Variables
(SETQ)
(SETQ X 1)
SETQ is the function
X is the variable name
1 is the value
• Alpha-numeric
• May not contain spaces
• should not replace existing preset values
such as T or pi
(setq X 1 Y 2)
Command: !X
returns 1
Command: circle
3P/2P/TTR/<Center point>:
Diameter/<Radius>:!Y
Ways to ruin your Visual Lisp life
(setq + -)
(setq * /)
(setq pi 2.5)
(initdia)
(command “layer”)
pause allow for user input
(command) cancel
“” enter
(Command “ZOOM” “A”)
(Command)
Creating your own AutoCAD
Commands
(DEFUN)
DEFUN binds a set of expressions to a
variable.
(DEFUN C:ZAP ( )
Command: zap
Anatomy of DEFUN
• 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!)
DEFUN examples
(DEFUN C:ZA ( )
(Command “ZOOM” “A”)
)
(DEFUN C:SQ ( )
(Command “POLYGON” 4 “E” pause pause)
)
(DEFUN C:ZAP ( )
(Command “erase” “all” ““)
)
(defun c:ls ( ) SHORT.LSP
(command “layer” “M” pause ““)
)
(defun c:ZO ( )
(command “ZOOM” “O”)
)
(defun c:ttr ( )
(command “circle” “ttr” pause pause pause)
)
(defun c:Jellydonut ( )
(command “donut” “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 with this picture?
(defun c:door
(“insert” “door” pause 1 1 45)
)
(defun c:fun ())
(prompt “are we having fun yet?)
)
PPurge.LSP
(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 your ACT!
(Defun c:ppurge ( )
(command “purge” “all” “*” “N”)
(princ)
)
Just for fun!
ALERT
ALERT sends an ALERT box to the screen
with the indicated text
Example:
(ALERT “Formatting the hard drive”)
ACAD.LSP or ACADDOC.LSP
Automatic Visual Lisp Loading
(defun c:ZA ( )
(command “Zoom” “All”)
(princ))
(defun c:DT ( )
(setvar “clayer” “TEXT”)
(command “Dtext”)
(princ))
(defun c:bolt ( )
(command “insert” “bolt” pause pause pause)
(princ))
Automatic loading LISP files
ACAD.LSP 2
ACADDOC.LSP 4
ACAD.MNL 5
-------------
ACAD200X.LSP 1
ACAD200XDOC.LSP 3
Undefine and Redefine
(defun C:LINE ( )
(prompt “Shouldn’t you be using Polylines?”)
(command “PLINE”))
(defun S::STARTUP ( )
(command “undefine” “line”)
)
S::STARTUP
(setvar “angbase” “180”)
OR
(setvar “snapang” 0.000001)
OR
(command “vpoint” “0,0,-1”)
(command “ucsicon” “off”)
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 ( )
Review
• LISP stands for. . . • undefine
• List • Acad.lsp
• Function • s::startup
• Command
• string
• pause
• “”
• DEFUN
• (princ)
• (d)
Lynn Allen
lynn.allen@autodesk.com
Lynn’s Blog
www.autodesk.com/blog