How to Run an AutoLISP Program

Saving the AutoLISP File

Before we can run any programs we must make sure that the program file (.lsp file in this case) resides on the system.

If you are downloading programs from my site, the method of saving the AutoLISP file may depend on the browser you are using. For example, IE8 may prompt you to save the file directly to your computer, but I believe Firefox allows you to view the file contents in the browser itself, in which case, you can either go to File » Save Page As making sure that the Save as Type panel is set to All Files; or, you can simply copy the contents into an open Notepad file and save this as 'filename.lsp' (again, ensuring File Type is set to 'All Files').

SaveasType.png

Note that the filename used to save the source code is arbitrary and will not affect the program in any way - however, the majority of users use the function syntax (and perhaps include the program version) for convenience.

Loading the Program

Method 1: Using AppLoad

At the AutoCAD command line, type AppLoad (alternatively go to Tools » Load Application).

Select the program file as previously saved and click Load to load the program into the current drawing session. Click Close to close the Appload Dialog.

Appload.png

The command line should display whether the program has indeed loaded successfully, and any loading messages the author may have decided to include.

Loaded.png

Method 2: Using the ACADDOC.lsp

Another way to load an AutoLISP program is to include a load call in the ACADDOC.lsp.

Everytime a new drawing is opened, AutoCAD will search the Support Paths for any ACADDOC.lsp files, and will proceed to load the first one it discovers.

First we must check that an ACADDOC.lsp file exists - to do this type at the AutoCAD command line:

(findfile "ACADDOC.lsp")

If this returns a filepath, navigate to the existing ACADDOC.lsp file and in the steps that follow, amend its contents.

Else, you can create a new ACADDOC.lsp file by opening a new Notepad document (or other plain text editor) and saving it as ACADDOC.lsp in an AutoCAD Support Path (ensuring the Save As Type panel is once again set to All Files).

In the ACADDOC.lsp, add a line similar to this:

(load "C:\\MyFolder\\MyLISP.lsp" "MyLISP Failed to Load")

If the LISP file does not reside in the AutoCAD Support Path, a full filepath is needed so that the LISP file may be located; in this case, be sure to use double backslashes when specifying the path.

When finished, open a new drawing and the LISP files should load.

Note: if using this method to load many LISP files on startup is causing drawings to open slower, refer to my tutorial on the use of AutoLoad to demand load LISP files.

Method 3: Using the Visual LISP Integrated Development Environment (VLIDE)

This method is aimed primarily at developers, as the VLIDE offers many debugging utilities when writing & loading code.

To load a program using this method, type VLIDE at the AutoCAD command line. In the window that subsequently appears, go to File » Open File (alternatively, Ctrl+O), and select the previously saved file.

Now go to Tools » Load Text in Editor (alternatively, Ctrl+Alt+E)

Running the Program

If the program has loaded successfully, you can now proceed to run the program in the current drawing session.

The syntax (command name) to call the program may be displayed in the author's loading messages, or perhaps noted in the program header. If it cannot be found in either of these locations, you can inspect the source code itself to determine the command to use.

The syntax will be located after the c: in a defun function call, for example:

(defun c:MyCommand ( )
...

In the above example, the command would be MyCommand.

If the c: does not appear after the defun function, this indicates that the function is a subfunction and is designed to be called from another program.

When the command syntax is known, it may be typed at the AutoCAD command line to invoke the program.

Loading Programs Automatically

See my tutorial on Loading Programs at Startup

textsize

increase · reset · decrease

Designed & Created by Lee Mac © 2010