Content area

Abstract

In response to a reader's question, a beginner's guide to AutoCAD's AutoLISP is presented. AutoLISP is based on the common LISP programming language, which is widely used in artificial intelligence applications. It can be used in 3 different ways: 1. Type the AutoLISP functions at the AutoCAD command line. 2. Use a text editor to create an AutoLISP function or program. 3. Use Windows Explorer to save a program created by another user into the AutoCAD directory and load the program while in AutoCAD.

Full text

Turn on search term navigation
 

As a new AutoCAD user, the AutoLISP articles vour journal publishes each month fascinate mi. However, I don't feel comfortable using AutoLISP programs. Could you provide a beginner's guide to AutoLISP?

Suzy Martin

via email

Well, Suzy, you're not alone. We often receive emails from AutoCAD novices who want more information about using AutoLISP programs. We've compiled a selection of AutoLISP basics that you should find useful.

What is AutoLISP?

AutoLISP is based on the common LISP programming language, which is widely used in artificial intelligence applications. As a subset of the LISP language, AutoLISP derives the majority of its functions directly from the LISP language itself. LISP excels at working with lists, which is exactly the type of information used in AutoCAD. LISP is among the easiest of all programming languages to learn.

How can I use AutoLISP?

AutoLISP can be used in three different ways:

1. Type the AutoLISP functions at the AutoCAD command line.

2. Use a text editor, such as Windows Notepad, to create an AutoLISP function or program.

3. Use Windows Explorer to save a program created by another user into the AutoCAD directory and load the program while in AutoCAD.

Using AutoLISP at the command line

AutoLISP has built-in expressions, or functions, that evaluate and return a result. Any AutoLISP expression may be typed at the AutoCAD command line. For example, at the command line, type (+ 2.5 6.5). This AutoLISP function executes the addition of 2.5 and 6.5, and returns the result 9.0

Creating and saving AutoLISP programs

As we mentioned earlier, AutoLISP programs can be written in any text editor, such as Windows Notepad. To do so, save the AutoLISP program in the AutoCAD directory and load the program into AutoCAD memory to execute. The AutoCAD R14 directory structure is C:\ Program Files\AutoCAD R14 or C:\ Program Files\ AutoCAD R14 \ SUPPORT.

To save AutoLISP programs created by another AutoCAD user, simply find the AutoLISP program files and copy the file to one of the above named directories. For example, all of the program files associated with back issues of Inside AutoCAD articles are available on the Internet at ftp.zdjournals.com/iac.

Go to the above address on the Internet, and then click on the directory for the month that contains the past article that you want to save. Save each file to the AutoCAD directory by right-clicking on the file and selecting the Save Target As option. Select the AutoCAD directory in the Save As window.

After saving the files to the AutoCAD directory, open the file in Microsoft Word and select File I Save As. Select Text Only from the Save As Type dropdown list. Then, save the file with the name of the AutoLISP program and an LSP extension for the AutoLISP file, and a DCL extension for the dialog box control language.

Loading programs into AutoCAD memory

There are three methods for loading any AutoLISP program into AutoCAD memory. You can execute the AutoLISP (load) function from the AutoCAD command by typing (load "AutoLISP filename").

View Image -

Another method for loading the program is to add the following code to the Acad.lsp file:

The Acad.lsp file is loaded, and the contents are automatically executed each time a drawing is opened in AutoCAD. Any commands and AutoLISP functions listed in the S::STARTUP function are automatically executed when the drawing is opened. The code ( load "AutoLISP fi lename") will load the program associated with the "AutoLISP fi rename" into AutoCAD memory. This method is the same as typing the code on the command line in AutoCAD; however, it's executed automatically. This method is good for loading many AutoLISP programs at one time without typing the AutoLISP (load) function for each program.

The third method of loading the AutoCAD APPLOAD command allows the user to select and save a list of programs that can be loaded whenever a drawing is opened in AutoCAD. Add the AutoLISP filename to the APPLOAD list and click the Load button in the APPLOAD dialog box. The AutoLISP program file is then loaded into AutoCAD memory and is available for execution.

Executing an AutoLISP program

After a program has been loaded into AutoCAD memory, the program needs to be executed to run. To execute an AutoLISP program, look inside the AutoLISP program file and find a (DEFUN) function. Any (DEFUN) function that has a C:execu table name can be executed at the AutoCAD command line. Simply type the executable name on the AutoCAD command line to start the program.

View Image -

Other AutoLISP (DEFUN) functions that do not have a C: before the function name may be executed by typing the function name at the AutoCAD command line. For example, if the following is found in the AutoLISP file:

the function may be executed by typing (myfunction) at the AutoCAD command line. The function would execute and return the value of the current layer system variable.

Rules of AutoLISP

There are a couple programming rules to remember. Every AutoLISP function must begin and end with a set of parentheses (). In addition, any number in an AutoLISP program that is less than 1, must include the zero before the decimal point, such as 0.50, 0.75, and 0.0625.

Setting variables in AutoLISP

The (setq) function sets variables in AutoLISP. For example, type (setq a 3.4) at the AutoCAD command line. This sets the variable a to the real number 3.4. Another example would be to type (setq name ,TOM"). This sets the variable name to the string "TOM".

For more information...

We hope the information provided here will help you make effective use of the AutoLISP programs found in this journal. In addition, you can find more AutoLISP function definitions and examples on the Web at www.ntr.net/-gregmac. Just go to this address and select the AutoLISP Tutorial option.

If you have questions or concerns about your subscription, you can contact our Customer Relations department by sending emial to [email protected]. You can also contact us by phone at (800) 223-8720. Be sure to include or have on hand your customer number when you contact us. Doing, so will help us to assist you quickly and easily.

Copyright ZD Journals Apr 2000