Once in a file, it is easy enough to determine the total number of polylines, and then to use the itoa AutoLISP function to convert that integer to a string and the strlen function to determine how many characters that largest number would have. To get all of the numeric strings to that same length, I wanted to use "zero padding"; that is, I wanted to add "0" characters to the front of shorter numeric strings to bring the total string length up to that of the largest number. The subroutine I wrote to do this turned out to be elegantly simple (or so I thought), so I decided to post the code here.
(defun ZEROPAD ( ;_ Arguments:
inum ; Number to be converted to zero-padded string [integer].
ichar ; Number of characters in zero-padded string [integer].
/ ;_ Local variables:
ilen ; Number of characters in integer to be converted [integer].
snum ; Integer to be converted as a string [string].
) ;_ End arguments and local variables.
(setq snum (itoa inum) ; String equivalent of integer to be converted.
ilen (strlen snum) ; Length of integer string.
) ;_ End setq.
(while (< ilen ichar) ; While integer string length is less than target length...
(setq snum (strcat "0" snum) ; ...add "0" to front of string and...
ilen (1+ ilen) ; ...increment string length.
) ;_ End setq.
) ;_ End while.
snum ; Return final string.
) ;_ End ZEROPAD.
Somewhat less complicated than the Integer To String - Zero Padding node I created for Dynamo, but the same net result.
1 comment:
Or a less readable but much shorter and faster:
(defun ZEROPAD (n z)
(substr (strcat "00000000000000000000" (itoa n)) (+ (strlen (itoa n)) (- z) 21))
)
Post a Comment