AutoCAD

AutoLISP Lesson #2 - THE AutoLISP ENVIRONMENT:
The AutoLISP environment is entered when AutoCAD receives a "(" symbol at the command line. When AutoCAD sees the opening parentheses, it runs the AutoLISP interpreter. The interpreter then evaluates, as a list, what is between the opening "(" symbol and the closing ")" symbol.

Data types
There are three basic data types in AutoLISP. They are functions, symbols, and lists.

Function: is the first item in every list. The remainder of the list is used as the parameters or arguments to the function. If the first item in the list is not a function you will receive an error message stating: "bad function."

Symbol: is what other programming languages call a variable, and from this point on when I refer to a variable I am really talking about a symbol. A symbol in LISP however can be a whole lot more than just a value, as a normal variable would hold. A symbol can contain a list or a function definition. A symbol has a value bound to it. If it has no binding it is said to be
"nil". All symbols should be set to nil upon ending a program or encountering an error.

List: is anything that is not a symbol.

Errors
If an error occurs while running an AutoLISP program, the interpreter will return a trace of the function starting with the most recent function and continuing to the top of the program. This is handy for debugging. You should define your own error handler so the user doesn't have to see this and wonder what is going on.

Special characters
There are some characters in AutoLISP, like any language, which cannot be used under certain circumstances.

( ) ' " ;

\\ Gives a normal backslash character.

\" Gives a normal double quote character.

\e Gives an escape character.

\n Gives a new line character.

\r Gives a return character.

\t Gives the tab character.

\nnn Gives a character who’s octal code is nnn.

'This is equivalent to (quote this)

; This is a comment

Comments will be discussed in depth later in the lessons.

The LISP interpreter
At the heart of the LISP interpreter is the evaluator. The evaluator takes a line of lisp code, evaluates it, and returns a result. Evaluation happens in the following manner:

The current bindings of functions and variables can be retrieved at the command line by using the exclamation character "!". If the value being retrieved is a function the entire function list will scroll across the screen. If the value being retrieved is a variable, the value of the current binding will be returned. This is normally what you are after when you check the current binding of an object. When checking for errors in code it is sometimes helpful to see that your variables are getting set correctly.

Command functions
You can create your own command functions within AutoLISP. The DEFUN function is used to create functions in AutoLISP. If when creating your function you define them like this:

(defun c:function_name ( / )
(do something)
)

The function can be called within AutoCAD without having to put parenthesis around it. The "c:" has nothing to do with your disk drives; it merely tells LISP that the function should be defined as a command line function.

Lisp libraries
You can create your own library of lisp routines using the ACAD.LSP file. If one does not exist on your system you can create one and add your routines to it. If it already exists, your routines can be added to the end so they load every time you enter AutoCAD.
If you have certain things you do every time you enter a drawing, they can be added to the ACAD.LSP file and automatically executed every time the ACAD.LSP file is loaded. To do this you must define a function that is called "s::startup". The s::startup function is executed every time ACAD.LSP is loaded.
Any lisp function may be loaded by typing (load "lispname"). The command must be inside parenthesis, and the ".lsp" extension should not be added. If you find your lisp programs will not load, make sure you have followed the rules above, then try what comes next.
AutoLISP will search the support file search path that is set in the CONFIG command. If it doesn't find your program the search path it will not load it and will tell you it couldn't find it. It will list all the places it looked. If your file is not in a search path, and can't be put there, you can still load it by giving AutoLISP the entire path to the file in the load command, example:

(load "c:/acad/dave/myprog")

Note I used forward slashes instead of back slashes. AutoLISP will not interpret back slashes correctly if you only use (1) one. Remember from earlier in the lesson the back slash is a special character in AutoLISP and cannot be used in the literal use. Also remember that it can be used literally if you put (2) two in a row. Like this:

(load "c:\\acad\\dave\\myprog")