Professional Documents
Culture Documents
Definitions
y Function- pre-defined set of instructions that can perform a specific task in AutoCAD Example: * = multiple += add Arguments - the values passed to a function for manipulation Example: (*2 4) = multiply the arguments 2 and 4 to return the answer 8 (* 2 4 6) = multiply the arguments 2, 4 & 6 to return 48 (* 2 4 6.0) = multiply the arguments 2, 4 & 6.0 to return 48.0 In the first two cases the arguments are integers, therefore the answers are integers In the third case, one of the arguments is a real number, therefore the answer is a real number Expressions - AutoLISP code to be evaluated Local Variables - Variables that are only valid a specific function and while the function is running Global Variables - Variables that are available to all functions during a session of AutoCAD
y y
or if no expression is specified, foreach returns nil. Example Command: (foreach NUMBER (list 1 2 3) (princ NUMBER)) returns to the command prompt 1 2 33 (note the function prints the 3 and returns 3 to the command prompt when finished)
Notes You can interactively add objects to or remove objects from the selection set Entity Names = a numeric label assigned to every object (entity) in the drawing Notes This is an efficient method of obtaining the information about an object in the drawing File Descriptors = a pointer to a file opened in AutoLISP by the open function Notes File Descriptors are used to read or write to ASCII based files
Predefined Variables
T nil pi this variable is the constant TRUE, used as a non-nil value this variable is the constant NIL, used as a non-T value this variable is the constant for the ratio of the circumference to the diameter of a circle
pause this variable is used with the command function to pause for user input Note - these variables can be changed by the setq function, but it is highly recommended that you don't!
Comments
; (semicolon) is used to denote comments in a program Example ; this is a commented line, explaining the program
Operators
Process + add * / subtract multiply divide (+ [number number] ...) (- [number number] ...) (* [number number] ...) (/ [number number] ...) Examples (+ 1 2) returns 3; (+ 1 2.0) returns 3.0; (+ 1 2 3 4) returns 10 (- 2 1) returns 1; (- 2 1.0) returns 1.0; (- 10 4 2) returns 4; (- 5) returns -5 (* 1 2) returns 2; (* 1 2.0) returns 2.0; (* 1 2 3 4) returns 24; (* 3) returns 3 (/ 4 2) returns 2; (/ 4 3) returns 1; (/ 4 3.0) returns 1.33333; (/ 10 2 2.0) returns 2.5; (/ 3) returns 3 (= 4 4) returns T; (= 4 4.0) returns T; (= 4 4.0 4) returns T; (= 4 4 3) returns nil; (= "me" "me") returns T; (= "me" "Me") returns nil; (= 1 "1") returns nil (/= 1 2) returns T; (/= 1 1) returns nil; (/= 1 1 1.0) returns nil; (/= "me" "Me") returns T (< 1 2) returns T; (< 1 2 3) returns T; (< 2.0 3) returns T; (< "a" "b") returns T; (< "a" "B") returns nil
= equal to
(<= 1 2) returns T; (<= 1 2 3) returns T; (<= <= less than or equal to (<= number [number] ...) 2.0 3) returns T; (<= "a" "a") returns T; (< "a" "A") returns nil > greater than >= (>number [number] ...) (> 1 2) returns nil; (> 1 2 3) returns nil; (> 2.0 3) returns nil; (> "a" "b") returns nil; (> "a" "B") returns T
greater than or equal (>= 1 2) returns nil; (>= 1 2 3) returns nil; (>= number [number] ...) to (>= 2.0 3) returns nil; (>= "a" "a") returns T
1+ increment by 1 1- decrement by 1
(1+ 1) returns 2; (1+ 1.0) returns 2.0; (1+ -1) returns 0 (1- 2) returns 1; (1- 2.0) returns 1.0; (1- -2) returns -3
Notes A Variable can be supplied instead of a [number] Only numeric number values are valid with +,-,*,/ If only 1 number is supplied to comparison, the result is always T When comparing String values, the strings are compared character by character /= compares successive arguments only, not the complete list of numbers
Getting Started
setq function to store a value to a variable (setqVariableName Expression [VariableName Expression] ...) Notes setq can assign multiple Variables in one call to the function (setq Var1 1.0 Var2 2.0) setq returns the last evaluated expression (previous example returns 2.0) Examples - (setq Var1 1.0), (setq Var1 "Text"), (setq Var1 (list 1 2 3)), (setq Var1 T) defun function defines a user function (defunSymbolName ([Arguments ] [/ LocalVariables ...] ) Expressions ...) Notes If you do not supply any Arguments or Local Variables you must supply an empty set of parentheses after the SymbolName Warning! Never use the SymbolName of a built-in function or symbol This will overwrite the original definition and make the built-in function inaccessible Examples Function without Arguments and Local Variables (defunMyFunc () ...) (defunMyFunc (Arg1 Arg2) ...) (defunMyFunc (/ LVar1 LVar2) ...) (defunMyFunc (Arg1 Arg2 / Var1 LVar2) ...) (defun C:MyFunc () ...) (defun C:MyFunc (/ LVar1 LVar2) ...) Function with 2 Arguments Function with 2 Local Variables Function with 2 Arguments and 2 Local Variables Function that can be called like any AutoCAD command Function that can be called like any AutoCAD command with 2 Local Variables
First programs
; TEST1 program, sets 2 variables, then adds the 2 variables and saves to the 3rd variable; program returns the 3rd variable (4.0) (defun C:TEST1 () (setq Var1 1.0) (setq Var2 3.0) (setq Var3 (+ Var1 Var2)) ) ; TEST2 program, sets 2 variables, then adds the 2 variables, saves the sum to the 3rd variable; multiples the 2 variables, saves the sum to the 4th variable, program returns whether the 3rd and 4th variables are the same (nil) (defun C:TEST2 () (setq Var1 1.0) (setq Var2 3.0) (setq Var3 (+ Var1 Var2)) (setq Var4 (* Var1 Var2)) (= Var3 Var4) )