Jump to content

How to use the LISP routines in this archive


CADTutor

Recommended Posts

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:

 

zone-text.png.5892e596eb93fb002974e7b50d5de6c1.png

 

 

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:

 

zone-ribbon.png.dbfe9944b6bc9201bce47c8415c58c46.png

 

Alternatively, you can enter appload at the command line. Both methods will display the Load/Unload Applications dialogue box as shown below.

 

zone-load.png.14bca493ba08dc3da6af0b7e2f92ebba.png

 

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.

 

Link to comment
Share on other sites

  • 5 years later...

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.

 

VLX/FAS Files

 

You may discover that some programs are .vlx or .fas files. These are programs which have been compiled using the Visual LISP IDE in order to encrypt the source code and also optimise evaluation of the program, as function definitions are essentially evaluated in-line.

 

To load a .vlx or .fas file, simply save the file to a known location, and follow the instructions in the above post. Of course, since the program file is encrypted, the command syntax to invoke the program is not readily available, but may be either provided by the program developer, or may appear at the AutoCAD command line upon loading the file.

 

Disclaimer: Given that .vlx and .fas files are not open-source, the operations performed by a program cannot be determined ahead of time. As such, it is strongly recommended that you only load & run encrypted programs from trusted authors.

 

DCL Files

 

Dialog Control Language (DCL) gives the user the ability to create dialog boxes with relative ease. These files run in conjunction with AutoLISP files, and may be compiled with the relevant AutoLISP file into a single .vlx 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 = "-"; 
        }
     }
    ok_only;
}

To run a program which uses a DCL file, first save the provided DCL file to an AutoCAD Support File Search Path.

 

The AutoLISP 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.

Edited by Lee Mac
Link to comment
Share on other sites

  • 11 months later...

"; 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 ActiveX component of the Visual LISP API has not been loaded prior to running a program which relies on this function library.

 

The Visual LISP ActiveX functions may 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 the expression is automatically evaluated 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, first try adding (vl-load-com) on a new-line at the top of the relevant LISP file.

 

Example:

 

Code without (vl-load-com):

(defun c:test ( / )
   (vlax-get-acad-object)
   (princ)
)

Modified:

(vl-load-com)
(defun c:test ( / )
   (vlax-get-acad-object)
   (princ)
)

If you still receive either of the above errors after reloading the program equipped with (vl-load-com) consider performing a repair or reinstallation of your AutoCAD software, as the Visual LISP ActiveX component can occasionally become corrupted following the installation of an AutoCAD patch.

 

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

Edited by Lee Mac
  • Like 1
Link to comment
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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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...