CADTutor: The best free help for AutoCAD on the web

AutoCAD FAQ

Knowledgebase Home | Glossary | Favorites | Login Knowledgebase Home | Glossary | Favorites | Login

How do I use an AutoLISP routine?

Article Details

Last Updated
3rd o May, 2009

Original Author
Alan Cullen

User Opinions (16 votes)
43% thumbs up 56% thumbs down

How would you rate this answer?
Helpful
Not helpful

AutoLISP is AutoCAD's built-in programming language. It can be used to create new commands or to redefine existing ones. AutoLISP is a very powerful customisation tool. This article aims to help you load AutoLISP routines that you have found on the web or that have been given to you.

You will usually find AutoLISP routines delivered in one of two formats. Either you may see a bunch of code that you can copy and paste, as in the example below, or you may be able to download a .LSP file. In the former case, the only tool you need to start using the routine (apart from AutoCAD) is Notepad. AutoLISP .LSP files are just ordinary text files. Looking at other people's AutoLISP code is a good way to learn so even if you downloaded a .LSP file, it's a good idea to take a quick look at the code. This will also help you identify the name of the routine (see below) in case it differs from the filename.

The basic process is pretty simple and is set out below. There are 3 main steps (or just 2 if you have downloaded a .LSP file), creating the lisp file, loading the lisp file and running the lisp routine.

Note: AutoLISP routines will only run in full versions of AutoCAD, they will not run in AutoCAD LT (without the addition of a 3rd party add-on, such as LT-Extender).

Creating the lisp file

Copy and paste all of the text in the routine into Windows Notepad. Take care not to miss anything out.


(defun c:zone ( / ss la rv i tv op en)    (while (not ss)           (princ "\nPick any object on the required layer: ")           (setq ss (ssget)))    (initget "Length Area")    (setq rv (getkword "\nWould you like to measure Length/Area <Area>: "))    (and (not rv)         (setq rv "Area"))    (setq la (cdr (assoc 8 (entget (ssname ss 0))))          ss (ssget "X" (list (cons 0 "*POLYLINE")                              (cons 8 la)))           i (sslength ss)          tv 0          op 0)    (while (not (minusp (setq i (1- i))))           (setq en (ssname ss i))           (command "_.AREA" "_E" en)           (cond ((= rv "Length")                  (setq tv (+ tv (getvar "PERIMETER"))))                 (T                  (setq tv (+ tv (getvar "AREA")))                  (if (/= (logand (cdr (assoc 70 (entget en))) 1) 1)                      (setq op (1+ op))))))    (princ (strcat "\nTotal " rv                   " for layer " la                   " = " (rtos tv 2 2)                   " in " (itoa (sslength ss)) " polylines\n"                   (if (/= rv "Length")                       (strcat (itoa op) " with open polylines") "")))    (prin1))

 

When you have pasted the code into Notepad, it should look something like this:

Now save the file. Give it an appropriate filename and make sure it has a .LSP file extension (make sure you set the Save as type: dropdown to "All Files"). It is usually a good idea to give the file the same name as the lisp routine to avoid confusion. The name of the routine will always be preceded with a c: at the beginning of the code. In the example above, the routine is called "zone". So, in this case, the file should be saved as zone.lsp (or a name to indicate what the routine does).

Loading the lisp file

There are a few different ways to load a lisp routine :

Method 1

Open AutoCAD and select the menu ToolsAutoLISPLoad Application…. The dialogue box shown below will appear :

Use the following sequence to load zone.lsp:

  1. Navigate to the folder where the lisp file was saved.
  2. Select the required file from the list.
  3. Click the Load button. If all went well, a message saying "zone.lsp successfully loaded" will appear.
  4. Click the Close button to close the dialogue box.

Method 2

Put the lisp file in a folder that Autocad can find (ToolsOptionsFilesSupport File Search Path) and type (load "zone") on the command line. Provided the file is somewhere on the search path, it will be loaded.

Method 3

Drag the lisp file from Windows Explorer and drop it straight into the drawing.

Running the AutoLISP routine

Once the lisp file is loaded, it can be run from the command line. The routine is run simply by entering the command name (not the file name). The command name is that text following (defun c:. In this example, enter zone at the command line.

Resources

If you'd like to learn more about AutoLISP or fancy writing a routine of your own, check out the AfraLisp AutoLISP Tutorials.

You'll find some useful AutoLISP routines in our AutoLISP Archive.

Did you know? LISP stands for LISt Processor

Visitor Comments

  1. Comment #1 (Posted by imran )
    i am greatful for this page (Thank you!-080319-Tiger)
  2. Comment #2 (Posted by Gary )
    excellent write up, I look forward to reading the rest of them. (Thank You! - Tiger - 8 July 08)
  3. Comment #3 (Posted by irneb )
    Great explanation. Could I just ask that you add stuff like DCL (which needs to be in the search path). Maybe also include FAS & VLX files? (Thanks and Under consideration - Tiger 081110)
  4. Comment #4 (Posted by Ranjit )
    Thaks for giving such nice information before reading this file i donot know to load lisp file this is very valuable iformation for me................Thak u once again (Thank You - Tiger 090105)

Post Comment for "How do I use an AutoLISP routine?"

To post a comment for this article, simply complete the form below. Fields marked with an asterisk are required.
   Your Name:
   Email Address:
* Your Comment:
* Enter the code below:
 

Related Articles

Attachments

No attachments were found.

Continue
Creative Commons Licence
Powered by Interspire Knowledge Manager