AutoLISP - AutoCAD and IntelliCAD's programming language

 

LISP is an acronym for LISt Processing, a simple, yet deceptively powerful computer language. It was developed in the 1960's and was first used to control powerful telescopes for astronomy. Not only is this useful for astronomy, it becomes a very sensible choice for Computer Aided Design where we are dealing with the placement of entities in drawings at very tightly defined coordinate positions.

As an example, it is possible to use lisp to read a line of text in a file describing the x, y and z coordinates of a point in space. Lisp can interpret the information, store the position of the point in space and other commands can then be called to create useful geometry.

AutoLISP

AutoLISP is Autodesk's implementation of the lisp programming language. The major advantage of using AutoLISP in your AutoCAD or IntelliCAD drawings is that it enables the creation of new commands in AutoCAD.

Click here to see an example of AutoLISP in action. We have used AutoLISP in the IntelliCAD environment to create a new command (LBL) which enables us to label a plant symbol with a particular font and text height.

Other examples

We have used AutoLISP to create a new command which, when implemented, reads a huge file which contained the XYZ coordinates of light poles in a local council area. LISP then produced a drawing showing a point at every position and XREFERENCED a cadastral drawing showing the council boundaries. This enabled council to make sure that they were only paying for lighting in their area.

Sample AutoLISP programs

Many sample AutoLISP programs are available on the Internet. The instructions below provide some guidance on testing them out.

Testing an unfamiliar AutoLISP program (simple approach)

Copy the AutoLISP file to the directory in which your drawings normally reside. 

If you have never used the AutoLISP program before, start Windows Notepad and open the LISP file to view it. AutoLISP programs have the .LSP extension.

The LISP program may read like 'gobbledgook', but somewhere in it, usually at the beginning, some instruction on how to use the program may be given. Print out the instruction part of the file. The amount of instruction will vary according to the author.

Operation

Most AutoLISP programs will create a new command when loaded which when executed, will carry out some (new) function. An example is shown below.

**(defun c:chgsc ()
(initget "10 20 50 100 200")
(setq newdwgsc (getkword "What is your scale: "))
(setq #dwgsc (atof newdwgsc))
(command "INSERT" (strcat "1-" newdwgsc) "0,0" "1.0" "1.0" "0.0")
(command "DIMSTYLE" "R" (strcat "1-" newdwgsc))
)

Look at the beginning of the file for a line similar to that marked with the * *.

The part that says c:chgsc indicates that the author has decided to create a new command to start the program CHGSC - short for change scale. 

Syntax

Either upper of lower case will work in AutoLISP, it's not case sensitive.

Once the LISP file is loaded a new command is created; in this case CHGSC.

If the AutoLISP program reads a data file, then make sure that the required data file also resides in the same directory. 

Loading the AutoLISP program

Any AutoLISP program can be loaded from the command line using the following syntax:

(load "filename")

The  brackets are essential.

This syntax will only work if the drawing file and the AutoLISP file are saved in the same folder. If the LISP file was in another folder (directory), you need to type the full path. E.g. To load a LISP file called VERIFY.LSP in a folder called Verify on the C drive, you would type:

      (load "c:/verify/verify.lsp")
Note the use of the forward slash. This is different to Windows paths used when we are setting the path command in the PREFERENCES dialog box. 

Once the LISP file has been loaded, you can then type your new command.

Loading AutoLISP programs - Method 2:

Create a folder for all of your AutoLISP programs so that they are all kept in one place. E.g. create a folder called C:/AutoLISP

Load the program with the syntax:

(load "C:/AutoLISP/filename.lsp")

Loading AutoLISP programs - Method 3

Edit the file ACAD.LSP (or ICAD.LSP) using Windows Notepad and add the "load" command that you have used above into this file.

When AutoCAD or IntelliCAD starts, all these LISP files will be loaded, ready to run. An example of part of an ICAD.LSP file is shown below. 

(defun S::STARTUP()
(setvar "CMDECHO" 0)
(setvar "MENUECHO" 0)
; 1. Load special lisp functions
; (load (strcat drive "icad_ase/descad.lsp")) johns mod
(load "c:/icad_ase/descad.lsp")
(load "c:/icad_ase/lines.lsp")
(load "c:/icad_ase/cloud1.lsp")
(load "c:/icad_ase/chgsc.lsp") ; change scale 
(load "c:/icad_ase/zc.LSP") ;scales and centers view port
(load "c:/icad_ase/pinter.LSP") ;interrogates viewport in paper space
; (load "c:/icad_ase/land/trees/shadow.LSP") ;applies shadowing
(LOAD "C:/ICAD_ASE/JP.LSP") ; JOIN POLYLINES
(defun S::STARTUP()
(setvar "CMDECHO" 0)
(setvar "MENUECHO" 0)
; 1. Load special lisp functions
(load "c:/icad_ase/descad.lsp")
(load "c:/icad_ase/lines.lsp")
(load "c:/icad_ase/cloud1.lsp")
(load "c:/icad_ase/chgsc.lsp") ; change scale 
(load "c:/icad_ase/zc.LSP") ;scales and centers view port
(load "c:/icad_ase/pinter.LSP") ;interrogates viewport in paper space
(load "c:/icad_ase/land/trees/shadow.LSP") ;applies shadowing
(LOAD "C:/ICAD_ASE/JP.LSP") ; JOIN POLYLINES

Error messages

It is not uncommon for there to be errors in LISP files, especially those downloaded free of charge from the Internet. 

Copyright issues

If you do use a program created by someone else, keep that person's copyright notice intact in the file and acknowledge their effort.

We are in the process of developing an online course teaching LISP programming. Please send an email message to info@designcad.com.au if you are interested.