AutoLISP Solutions: Automatically Generate a Series of Decimal Numbers15 Nov, 2005 By: Tony Hotchkiss Cadalyst
An automatic decimal numbering routine eases tedious typing.
This month's request came from Mr. Sven de Troch of Antwerp, Belgium. Sven asked for an automatic numbering routine with decimals to help with numbering brick positions in walls.
The AutoLISP Solution is DECIMAL-INC.LSP and DECIMAL-INC.DCL, which allows the user to enter a start number, an increment value, the precision (number of decimal places) and the text height from a dialog box. A series of numbers is then placed in the user-selected positions. The numbers may vary incrementally from zero to six decimal places. The default is two decimal places with an increment of 0.25 and a text height of 0.2 units
Get the Code
Download the DECIMAL-INC.LSP and DECIMAL-INC.DCL files from Cadalyst's CAD Tips site and save them in AutoCAD's Support directory. Use the Appload facility by selecting Tools / Load Application, then select the DECIMAL-INC.LSP program from where you stored it.
How to Use the DECIMAL-INC Code
After you load the code, the system prompts you to enter DIN to start the program. To see this prompt, you may need to set your Command window size to 3 lines by dragging the Command window splitter bar appropriately. If you are using AutoCAD 2006 and you are not using the Command line area, you can still just enter DIN to start. After you enter DIN, you will see the Decimal Precision dialog box (figure 1).
Figure 1. The Decimal Precision dialog box.
You may enter the start number, the increment value and the text height in the edit boxes, and you may select a precision value from the pop-up list (figure 2).
Figure 2. Precision choices.
Click OK in the dialog box to start adding numbers to the drawing. Figure 3 shows the start point prompt for AutoCAD 2006. For earlier versions of AutoCAD the prompt is shown at the Command line.
Figure 3. The start point prompt.
The program will then prompt you for the next point, and you may continue to add numbers simply by clicking a location on the screen until you press Enter or the Spacebar to stop incrementing the text. Figure 4 shows a sequence of generated numbers.
Figure 4. The result of decimal number increments.
The program was written in AutoCAD 2006, and it was tested in both AutoCAD 2004 and 2006. DECIMAL-INC.LSP starts as usual with my error handler and system variable functions. The function DECIMAL-INC is the dialog box driver function that sets the initial values for the precision, start number, increment and text height. When the precision value is selected, the DO-PRECISION function is called by the following ACTION_TILE function:
(action_tile "precision" "(do-precision $value)")
DO-PRECISION re-sets the values of the start number and increment boxes to the newly selected precision value as shown here:
(defun do-precision (val)
(setq *num* (get_tile "num"))
(setq *inc* (get_tile "inc"))
(setq *num* (rtos (atof *num*) 2 (atoi val)))
(setq *inc* (rtos (atof *inc*) 2 (atoi val)))
(set_tile "num" *num*)
(set_tile "inc" *inc*)
) ;_ end of do-precision
Here, the standard RTOS function specifies the number 2 denoting decimal units and uses the value VAL from the precision box to set the displayed number of decimal places.
A while loop is used in the function DO-INC as shown to place the incremental numbers.
(setq p (getpoint "\nNext point (
or to stop):"))
(if (or (null p) (= p " "))
(setq more nil)
(setq *num*(rtos(+ (atof *num*)(atof *inc*))
2 (atoi *precision*)))
) ;_ end of vla-AddText
) ;_ end of progn
) ;_ end of if
) ;_ end of while
The AddText method is used in the while loop as shown to create the text at the point "p" selected by the user. If the point value is null or a space (indicating that the spacebar was pressed), the while loop ends and no further text is added.
Corrections to the SPURGEAR program
Some of our readers have indicated that the SPURGEAR program does not always work as it should, so I have included an updated version, SPURGEAR3.LSP and SPURGEAR.DCL this month. I have corrected the problem that prevented the array of gear teeth from being displayed, so you shouldn't have any further problems with it. It just goes to show that you can never do enough testing, and even relatively simple programs that appear to work for me do not necessarily work for everyone.
As always, I look forward to receiving your requests for AutoLISP Solutions. Contact me using the links below.