Jump to content

How to use the LISP routines in this archive

Recommended Posts


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")))) 
                (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") ""))) 

When you have pasted the code into Notepad, 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 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 Toolssubmenu.gifAutoLISPsubmenu.gifLoad... from the pull-down menu. You will see the dialogue box 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 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.

Share this post

Link to post
Share on other sites
Lee Mac

Additional Loading Instructions


In addition to the above advice, there are a few other filetypes to be aware of when delving into the world of LISP programming.




You may discover that some programs are posted in the form of either a .VLX or .FAS file. These are programs that have been compiled into one file to make them not only easier to load (with only one file to deal with), but also more secure as far as code protection is concerned.


To load a .VLX or .FAS file, just save the file to a known location, and follow the instructions in the above post, as if you were dealing with a LISP file. Of course, the syntax to invoke the function is not readily available, but may be either provided by the program developer, or appear at the command line upon loading the file.


DCL Files


DCL (Dialog Control Language) gives the user the ability to create dialog boxes with relative ease. These files run in conjunction with LISP files, and may be compiled with the relevant LISP file into a VLX or FAS file.


DCL language looks like this:


// Increment Numerical Text Sign Selector

adder : dialog { label = "Specify Increment Direction";
     : text { label = ""; key = "sel_text"; alignment = centered; }
     : row {
        : button { label = "+1"; key = "sel_add"; fixed_width = true; mnemonic = "+"; }
        : button { label = "-1"; key = "sel_sub"; fixed_width = true; mnemonic = "-"; }
     } // row
} // dialog 


To run a program that uses a DCL file, save the provided DCL file into your AutoCAD Search Path.


The AutoCAD Search Path is normally found:


C:\Program Files\AutoCAD \Support\


But additonal Search Path locations may be added by going to:


Tools > Options > Files (tab) > Support File Search Path


And adding a new filepath.


The LISP file associated with the DCL file can be loaded and run as normal, using the instructions provided in the above post.


If you are still puzzled, search the forums, or our FAQ for more information.

Share this post

Link to post
Share on other sites
Lee Mac

"; error: no function definition: vlax-get-acad-object"


"; error: no function definition: vlax-ename->vla-object"



If you find yourself receiving either of these errors, this is an indication that the Visual LISP ActiveX functions required by some programs have not been loaded on your system.


The Visual LISP ActiveX functions can be loaded using the (vl-load-com) function.


This function need only be called once per session to ensure the functions are available throughout the drawing session, hence many users will have (vl-load-com) located at the top of their ACADDOC.lsp / ACAD.lsp customisation files so that it may be loaded on startup. For this reason, a developer may not notice the omission of this function in their program.


If you are receiving one of the above error messages when running a program, simply add (vl-load-com) on a new-line at the top of the relevant LISP file.




Code without (vl-load-com) :

(defun c:test ( / )


(defun c:test ( / )

If you are still stuck, search the forums/FAQ for more help.

Edited by Lee Mac

Share this post

Link to post
Share on other sites

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.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...