Tuesday, April 25, 2006

Kitchen LISP

Last week we looked at editing the ACAD.PGP to create command aliases. However, PGP defined command aliases are limited; they cannot call command options, prompt for user input, or make decisions. For instance, you couldn't define an alias for Zoom Previous in the ACAD.PGP. Using AutoLISP for command aliases is like the ACAD.PGP on steroids. LISP can pass options to a command, make decisions, and pause for user input.

Create your own LISP shortcut library file and add it to your Startup Suite using the APPLOAD command. You can create this file with Notepad, just make sure that Notepad's Save as type drop down is set to All files instead of Text Documents (*.txt), or Notepad will add a .TXT extension to your new file. You can also create/edit your user LISP file with AutoCAD's built-in LISP editor by typing VLISP at the command line.

LISP syntax can be baffling, but command aliases are relatively easy to master. To define ZP as the command alias for ZOOM Previous, the syntax would be:

(defun C:ZP () (command ".ZOOM" "P"))

In this example we define a function (defun) ZP, the C: prefix means that the function can be used as an AutoCAD command. The (command) function passes "ZOOM" and the "Previous" option to the AutoCAD command line. Now you can type ZP at the command line for ZOOM Previous. Define additional command aliases similarly. Pay attention to the location of opening and closing parenthesis and use quotes as shown.

More Examples:

; Type "ZE" to ZOOM Extents plus a bit more
(defun C:ZE ()

(command ".ZOOM" "E" ".ZOOM" "0.95x"))

; Type "LE" to LENGTHEN with the Dynamic option
(defun C:LE () (command ".LENGTHEN" "DY"))


; Type "STR" to STRETCH with the

; Crossing Polygon option
(defun C:STR () (command ".STRETCH" "CP"))


; Type "FD" to toggle ON/OFF the gray
; background display of Fields
; this bit of code shows more of the power
; of AutoLISP. Reading it backward, we use
; (getvar) to get the current value of the
; FIELDDISPLAY system variable, then we
; subtract 1 with function (1-), take the
; absolute value with (abs), and finally
; pass the result to (setvar) to set the
; new value.
(defun C:FD ()
(setvar "FIELDDISPLAY"
(abs (1- (getvar "FIELDDISPLAY")))))


Notice the explanatory comments in the code. AutoLISP ignores text following a semi-colon.

There's virtually no limit to the amount of customization you can include in your personal LISP file. For more information on LISP functions and syntax, see the Developer Help under Additional Resources of the AutoCAD Help menu.

2 Comments:

At 11:03 AM , Anonymous Anonymous said...

I'm just getting back into Autocad after a 6 year hiatus because of my new job. My old company had a ACAD set up quite well with lots of neat commands, etc. already set, I got so used to them that I did not realize until now that they weren't defaults! Anyway, this entry is exactly what I was looking for on how to add a bunch of these quick commands that I remember.

thanks! can't wait to see what other useful stuff you share with the world!

 
At 9:18 AM , Anonymous Anonymous said...

Thanks for the tip -- very helpful!

- miranda in chicago

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home