Looking for a lisp that will increment the number by 1, but I need it to start at 001
Solved! Go to Solution.
Solved by smaher12. Go to Solution.
@tsbrown wrote:Looking for a lisp that will increment the number by 1, but I need it to start at 001
You could use any kind of incrementing, keeping the integer as a numerical value, and with the result each time, apply something like this:
(setq numtext (itoa YourIncrementedNumber))
(while (< (strlen numtext) 3)
(setq numtext (strcat "0" numtext))
); while
That should force all to three digits, adding as many zeros to the front as needed to get there.
Hi tsbrown,
Assuming you have to increment only Integers you will have to follow the sequence:
1) Start with String "001"
2) Convert string to an Integer using (atoi) function
3) add 1 to integer
4) convert the integer back to a string with (itoa) and pad with zeros.
Below is a simple function for padding a string with zeros:
;; Function pads a string representing an integer with zeros
;; Example: (pjk-leadzeros 3 "2") returns "002"
;; (pjk-leadzeros 3 "11") returns "011"
(defun pjk-leadzeros (dig str)
(if (and dig str
(= (type dig) 'INT)
(= (type str) 'STR)
(> dig 0)
)
(repeat (- dig (strlen str))(setq str (strcat "0" str)))
)
str
)
Regards,
Phil
One of the methods is using attached ALTEXT.vlx, copying 001 to new places, then drawing a polyline representing the path. With "Along paths" option in "Sort selection", you can change all numbers in a sequence quickly.
Recursion example:
(defun _leadingZero (str num) (if (< (strlen str) num) (_leadingZero (strcat "0" str) num) str ) )
You can try this. I'm sure there is a better way. I would love to see another point of view...
(defun c:tmp () (setvar 'CMDECHO 0) (setq pt# (getint "\nEnter start number: ")) (setq pt (getpoint "\nSelect point: " )) (while pt (setq pt#t (strcat "00" (rtos pt# 2 0))) (if (> pt# 9) (setq pt#t (strcat "0" (rtos pt# 2 0))) ) (if (/= pt nil)(prnt)) (setq pt (getpoint "\nSelect point: ")) (setq pt# (1+ pt#)) ) ;end while (princ) ) (defun prnt () (entmake (list '(0 . "MTEXT") '(100 . "AcDbEntity") '(100 . "AcDbMText") '(8 . "S-ANNO-TEXT") ;layer '(7 . "Standard") '(71 . 5) ;justify '(72 . 5) '(73 . 1) (cons 10 pt) ;insert point '(50 . 0) ;angle '(40 . 10) ;size '(41 . 0.0) ;textbox width (cons 1 pt#t) );list );entmake (princ) )
Using Kent1Copper's suggestion this should work if you don't get into the hundreds. Thanks Kent1Copper, as I just learned a little somthing about "strlen" today. This is also a little shorter than my previous post. Enjoy...
(defun c:tmp () (setq num (getint "\nEnter number: ")) (while (setq pt (getpoint "\nSelect point: " )) (setq numtxt (strcat "0" (itoa num))) (while (< (strlen numtxt) 3) (setq numtxt (strcat "0" numtxt)) ); while (if (/= pt nil) (progn (entmake (list '(0 . "MTEXT") '(100 . "AcDbEntity") '(100 . "AcDbMText") '(8 . "S-ANNO-TEXT") ;layer '(7 . "Standard") '(71 . 5) ;justify '(72 . 5) '(73 . 1) (cons 10 pt) ;insert point '(50 . 0) ;angle '(40 . 10) ;size '(41 . 0.0) ;textbox width (cons 1 numtxt) );list );entmake );progn );if (setq num (1+ num)) );while (princ) )
Hi,
You can also use the Increment application available on Exchange Apps which can do what you want and much more...
another rutine that worked for me for blocks
http://www.cadforum.cz/cadforum_en/autonumbering-of-autocad-blocks-incremental-counter-tip6678
another lisp routines that worked:
http://www.lee-mac.com/numinc.html
http://www.lee-mac.com/blockcounter.html
@tsbrown wrote:Can this be done if you want to used it on existing text?
If you want something that works with existing text, check out Message 14 on http://forums.autodesk.com/t5/Visual-LISP-AutoLISP-and-General/Difficulties-getting-insert-point-for...
With a little tweeking you should be able to get it to do what you want.
@tsbrown wrote:Can this be done if you want to used it on existing text?
Give this a shot
(defun c:test (/ txttmp no# ss obj) (vl-load-com) (initget 1) (setq no# (getint "\nEnter number: ") ); setq (setvar "errno" 0) (while (while (and (/= (getvar "errno") 52) (not ss) ); and (setq ss (ssget "+.:S" '((0 . "*TEXT")))) ); while (setq obj (vlax-ename->vla-object (ssname ss 0)) no#t (strcat "0" (itoa no#)) ); set (while (< (strlen no#t) 3) (setq no#t (strcat "0" no#t)) ); while (vlax-put obj 'textstring no#t) (vlax-release-object obj) (setq no# (1+ no#) ss nil ); set (setvar "errno" 0) ); while )
Yey another one.
(defun _pad0 (str ln zn) (setvar 'dimzin 4) (setq str (strcat (substr (rtos (* 0.0) 2 (- ln (Strlen str))) 2) str)) (setvar 'dimzin zn) str )
_$ (_pad0 "1" 2 (getvar 'dimzin))
"01"
_$ (_pad0 "1" 3 (getvar 'dimzin))
"001"
_$ (_pad0 "1" 4 (getvar 'dimzin))
"0001"
_$ (_pad0 "100" 4 (getvar 'dimzin))
"0100"
@smaher12 wrote:Using Kent1Copper's suggestion this should work if you don't get into the hundreds. ....
(defun c:tmp () (setq num (getint "\nEnter number: ")) (while (setq pt (getpoint "\nSelect point: " )) (setq numtxt (strcat "0" (itoa num))) (while (< (strlen numtxt) 3) (setq numtxt (strcat "0" numtxt)) ); while ....
If you change the red line above, to just:
(setq numtxt (itoa num))
it actually will work into the hundreds, and in fact, into the thousands and beyond. The (while) function simply won't do anything if it's already 3 [or more] digits. The "0" you're adding with the red line will be added by the first pass through the (while) loop, but only if needed.
[EDIT: For some unknown reason, the system moved the first line of the (while) function up to a continuation of the red line. I tried putting it back where it was, but somehow it didn't take. Replace only the red part, not that whole line as it shows above.]