Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Automatic Numbering that starts at 001

16 REPLIES 16
SOLVED
Reply
Message 1 of 17
tsbrown
4668 Views, 16 Replies

Automatic Numbering that starts at 001

Looking for a lisp that will increment the number by 1, but I need it to start at 001

16 REPLIES 16
Message 2 of 17
Kent1Cooper
in reply to: tsbrown


@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.

Kent Cooper, AIA
Message 3 of 17
pkenewell6347
in reply to: tsbrown

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

Message 4 of 17
3wood
in reply to: tsbrown

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.

altext.png

Message 5 of 17
alanjt_
in reply to: 3wood

Recursion example:

 

(defun _leadingZero (str num)
  (if (< (strlen str) num)
    (_leadingZero (strcat "0" str) num)
    str
  )
)

 

Message 6 of 17
smaher12
in reply to: alanjt_

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)
)

 

Message 7 of 17
tsbrown
in reply to: smaher12

Thanks for all the help

I'll try these out later today

Message 8 of 17
smaher12
in reply to: tsbrown

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)
)

 

Message 9 of 17
_gile
in reply to: smaher12

Hi,

 

You can also use the Increment application available on Exchange Apps which can do what you want and much more...



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 10 of 17
gilbertomejiac
in reply to: tsbrown

Message 11 of 17
gilbertomejiac
in reply to: tsbrown

Message 12 of 17
tsbrown
in reply to: smaher12

Can this be done if you want to used it on existing text?

Message 13 of 17
smaher12
in reply to: tsbrown


@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.

 

Message 14 of 17
smaher12
in reply to: tsbrown


@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
)

 

Message 15 of 17
pbejse
in reply to: tsbrown

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"

 

Message 16 of 17
Kent1Cooper
in reply to: smaher12


@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.]

Kent Cooper, AIA
Message 17 of 17
tsbrown
in reply to: smaher12

Thanks again for all the help

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost