Home page

Our next meeting will be held on Thursday, July 17, 2008.

For more information (such as meeting topics or directions) please click the Calendar link above.

Eastern Virginia AutoCAD Users' Group
EVAUG information Upcoming events Become a member! EVAUG officer profiles More EVAUG content
Resources Past meeting minutes Tutorial Gallery - pictures of EVAUG events Local employment opportunities

AutoLISP Basic Customization

Getting started with AutoLISP is easiest and immediately useful if you use it to issue commands in the same way you use menus and toolbars.

Basic Syntax

An affectionate name given to LISP by programmers is Lost In Stupid Parenthesis. Guess what syntax LISP uses to delineate function calls. Yup. Parenthesis. If that didn't make no sense to you, here's an example of a LISP function call:

(setq temp (and (getvar "OSMODE") 16384))

Basically you call a function to do something. The function is the first word inside the parenthesis. Take the setq function above as an example. (setq) stores a value into a variable so you can use that value later. Its signature is (setq variable value).

Signature describes the function syntax, which is basically the name of the function and the arguments you pass to it so that it can do what it does.

For setq here's the breakdown:

  • setq - the function name.
  • variable - pass the name of a variable which will be set to the value you pass.
  • value - the value the variable will be set to. In LISP, this can be a number, string, or something more complicated.

That's the gist of programming in LISP. I think that's how gist is spelled.

Creating Functions

Here's how to create your own function. Once you create a function, you can call it from LISP. If you use a special syntax, you can even call the function from the AutoCAD command line, which is where we're headed in this lesson.

Use the (defun) function to create your own function.

(defun func_name ( arg1 arg2 / temp1 temp2)

(some functions)

)

  • func_name - put the name of your function here. Don't use the name of an existing function. That would be bad.
  • arg1, arg2 - these are arguments that you would pass to your function for it to do something with. You don't have to have any arguments at all or you have as many as a whole bunch.
  • temp1, temp2 - these are local variables that exist only inside your function. Use them to store temporary values for junk. Again, you can have no variables or a bunch.
  • some functions - you use as many functions as you need. Make sure each one starts and ends with a parenthesis. Also notice that the ending parenthesis for the defun function comes at the end of your function.

c: Functions

If you create a function whose name begins with c:, then it can be called from the command line in AutoCAD. This is one way to create some quick command line short cuts:

(defun c:zw ( / )

(command "zoom" "window")

)

Once this function is loaded, the user can do a zoom window by punching ZW at the command line. Minor enhancement? Yes. Lazy? Oh yeah. But these things add up, considering you'll run hundreds or thousands of commands each week. It gets even better when you really put together some intelligent functions.

command

The command function is a great place to start your LISP customization. command sends commands and arguments to the command line after evaluating them to see if they're correct. Using command is very much like using scripts or menus.

Just place your AutoCAD commands and their arguments inside the function's parenthesis, each one in double quotes and separated like this:

(command "zoom" "window")

This line simply issues the ZOOM command and then the WINDOW argument. If the LISP code ends in the middle of a command then the user can finish it manually.

Variables

As mentioned before, you use the setq function to store values into variables. You can store any value you want in a variable and then reference the variable when you need that value. Variables can hold integers, floating point numbers (ones with decimal points), strings, or even AutoCAD entities. Here's a useful example using the ssget function:

(defun c:CM ( / ss )

(princ "COPY MULTIPLE\n")

(setq ss (ssget))

(command "COPY" ss "" "M" "\\")

(princ)

)

Note the line (setq ss (ssget)). ss is defined as a temporary variable on the first line, so it only is usable within the c:CM custom function. This line runs the ssget function, which lets the user select CAD entities and returns those entities as a selection set, which are then stored into the variable ss.

On the next line, command is used to issue the COPY command. Remember that the first thing you do when you use the COPY command is to select the entities to copy. Here the user has already selected the entities and those went into the ss variable.

Remeber also that you have to hit the enter or space key one last time to finish selecting, which is exactly what the "" after the ss does. Use "" to supply an enter keystroke during a command.

The command function ends with entering the M option (Multiple copy) for the user and then pausing for user input to select the base point. A backslash in the command function pauses the function to let the user supply the needed info. A backslash in LISP, however, is an escape code for strings which lets you add special characters to your strings. Using a double backslash (//) in a string actually gives you the backslash character you need to pause for user input.

Since the function ends in the middle of the COPY command (with the entities selected, Multiple mode, and the base point set) the user is free to finish using the COPY Multiple command by placing multiple copies of the entities wherever s/he chooses.

The reason the selection set was used is because it pauses the function until the user is finished selecting entities. If a single pause was used, then the user could only pick one entity, or if the user missed then a selection window is started, but then the function continues and the whole thing goes to hell. That's right, Hell! Muhahahahaha!

AutoCAD System Settings

You might find it helpful to control some of the AutoCAD system settings using LISP. To retrieve the value of a setting, use the getvar function. To set the value of a setting, use the setvar function. Here's a function that lets you toggle the UCS icon on/off:

(defun c:ui ( / )

(if (= (getvar "UCSICON") 0)

(setvar "UCSICON" 1)

(setvar "UCSICON" 0)

)

(princ)

)

The setting UCSICON stores a 1 if the icon is on or a 0 if it's off. SOooo, this function uses the (if) and (=) functions to test if UCSICON is equal to 0. If it is, then the first setvar line is run and UCSICON is then set to 1 & the icon is displayed. If it was set to 1 then the second setvar line would be run and UCSICON would be set to 0 & the icon turned off.

You only supply the setting name to getvar to have it return the setting's value. You supply the name and the new value to setvar to give the setting a new value.

 

 

Loading LISP Files