Part Five - Spiral


I added a simple error handler to catch any possible errors that the spiral program might generate. It is not really needed yet since we don't do anything that might need to be reset in an error handler. We will need it in Part Six. Also, with AutoCAD 2000 and later, errors no longer cause code to stream out to the console. So debugging at the command line is now more difficult when you want to see where an error occurred in the program. Even so, it is good practice to include the *error* function so that you have some control over how errors are handled.

A single setq function can have multiple pairs of arguments. So to improve efficiency, I combined all sequential setq functions

You should view the updated version of spiral-3.lsp or spiral-3.txt and look at the code and try it out.

Analyze Code


Include an *error* Function

...
 (defun *error* (%A)
  (cond
   ((= %A "Function cancelled") nil)
   (t (princ (strcat "\nerror: " %A "\007\n"))))
  (princ))
...

Any errors generated by AutoLISP are passed to a function named *error*. That function has a single argument, which is a string describing the error. The error function can do anything that you want; close any open files, terminate any active commands, reset any variables, or just alert the user.

I included *error* in the local variable list so that it is active only for the spiral program. That way other lisp programs can define their own error handler.

>(defun *error* (%A)
The *error* function is defined inside the C:SPIRAL function. So every time spiral is executed, my error handler is defined. My convention for argument variable is to prefix each with a % character. In this function, I used %A to pass the error string.

>>(cond
>>>((= %A "Function cancelled") nil)
>>>(t (princ (strcat "\nerror: " %A "\007\n"))))

The cond function first checks to see if the function was cancelled or interrupted by the user, and if so, simply stops the program. If some other error was encountered, causing a true or "t" condition, the princ function is used to display the error string.


Combine setq Functions

Example:
 (setq #ABC 2.5)
 (setq #STRING "ABCDEFG")
 (setq #INT 3)
 (setq #ABC 2.5 #STRING "ABCDEFG" #INT 3)
 (setq #ABC 2.5
       #STRING "ABCDEFG"
       #INT 3)

Each of the three sequences above performs the same function. The first sequence may be the easiest to understand but is the least efficient. When combining a small number of setq functions, I'll often use a single line as demonstrated in the second sequence above. Since I was combining many setq functions in the spiral program, I followed the example from the third sequence shown above.


Gracious Exit

...
 (princ))

Normally a completed lisp function or program returns the last value that it encounters. In order to have a program complete without displaying any more information, a princ function is used with no string argument. I added the last princ call to the spiral program. The program now ends cleanly.

In Part Six, I'll change the program so that it can work correctly regardless of the object snap settings. I'll also do a little more clean up.


1  2  3  4  5  6