AutoLISP Solutions: Generate a series of alphanumeric text strings15 Jul, 2005 By: Tony Hotchkiss Cadalyst
AutoLISP helps you create alphanumeric text strings where the letters or numbers can change incrementally
This month's request came from Mr. Bruce Moore of Perrysburg, OH. Brian creates electrical drawings with a variety of wire numbering combinations. He wants to create text strings that can be incremented by either number or letter, and that can have the number before the letter(s) or the letter(s) before the number. In either case, the number or the letter may be incrementally changed. The letters may be upper or lower case.
The AutoLISP Solution is ALPHA-NUM.LSP, which allows the user to select any alphanumeric string and whether to increment the number or the letters with any incremental value. The letters may come before or after the numbers, and they may be upper or lower case.
Get the Code
Download the ALPHA-NUM.LSP file from Cadalyst's CAD Tips site. Save the file in AutoCAD's Support directory. Use the Appload facility by selecting Tools / Load Application, and then use the browser to select the file.
How to Use the ALPHA-NUM Code
After you load the code, the system prompts you to enter AN to start the program. To see this prompt, you may need to set your Command window size to three 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 AN to start. After you enter AN, you will be prompted to select text. The dynamic textbox of AutoCAD 2006 contains this prompt (figure 1). Earlier versions of AutoCAD will display the prompt at the command line. The prompt will persist until you select a text string with two parts, letters and numbers, either of which may come first in the string.
Figure 1. The select text prompt.
When an appropriate text string is selected, you will see a prompt to indicate the part of text to be incremented with choices for letter or number. The default value is the number, for which you simply hit Enter. Type L for the letter selection. The prompt for AutoCAD 2006 is shown in figure 2. Earlier versions of AutoCAD again will show the prompt at the command line.
Figure 2. The prompt to indicate the part of text to be incremented.
The next prompt is the increment value (figure 3). If the letter option was chosen earlier, the character increment prompt is shown as in figure 3, otherwise the number increment prompt appears.
Figure 3. The character increment prompt.
In either case, you will then see the start point prompt (figure 4). The prompt also shows that you may hit Enter to stop incrementing the text.
Figure 4. The start of text to be incremented prompt.
Some typical results of incrementing letters and numbers are shown in figure 5.
Figure 5. Results of incrementing text or numbers.
Figure 6. Text Not Recognized alert box.
The program was written in AutoCAD 2006, and it was tested in both AutoCAD 2004 and 2006, but it should work in any version of AutoCAD later than 2000. ALPHA-NUM.LSP starts as usual with my error handler and system variable functions. The function ALPHA-NUM is the main function that essentially contains the algorithm for the program as follows:
(setq textobj (get-textobj)) ; user selected text object
(setq *txtheight* (vla-get-height textobj))
(setq txtlayer (vla-get-layer textobj))
(setv "CLAYER" txtlayer)
(setq a-n-type (get-antype textobj))
; test for stringtype
(setq text-params (get-txt-data a-n-type))
; get increment parameters
(do-a-n-inc textobj text-params a-n-type)
; create string increments
The sequence here is to 1) get the user selected text object, 2) get the height and 3) layer of the text, 4) set the text layer as the current layer, 5) check for the format or type of the text string (whether the text is first or the number is first), 6) get the increment data --which part is to be incremented and by how much, 7) create the increments at user specified locations and finally to 8) reset the current layer to its former state. Each of these events is represented in turn by the eight lines of code above.
The text selection GET-TEXTOBJ function uses a while loop to determine if an appropriate alphanumeric string is selected. The object-orientated nature of Visual LISP allows the text height and layer properties to be extracted very simply by the VLA-GET-PROPERTY functions shown, for which you can replace the word PROPERTY with any property that the text object can possess, such as height, color, layer, etc. SETV and RSETV are my system variable management functions, used in this case to set the current layer and then later to restore the current layer to its previous value.
GET-ANTYPE examines the text object and returns 1 if the string starts with an integer and ends with something other than an integer, 2 if the string ends with an integer and starts with something other than an integer and zero if none of these conditions apply. GET-TXT-DATA returns Number or Letter if the text object code is either 1 or 2, and it displays the alert box "Text string not recognized" otherwise.
DO-A-N-INC takes the user selected text object, the Letter or Number code, and the 1 or 2 code, and calls an INCREMENT function depending on which of the four possible combinations exist:
- Number followed by letter(s) with number increment
- Number followed by letter(s) with letter increment
- Letter(s) followed by number with number increment
- Letter(s) followed by number with letter increment
The INCREMENT function writes the new incremented alphanumeric text with the added complication of determining whether the text is upper or lower case. This function calls three other functions, GET-LEAD-NUMBER, GET-LEAD-TEXT and INCR-STRING in order to separate the alpha and numeric sections of the text and also to increment the text itself.
INCR-STRING uses the visual LISP function VL-STRING->LIST in order to convert a text string to a list of individual ASCII coded numbers. I used that list in reverse order to perform what in effect is an incrementing system with a base of 26, so that if the 26th letter code is exceeded, start again in that column with the ASCII code corresponding to A in upper or lower case, and at the same time set a carry-1 variable to increment the next ASCII code in the list if required. Doing that in a repeat loop ensured that the proper increment was added to the proper ASCII code in the list. The repeat loop of INCR-STRING is as follows:
(setq charnum (nth (setq i (1+ i)) rev-list))
(if (= i 0)
(setq charnum (+ charnum inc))
(if (= carry 1)
(setq charnum (1+ charnum))
) ;_ end of if
) ;_ end of progn
) ;_ end of if
(if (> charnum (+ caseval 26))
(setq charnum (- charnum 26))
(setq carry 1)
) ;_ end of progn
(setq carry 0)
) ;_ end of if
(setq newlist (append newlist (list charnum)))
) ;_ end of repeat
At the end of the function, I reversed the list again and used the MAPCAR function to convert each ASCII code in turn back to the corresponding character. Finally, INCR-STRING used the FOREACH loop with STRCAT to concatenate the list of characters into an incremented string. For this function, I resorted to a flow diagram in order to sort out the logical flow before I could begin to write the function.
As always, I look forward to receiving your requests for AutoLISP Solutions. Contact me using the links below.
Autodesk Technical Evangelist Lynn Allen guides you through a different AutoCAD feature in every edition of her popular "Circles and Lines" tutorial series. For even more AutoCAD how-to, check out Lynn's quick tips in the Cadalyst Video Gallery. Subscribe to Cadalyst's free Tips & Tools Weekly e-newsletter and we'll notify you every time a new video tip is published. All exclusively from Cadalyst!