AutoLISP - A Simple Starting Point

What is AutoLISP? AutoLISP is a LISP based language that comes as part of AutoCAD computer aided drafting software. AutoLISP can interface with the drafting system to automate tasks, if you know the code. I know that there are a lot of real complex AutoLISP functions out there that probably make people think that they could never write one themselves, but I aim to change that. Hit the jump for a quick intro lesson to AutoLISP functions.

AutoLISP functions don't have to be complicated. A real simple AutoLISP function could literally just call a single command or replace a system variable. For example, I have function that I wrote to simple toggle my view from model space to paper space. The beauty of making that into a function is then you can assign a 2-key for that new command in your PGP file making it feel like a native function.

Writing AutoLISP is very simple and I think one can learn from example, as I did. One of the first functions I ever wrote was for a friend of mine back in 1998. All he wanted to be able to do was have a single command that performed a zoom-previous. So I wrote a command that called ZOOM and then selected PREVIOUS and that was it. That function looks like this:

;; function to perform a zoom previous

(defun c:zoom_previous() 
  (command "zoom" "p")
)

This AutoLISP function, called zoom_previous, has only one line in which I used the AutoLISP command function to call the AutoCAD command ZOOM. The AutoLISP command function calls commands exactly as they would be called from the command line in AutoCAD. Responses to the normal command line prompts for input to a command have to be included one after the other in the same order as you would be prompted for them in the command function. As you can see in my function I call ZOOM and the next prompt would be for the type of zoom I want to perform. To that I answer "p" for previous and that's it. As with calling that same command at the command line, the AutoCAD system would perform the zoom previous command and you'd be...zoomed previous.

This is about as simple as it gets. No programming logic structures and no variables being set. This is really more of a macro than anything. Now I'll demonstrate something slightly more complex. This is the code to toggle between model space and paper space. All you need to do to accomplish that is change the system variable, TILEMODE.

;; function to toggle the tilemode setting

(defun c:toggle_tilemode()
  (setq n_currentSetting (getvar "TILEMODE"))
  (setvar "TILEMODE" (+ 1 (* -1 n_currentSetting)))
  (princ)
)

This time things might get a little confusing because of the nature of LISP programming syntax. Line by line, this function first gets the value of the TILEMODE system variable and stores it in the variable, n_currentSetting. The second line has a simple trick. If you are feeding a single function either a 1 or a 0 and you want the output of that function to always be the other of the two (output a 0 with input of 1 and vice versa) then all you have to do is multiply the input by -1 and then add 1 to the product. Run that through your head a few times and you'll see that it's fool-proof. In AutoLISP, math is done a little strangely. To add two numbers, the syntax is: (+ number1 number2). It's the same for all math functions. For division just remember that it's the first number that is divided by the second. If you forget just run some tests. LISP is also executed by the system from the inside out. So as you read from the left-to-right, every time you hit a new open paren, (, you kinda say, "ok, remember where I was, now evaluate what's inside of this set of parenthesis". So the first thing evaluated in the second line is (* -1 n_currentSetting) which multiplies the value of n_currentSetting by negative 1. Then the addition is evaluated and finally the setvar is evaluated. The final line is just a clean-up line that suppresses the output that is displayed at the end of a function. AutoLISP always echos the result of a function and in this case it would be the value that we set TILEMODE to in the second line of code. By adding (princ) to the end, that is suppressed and it exits quietly.

So there's a start. I'll be posting more and more of this stuff to get into more complex things and to post up some of the AutoLISP commands that I have made over the years. Stay tuned and if you have any questions feel free to drop them in the comments.

Syndicate content