ADDING UP (stradd.lsp)You often have text in your drawings that are numbers but since they are text strings you can't perform any mathematical functions on them. STRADD.LSP is a Lisp program that allows you to add (or subtract) a numeric value to text that is numeric. Text that contains non-numeric characters is not handled very well though.The ListingAfter the program description, the program name is defined and variables are localized. Note that the user-defined function (or sub-routine) TZSUPP is also localized. This is done so that if another program is loaded that contains a user-defined function called TZSUPP, then one won't overwrite the other. TZSUPP is defined to take one argument (the variable name before the slash) and the other variable names it uses are localized. The TZSUPP sub-routine is used to suppress trailing zeros in a numeric string.It starts by setting four variables; IT2, STR3, STR4 and STR5. IT2 is a counter and is set to the number of characters in the string STR2 which is the argument passed to the TZSUPP function. STR3 is set to the whole number portion of STR2 as a string. This is done by converting STR2 into an integer with the ATOI function and then converting back into a string with the ITOA function, effectively stripping off any fractional value. STR4 and STR5 are each set to an empty string so that new characters can be added to them later. A WHILE loop is then started which will run if the IT2'th character in the STR2 string is not a dot (.). This loop will add the IT2'th character in STR2 to the string in STR4, prefixing it to any that are already there. The counter IT2 is then decremented by one to then get the preceding character in STR2 the next time through the loop. This continues until the character is a dot at which time the loop will be exited. This leaves STR4 containing a string with only the fractional part of the number. Variable IT3 is then set to the number of characters in STR4. A REPEAT loop is then started which loops through for each character in STR4. This contains an IF function whose then-expression will run if the IT3'th character is not a zero and variable FLG2 is one. This expression is a SETQ function which adds the IT3'th character to the string in STR5, prefixing it to any that are already there, decrementing IT3 by one, and setting FLG2 to one. The IF's else-expression simply decrements IT3 by one. What actually happens here is that initially STR4 will usually have a number of trailing zeros and the else-expression will run. Once a non-zero character has been encountered, the then-expression will run. Setting the FLG2 variable to one then ensures that the then-expression will run for the remaining number of loops. This prevents zeros further up the string from being deleted. An If function then checks to see if STR5 still contains an empty string and if so, its then-expression will quote STR3 (the integer part of the number), returning its value to the calling function. If not, the else-expression will concatenate STR3, a dot and STR5 and this will be returned to the calling function. The TZSUPP function is then closed. We then move onto the main function which prompts the user to select some numeric text objects. The objects selected are then stored in a selection set in ST2. An IF then checks to see if ST2 has a value (objects were selected) and if so, its then-expression, grouped by a PROGN will run. This PROGN comprises most of the rest of the program. This starts with a SETQ that sets IT2 to the number of objects selected, IT3 to zero and requesting a value from the user to add to the numbers selected, storing it in NUM2. A REPEAT loop is then started which will loop through for each object in the selection set. This starts by setting ENT2 to the entity data list (association lists) for the IT3'th object in the selection set. The next line uses some moderately complex nested code to extract the string value of the text object, convert it to a real number in decimal form to 13 decimal places, add the user supplied value to it and then reconvert it back into a string before saving it to NST2. The next line uses the TZSUPP function defined earlier to strip trailing zeros by calling it and supplying NST2 as the argument it requires. The string it returns will be stored in NST3. The line following creates a new association list for the text value of the object and substitutes it for the old one in the entity data list. Variable IT3 is then incremented by one so as to get the next object in the selection set. The ENTMOD function is then called with the new entity data list in ENT2 as an argument to update the object on the screen. After closing off the REPEAT loop and the PROGN grouping the IF's then-expression, its else-expression would inform the user that nothing had been selected if that were the case. After closing the IF, a PRINC function call prevents the value of IT3
being echoed to the screen as the program exits.
|
©1996-2001 ZOTO Technologies