| Knowledgebase Home | Glossary | Favorites | Login |
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 fileCopy 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 fileThere are a few different ways to load a lisp routine : Method 1Open AutoCAD and select the menu . The dialogue box shown below will appear :
Use the following sequence to load zone.lsp:
Method 2Put the lisp file in a folder that Autocad can find () and type (load "zone") on the command line. Provided the file is somewhere on the search path, it will be loaded. Method 3Drag the lisp file from Windows Explorer and drop it straight into the drawing. Running the AutoLISP routineOnce 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. ResourcesIf 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 |