AutoLISP in 90 minutes or less! - PowerPoint PPT Presentation

About This Presentation
Title:

AutoLISP in 90 minutes or less!

Description:

Instructor: Lynn Allen Course Summary: AutoLISP has been around for a long time and has always separated the AutoCAD green thumbs from the gurus. – PowerPoint PPT presentation

Number of Views:532
Avg rating:3.0/5.0
Slides: 53
Provided by: LynnA5
Category:

less

Transcript and Presenter's Notes

Title: AutoLISP in 90 minutes or less!


1
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, 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.

2
You have come to the right place if...
  • You know nothing or very little about Visual
    Lisp
  • 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

3
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!

4
Hold on - we have a lot of information to cover
in 80 minutes!
5
First and Foremost! Dont let Visual Lisp
intimidate you!
6
What does LISP stand for?
  • LISt Processor
  • (not Lost In Stupid Parentheses!)

7
The Basics
  • Lists
  • Functions
  • Arguments
  • Golden Rules of AutoLISP

8
What is a LIST?
  • Anything inside of parentheses
  • Examples of LISTS
  • (a b c)
  • (setq x 1)
  • (princ)

9
What is a FUNCTION?(or subr)
  • The ACTION you want Visual Lisp to do!

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

11
Visual Lisp as a Calculator
  • INFIX Notation
  • (1 1)
  • (3 4)
  • (6 / 2)
  • PREFIX Notation
  • ( 1 1)
  • ( 3 4)
  • (/ 6 2)

12
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

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

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

15
Quiz Time!
  • ( 4 (/ ( 6 3) 3))
  • 12
  • ( ( (- 5 2) (/ 15 3)) 6)
  • 21
  • (/ ( (- 11 9) ( 25 5)) ( 3 2))
  • 10

16
Some popular Data Types
  • Real Numbers 1.5
  • Integers 5
  • Strings LINE
  • Lists (8 . DIM)
  • Subrs (or functions) SETQ

17
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 0.5 is correct
  • Dotted pair (0 . CIRCLE)
  • error misplaced dot on input

18
  • (/ 7 2) gt 3
  • (/ 7 2.0) gt 3.5
  • ( 1 2 3 4 5 6. ) gt 21.0
  • ( 1 .5) gt invalid dotted pair
  • ( 1 0.5) gt 1.5

One real number changes the entire pot!
19
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)

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

21
Strings
  • Usually Text (literals)
  • Always double-quoted
  • Spaces accepted
  • Examples autodesk
  • line
  • 1.25

22
Setting Variables (SETQ)(SETQ X 1)SETQ is
the functionX is the variable name1 is the
valueSetting several variables at once(SETQ
A 1 B 2 C 3)
23
Variable Names
  • Alpha-numeric
  • May not contain spaces
  • should not replace existing preset values such as
    T or pi
  • Note A variable that hasnt been set is equal
    to nil

24
Using Visual Lisp variables in AutoCAD
  • (setq X 1 Y 2)
  • Command !X
  • returns 1
  • Command circle
  • 3P/2P/TTR/ltCenter pointgt
  • Diameter/ltRadiusgt!Y

25
Ways to ruin your Visual Lisp life
  • (setq -)
  • (setq /)
  • (setq pi 2.5)
  • Visual Lisp will let you abuse yourself. . .

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

27
By default, Visual Lisp doesnt 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)

28
  • pause allow for user input
  • (command) cancel
  • enter

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

30
Creating your own AutoCAD Commands(DEFUN)
  • DEFUN binds a set of expressions to a variable.
  • (DEFUN CZAP ( )
  • Command zap

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

32
DEFUN examples
  • (DEFUN CZA ( )
  • (Command ZOOM A)
  • )
  • (DEFUN CSQ ( )
  • (Command POLYGON 4 E pause pause)
  • )
  • (DEFUN CZAP ( )
  • (Command erase all )
  • )

33
SHORT.LSP
  • (defun cls ( )
  • (command layer M pause )
  • )
  • (defun cZO ( )
  • (command ZOOM O)
  • )
  • (defun cttr ( )
  • (command circle ttr pause pause pause)
  • )
  • (defun cJellydonut ( )
  • (command donut 0 pause )
  • )

34
Loading Visual Lisp routines
  • APPLOAD - used to load one or more Visual Lisp
    routines
  • (load short)

35
Opening a dialog to a specific tab
  • (command dialogname X)
  • (command options 7)
  • will open the Options dialog to tab 8
  • (command customize 0)

36
Whats wrong with this picture?
  • (defun cdoor
  • (insert door pause 1 1 45)
  • )
  • (defun cfun ())
  • (prompt are we having fun yet?)
  • )

37
PPurge.LSP
  • (Defun cppurge ( )
  • (command purge all N)
  • )

38
Lets create a command that breaks an object in
the same spot twice
  • (defun ccrack ()

39
Clean up your ACT!
  • PRINC (get rid of the nils!)

40
PPurge.LSP
  • (Defun cppurge ( )
  • (command purge all N)
  • (princ)
  • )

41
Blind the user!(he doesnt really want to know
whats going on. ..)
  • (SETVAR CMDECHO 0)
  • but dont forget to turn it back on!
  • (SETVAR CMDECHO 1)

42
PPurge.LSP
  • (Defun cppurge ( )
  • (setvar cmdecho 0)
  • (command purge all N)
  • (setvar cmdecho 1)
  • (princ)
  • )

43
Talk to your user with PROMPT
  • (defun cclean ( )
  • (setvar cmdecho 0)
  • (prompt this command will pick up all of your
    objects and clean the drawing fileplease wait)
  • (command erase all )
  • (prompt \nthere..thats better)
  • (setvar cmdecho 1)
  • (princ)
  • )

44
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

45
Using SSGET
  • (defun ccm ( )
  • (setq ss1 (ssget))
  • (command copy ss1 m)
  • )

46
Lets create a command that does a full circle,
rotated objects Polar Array
  • (defun cpolar ()

47
Just for fun!ALERT
  • ALERT sends an ALERT box to the screen with the
    indicated text
  • Example
  • (ALERT Formatting the hard drive)

48
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

49
ACAD.LSP
  • (defun cZA ( )
  • (command Zoom All)
  • (princ))
  • (defun cDT ( )
  • (setvar clayer TEXT)
  • (command Dtext)
  • (princ))
  • (defun cbolt ( )
  • (command insert bolt pause pause pause)
  • (princ))

50
Automatic loading LISP files
  • ACAD.LSP 2
  • ACADDOC.LSP 4
  • ACAD.MNL 5
  • -------------
  • ACAD200X.LSP 1
  • ACAD200XDOC.LSP 3

51
Undefine and Redefine
  • Permits undefining and redefining the internal
    AutoCAD commands
  • Note AutoCAD commands can always be executed
    with a leading period.

52
SSTARTUPa special section of ACAD.LSP
  • (defun CLINE ( )
  • (prompt Shouldnt you be using Polylines?)
  • (command PLINE))
  • (defun SSTARTUP ( )
  • (command undefine line)
  • )
  • Note sstartup is the last file to be loaded
    before control is handed over to the user.

53
Ways to torture your coworkers
  • ACAD.LSP
  • (defun cqsave ( )
  • (command undo b y)
  • (command .qsave .qsave)
  • (defun sstartup ()
  • (command undefine save)
  • (command undefine qsave)
  • (command undefine saveas)
  • )

54
one more means of torture
  • (defun czoom ( )
  • (command erase L )
  • (command .zoom)
  • (princ)
  • )
  • (defun credo ( )
  • (prompt You goofed - deal with it!)
  • )
  • (defun cundo ( )
  • (alert Get it right the first time!)

55
  • (defun cregen ()
  • (setvar cmdecho 0)
  • (command donut 0 300000000000 10,10)
  • (command regen)
  • (command cmdecho 1)
  • )
  • (defun sstartup ( )
  • (command undefine zoom)
  • (command undefine undo)
  • (command undefine redo)
  • (command undefine regen)
  • )

56
Other evil things to put in sstartup!
  • SSTARTUP
  • (setvar angbase 180)
  • OR
  • (setvar snapang 0.000001)
  • OR
  • (command vpoint 0,0,-1)
  • (command ucsicon off)

57
Whats wrong with this picture?
  • (1 1)
  • ( 5 .25)
  • (/ 9 2)
  • (setq x ( 1 2)
  • (defun ddimwit
  • (command text .5 90 pause)
  • (alert hit cancel to exit dialog box)
  • (defun sstartup ( )

58
Review
  • undefine
  • Acad.lsp
  • sstartup
  • string
  • LISP stands for. . .
  • List
  • Function
  • Command
  • pause
  • DEFUN
  • (princ)
  • (d)

59
Lynn Allenlynn.allen_at_autodesk.comLynns
Blogwww.autodesk.com/blog
Write a Comment
User Comments (0)
About PowerShow.com