CADTutor Posted May 5, 2004 Share Posted May 5, 2004 How to use the LISP routines on this site All of the lisp code posted on this forum 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. To use an application in a downloaded .LSP file: Skip to the section headed "Loading the lisp file" below. To use an application posted in a code block: Start here. Creating the lisp file Copy and paste all of the text in the code block into Windows Notepad or your favourite code editor. Take care not to miss anything out. Below is an example: ; This routine will calculate the length or area ; of all polylines on a selected layer. (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 your editor, you should have something like this: 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 "defun 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 the Drafting & Annotation workspace. On the Manage tab, you will find the Load button: Alternatively, you can enter appload at the command line. Both methods will display the Load/Unload Applications dialogue box as shown below. 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 you see a security warning, that just means the file is located outside of the normal AutoCAD search path. If you are sure the code is safe, go ahead and load it anyway. If all went well, you will see a message saying "zone.lsp successfully loaded" in the panel at the bottom of the dialogue box. 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 "defun 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. Quote Link to comment Share on other sites More sharing options...
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.