autolisp in 90 minutes or less! - autodeskdownload.autodesk.com/us/lynnallen/seattl… · ppt...

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

Upload: doantuong

Post on 02-May-2018

240 views

Category:

Documents


7 download

TRANSCRIPT

Page 1: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

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.

Page 2: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

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!

Page 3: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

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

Page 4: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

First and Foremost! Don’t let Visual Lisp intimidate

you!

Page 5: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

What does LISP stand for?LISt Processor(not Lost In Stupid

Parentheses!)

Page 6: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

The Basics

• Lists• Functions• Arguments• Golden Rules of AutoLISP

Page 7: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

What is a LIST?

Anything inside of parentheses

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

Page 8: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

What is a FUNCTION?(or subr)

The ACTION you want Visual Lisp to do!

Page 9: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

In Visual Lisp the function ALWAYS go first!!!

Visual Lisp uses Prefix notation

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

Page 10: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

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

Page 11: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

Arguments• Arguments are the values you pass to a

function(+ 5 6)+ is the function5 and 6 are the arguments

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

Page 12: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

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?”)

Page 13: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

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)

Page 14: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

Quiz Time!

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

Page 15: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

Some popular Data Types:

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

Page 16: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

Real Numbers and Integers• Real Numbers have decimal points

Example: 1.35.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

Page 17: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

(/ 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!

Page 18: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

Basic Arithmetic Functions (for you math-heads):

+ = addition * = multiplication

/ = division - = subtraction

(sqrt x) (sin ang) (atan x)

(expt x y) (cos ang)(abs x) (log x)(float x) (fix x)

Page 19: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

btw...

Angles are measured in radians!

(not degrees)and you’ll need to remember

that.

Page 20: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

StringsUsually Text (literals)Always double-quotedSpaces accepted

Examples: “autodesk” “line” “1.25”

Page 21: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

Setting Variables(SETQ)

(SETQ X 1)SETQ is the function

X is the variable name1 is the value

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

Page 22: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

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

Page 23: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

Using Visual Lisp variables in AutoCAD

(setq X 1 Y 2)

Command: !Xreturns 1Command: circle3P/2P/TTR/<Center point>:Diameter/<Radius>:!Y

Page 24: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

Ways to ruin your Visual Lisp life

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

Visual Lisp will let you abuse yourself. . .

Page 25: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

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”)

Page 26: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

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”)

Page 27: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

pause allow for user input

(command) cancel

“” enter

Page 28: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

(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)

Page 29: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

Creating your own AutoCAD Commands

(DEFUN)DEFUN binds a set of expressions to a

variable.(DEFUN C:ZAP ( )

Command: zap

Page 30: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

• 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

Page 31: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

DEFUN examples

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

)(DEFUN C:SQ ( )

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

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

Page 32: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

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 ))

Page 33: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

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

Visual Lisp routines

• (load “short”)

Page 34: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

Opening a dialog to a specific tab

(command “+dialogname” X)

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

(command “+customize” 0)

Page 35: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

What’s wrong with this picture?

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

)(defun c:fun ())

(prompt “are we having fun yet?))

Page 36: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

PPurge.LSP

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

)

Page 37: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

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

same spot twice(defun c:crack ()

Page 38: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

Clean up your ACT!

• PRINC (get rid of the nils!)

Page 39: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

PPurge.LSP

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

)

Page 40: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

Just for fun!ALERT

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

Example: (ALERT “Formatting the hard drive”)

Page 41: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

ACAD.LSP or ACADDOC.LSPAutomatic 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

Page 42: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

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))

Page 43: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

Automatic loading LISP files

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

Page 44: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

Undefine and Redefine

Permits undefining and redefining the internal AutoCAD commands

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

Page 45: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

S::STARTUPa 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.

Page 46: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

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”)

)

Page 47: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

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!”)

Page 48: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

(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”)

)

Page 49: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

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”)

Page 50: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

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 ( )

Page 51: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

Review • LISP stands for. . .• List• Function• Command• pause• “”• DEFUN• (princ)• (d)

• undefine• Acad.lsp• s::startup• string

Page 52: AutoLISP in 90 minutes or less! - Autodeskdownload.autodesk.com/us/lynnallen/Seattl… · PPT file · Web view · 2006-03-20Instructor: Lynn Allen Course Summary: AutoLISP has been

Lynn [email protected]

Lynn’s Blogwww.autodesk.com/blog