*.lsp Commands: + - * / setq setvar getvar getpoint defun command getstring appload vlide Autolisp is version of an old programming language (circa 1960) called LISP (List Processor) Autolisp programs can be written in any plain text editor (ASCII). This programming language has been part of Autocad since Release 2. It is integrated into Autocad but is a separate program. The Lisp programs are used to customize Autocad and to provide functions not available in the Autocad Product. Some of the functions in Release 2004 are lisp programs. The rectang (rectangle) command and 3D Rotate are two examples. Autolisp is invoked at the command line with the use of parentheses. Command: (function) calls the function and executes the program Mathematical Operators : A space separates the function and each argument. (function argument1 argument2) Addition (+ 5 3) returns 8 Subtraction (- 5 3) returns 2 Multiplication (* 5 3) returns 15 Division (/ 5 3) returns 1[assumes integer values unless a real number is specified as one of the arguments.] Division (/ 5.0 3) returns 1.6667 If there are more arguments in these expressions, Lisp does some interesting things: Addition: (+ arg 1 arg 2 arg n ) all arguments are added Subtraction (- arg 1 arg 2 arg n ) arg 2 through arg n are added then subtracted from arg 1 Multiplication (* arg 1 arg 2 arg n ) all arguments are multiplied Division (/ arg 1 arg 2 arg n ) arg 2 through arg n are multiplied then divided into arg 1. Increment (1+ num) num = num + 1 (uses numbers or variables) Decrement (1- num) num=num-1 Absolute (abs num) returns the absolute value of a number or variable Trigonometric functions returns a value in radians (sin 0) returns 0 (cos 0) returns 1 (sin 45) returns 0.850904 Relational operators Relational operators are used to test conditions in a program. The arguments are called atoms. They can be numbers, variables or reserved variables Equal to (= var1 var2) if equal returns T or true Not equal to (/= var1 var2) if not equal returns true Less than (< var1 var2) if first < second returns true, else nil
(<= var1 var2) less than or equal to Greater than (> var1 var2) returns true if var1 > var2 (>= var1 var2) greater than or equal to Variables: any number of variables can be created with Lisp. There are a large number of reserved or key words that are not allowed. Any programming word, mathematical operator or name, or Autocad system variable name should not be used. Setq Defines a variable name and assigns a value. (setq num1 value) A variable called num1 is created and assigned a value of value. A value can be a number, or a string. Getvar Acquires a value of an Autocad system variable (there are hundreds of these) (getvar ltscale ) returns the current value of the line type scale factor variable. Setvar Sets a new value for a system variable (setvar name (value)) Sets a value for a system variable based on the evaluation of the expression of (value)!(var) Returns current value of variable User Interaction: There are a number of commands that allow a program to solicit data from a user such as a point, some text, etc. (getpoint point prompt) (getpoint var1 enter a start point ) points can be keyed in or cursor selected. The value for the point is stored in the named variable. (getpoint p1 /n Enter a point ) A point is entered from the user to variable p1. The /n, or new line will print the next prompt on the next line. (getstring enter text ) Defining functions: (defun name (arguments)) Defines a function called name with arguments or variables to be used within the program. There are global variables and local variables. Global variables do not lose values when the program terminates. Local variables are valid only during program execution. (defun c:test ()) Defines a function called test. With no arguments specified, all variables are from outside the program. The c: allows the function to be typed in at the command line without parentheses. (treated like an Autocad command. Using Autocad Commands within Autolisp: (command autocad_command opt 1 opt 2 opt n ) The Command, command followed by an Autocad command exactly as it would be typed at the keyboard. Knowledge of Autocad commands and options is helpful. The Autocad command and all the options must be
enclosed in quotes. Aliases can be used but is discouraged. (acad.pgp can be changed or corrupted and thus change the behavior of the Autolisp program.) (Command line 0,0 5,5 ) Draws a line from 0,0 to 5,5 and cancels the command. (Command redraw ) Redraws the display. Writing to the drawing editor. (princ variable or string ) (princ string ) Displays the string, string (princ var1) Displays the value of var1 The Text Editor: As mentioned, any plain text editor can be used to create Autolisp programs. (Notepad, WordPad in plain text format. When using these, the extension.lsp must be added to the file name. Autocad2000 has a special text editor for the creation of lisp programs called VLIDE for Visual Lisp Interactive Development Environment. It works like a standard text editor but has special functions for testing a program for syntax errors and run time errors. In addition, the text is color coded making it easier to distinguish between commands and variables. Tools-Autolisp-Visual Lisp Editor Or type VLIDE at the command prompt Red Blue Magenta Cyan Green Black protected items functions strings and comments real numbers integers variables and user defined items
A function that will erase all the items from the screen by typing in the letters ea for erase all. The Command in Autocad is: Erase <enter> All <enter> <enter> In the Autolisp editor type the following commands: ; program to erase all objects from the screen (defun c:ea () (command "erase" "all" "") ) The close parentheses on the last line completes the defun statement. It is a good practice to leave this on a separate line for ease of reading. Also for ease of understanding this program, a good programmer will comment the file. A comment is placed using a semicolon. Comments are ignored by Autolisp. Save to file to disk with the name erasekey.lsp To use this program in Autocad, it must first be loaded. The appload command, or Tools Autolisp Load can be used to locate and load the file. Once successfully loaded, close the dialog box. Test the program.
Exercise: Write an Autolisp program that inputs four points from a user and draws a closed box. Call it box not to confuse it with the solid box alias. ;This program will prompt the user to enter four points from the keyboard or ;cursor and then draw a lines between each point forming the vertices of a ;rectangular shape. (defun c:boxx () (setq p1 (getpoint \n enter first vertex )) (setq p2 (getpoint \n enter second vertex )) (setq p3 (getpoint \n enter third vertex )) (setq p4 (getpoint \n enter fourth vertex )) (command line p1 p2 p3 p4 close ) ) Save as box.lsp Close or task switch to the Autocad Drawing Editor. Load the lisp routine and test the program. Write a lisp program that will draw a 17x11 rectangle on the screen. Then draw a 5 x 2 rectangle starting at the lower right of the first rectangle, and prints the word test in the smaller rectangle.