CADTutor
5th May 2004, 10:31 am
How to use the LISP routines in this archive
All of the lisp code posted on this bulletin board may be run on your own installation of AutoCAD. The basic process is pretty simple and is set out below. There are 3 main steps, creating the lisp file, loading the lisp file and running the lisp routine.
Note: AutoLISP routines will only run on full versions of AutoCAD, they will not run on AutoCAD LT.
Creating the lisp file
Copy and paste all of the text in the Code: box into Windows Notepad. Take care not to miss anything out. Below is an example:
(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> : "))
(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, you should have something like this:
http://www.cadimage.net/cadtutor/lisp/lisp-01.gif
You must now save the file. You can call it whatever you like as long as it has a .LSP file extension but it is always a good idea to give the file the same name at the lisp routine to avoid confusion. You will always find the name of the routine preceded with a c: at the beginning of the code. In the example above, you will see that that the routine is called "zone". So, in this case, the file should be saved as zone.lsp.
Loading the lisp file
Next, open AutoCAD and select Toolshttp://www.cadimage.net/cadtutor/submenu.gifAutoLISPhttp://www.cadimage.net/cadtutor/submenu.gifLoad... from the pull-down menu. You will see the dialogue box shown below:
http://www.cadimage.net/cadtutor/lisp/lisp-02.gif
Use the following sequence to load zone.lsp:
1. Navigate to the folder where you saved the lisp file.
2. Select the file you want from the list.
3. Click the Load button.
If all went well, you will see a message saying "zone.lsp successfully loaded".
4. Click the Close button to close the dialogue box.
Running the lisp routine
Once the lisp file is loaded, you can run the routine from the command line. The routine is run simply by entering its name. In this example, enter zone at the command line. Remember, the routine name is the bit following the c: near the beginning of the code.
Tip: You may also load the lisp file by dragging-and-dropping the file icon onto the AutoCAD drawing area.
All of the lisp code posted on this bulletin board may be run on your own installation of AutoCAD. The basic process is pretty simple and is set out below. There are 3 main steps, creating the lisp file, loading the lisp file and running the lisp routine.
Note: AutoLISP routines will only run on full versions of AutoCAD, they will not run on AutoCAD LT.
Creating the lisp file
Copy and paste all of the text in the Code: box into Windows Notepad. Take care not to miss anything out. Below is an example:
(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> : "))
(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, you should have something like this:
http://www.cadimage.net/cadtutor/lisp/lisp-01.gif
You must now save the file. You can call it whatever you like as long as it has a .LSP file extension but it is always a good idea to give the file the same name at the lisp routine to avoid confusion. You will always find the name of the routine preceded with a c: at the beginning of the code. In the example above, you will see that that the routine is called "zone". So, in this case, the file should be saved as zone.lsp.
Loading the lisp file
Next, open AutoCAD and select Toolshttp://www.cadimage.net/cadtutor/submenu.gifAutoLISPhttp://www.cadimage.net/cadtutor/submenu.gifLoad... from the pull-down menu. You will see the dialogue box shown below:
http://www.cadimage.net/cadtutor/lisp/lisp-02.gif
Use the following sequence to load zone.lsp:
1. Navigate to the folder where you saved the lisp file.
2. Select the file you want from the list.
3. Click the Load button.
If all went well, you will see a message saying "zone.lsp successfully loaded".
4. Click the Close button to close the dialogue box.
Running the lisp routine
Once the lisp file is loaded, you can run the routine from the command line. The routine is run simply by entering its name. In this example, enter zone at the command line. Remember, the routine name is the bit following the c: near the beginning of the code.
Tip: You may also load the lisp file by dragging-and-dropping the file icon onto the AutoCAD drawing area.