Loading Programs Automatically

This tutorial aims to guide you through the various methods which can be used to load a program automatically on startup.

If you have a toolbox of LISP programs that you use regularly, it would be tedious to have to load each and every program whenever a drawing is opened. Thankfully, I shall introduce two methods to accomplish this task, each with their own advantages in different situations.

Method 1: Using the Startup Suite

This is perhaps the most user-friendly method to automatically load a program at startup, as the user needn't have any LISP experience whatsoever.

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

In the dialog that subsequently appears, click on the Contents button beneath the Startup Suite heading:

Startup Suite Contents.png

A secondary dialog will consequently appear, to which programs to be loaded on startup may be added. To do this, click Add and navigate to the desired program.

Startup Suite.png

All programs listed in the above panel will be loaded every time a drawing is opened.

The method of using the Startup Suite to load programs automatically does have a few disadvantages however: should the user wish to migrate his or her programs to another computer, all programs will need to be added to the Startup Suite on the new computer.

There have also been a few known bugs surrounding the Startup Suite in past versions of AutoCAD. For these reasons, I shall introduce another method which, although requires the user to have some basic knowledge of AutoLISP, overcomes these difficulties.

Method 2: Using the ACADDOC.lsp

Upon opening a drawing or starting a new drawing, AutoCAD will search all listed support paths including the working directory for a file with the filename: ACADDOC.lsp. If one or more such files are found, AutoCAD will proceed to load the first file found.

With this knowledge one can edit or create an ACADDOC.lsp to include any AutoLISP expressions to be evaluated upon startup.

Things get a little complicated should there exist more than one ACADDOC.lsp file, so, to check if such a file already exists, type or copy the following line to the AutoCAD command line:

(findfile "ACADDOC.lsp")

Should a filepath be returned, in the steps that follow, navigate to this file and amend its contents. Else, if the above line returns nil, you can create your own ACADDOC.lsp in Notepad or the VLIDE and save it in an AutoCAD Support Path.

One clear advantage to using the ACADDOC.lsp to automatically load programs is that, upon migration, it may easily be copied from computer to computer, or indeed reside on a network to load programs on many computers simultaneously.

The Load Function

The load function will evaluate all AutoLISP expressions found within a file with a supplied filename (if found), effectively loading an program file (.lsp/.fas/.vlx) into the current drawing session.

The load function takes two arguments:

(load <filename> [on failure])

Where filename is the filename of a program file to load, and on failure is an optional argument to be returned should the program fail to load (this is usually a string or nil).

If the program file resides in an AutoCAD Support Path, the filename need only be the name of the file to load; else a full filepath is required to locate the file, for example:

File Residing in AutoCAD Support Path:

(load "MyProgram" "MyProgram Failed to Load.")
(load "MyLISP.lsp" "MyLISP Failed to Load.")

File Residing Elsewhere:

(load "C:\\My Folder\\MyProgram.vlx" "MyProgram Failed to Load.")

Note that should the file extension be omitted, the load function will add .vlx, .fas, .lsp (in that order) stopping when a file is found.

Although the on failure argument is optional, I would recommend to include it in the load statement, else the function will return an error should the file fail to load.

Using this knowledge we can populate our ACADDOC.lsp file with load statements to be evaluated when the ACADDOC.lsp file is loaded on startup.

In this way, the ACADDOC.lsp may look something like this:

(load "C:\\MyPrograms\\MyLISP.lsp" "MyLISP Failed to Load.")
(load "F:\\My Folder\\MyApp.fas" "MyApp Failed to Load.")
(load "MyProgram" "MyProgram Failed to Load.")

To aid in the construction of an ACADDOC.lsp utilising the load function I have constructed an ACADDOC.lsp Creator Program which can write statements to the ACADDOC.lsp to load all programs in a directory (and subdirectories) - saving you all the tedious typing.

One disadvantage of using the load function arises when the user wishes to load a large number of programs. This method will load every program into memory everytime a drawing is opened, hence should a large number of programs require loading, there may be a noticeable increase in the time taken to open a drawing.

To avoid this time delay, programs can be 'demand-loaded', i.e. loaded as and when a user needs them; for this I introduce the AutoLoad function.

The AutoLoad Function

The AutoLoad function enables a user to load an AutoLISP file when a specific command is entered at the command line. This can greatly reduce the time taken to open a drawing and the memory used should the user wish to load many programs automatically as programs are only loaded into the drawing session when required by the user.

The AutoLoad function takes the following format:

(autoload <filename> <cmdlist>)

Where filename is the name of an AutoLISP file and follows much the same rules as with the load function: should the AutoLISP file reside in an AutoCAD Support Path, only the filename is needed, else a full filepath is required.

The cmdlist is a list of commands that, upon the user typing a command present in the list, the filename supplied will be loaded into the current drawing session.

This method is best demonstrated with an example. Consider the following LISP program:

(defun c:DrawLine ( / p q )

  (if (and (setq p (getpoint "\nSpecify First Point: "))
           (setq q (getpoint "\nSpecify Next Point: " p)))
    (entmake (list (cons 0 "LINE") (cons 10 (trans p 1 0)) (cons 11 (trans q 1 0))))

The above program will simply construct a line between two selected points; but, more importantly for this example, it may be called with the syntax: DrawLine.

Let's assume the above code is saved to a LISP file in an AutoCAD Support Path called: LineProgram.lsp.

To demand-load this program automatically we could include this line in the ACADDOC.lsp:

(autoload "LineProgram" '("DrawLine"))

Now, upon opening a drawing or creating a new drawing, should the user type DrawLine at the command line, the LineProgram.lsp file will be loaded into the drawing session and the program will start.

Should the program file contain more than one command, additional commands may be added to the list supplied to the AutoLoad function, hence triggering the load of the supplied file upon the user typing any command present in the list.

Further Information

Further information about the functions I have demonstrated in this tutorial may be found in the VLIDE Help Documentation. Struggling to access the help? See my tutorial here.


increase · reset · decrease

Designed & Created by Lee Mac © 2010