mastering autolisp in 80 minutes

59
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 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.

Upload: albrodz

Post on 13-Apr-2015

153 views

Category:

Documents


42 download

TRANSCRIPT

Page 1: Mastering AutoLISP in 80 Minutes

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 rumorsgreen 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. p g g g g p g gThe class is designed for intermediate-level AutoCAD users who have never programmed in AutoLISP before.

Page 2: Mastering AutoLISP in 80 Minutes

You have come to the right place ifYou have come to the right place if...

• You know nothing or very little about Visual LispY t t it Vi l Li ti b t• You want to write your own Visual Lisp routines but 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

Page 3: Mastering AutoLISP in 80 Minutes

Objectives

• To lay a firm foundation of the basics of Visual LiLisp.

• Prepare you to write your own Visual Lisp tiroutines

• Start you down the path to official AutoCAD G d ( “N d ”)Gurudom ( or “Nerdom”)

• Teach you some quick and dirty basics of Visual Li (d ’t l k t l !)Lisp (don’t look too close!).

• Discover new ways to torture your coworkers!

Page 4: Mastering AutoLISP in 80 Minutes

Hold on - we have a lot of information to cover in 80 minutes!cover in 80 minutes!

Page 5: Mastering AutoLISP in 80 Minutes

First and Foremost!Don’t let Visual Lisp intimidate you!Don t let Visual Lisp intimidate you!

Page 6: Mastering AutoLISP in 80 Minutes

What does LISP stand for?What does LISP stand for?

LISt ProcessorLISt Processor(not Lost In Stupid(not Lost In Stupid

Parentheses!)

Page 7: Mastering AutoLISP in 80 Minutes

The BasicsThe Basics

• Lists• Functions• Functions• Arguments• Golden Rules of AutoLISP

Page 8: Mastering AutoLISP in 80 Minutes

Wh t i LIST?What is a LIST?

Anything inside of parentheses

Examples of LISTS:Examples of LISTS:(a b c)(setq x 1)(princ)(princ)

Page 9: Mastering AutoLISP in 80 Minutes

What is a FUNCTION?What is a FUNCTION?(or subr)

The ACTION you want Visual Lisp to do!

Page 10: Mastering AutoLISP in 80 Minutes

In Visual Lisp the function ALWAYS go first!!!go first!!!

Visual Lisp uses Prefix notation

Example: (+ 1 2)(- 5 3)(inters A B C D)(setq x 3)

Page 11: Mastering AutoLISP in 80 Minutes

Visual Lisp as a CalculatorVisual Lisp as a CalculatorINFIX Notation(1 + 1)(3 * 4)(3 * 4)(6 / 2)(6 / )PREFIX Notation(+ 1 1)(* 3 4)( 3 4)(/ 6 2)

Page 12: Mastering AutoLISP in 80 Minutes

ArgumentsArguments• Arguments are the values you pass to a

f tifunction(+ 5 6)+ is the function5 and 6 are the argumentsg

(setq x “Autodesk”)(setq x Autodesk )Setq is the functionX and “Autodesk” are theX and Autodesk are the arguments

Page 13: Mastering AutoLISP in 80 Minutes

The Golden Rules of Visual Lisp

• For every open paren, you must have a l dclosed parenExample: (setq x (+ a b))p ( q ( ))

• For every open double quote, you must have a closed double quotehave a closed double quote.Example: (prompt “How are you?”)

Page 14: Mastering AutoLISP in 80 Minutes

The Key to unlocking complicated LISP tiLISP routines:

Visual Lisp works from the Inside Outp(+ 5 (* 4 3))is equal to (4 * 3) + 5( 3) 5(- (+ 5 2) (* 6 (- 7 6)))i l tis equal to(5 + 2) - (6 * (7 - 6))( ) ( ( ))7 - (6 * 1)

Page 15: Mastering AutoLISP in 80 Minutes

Quiz Time!Q

(* 4 (/ (+ 6 3) 3))(* 4 (/ (+ 6 3) 3))1212(+ (* (- 5 2) (/ 15 3)) 6)21(/ (* ( 11 9) ( 25 5)) (* 3 2))(/ (* (- 11 9) (+ 25 5)) (* 3 2))1010

Page 16: Mastering AutoLISP in 80 Minutes

Some popular Data Types:p p yp

• Real Numbers 1.5• Integers 5• Integers 5• Strings “LINE”• Lists (8 . “DIM”)• Subrs (or functions) SETQ• Subrs (or functions) SETQ

Page 17: Mastering AutoLISP in 80 Minutes

Real Numbers and Integers

• Real Numbers have decimal pointsExample: 1.3 5.0

• Integers do not!Example: 25 11

• Real Numbers must have a leading zero.5 is incorrect 0 5 is correct.5 is incorrect 0.5 is correct

D tt d i (0 “CIRCLE”)Dotted pair: (0 . “CIRCLE”)error: misplaced dot on input

Page 18: Mastering AutoLISP in 80 Minutes

(/ 7 2) => 3(/ 7 2) => 3(/ 7 2.0) => 3.5(+ 1 2 3 4 5 6. ) => 21.0(+ 1 5) => invalid dotted pair(+ 1 .5) => invalid dotted pair(+ 1 0.5) => 1.5

One real number changes the entire pot!

Page 19: Mastering AutoLISP in 80 Minutes

Basic Arithmetic Functions (for you math-heads):

+ = addition * = multiplication/ division subtraction/ = division - = subtraction

(sqrt x) (sin ang) (atan x)( t ) ( )(expt x y) (cos ang)(abs x) (log x)( ) ( g )(float x) (fix x)

Page 20: Mastering AutoLISP in 80 Minutes

btwbtw...

Angles are measured in radians!(not degrees)

and you’ll need to remember thatand you’ll need to remember that.

Page 21: Mastering AutoLISP in 80 Minutes

St iStringsUsually Text (literals)Usually Text (literals)Always double-quotedAlways double quotedSpaces accepted

E l “ t d k”Examples: “autodesk”“line”line“1.25”

Page 22: Mastering AutoLISP in 80 Minutes

Setting VariablesSetting Variables(SETQ)

(SETQ X 1)(SETQ X 1)SETQ is the function

X is the variable name1 is the value1 is the value

Setting several variables at once:Setting several variables at once:(SETQ A 1 B 2 C 3)

Page 23: Mastering AutoLISP in 80 Minutes

Variable Names

• Alpha-numeric• May not contain spaces• May not contain spaces• should not replace existing preset values

h T isuch as T or pi

Note: A variable that hasn’t been set isNote: A variable that hasn t been set is equal to nil

Page 24: Mastering AutoLISP in 80 Minutes

Using Visual Lisp variables in AutoCADAutoCAD

(setq X 1 Y 2)

Command: !Xreturns 1C d i lCommand: circle3P/2P/TTR/<Center point>:3P/2P/TTR/ Center point :Diameter/<Radius>:!Y

Page 25: Mastering AutoLISP in 80 Minutes

Ways to ruin your Visual Lisp life

(setq + -)( q )(setq * /)( i )(setq pi 2.5)

Visual Lisp will let you abuseVisual Lisp will let you abuse yourself. . .

Page 26: Mastering AutoLISP in 80 Minutes

Using AutoCAD commandsUsing AutoCAD commands in Visual Lisp (the good stuff!)

Using the COMMAND function, you can th A t CAD daccess the AutoCAD commands

Example:a p e(command “QSAVE”)(command “TRIM”)(command “ZOOM” “P”)(command ZOOM P )(command “LAYER”)

Page 27: Mastering AutoLISP in 80 Minutes

By default, Visual Lisp doesn’t display dialog boxesdisplay dialog boxes

Vi l Li di l th d li i t fVisual Lisp displays the command line interface for commands.

To force the dialog box use:(initdia)

Before the command:

(initdia)(initdia)(command “layer”)

Page 28: Mastering AutoLISP in 80 Minutes

ll f i tpause allow for user input

(command) cancel

“” enter

Page 29: Mastering AutoLISP in 80 Minutes

(Command “ZOOM” “A”)

(C d “ERASE” “L” ““)(Command “ERASE” “L” ““)

(Command “INSERT” “DESK” pause 1 1 pause)(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)

Page 30: Mastering AutoLISP in 80 Minutes

Creating your own AutoCADCreating your own AutoCAD Commands

(DEFUN)DEFUN binds a set of expressions to aDEFUN binds a set of expressions to a

variable.(DEFUN C ZAP ( )(DEFUN C:ZAP ( )

dCommand: zap

Page 31: Mastering AutoLISP in 80 Minutes

Anatomy of DEFUN• DEFUN

i th f ti

ato y o U

is the function• C:C:

indicates the function will be an AutoCAD commandAutoCAD command

• ( )( )indicates no local variables and no arguments (we’ll get to that anotherarguments (we ll get to that another

time!)

Page 32: Mastering AutoLISP in 80 Minutes

DEFUN examplesp

(DEFUN C ZA ( )(DEFUN C:ZA ( )(Command “ZOOM” “A”)

))(DEFUN C:SQ ( )

( d “ ” “ ” )(Command “POLYGON” 4 “E” pause pause))(DEFUN C:ZAP ( )

(Command “erase” “all” ““))

Page 33: Mastering AutoLISP in 80 Minutes

SHORT.LSP(defun c:ls ( )(command “la e ” “M” pa se ““)(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 )(command donut 0 pause ))

Page 34: Mastering AutoLISP in 80 Minutes

Loading Visual Lisp routinesLoading Visual Lisp routines • APPLOAD - used to load one or more VisualAPPLOAD used to load one or more Visual

Lisp routines

• (load “short”)( )

Page 35: Mastering AutoLISP in 80 Minutes

Opening a dialog to a specific tabOpening a dialog to a specific tab

(command “+dialogname” X)

(command “+options” 7)will open the Options dialog to tab #8

(command “+customize” 0)

Page 36: Mastering AutoLISP in 80 Minutes

What’s wrong with this picture?g p

(defun c:door (“insert” “door” pause 1 1 45)( insert door pause 1 1 45)

)(defun c:fun ())

(prompt “are we having fun yet?)(prompt are we having fun yet?))

Page 37: Mastering AutoLISP in 80 Minutes

PPurge.LSPg

(Defun c:ppurge ( )(Defun c:ppurge ( )(command “purge” “all” “*” “N”)

)

Page 38: Mastering AutoLISP in 80 Minutes

Let’s create a command that breaks an object in the samebreaks an object in the same

spot twice

(defun c:crack ()

Page 39: Mastering AutoLISP in 80 Minutes

Clean up your ACT!p y

• PRINC (get rid of the nils!)

Page 40: Mastering AutoLISP in 80 Minutes

PPurge.LSPg

(Defun c:ppurge ( )(Defun c:ppurge ( )(command “purge” “all” “*” “N”)(princ)

))

Page 41: Mastering AutoLISP in 80 Minutes

Blind the user!Blind the user!(he doesn’t really want to know

h t’ i )what’s going on. ..)

(SETVAR “CMDECHO” 0)but don’t forget to turn it back on!but don t forget to turn it back on!

(SETVAR “CMDECHO” 1)

Page 42: Mastering AutoLISP in 80 Minutes

PPurge.LSPg

(Defun c:ppurge ( )(Defun c:ppurge ( )(setvar “cmdecho” 0)(command “purge” “all” “*” “N”)(command purge all * N )(setvar “cmdecho” 1)(princ)

))

Page 43: Mastering AutoLISP in 80 Minutes

Talk to your user with PROMPTTalk to your user with PROMPT

(defun c:clean ( )(defun c:clean ( )(setvar “cmdecho” 0)(prompt “this command will pick up all of your(prompt this command will pick up all of your objects and clean the drawing file…please wait”)(command “erase” “all” “”)(command erase all )(prompt “\nthere..that’s better”)( t “ d h ” 1)(setvar “cmdecho” 1)(princ)

)

Page 44: Mastering AutoLISP in 80 Minutes

SSGET-Getting a selection setSSGET 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

Page 45: Mastering AutoLISP in 80 Minutes

Using SSGET

(defun c:cm ( )(setq ss1 (ssget))(setq ss1 (ssget))(command “copy” ss1 “” “m”)

)

Page 46: Mastering AutoLISP in 80 Minutes

Let’s create a command that does aLet s create a command that does a full circle, rotated objects Polar Array

(defun c:polar ()

Page 47: Mastering AutoLISP in 80 Minutes

Just for fun!ALERTALERT

ALERT sends an ALERT box to the screenALERT sends an ALERT box to the screen with the indicated text

Example: (ALERT “Formatting the hard drive”)(ALERT Formatting the hard drive )

Page 48: Mastering AutoLISP in 80 Minutes

ACAD LSP or ACADDOC LSPACAD.LSP or ACADDOC.LSPAutomatic Visual Lisp Loading

Put frequently used Visual Lisp routines• Put frequently used Visual Lisp routines.• Undefine those AutoCAD commands you y

want to automatically replace with Visual Lisp routinesLisp routines.

• Place partial menu loading instructions

Page 49: Mastering AutoLISP in 80 Minutes

ACAD.LSP

(defun c:ZA ( )(command “Zoom” “All”)(princ))

(defun c:DT ( )(setvar “clayer” “TEXT”)( y )(command “Dtext”)(princ))(princ))

(defun c:bolt ( )(command “insert” “bolt” pause pause pause)(command insert bolt pause pause pause)(princ))

Page 50: Mastering AutoLISP in 80 Minutes

Automatic loading LISP filesAutomatic loading LISP files

ACAD.LSP 2ACADDOC LSP 4ACADDOC.LSP 4ACAD.MNL 5-------------ACAD200X LSP 1ACAD200X.LSP 1ACAD200XDOC.LSP 3

Page 51: Mastering AutoLISP in 80 Minutes

Undefine and Redefine

Permits undefining and redefining the internal AutoCAD commandsinternal AutoCAD commands

Note: AutoCAD commands can always be executed with a leading periodexecuted with a leading period.

Page 52: Mastering AutoLISP in 80 Minutes

S::STARTUPi l ti f ACAD LSPa special section of ACAD.LSP

(defun C:LINE ( )(defun C:LINE ( )(prompt “Shouldn’t you be using Polylines?”)(command “PLINE”))(command PLINE ))

(defun S::STARTUP ( )(command “undefine” “line”)

))

Note: s::startup is the last file to be loaded beforeNote: s::startup is the last file to be loaded before control is handed over to the user.

Page 53: Mastering AutoLISP in 80 Minutes

Ways to torture your coworkers:

ACAD.LSP(defun c:qsave ( )(defun c:qsave ( )

(command “undo” “b” “y”)(command “ qsave” “ qsave”)(command .qsave .qsave )

(defun s::startup ()(command “undefine” “save”)(command undefine save )(command “undefine” “qsave”)(command “undefine” “saveas”)(command undefine saveas )

)

Page 54: Mastering AutoLISP in 80 Minutes

one more means of torture:

(defun c:zoom ( )(command “erase” “L” ““)(command “.zoom”)(princ)

)(defun c:redo ( )

(prompt “You goofed - deal with it!”)(p p g ))(defun c:undo ( )(d u u do ( )

(alert “Get it right the first time!”)

Page 55: Mastering AutoLISP in 80 Minutes

(d f ()(defun c:regen ()(setvar “cmdecho” 0)(command “donut” 0 300000000000 “10,10”)(command “regen”)( g )(command “cmdecho” 1)

))(defun s::startup ( )

(command “undefine” “zoom”)(command undefine zoom )(command “undefine” “undo”)( d “ d fi ” “ d ”)(command “undefine” “redo”)(command “undefine” “regen”)

)

Page 56: Mastering AutoLISP in 80 Minutes

Other evil things to put in s::startup!s::startup!

S::STARTUP(setvar “angbase” “180”)(setvar angbase 180 )OR(setvar “snapang” 0.000001)OROR(command “vpoint” “0,0,-1”)(command “ucsicon” “off”)

Page 57: Mastering AutoLISP in 80 Minutes

What’s wrong with this picture?g p

(1 + 1)(1 + 1)(* 5 .25)(/ 9 2)(/ 9 2)(setq x (+ 1 2)(d f d d(defun d:dimwit (command “text” .5 90 pause)(alert “hit cancel to exit dialog box”)(defun s:startup ( )( p ( )

Page 58: Mastering AutoLISP in 80 Minutes

Review

• LISP stands for. . .• List

• undefine• Acad lsp• List

• Function• Command

• Acad.lsp• s::startup• stringCo a d

• pause• “”

• string

• DEFUN• (princ)

(d)• (d)

Page 59: Mastering AutoLISP in 80 Minutes

Lynn Allenl nn allen@a todesk [email protected]

Lynn’s Blogwww.autodesk.com/blog