Вы находитесь на странице: 1из 36

AutoLISP for Everyone!

Robert Green
rgreen@CAD-Manager.com
FROM THE TRAILHEAD TO YOUR CAREER SUMMIT.
My background My background
ME degree from Georgia Tech ME degree from Georgia Tech
AutoCAD user since 1985
AutoLISP programmer since 1990 AutoLISP programmer since 1990
Self confessed computer geek
F Atl t GA From Atlanta GA so
FROM THE TRAILHEAD TO YOUR CAREER SUMMIT.
Lets learn some
LISP yall! LISP y all!
(This stuff is fun)
FROM THE TRAILHEAD TO YOUR CAREER SUMMIT.
Some fundamentals
LISP LISt Processor
Everything is in lists Everything is in lists
FROM THE TRAILHEAD TO YOUR CAREER SUMMIT.
Some history Some history
LISP is an old language LISP is an old language
It was open source (cheap)
It made sense for AutoCAD because AutoCAD It made sense for AutoCAD because AutoCAD
entitles are lists
It runs without compiling (simple) It runs without compiling (simple)
It made sense at the time (cheap)
FROM THE TRAILHEAD TO YOUR CAREER SUMMIT.
Some advice Some advice
AutoLISP is still very valid! AutoLISP is still very valid!
A HUGE base of LISP code exists
LISP shareware is abundant LISP shareware is abundant
LISP does many things very easily as
compared to VB/VBA/.NET compared to VB/VBA/.NET
LISP is weak on dialog controls
LISP is weak on graphical interfaces LISP is weak on graphical interfaces
View it as an 80/20 language
FROM THE TRAILHEAD TO YOUR CAREER SUMMIT.
Key files and why they matter
LSP
Load in order Load in order
What to mess with
What not to! What not to!
FROM THE TRAILHEAD TO YOUR CAREER SUMMIT.
All those wacky file names All those wacky file names
ACAD20XX.LSP (XX is version) ACAD20XX.LSP (XX is version)
ACAD.LSP
ACAD20XXDOC LSP (XX is version) ACAD20XXDOC.LSP (XX is version)
ACADDOC.LSP
MENUNAME MNL MENUNAME.MNL
Help
FROM THE TRAILHEAD TO YOUR CAREER SUMMIT.
What do they do What do they do
They load on startup of AutoCAD They load on startup of AutoCAD
They load in a certain order (listed on previous
slide)
Some have code in them and some dont
(ACADDOC.LSP and ACAD.LSP dont as an
l ) example)
They reside in the SUPPORT folder
FROM THE TRAILHEAD TO YOUR CAREER SUMMIT.
So what should I do So what should I do
Use ACADDOC.LSP to get started Use ACADDOC.LSP to get started
You create your own ACADDOC.LSP so you
cant really mess it up
It loads with every new drawing
Put in in the SUPPORT folder and start hacking Put in in the SUPPORT folder and start hacking
away
If you mess up too bad, just delete!
FROM THE TRAILHEAD TO YOUR CAREER SUMMIT.
Syntax Basics
Lists and Arguments
Rules of AutoLISP Rules of AutoLISP
Variables
Functions Functions
Accessing the
command line command line
Special characters
User input User input
FROM THE TRAILHEAD TO YOUR CAREER SUMMIT.
Lists and Arguments Lists and Arguments
(+ 20 30) ( 20 30)
o Here the + is a FUNCTION and the two numbers
are ARGUMENTS
(command line 0,0 1,1 )
o Here COMMAND is the function, all others are
ARGUMENTS
FROM THE TRAILHEAD TO YOUR CAREER SUMMIT.
Some Rules
For every ( there is an equal and opposite ) Newton
Like this: (setq A 3.0)
N lik hi ( A 3 0)) ( A 3 0 Not like this: (setq A 3.0)) or (setq A 3.0
Same goes for quote marks! g q
Like this: (setq name Robert Green)
Not like this: (setq name Robert Green)
(setq name Robert Green) (setq name Robert Green)
When formatting numbers always avoid invalid dotted
i pairs
Like this: (setq A 0.5)
Not like this: (setq A .5)
FROM THE TRAILHEAD TO YOUR CAREER SUMMIT.
Function Lists Embedded Lists
Standard AutoLISP Result
8 + 4 (+ 8 4) 12 8 + 4 (+ 8 4) 12
8 + 2.3 (+ 8 2.3) 10.3
5 0 4 0 (/ 5 0 4 0) 1 25 5.0 4.0 (/ 5.0 4.0) 1.25
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
FROM THE TRAILHEAD TO YOUR CAREER SUMMIT.
The Command Line The Command Line
How would you work normally?
Draw a line between two user points Draw a line between two user points
o Type in LINE to start the line command
o Click POINT for first point location o Click POINT for first point location
o Click POINT for second point location
o Type in ENTER to halt the command o Type in ENTER to halt the command
In AutoLISP: (command line pause pause ) In AutoLISP: (command line pause pause )
PAUSE waits for the user
Th i l t hitti th ENTER k
FROM THE TRAILHEAD TO YOUR CAREER SUMMIT.
The is equal to hitting the ENTER key
Whats Going On Here?
(command viewres y 5000)
(command -color BYLAYER) (command color BYLAYER )
(command -linetype set BYLAYER )
(command menu menuname mnc) (command menu menuname.mnc)
(command viewres y pause)
Thats not so bad intuitive actually
FROM THE TRAILHEAD TO YOUR CAREER SUMMIT.
Variables and Data Types
Real
Integer Integer
String
U i t User input
FROM THE TRAILHEAD TO YOUR CAREER SUMMIT.
Variables and data types
SETQ (SET eQual)
(setq VALUE 12 7) real number (setq VALUE 12.7) real number
(setq VALUE 1) integer number
(setq VALUE Happy) string variable (setq VALUE Happy) string variable
(setq VALUE 1.0) - ?
( S ) ? (setq VIEWRES_VALUE 5000) - ?
FROM THE TRAILHEAD TO YOUR CAREER SUMMIT.
Variables and Commands
(setq VIEWRES_VALUE 5000)
o (command viewres y viewres value) o (command viewres y viewres_value)
o What would happen in this case?
(setq VIEWRES VALUE Happy Bday!) (setq VIEWRES_VALUE Happy Bday! )
o (command viewres y viewres_value)
o What would happen in this case? o What would happen in this case?
Variable TYPES and how they interact with
commands are key commands are key
FROM THE TRAILHEAD TO YOUR CAREER SUMMIT.
User functions
Speed for the user
Lower support for you Lower support for you
A win-win scenario
L t t thi Lets put everything
weve learned into action
to build some functions. to bu d so e u ct o s
FROM THE TRAILHEAD TO YOUR CAREER SUMMIT.
User Function Examples
(defun C:ZA () (defun C:ZA ()
(command .zoom a)
(princ) (princ)
)
FROM THE TRAILHEAD TO YOUR CAREER SUMMIT.
User Function Examples
(defun C:VR ()
(command viewres y 5000) (co a d e es y 5000 )
)
(defun C:BL ()
(command -color BYLAYER) (command -color BYLAYER )
(command -linetype set BYLAYER )
(princ) (princ)
)
FROM THE TRAILHEAD TO YOUR CAREER SUMMIT.
Fillet Zero Function
Fillet Zero
(defun c:fz () (defun c:fz ()
(setvar filletrad 0.0)
(command fillet pause pause) (command .fillet pause pause)
(princ)
))
FROM THE TRAILHEAD TO YOUR CAREER SUMMIT.
Improved Fillet Zero
(defun c:fz ()
(setq old filletrad (getvar filletrad)) (setq old_filletrad (getvar filletrad ))
(setvar filletrad 0.0)
(command fillet pause pause) (command .fillet pause pause)
(setvar filletrad old_filletrad)
( i ) (princ)
)
FROM THE TRAILHEAD TO YOUR CAREER SUMMIT.
Auto Purge Function
Auto Purge
(defun c:atp () (defun c:atp ()
(command -purge a * n .qsave)
(princ) (princ)
)
FROM THE TRAILHEAD TO YOUR CAREER SUMMIT.
More Bits and Bytes
Undefine
Dot form
Redefine Redefine
Alerts
CMDECHO CMDECHO
FROM THE TRAILHEAD TO YOUR CAREER SUMMIT.
Undefining
(command .undefine LINE)
(command .undefine TORUS) (command .undefine TORUS )
Dont want them messing with a command? Just
undefine it
Now you can SUBTRACT from the y
AutoCAD Command set in your
ACADDOC.LSP file.
FROM THE TRAILHEAD TO YOUR CAREER SUMMIT.
The DOT form
Invoke commands like this: .LINE
Note the dot . character? Note the dot . character?
This allows you to invoke a command whether it has
been undefined or not!
This is our little secret right ...
FROM THE TRAILHEAD TO YOUR CAREER SUMMIT.
Redefining
(command .redefine LINE)
(command .redefine TORUS) (command .redefine TORUS )
Want to be sure that a command is active?
Just redefine it Just redefine it
Now you can UNSUBTRACT from the y
AutoCAD Command set with ease.
FROM THE TRAILHEAD TO YOUR CAREER SUMMIT.
Undefining revisted
What if your users find out about REDEFINE and start
REDEFINING your UNDEFINES?
Just undefine the redefine like this:
(command .undefine REDEFINE) ( )
Thatll show em
FROM THE TRAILHEAD TO YOUR CAREER SUMMIT.
Redefining
You can undefine a command and redefine it like this:
(command .undefine TORUS) (command .undefine TORUS )
(defun C:TORUS ()
(alert Dont use that command!) (alert Don t use that command! )
(princ)
))
Now you do whatever you want!
FROM THE TRAILHEAD TO YOUR CAREER SUMMIT.
What Does This Do?
(command .undefine QSAVE)
(defun c:qsave () (defun c:qsave ()
(command -purge b * n)
(command qsave) (command .qsave )
(princ)
))
FROM THE TRAILHEAD TO YOUR CAREER SUMMIT.
Alerting the user
You can send a message to the user like this:
(alert Message goes here)
FROM THE TRAILHEAD TO YOUR CAREER SUMMIT.
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)
))
FROM THE TRAILHEAD TO YOUR CAREER SUMMIT.
Wrapping up: Resources
CADDEPOT.com
CAD-Manager com CAD-Manager.com
Cadalyst Magazine
Th G d P th The Garden Path
Developers Help
FROM THE TRAILHEAD TO YOUR CAREER SUMMIT.
Thank you for attending! y g
Any questions?
Email me: rgreen@CAD-Manager.com
Be sure to check out the video segments Ill post at my
web site: web site:
www.cad-manager.com/cui www.cad manager.com/cui
FROM THE TRAILHEAD TO YOUR CAREER SUMMIT.

Вам также может понравиться