Professional Documents
Culture Documents
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, its 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 (dont look too close!).
Discover new ways to torture your
coworkers!
The Basics
Lists
Functions
Arguments
Golden Rules of AutoLISP
What is a LIST?
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!
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
Quiz Time!
(* 4 (/ (+ 6 3) 3))
12
(+ (* (- 5 2) (/ 15 3)) 6)
21
(/ (* (- 11 9) (+ 25 5)) (* 3 2))
10
Real Numbers
1.5
Integers
5
Strings
LINE
Lists
(8 . DIM)
Subrs (or functions) SETQ
Example: 25
(/ 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
* = multiplication
- = subtraction
(sqrt x)
(sin ang) (atan x)
(expt x y)
(cos ang)
(abs x)
(log x)
(float x) (fix x)
btw...
Strings
Usually Text (literals)
Always double-quoted
Spaces accepted
Examples: autodesk
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 as T or pi
(setq + -)
(setq * /)
(setq pi 2.5)
Visual Lisp will let you
abuse yourself. . .
pause
(command)
cancel
enter
(Command ZOOM A)
(Command ERASE L )
(Command INSERT DESK pause 1 1
pause)
(Command LINE A B C C)
(Command TEXT pause .5 0 Visual
Lisp)
(Command LAYER S pause )
(Command)
Anatomy of DEFUN
DEFUN
is the function
C:
indicates the
function will be an AutoCAD
command
()
indicates no
local variables and no
arguments
(well 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 )
)
SHORT.LSP
(defun c:ls ( )
(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 )
)
PPurge.LSP
PPurge.LSP
(Defun c:ppurge ( )
(command purge all * N)
)
PPurge.LSP
PPurge.LSP
(Defun c:ppurge ( )
(command purge all * N)
(princ)
)
ALERT
ALERT sends an ALERT box to the
screen with the indicated text
Example:
(ALERT Formatting the hard drive)
ACAD.LSP
ACAD.LSP or
or
ACADDOC.LSP
ACADDOC.LSP
Automatic
Automatic Visual
Visual Lisp
Lisp Loading
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
ACAD.LSP
ACAD.LSP
(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
Automatic loading
loading LISP
LISP
files
files
ACAD.LSP
2
ACADDOC.LSP
4
ACAD.MNL
5
------------ACAD200X.LSP
1
ACAD200XDOC.LSP 3
Undefine
Undefine and
and Redefine
Redefine
Permits undefining and redefining the
internal AutoCAD commands
Note: AutoCAD commands can always
be executed with a leading period.
S::STARTUP
S::STARTUP
aa special
special section
section of
of ACAD.LSP
ACAD.LSP
(defun C:LINE ( )
(prompt Shouldnt 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.
Ways
Ways to
to torture
torture your
your
coworkers:
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)
)
(defun c:regen ()
(setvar cmdecho 0)
(command donut 0 300000000000 10,10)
(command regen)
(command cmdecho 1)
)
(defun s::startup ( )
(command undefine zoom)
(command undefine undo)
(command undefine redo)
(command undefine regen)
)
Whats
Whats wrong
wrong with
with this
this
picture?
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
Review
DEFUN
(princ)
(d)
undefine
Acad.lsp
s::startup
string
Lynn Allen
lynn.allen@autodesk.com
Lynns Blog
www.autodesk.com/blog