How to Install and Run AutoLISP Programs
I. Installation
1. Create a file directory ("folder") somewhere convenient for you, either on your own workstation or your network file server. I recommend this directory be entirely separate from your AutoCAD installation. Call the folder anything you like, but descriptive names are always helpful. You may wish to create subdirectories under this directory in the future, so something like AutoCAD Applications might be appropriate.
2. Copy the program file or files, with all associated support files provided by the developer, into your chosen AutoCAD applications directory.
3. In AutoCAD, invoke the “Options” command. You can type “Options” at the command line, or pick Options from the Tools pulldown menu.
4. In the Options dialog, choose the Files tab, then expand Support File Search Path by clicking on the “+” button on the left.
5. Pick the “Add” button on the right You will see a blank box at the bottom of the file tree.
6. Pick the “Browse” button on the right, and navigate to the directory where you previously stored the programs you wish to use (step 2 above). When you select your folder, it will be added to the bottom of the Support File Search Path. What does this mean? I t means that, from now on, whenever AutoCAD opens a drawing file, it will be able to find files which are stored in this folder. The order in which AutoCAD searches for support files corresponds to their position in the search path, as shown in this dialog. You will note that the search order may be changed by using the “Move Up” and “Move Down” buttons on the right side of the dialog.
Note: AutoCAD will not search any sub-folders unless you add those to the search path as well. So, if your top level customization directory is, for example, “Applications,” (maybe with nothing in it - your discretion), and you have stored the actual programs in sub-folders called, e.g., “Harry’s Stuff” and “Fred’s Stuff, you would need to add only the “Harry’s Stuff” & “Fred’s Stuff ” folders if those contain the actual programs & data files which are necessary to run the programs under AutoCAD.
II. Loading the Programs Manually
To load any AutoLISP program into an AutoCAD document (*.dwg file), you can just drag and drop the file from Windows Explorer (or your favorite file management program) into the AutoCAD drawing window. Alternatively, you can type:
(load “<the filename>”)
in the AutoCAD command line. Once you have added the directory containing the file to the AutoCAD search path, you do not need to type the “fully qualified path name,” only the file name itself, which generally will have one of three extensions, either *.lsp, *.fas, or *.vlx. If the file has one of these three extensions, you do not need to even type the extension. So, for example, to load a file named: Welding.lsp which is in AutoCAD’s search path, you would only need to type:
(load “welding”)
& press <enter>
at the command line to load the file.
Three things to note:
1. The file name is not case sensitive, so (load “welding”) is identical to (load “WelDiNG”), etc.
2. The command line entry is enclosed in parentheses because it is itself an AutoLISP expression. The AutoLISP interpreter is always available at the command line, and is used to load AutoLISP programs which are stored in external files.
3. The program must be loaded into every dwg in which you wish to invoke its function or functions. So, you would have to either drag & drop, or type the (load “xxx”) statement, every time you open a dwg, in case you wished to use a particular program. Fortunately, this is not necessary.
III. Loading the Programs Automatically
Whenever AutoCAD opens a dwg, it searches the Support File Search Path for a file named:
acaddoc.lsp
If it finds such a file in the support file search path, AutoCAD opens that file and executes any AutoLISP statements contained therein. It stops searching once it has found an instance of acaddoc.lsp. So, if you have more than one file named acaddoc.lsp, the one which is located highest in the search path is the one which is used.
How is acaddoc.lsp useful?
You can put any LISP code you like in acaddoc.lsp, but one major use of this file is to load AutoLISP programs automatically every time a drawing file is opened, instead of having to load them manually, as described above.
So, referring to Section II above, the way you load LISP files, using LISP, is to execute a (load) statement. Thus, acaddoc.lsp typically will contain multiple load statements, such as:
(load “program a”)
(load “program b”)
(load “program c”)
etc.
In fact, it might contain nothing else.
However, it may be that there are some programs which you wish to use regularly, but not necessarily in every drawing. You might have dozens (or even hundreds!) of these programs, and not wish to wait while every one of them gets loaded every time you open a drawing, but still be able to have it available “on demand” without having to manually load the program as described in Section II above.
For this purpose, AutoLISP has a demand loading feature. Here is how to use it.
To demand load an AutoLISP program, use an (autoload) statement in acaddoc.lsp instead of a (load) statement.
What is the syntax of an (autoload) statement?
An (autoload) statement has the following syntax:
(autoload <filename> <command list>)
Example:
(autoload "ALIGNTXT" '("ALIGNTXT" "ROTTXT" "ROT180"))
in this case, the file to be autoloaded is named aligntxt.lsp, and the commands which are defined by LISP functions in that file are: Aligntxt, Rottxt, and Rot180 (again, not case sensitive).
How does this work?
When AutoLISP encounters an (autoload) statement, it keeps the list of commands defined within the internal set of parentheses, which must be preceded by a “single quote” mark, as shown above.
Then, if one of these commands is actually invoked, either by typing at the command line, picking a menu item, toolbar button, or palette item, AutoLISP attempts to execute a (load) statement, using the filename you have provided in the first part of the (autoload) statement.
If the (load) statement succeeds, AutoLISP then runs the command which you invoked. With the AutoLISP file loaded, all commands defined in that file become available as long as the dwg file remains open.
So, in order to write your (autoload) statement, you will need to know the name of the AutoLISP file to load, as well as the names of commands defined in that file which you wish to “trigger” loading the program when they are invoked in AutoCAD.
The command names defined by an application will usually be provided by the developer, since he wants you to use his products. However, it may be that the documentation is incomplete for whatever reason, in which case you may need to discover what new AutoCAD commands have been defined in the program file.
To determine these command names, open the program *.lsp file in Notepad, or alternatively some other programmer’s text editor and use its search or find command to search for the character sequence “defun C:” or just “C:” All AutoLISP functions which define a new AutoCAD command must have the form:
(defun C:xxx ( / )
<some LISP statements> )
So, you can copy whatever characters follow the colon into the “list of commands” in your (autoload) statement. You do not need the “C” or the “:” anymore than you would need them to invoke the actual command in AutoCAD once AutoLISP has evaluated the (defun) statement. Be sure to discard any changes you may have made inadvertently when closing the program file in text editor, as you do not wish to break someone else’s program.
If the program file has the extension *.fas or *vlx, it is a so-called “compiled” AutoLISP file, and you do not have access to the file contents as detailed above. In that case you must rely on the published documentation, or contact the developer for further information.
Does AutoCAD come with an acaddoc.lsp file?
No. You will need to create an acaddoc.lsp file to do whatever you wish AutoLISP to do for you every time AutoCAD opens a drawing. This is not limited to loading other LISP files, although that is a major usage of acaddoc.lsp.
How do I create an acaddoc.lsp file?
This is very simple. You do not need any special tools, just a text editor. Windows Notepad will work just fine for this purpose. Be sure you do not use a so-called “word processing” program for this purpose, unless you are very careful to set that program to create a “plain text “ program. “Word processing” programs typically, when using default settings, create files which contain formatting codes, and are stored in a file format which is proprietary to the program which creates it. AutoLISP cannot read such files, and will emit an error message.
Step by step:
1. Open Notepad
2. Create a new file and save it to your customization folder as described in Section I above, with the file name acaddoc.lsp. Again, the required file name is not case sensitive.
3. Type in whatever AutoLISP statements you wish, and save the file. For purposes of this discussion, these will be (load) and (autoload) statements as described above.
4. You may wish to include comments in the file to describe what you are loading, in order to refresh your memory, and supply information to anyone else who may need to edit the file, at a later date. Comments (text which is not considered part of a program statement) in AutoLISP are always preceded by one or more semicolons, which tells the AutoLISP interpreter to ignore any characters following the semicolon until it encounters an end-of-line marker (a “carriage return” followed by a “line feed”).
The example which follows is a part of my own acaddoc.lsp
You may ignore everything but the (load) & (autoload) statements, if you wish.
The other statements, with comments, are included here only to give an idea of other tasks which may be performed by statements contained in this file, which is loaded every time a drawing file is opened, and possibly to interest the reader in expanding his knowledge of AutoLISP.
Example Acaddoc.lsp
;;;Tekton Construction Services © 2008
(vl-load-com);loads Active X support for AutoLISP
(setvar "EXPERT" 1);sets a system variable
(setq tktn_annolayer "ANNOBJ");defines a global AutoLISP symbol
;;;define a global AutoLISP function for use by other functions
(defun thisdwg () (setq *THISDWG* (vla-get-activedocument (vlax-get-acad-object))))
;;;initialization is now complete
;;;the following statements unconditionally load AutoLISP programs when a dwg is opened.
(load "tktn_001.llb");Tekton Lisp Lib 001
(load "DDEDREG") ; by cad nauseam - highly recommended
(load "DDCHREG") ; by cad nauseam - highly recommended
(load "NTEXT")
(load "XTSWITCH") ; my first LISP Program
;;;the following statements load a program on demand when its specified commands are invoked
(autoload "ALIGNTXT" '("ALIGNTXT" "ROTTXT" "ROT180"))
(autoload "BEVEL" '("BEVEL" "BEVELQUERY"))
(autoload "BILLBOLTS" '("BILLBOLTS"))
(autoload "RPN" '("RPN")) ; RPN calculator
(autoload "TKTN_HOLES" '("HOLE" "HOLSCT" "HOLEGROUP" "HOLEGROUP-M"))
(autoload "MYWELD1-1" '("WELD")) ; draws weld symbols
(autoload "VPLOCK" '("VPLOCK" "VPUNLOCK"))
Copyright 2008-2012 Herman Mayfarth
Last updated 21 Jan 2012