An Introduction to AutoLISP
[Set-ups]
[Edit-LSP]^C^Cshell ed C:/acad11/lisp/test.lsp;graphscr
[Test-LSP]^C^C(load "/acad11/lisp/test");(test);
[Print test.lsp]^C^C(load "/acad11/lisp/printest");(printest);
[~--]
|
In the above, when the POP2 menu 'Set-ups' is selected, one of the choices is 'Lisp program development', and this
displays a new page that includes items to edit a file named test.lsp in the subdirectory lisp which is contained in the
AutoCAD directory, acad11. The call to the editor in this case is ed, because that is the name of the editor I use. You
may substitute edlin or edit or the name of your favorite text editor (provided that it produces ASCII output) in place of
this.
This menu macro only edits the file called test.lsp, which is the name I give to the current LISP program that I am
developing. To edit any other file, most text editors allow you to choose the filename after the editor has been entered,
so you could simply leave out the filename until the editor is running. Edlin requires a filename to be supplied on
execution, so simply give the shell command and issue the edlin command from the operating system.
The use of separate directories for lisp programs is a good management procedure that I recommend. After the lisp file
has been created or edited, it can be immediately tested by the Test-LSP macro, which loads and executes test.lsp
assuming a defined function named 'test' has been created.
The 'Print test.lsp' option calls a LISP program that prints the test.lsp file. The AutoLISP program to do this has been
adapted from the Autodesk Inc. 'Fprint.lsp' program, which is freely available to you for modification (with the
appropriate acknowledgements to Autodesk). We will examine this program in detail later in the course. In the
meantime, you may use your text editor or the DOS print command to print your program.
GLOBAL AND LOCAL VARIABLES
The default mode of AutoLISP is to define all variables to be 'global', which means that the variables are generally
available not only when the program containing them is executed, but also after the execution is completed. This means
that other programs you write will have access to variables that were defined by previous programs. It is possible to see
the values of global variables by typing their names preceded by ! at the command prompt. For instance, if pt1 is a
variable that represents a point whose coordinates are 2.5,3.0,0.0, then typing !pt1 will display the three coordinate
values.
Global variables appear in the ATOMLIST, and take up memory in the computer. For these reasons, and because you
will probably not want variables such as PT1 to take values left over by previous programs, it is wise not to have global
variables hanging around at the end of execution.
In order to avoid global variables, you may declare the program variables to be 'local' to a particular defined function by
adding their names, preceded by a slash mark and a space (/ ) in the parentheses which are supplied for that purpose in
the defun statement, thus:
(defun test2 (/ pt1 pt2 pt3 pt4)...........)
will make all of the point variables local. Note here that other variables may be defined as arguments of the defun
function, appearing before the slash mark, for instance,
(defun test3 (a b / pt1 pt2 pt3 pt4).............)
means that the two arguments a and b must be supplied to the defined function test3 before it can be executed, and
instead of executing with (test3) as before, you must supply values, such as (test3 2.2 3.0) so that the variables a and b
can take any value you wish, in this case, 2.2 and 3.0 respectively. We will see examples of programs using global and
local variables in the next lesson in this series.
Home work questions:
1. Why are the closing parentheses in the last 2 defined function examples on the same line as the opening
parentheses, instead of on a separate line as in our previous defined function examples?
2. Are the arguments a and b in the last defined function example local or global variables?
Page 4 of 64