Search This Blog

Saturday, January 01, 2005

Loading Custom Lisp files from a text file.

Happy New Year!
Would you like to control the number and load order of your custom lisp files? Of course you would, wouldn't it be nice to simply add a filename and path to a data file and have AutoCAD load the file for you? What if you want to add a new file? Do you want to re-write the code for each additional file? Probably not! Here is a way to set up a system so that you can simply add and subtract lisp files from a list contained in a text file. (Note: the list should contain the entire name and path to the autolisp file.)


  • A folder containing the lisp files to be loaded. This could be your office standard location or a user maintained folder.
  • An acad.lsp file containing the code shown in Listing 1. (Note: this code could be added to your own acad.lsp if you have already defined one!) Alternately, you could load this file from a menu selection, but I like it to be automatic and transparent.
  • A text based file containing a list of each autolisp file. One file and path on each line. See the graphic below.
Listing 1:
How to Do It:
Here is a working example, walkthru, and the files required to repeat this on your system.
1.) Create a folder on your root drive (C:) and call it "customlisp".
2.) Now browse to this new folder and create your datafile. I called mine "customlisp.dat".
3.) Open your new data file ("c:\customlisp\customlisp.dat") in the text editor of your choice. I prefer TextPad. Add the full path and filename to every autolisp or visual lisp file you would like to have loaded. Be sure to use normal naming conventions, no need to use double backslashes! My Example is shown above with files contained on another drive such as: "x:\customlisp\A_2k5custom.lsp" Click here to download my example files:
4.) Now ensure that all the files you have added to your data file are present and accounted for in the folder of your choosing. If you used names other than those provided in the example just be sure to perform the appropriate search and replace. Click here to download my example files.
5.) Now to automate this process, we will use an AutoCAD built in function by creating a lisp file called "acad.lsp". Click here to download the file I have created for this example (it is called "customlisp.lsp" so it wouldn't compete with any present files). Note: you can either add the code contained in the file to your own "acad.lsp" or simply open the downloaded file in your favorite text editor and rename it "acad.lsp". Be sure that it can be found somewhere in AutoCAD's search path.
6.) Now launch AutoCAD to see it run. If you have run this correctly, you should see the following displayed at the command line.
How it Works?
After AutoCAD loads the resident "acad200#.lsp" file is loaded, AutoCAD will look for and load any arx routines found listed in a file called "acad.rx". The next file that AutoCAD will look for and load is called "acad.lsp". If you have followed the instructions listed above, AutoCAD will find your acad.lsp file and load it. As soon as it loads the file, AutoCAD is instructed to perform the following tasks:
1.) Remember the location of a file named "c:\customlisp\customlisp.dat".

(setq fil "c:\\customlisp\\CustomLisp.Dat")
2.) Open the file remembered in step 1 and read each line resulting in a count of how many files are listed in the file known as "customlisp.dat"
(setq x (open fil "r") ct 0)
(while (read-line x)
(setq ct (1+ ct))
(close x)
3.) Re-open the "customlisp.dat" file, and use the count saved in the variable "ct" to verify that we haven't reached the end of the list, begin loading each file listed by reading the filename from "customlisp.dat" and verifying that the file exists and can be found on the network. This loop uses the visual lisp sub routine: "vl-file-systime". If the file cannot be found, there will be information written to the command line telling the user who to contact to correct this problem (this is stored in the variable "ccinfo"). When the file is found, it will be loaded. Note: each file has its own prompt to let the user know that the file has been correctly loaded.
(setq y (open fil "r") rt 0)
(setq ccinfo "... Contact CAD Coordinator #4855")
(while (<>
(setq filenom (read-line y))
(if (/= (vl-file-systime filenom) nil)
(load (findfile filenom)
(strcat "\n" filenom " not found " ccinfo)
(prompt (strcat "\n" filenom " not found " ccinfo))
(setq rt (1+ rt))
(close y)
That is all that is required to modularize and automatically load as many custom lisp routines as you desire.
I hope you put this to use in your own environments. If you want a method with a little more functionality and features, learn how to fully modularize this loading routine using the Registry by reading my "Controlling Customization" series which started in the November 2004 issue of AUGI's HotNews newsletter. The latest article (the third in the series) should be released any day now.

No comments: