Saturday, February 19, 2022

AutoLISP Tutorial for AutoCAD Users: Lesson 4

We need a way to fully and permanently integrate our programs into our AutoCAD working environment. AutoCAD provides several ways. This lesson shows you how to make a well-documented system that can grow with you.

Name your application

All of your programs and their supporting files are collectively your application. Name your application after yourself. In my case, the application name is Haws. This name will get repeated inside files including possibly a menu. So choose it well.

Create the folder

Create a folder anywhere dedicated to your application. This folder name is less important than the application name. In my case the folder name is HawsCAD or AutoCAD-Haws.

Add this folder to the Support Files Search Path and Trusted Locations under the Files tab of Options. This lets AutoCAD find anything you put there (code, blocks, menus, slides, etc).

Create some files

Put your first program file (from Lesson 3) in the application folder:


Use your file editor (I recommend Notepad++) or the procedure in Lesson 3 to create a catch-all AutoLISP file in the folder for AutoLISP code you aren't ready to organize better yet:


At the bottom of each file, put the following line with the correct name of the file:

(princ "\n{file name}.LSP loaded.")

Create a loader

Create a loader file. Mine is called HawsLoader.lsp.

Inside the file, put the following lines so that any time HawsLoader.lsp is loaded, it will load L0.LSP and HawsMisc.LSP:

(LOAD "L0")

(LOAD "HawsMisc")

(princ "\nHawsLoader.LSP loaded.")

About Autoloading

Autoloading is different than what we just did in your loader file. Autoloading is delayed loading on-demand. You can use autoloading instead of simple loading inside your loader if you want.

To delay the loading of your program code until it's needed (thus speeding initial drawing loading by loading one file instead of many), AutoCAD provides a function called autoload that says "Load this file when any of these commands are wanted. Then run the command that was wanted." As the given file is loaded, it redefines each of the given commands from their autoloader version to their real version and then runs the real version. You use autoload like this:

(AUTOLOAD "L0" '("L0"))



Adding your Application

You add your application to AutoCAD by adding your loader to appload, acaddoc.lsp, or your application menu.


Use the APPLOAD command to add your loader (HawsLoader.lsp in my case) to the Startup Suite. This is a very easy method.


AutoCAD does not come with an acaddoc.lsp. But if it finds one, it loads it in each drawing. To use this method, create this file in your application folder:


Inside the file, put the following lines:

(load "HawsLoader")
(princ "\nACADDOC.LSP loaded from the Haws application folder.")

This method requires one less step inside AutoCAD by using the magic acaddoc.lsp file. This is a good method for personal use or on a small team. But it would get messy if all applications tried to put their loaders (or worse, their code) in acaddoc.lsp.


If you use an application menu (like Haws.cuix or Haws.mnu) and create an accompanying MNL file (like Haws.mnl) in your application folder, AutoCAD will load the MNL file automatically. Inside your MNL file, you would just put the following lines:

(load "HawsLoader")
(princ "\nHaws menu utilities loaded.")

Note that the legacy term for your menu was "partial menu (MNU) file", and you used the MENULOAD command to load your partial menu. The current term is "customization (CUI/CUIx) file", and you can use either MENULOAD or CUILOAD to load it.

This is the professional method that I use for Construction Notes Manager. But you can't use it until you have a custom menu. In a future lesson, I hope to share a simple reason and method for you to have a simple menu.

Other resources

Much of this information can be found in the "About Auto-Loading and Running AutoLISP Routines" article of AutoCAD help.

My free libre open source Construction Notes Manager application shows an example of some of the principles above. You can download the setup file or browse the source code.

Previous lessons

No comments: