Part Six - Spiral


In Part One, I said that any object snap settings had to be turned off in order to use the spiral program. In this section, I will remove that restriction. We will have the spiral program save the original object snap settings, turn them all off, and once we draw the spiral, restore the object snap settings to the original value.

I added a couple of other lines to clean things up a bit.

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

Analyze Code


Object Snap Settings

...
 (setq #OSMODE (getvar "osmode"))
 (setvar "osmode" 0)
...

I added these two lines before the code that executes the spline command. Check near the end of the file.

>(setq #OSMODE (getvar "osmode"))
The current settings of the AutoCAD osmode system variable is stored in the #OSMODE lisp variable. Notice that I also added the #OSMODE variable to the list of local variables in the first line of the spiral program.

>(setvar "osmode" 0)
This line sets the osmode system variable to 0. That will turn off all object snap settings. Now the spline command will not be confused by trying to snap to other points. It will use the exact list of points that the spiral program is feeding to it.


Supress Command Echoing

...
 (setvar "cmdecho" 0)
...

I also added this line before the code that executes the spline command. Check near the end of the file.

The cmdecho system variable was set to 0 to instruct AutoCAD not to echo command prompts at the console while running lisp functions. Since all user input is provided by the spiral command, we do not need these prompts. Turning them off makes the program run "cleaner".


Restore Object Snap

...
  (if #OSMODE
   (setvar "osmode" #OSMODE))
...
 (setvar "osmode" #OSMODE)
...

It is not good practice to start changing system settings that the users might prefer to leave as is. A courteous program will always attempt to leave them the way that the user had them set. Most users do not care about the cmdecho system variable since it is for program use. But we will reset the osmode system variable.

>>(if #OSMODE (setvar "osmode" #OSMODE))
This line of code is used inside the *error* function. If an error occurs, and the #OSMODE variable was set, the osmode system variable will be reset to the original value.

>(setvar "osmode" #OSMODE)
This line of code is placed near the end of the program, after the code that draws the spiral. It resets the osmode system variable to the original value.


Simplify Code

I like to keep code as simple as possible. It is partly a holdover from when I was programming for earlier versions of AutoCAD that had a fairly restrictive code space. Current versions of AutoLisp have dynamic memory management and no program size restrictions. Even so, I often use abbreviated variable names to speed typing, shorten line lengths, and make the final program smaller.

This last version of the spiral program has no functional changes, but it does include shorter variable names. You can view this final version of spiral-5.lsp or spiral-5.txt and look at the code and try it out.

1  2  3  4  5  6