Part Three - Spiral


I added some new lines of code to the spiral program to correct the start and end tangency points of the spiral. You should view the updated version:-

spiral-1.lsp
spiral-1.txt

Look at the code and try it out.

Analyze Code

Portions of the column showing code that was used in the earlier version of the spiral program code will be in red.


New Variables and Calculations

...
 (setq #ANGLE-TAN (/ pi 500))
 (setq #Z-TAN (/ #PITCH 1000))
 (setq #TAN-0 (polar #POINT-0 (- #ANGLE-TAN) #RADIUS))
 (setq #TAN-0 (list (car #TAN-0)
                    (cadr #TAN-0)
                    (- (caddr #TAN-0) #Z-TAN)))
 (setq #Z-END (caddr (last #LIST)))
 (setq #TAN-1 (polar #POINT-0 #ANGLE-TAN #RADIUS))
 (setq #TAN-1 (list (car #TAN-1)
                    (cadr #TAN-1)
                    (+ #Z-END #Z-TAN)))
...

The AutoCAD spline command asks for tangency directions for the start and endpoints of splines. In the earlier version of SPIRAL, we used the defaults rather than calculate the correct tangency values. I used a value of 16 for the resolution, which is high enough to give a fairly accurate spiral. At much higher resolution values, the default tangency points would actually be fairly good. But speed of the spiral program would needlessly suffer. So we will use a high resolution of 1/1000th of a circle to calculate just the start and end tangency points. You could use 1/100 or 1/10000. But 1/1000 is good enough. All other points will remain at a resolution of 1/16th of a circle.

>(setq #ANGLE-TAN (/ pi 500))
Since 2pi radians make up a circle, I divided pi by 500 to get the correct resolution of 1/1000. #ANGLE-TAN is the angle component of the polar coordinates of the start and end tangency points.

>(setq #Z-TAN (/ #PITCH 1000))
#Z-TAN is the incremental distance between start or endpoints of the spiral in the Z axis and the tangency points. So rather than divide by 16 as was done for the coils, I divided by 1000.

>(setq #TAN-0 (polar #POINT-0 (- #ANGLE-TAN) #RADIUS))
>(setq #TAN-0 (list (car #TAN-0) (cadr #TAN-0) (- (caddr #TAN-0) #Z-TAN)))

#TAN-0 is the tangency point for the start of the spline. It is calculated in the same manner as #POINT-0 was in Part Two. The polar coordinates of a point that would come just before the first point in the list of points that will form the spiral. Since this point comes before the first point in the list, the #Z-TAN value is subtracted from the Z coordinate of the XYZ tangency point.

>(setq #Z-END (caddr (last #LIST)))
#Z-END is the last point in the spiral. I need to calculate a tangency point that will come right after this point.

>(setq #TAN-1 (polar #POINT-0 #ANGLE-TAN #RADIUS))
>(setq #TAN-1 (list (car #TAN-1) (cadr #TAN-1) (+ #Z-END #Z-TAN)))

#TAN-1 is the tangency point for the end of the spiral. It is calculated in the same manner as #TAN-0 above, but I substituted values to give me a point after the end of the spiral rather than before the start. Since this point comes after the last point in the list, the #Z-TAN value is added to the Z coordinate of the XYZ tangency point.


Use of New Tangency Points in Spline

...
 (command "" "" "")
...
...
 (command "" #TAN-0 #TAN-1)
...

I replaced the above line shown in red from spiral-0.lsp with the next line show in green. The variables #TAN-0 and #TAN-1 replaced the "" and "" which originally represented [Enter] to accept the default values. Now the #TAN-0 and #TAN-1 values are used as the input for the start and end tangency points.


Make Variables Local

(defun C:SPIRAL (/ #DIAMETER #PITCH)
...
(defun C:SPIRAL (/ #ANGLE #ANGLE-DELTA #ANGLE-TAN
 #COILS #DIAMETER #LIST #PITCH #POINT #POINT-0
 #POINT-1 #RADIUS #RESOLUTION #SEGMENTS #TAN-0
 #TAN-1 #Z #Z-DELTA #Z-END #Z-TAN)
...

In Part Two, I left most of the variable names out of the "local" variable list. Leaving the variables global can help in debugging but I decided now would be a good time to start cleaning things up a bit.

...
 (setq #LIST nil)
...

Since the #LIST variable is now local it gets reset to nil inside the program whenever it is executed. So I no longer need the above line that explicitly set it to nil. So this line is no longer included in the spiral program.


You can draw spirals using the same input applied to spiral-0.lsp and spiral-1.lsp and overlay them. When you zoom in, you will see the improvements to the start and end points.

Part Four will detail the modifications needed to insure correct user input and make the program easier to use.


1  2  3  4  5  6