Mastering AutoLISP in 80 Minutes
Mastering AutoLISP in 80 Minutes
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!
First and Foremost! Dont Don t let Visual Lisp intimidate you!
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)
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)
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
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!
(/ (/ (+ (+ (+
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
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
Variable Names
Alpha-numeric May not contain spaces should not replace existing preset values such as T or pi h i
( (setq + -) q ) (setq * /) (setq pi 2.5) ( i ) Visual Lisp will let you abuse yourself. . .
Example: a pe
(command (command (command (command QSAVE) TRIM) ZOOM P) ZOOM P ) LAYER)
pause (command)
(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)
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 )
PPurge.LSP g
Lets create a command that breaks an object in the same spot twice
(defun c:crack ()
PPurge.LSP g
(SETVAR CMDECHO 0)
PPurge.LSP g
(Defun c:ppurge ( ) (setvar cmdecho 0) (command purge all * N) purge all * N ) (setvar cmdecho 1) (princ) )
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 ()
ALERT
ALERT sends an ALERT box to the screen with the indicated text Example: (ALERT Formatting the hard drive ) Formatting drive)
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))
Note: AutoCAD commands can always be executed with a leading period period.
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.
(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) )
Review
LISP stands for. . . List Function Co Command a d pause DEFUN (princ) (d)