Part Four - Spiral


I added some new lines of code to the spiral program to insure that the user enters valid data. These are simple initget function calls. I also provided some default values for the diameter, pitch, and number of coils. This makes the program easier to use. You should view the updated version of spiral-2.lsp or spiral-2.txt and look at the code and try it out.

Analyze Code


Provide Default Values

...
 (if (not V:SPIRAL-DIAMETER)
  (setq V:SPIRAL-DIAMETER 1.0))
 (initget 6)
 (setq #DIAMETER (getreal
   (strcat "\nDiameter <" (rtos V:SPIRAL-DIAMETER) ">: ")))
 (if (not #DIAMETER)
  (setq #DIAMETER V:SPIRAL-DIAMETER)
  (setq V:SPIRAL-DIAMETER #DIAMETER))
...

V:SPIRAL-DIAMETER is a variable that is going to be left as a global variables My convention it to prefix global variables with a "V:". Since they are global, they must have a unique name to make sure that another program that they were not intended for does not accidentally use them. That is why the names I used are long and descriptive. Conversely, local variables can be as short and cryptic as needed since they only apply to the function that you use them in. I will use that fact in subsequent parts of this column to shorten some of the local variable names.

Since V:SPIRAL-DIAMETER will be global, it must not be included in the local variable list.

>(if (not V:SPIRAL-DIAMETER) (setq V:SPIRAL-DIAMETER 1.0))
The "if" function check to see if the V:SPIRAL-DIAMETER exists. If it does not, the program sets it to a common value that the user might want to try. If V:SPIRAL-DIAMETER does exist, than the value is left alone. V:SPIRAL-DIAMETER will become the default value that we present the user when we prompt them for the number of coils to draw.

>(initget 6)
The initget function is used to restrict user input. A bit setting of 2 disallows zero input and a bit setting of 4 disallows negative input. The 6 argument bit is merely the total of 2 and 4. So the program will only accept a positive value greater than 0.

>(setq #DIAMETER (getreal
>>>(strcat "\nDiameter <" (rtos V:SPIRAL-DIAMETER) ">: ")))

The rtos function is used to convert the real number to a string in order to show the user what the default value is. The strcat function is used to concatenate the string with the prompt for user input.

>(if (not #DIAMETER)
>>(setq #DIAMETER V:SPIRAL-DIAMETER)
>>(setq V:SPIRAL-DIAMETER #DIAMETER))

If the user had pressed [Enter] in response to the prior diameter prompt, then the #DIAMETER value is nil, in which case the very next line sets #DIAMETER to the default value, V:SPIRAL-DIAMETER. If the user entered a valid value, the third line above sets the default value V:SPIRAL-DIAMETER to what the user entered. So the next time the program is used in this drawing session, the new value is presented to the user as a default, and will not have to be reentered. This process requires a few additional lines of code but it makes the program easier to use and saves the user time.

I used two other default global variables, V:SPIRAL-COILS and V:SPIRAL-PITCH. They were handled exactly the same as V:SPIRAL-DIAMETER, except that the V:SPIRAL-COILS variable was converted to a string using the itoa or "integer-to-ascii" functions.


Initget for Centerpoint

...
 (initget 1)
...

It is often not very useful to have a default for point selections. So I did not give the prompt for the centerpoint a default value. But I did want to insure that the user would input a valid point. This initget function call comes just before the prompt for the centerpoint. A bit value of 1 disallows null input, so the user must input some valid point value, or correctly pick a point on the screen.

In Part Five, I'll add an error handler and combine setq functions.


1  2  3  4  5  6