0% found this document useful (0 votes)
1K views59 pages

Mastering AutoLISP in 80 Minutes

This 80-minute course provides an introduction to AutoLISP, a programming language used to customize and create new commands in AutoCAD. The course assumes intermediate AutoCAD skills and no prior programming experience. It covers basic AutoLISP concepts like lists, functions, arguments, and variables, as well as how to write simple programs to automate tasks and create new commands. The instructor aims to demystify AutoLISP and get students comfortable with core concepts in a short time through examples, exercises, and humor.

Uploaded by

albrodz
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views59 pages

Mastering AutoLISP in 80 Minutes

This 80-minute course provides an introduction to AutoLISP, a programming language used to customize and create new commands in AutoCAD. The course assumes intermediate AutoCAD skills and no prior programming experience. It covers basic AutoLISP concepts like lists, functions, arguments, and variables, as well as how to write simple programs to automate tasks and create new commands. The instructor aims to demystify AutoLISP and get students comfortable with core concepts in a short time through examples, exercises, and humor.

Uploaded by

albrodz
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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 gurus. 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 g p p g g g graphical language. g g The class is designed for intermediate-level AutoCAD users who have never programmed in AutoLISP before.

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


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

Objectives
To lay a firm foundation of the basics of Visual Lisp. Li Prepare you to write your own Visual Lisp routines ti Start you down the path to official AutoCAD Gurudom ( or N d ) G d Nerdom) Teach you some quick and dirty basics of Visual Lisp (d t l k t Li (dont look too close!). l !) Discover new ways to torture your coworkers!

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

First and Foremost! Dont 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 Wh t i 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!

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)

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 f ti (+ 5 6) + is the function g 5 and 6 are the arguments (setq x Autodesk) Autodesk ) Setq is the function X and Autodesk are the Autodesk arguments

The Golden Rules of Visual Lisp


For every open paren, you must have a closed paren l d Example: (setq x (+ a b)) p ( q ( )) For every open double quote, you must have a closed double quote. quote Example: (prompt How are you?)

The Key to unlocking complicated LISP routines: ti


Visual Lisp works from the Inside Out p
(+ 5 (* 4 3))

is equal to
( (4 * 3) + 5 (- (+ 5 2) (* 6 (- 7 6)))

is i equal to lt
( (5 + 2) - (6 * (7 - 6)) ) ( ( )) 7 - (6 * 1)

Q Quiz Time!

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

Some popular Data Types: p p yp


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

Real Numbers and Integers


Real Numbers have decimal points Example: 1.3 5.0 Integers do not! Example: 25 11

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

Dotted i D tt d pair: (0 . CIRCLE)


error: misplaced dot on input

(/ (/ (+ (+ (+

7 7 1 1 1

2) => 3 2.0) => 3.5 2 3 4 5 6. ) => 21.0 .5) 5) => invalid dotted pair 0.5) => 1.5

One real number changes the entire pot!

Basic Arithmetic Functions (for you math-heads):


+ = addition / = division (sqrt x) (expt x y) ( t ) ( (abs x) ) (float x) * = multiplication - = subtraction (sin ang) (cos ang) ( ) ( g ) (log x) (fix x) (atan x)

btw... btw

Angles are measured in radians! (not degrees) and youll need to remember that. that

Strings St i
Usually Text (literals) Always double quoted double-quoted Spaces accepted

Examples: E l

autodesk t d k line 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 h i

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

Using Visual Lisp variables in AutoCAD


(setq X 1 Y 2) Command: !X returns 1 Command: circle C d i l
3P/2P/TTR/ Center point : 3P/2P/TTR/<Center point>: Diameter/<Radius>:!Y

Ways to ruin your Visual Lisp life

( (setq + -) q ) (setq * /) (setq pi 2.5) ( i ) Visual Lisp will let you abuse yourself. . .

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


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

Example: a pe
(command (command (command (command QSAVE) TRIM) ZOOM P) ZOOM P ) LAYER)

By default, Visual Lisp doesnt display dialog boxes


Visual Lisp displays the command line interface Vi l Li di l th d li i t f for commands. To force the dialog box use: (initdia) Before the command: (initdia) (command layer)

pause (command)

allow for user input ll f i t cancel enter

(Command ZOOM A) (Command ERASE L ) (C d (Command INSERT DESK pause 1 1 pause) INSERT DESK ( (Command LINE A B C C) ) (Command TEXT pause .5 0 Visual Lisp) (Command LAYER S pause ) (Command)

DEFUN binds a set of expressions to a variable.


(DEFUN C ZAP ( ) C:ZAP Command: zap d

Creating your own AutoCAD Commands (DEFUN)

Anatomy o DEFUN ato y of U


DEFUN is th f i the function ti C: indicates the function will be an AutoCAD command () indicates no local variables and no arguments (well get to that another (we ll

time!)

DEFUN examples p
(DEFUN C:ZA ( ) C ZA (Command ZOOM A) ) (DEFUN C:SQ ( ) (Command ( d POLYGON 4 pause pause) E ) ) (DEFUN C:ZAP ( ) (Command erase all ) )

(defun c:ls ( ) SHORT.LSP (command layer M pa se ) la e pause ) (defun c:ZO ( ) (command ZOOM O) ) ( (defun c:ttr ( ) (command circle ttr pause pause pause) ) (defun c:Jellydonut ( ) (command donut 0 pause ) donut 0 )

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)

Whats wrong with this p g picture?


(defun c:door ( insert door (insert door pause 1 1 45) ) (defun c:fun ()) (prompt are we having fun yet?) are )

PPurge.LSP g

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

Lets create a command that breaks an object in the same spot twice
(defun c:crack ()

Clean up y p your ACT!


PRINC (get rid of the nils!)

PPurge.LSP g

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

(he doesnt really want to know whats going on. ..) h t i )

Blind the user!

(SETVAR CMDECHO 0)

but don t forget to turn it back on! dont


(SETVAR CMDECHO 1)

PPurge.LSP g

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

Talk to your user with PROMPT


(defun c:clean ( ) (setvar cmdecho 0) (prompt this command will pick up all of your this objects and clean the drawing fileplease wait) (command erase all ) erase all ) (prompt \nthere..thats better) (setvar d h 1) ( t cmdecho (princ) )

SSGET Getting SSGET-Getting a selection set

(a bit tougherhang on!)

Used to grab a group of objects. (setq ss1 (ssget)) Select objects: (ssget x) grabs all the objects in the drawing

Using SSGET
(defun c:cm ( ) (setq ss1 (ssget)) (command copy ss1 m) )

Let s Lets create a command that does a full circle, rotated objects Polar Array
(defun c:polar ()

Just for fun!

ALERT
ALERT sends an ALERT box to the screen with the indicated text Example: (ALERT Formatting the hard drive ) Formatting drive)

ACAD.LSP ACAD LSP or ACADDOC LSP ACADDOC.LSP


Automatic Visual Lisp Loading Put frequently used Visual Lisp routines routines. Undefine those AutoCAD commands you y want to automatically replace with Visual Lisp routines. routines Place partial menu loading instructions

ACAD.LSP

(defun c:ZA ( ) (command Zoom All) (princ)) (defun c:DT ( ) ( (setvar clayer TEXT) y ) (command Dtext) (princ)) (defun c:bolt ( ) (command insert bolt pause pause pause) insert bolt (princ))

Automatic loading LISP files


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

Undefine and Redefine


Permits undefining and redefining the internal AutoCAD commands

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

a special section of ACAD.LSP i l ti f ACAD LSP

S::STARTUP

(defun C:LINE ( ) (prompt Shouldnt you be using Polylines?) (command PLINE)) 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 to torture your coworkers:


ACAD.LSP (defun c:qsave ( ) (command undo b y) (command .qsave .qsave ) qsave qsave) (defun s::startup () (command undefine save) undefine save ) (command undefine qsave) (command undefine saveas) undefine saveas ) )

one more means of torture:


(defun c:zoom ( ) (command erase L ) (command .zoom) (princ) ) (defun c:redo ( ) (p (prompt You goofed - deal with it!) p g ) ) (d u (defun c:undo ( ) u do (alert Get it right the first time!)

(defun (d f c:regen () (setvar cmdecho 0) (command donut 0 300000000000 10,10) ( (command regen) g ) (command cmdecho 1) ) (defun s::startup ( ) (command undefine zoom) undefine zoom ) (command undefine undo) (command undefine redo) ( d d fi d ) (command undefine regen) )

Other evil things to put in s::startup!


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

Whats wrong with this p g picture?


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

Review
LISP stands for. . . List Function Co Command a d pause DEFUN (princ) (d)

undefine Acad.lsp Acad lsp s::startup string

Lynn Allen lynn.allen@autodesk.com l nn allen@a todesk com Lynns Blog www.autodesk.com/blog

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy