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

Autonumber with leading zeros

10 REPLIES 10
SOLVED
Reply
Message 1 of 11
JCprog
1702 Views, 10 Replies

Autonumber with leading zeros

Hello everyone!

 

I have a part of code that autonumbers attribute from 1 to how ever many. It works perfectly but needs to have leading zeros. Please help!

e.g. instead of 1,2,3 needs to be 001,002,003.

 

(setq 
 num_next (+ num_next num_max)             
 i -1
); end setq
 
(if (sslength ss_need)
  (progn
    (repeat (sslength ss_need)

     (setq         
                    l (entget (setq e (ssname ss_need (setq i (1+ i)))))
                  blk (ssname ss_need i)
           trimb_type (cdr (assoc 2 l))
     ); end setq


     (while (/= (cdr (assoc 0 l)) "ATTRIB")
      (setq l (entget (setq e (entnext e))))
     ); end while


     (setq att_name   (cdr (assoc 2 l))
         att_value  (cdr (assoc 1 l))
     ); end setq            


    (setq l (subst (cons 1 (rtos num_next 2 0)) (assoc 1 l) l)
         num_next (+ num_next 1)
          ss_need (ssdel blk ss_need)
          ss_have (ssadd blk ss_have)                i -1
    ); end setq
    (entmod l)
    (setq 
     i -1
    ); end setq
  ); end repeat
 ); end progn
); endif

 Thanks in advance!!!!Smiley Happy

10 REPLIES 10
Message 2 of 11
Shneuph
in reply to: JCprog

maybe like this..

 

(setq num_next_string (rtos num_next 2 0))
(while (< (strlen num_next_string) 3)
  (setq num_next_string (strcat "0" num_next_string))
 );while
(setq l (subst (cons 1 num_next_string) (assoc 1 l) l)
Message 3 of 11
Kent1Cooper
in reply to: JCprog

This has come up before, but since the context is probably different, very quickly --- up near the top of the (repeat):

 

(setq numtext (itoa num_next)); text-string equivalent

(while (< (strlen numtext) 3); not 3 digits yet?

  (setq numtext (strcat "0" numtext)); add a preceding 0

); while

 

and then use numtext in place of the (rtos) function in the (subst ... (cons 1 ...  part.

Kent Cooper, AIA
Message 4 of 11
JCprog
in reply to: Kent1Cooper

Thanks for the reply guys!
kinda bit lost on where to add or replace the codes provided. I get error bad function: "001"

Message 5 of 11
Kent1Cooper
in reply to: JCprog

I'm not sure this is the cause of that particular problem, but I think you want to remove the setq-ing of i to -1 [in two places] toward the end, and leave it to the one at the beginning and the stepping of it in the setting of e.

 

Also maybe not relevant to that problem, but (ssadd) and (ssdel) return the selection-set variables altered by the addition or deletion of the entity in question, without being wrapped in (setq) functions, e.g.:

....

    (setq

      l (subst (cons 1 (rtos num_next 2 0)) (assoc 1 l) l)
      num_next (+ num_next 1)
    ); end setq

    (ssdel blk ss_need)
    (ssadd blk ss_have)
    (entmod l)

....

 

If you post what you've done that gets that error, it may be obvious whether you've put something in the wrong place.

Kent Cooper, AIA
Message 6 of 11
JCprog
in reply to: Kent1Cooper

Here's how I have it:

 

(setq 
 num_next (+ num_next num_max)             
 i -1
); end setq

(if (sslength ss_need)
  (progn
  
 
    (repeat (sslength ss_need)
(setq numtext (itoa num_next)); text-string equivalent
(while (< (strlen numtext) 3); not 3 digits yet?
  (setq numtext (strcat "0" numtext)); add a preceding 0
); while
     (setq         
                    l (entget (setq e (ssname ss_need (setq i (1+ i)))))
                  blk (ssname ss_need i)
           trimb_type (cdr (assoc 2 l))
     ); end setq



     (while (/= (cdr (assoc 0 l)) "ATTRIB")
      (setq l (entget (setq e (entnext e))))
     ); end while



     (setq att_name   (cdr (assoc 2 l))
         att_value  (cdr (assoc 1 l))
     ); end setq            


    (setq l (subst (cons 1 (numtext num_next 2 0)) (assoc 1 l) l)
         num_next (+ num_next 1)
          ss_need (ssdel blk ss_need)
          ss_have (ssadd blk ss_have)                i -1
    ); end setq
	
	
	
    (entmod l)
    (setq 
     i -1
    ); end setq
  ); end repeat
 ); end progn
); endif

 

Message 7 of 11
Kent1Cooper
in reply to: JCprog

Try this adjustment [some of the changes I made are just suggestions that shouldn't affect whether it works]:

 

(setq
  num_next (+ num_next num_max)
  i -1
); end setq

;;; (if (sslength ss_need) ;;; can be [assuming it can't exist but not be a selection set] just:
(if ss_need
;;;  (progn ;;; not needed -- (repeat) is only function for 'then' argument
  (repeat (sslength ss_need); 'then'
    (setq numtext (itoa num_next)); text-string equivalent
    (while (< (strlen numtext) 3); not 3 digits yet?
      (setq numtext (strcat "0" numtext)); add a preceding 0
    ); while
    (setq
      l (entget (setq e (ssname ss_need (setq i (1+ i)))))
;;;      blk (ssname ss_need i);;; can be just:
      blk e
      trimb_type (cdr (assoc 2 l));;; [not used within quoted portion...]
    ); end setq

    (while (/= (cdr (assoc 0 l)) "ATTRIB")
      (setq l (entget (setq e (entnext e))))
    ); end while

    (setq
      att_name (cdr (assoc 2 l));;; [not used within quoted portion...]
      att_value (cdr (assoc 1 l));;; [not used within quoted portion...]
    ); end setq

    (setq
;;;;; likely the problem:      l (subst (cons 1 (numtext num_next 2 0)) (assoc 1 l) l)

;;; replace entire (rtos) function, rather than just rtos word, with numtext:
      l (subst (cons 1 numtext) (assoc 1 l) l)
      num_next (+ num_next 1)
;;;        ss_need (ssdel blk ss_need);;; simpler version below outside (setq)
;;;        ss_have (ssadd blk ss_have);;; simpler version below outside (setq)
;;;              i -1 ;;; interferes with one in (entget) above
    ); end setq
    (entmod l)
    (ssdel blk ss_need)
    (ssadd blk ss_have)
;;;  (setq ;;; interferes with one in (entget) above
;;;    i -1
;;;  ); end setq
  ); end repeat
;;;  ); end progn
); end if

Kent Cooper, AIA
Message 8 of 11
JCprog
in reply to: Kent1Cooper

We over think it Kent Smiley Wink......Shneuph's code works! I was probably thinking about what to have for lunch when I tried his code. It's working now Thanks a million to both of you Smiley Very Happy

 

(if (sslength ss_need)
  (progn
    (repeat (sslength ss_need)

     (setq         
                    l (entget (setq e (ssname ss_need (setq i (1+ i)))))
                  blk (ssname ss_need i)
           trimb_type (cdr (assoc 2 l))
     ); end setq


     (while (/= (cdr (assoc 0 l)) "ATTRIB")
      (setq l (entget (setq e (entnext e))))
     ); end while


     (setq att_name   (cdr (assoc 2 l))
         att_value  (cdr (assoc 1 l))
     ); end setq            


(setq num_next_string (rtos num_next 2 0))
(while (< (strlen num_next_string) 3)
  (setq num_next_string (strcat "0" num_next_string))
 );while
(setq l (subst (cons 1 num_next_string) (assoc 1 l) l)
         num_next (+ num_next 1)
          ss_need (ssdel blk ss_need)
          ss_have (ssadd blk ss_have)                i -1
    ); end setq
    (entmod l)
    (setq 
     i -1
    ); end setq
  ); end repeat
 ); end progn
); endif

 

Message 9 of 11
JCprog
in reply to: Shneuph

Thanks for the code! works perfect!!!!
Message 10 of 11
Kent1Cooper
in reply to: JCprog

Shneuph's solution is the same as mine except for the variable name.  But one thing I was misinterpreting was the use of the i variable, somehow not catching that since blk was being removed from the selection set, it doesn't step through the set with varying values of i, as is the usual way of using something like that.  Given that, there's no point in starting with i at -1, stepping it up to 0 to get the first thing in the selection, and then after that's been removed, setting i back to -1 [twice] to be re-stepped-up to 0 again the next time around.  You can simplify it to this, just using 0 outright every time, and omitting i altogether:

 

(if (sslength ss_need)
  (progn
    (repeat (sslength ss_need)
     (setq        
                    l (entget (setq e (ssname ss_need 0)))
                  blk e
           trimb_type (cdr (assoc 2 l))
     ); end setq

     (while (/= (cdr (assoc 0 l)) "ATTRIB")
      (setq l (entget (setq e (entnext e))))
     ); end while

     (setq att_name   (cdr (assoc 2 l))
         att_value  (cdr (assoc 1 l))
     ); end setq           

(setq num_next_string (rtos num_next 2 0))
(while (< (strlen num_next_string) 3)
  (setq num_next_string (strcat "0" num_next_string))
);while
(setq l (subst (cons 1 num_next_string) (assoc 1 l) l)
         num_next (+ num_next 1)
          ss_need (ssdel blk ss_need);;;; see below
          ss_have (ssadd blk ss_have);;;;                i -1 ;;;; remove
    ); end setq
    (entmod l)
;;;;    (setq   ;;;; remove
;;;;     i -1
;;;;    ); end setq
  ); end repeat
); end progn
); endif

 

And again, this:

(setq ss_need (ssdel blk ss_need))

works, but is redundant, since the same is accomplished by just:

(ssdel blk ss_need)

Making that change means pulling the (ssdel) and (ssadd) outside the (setq) function they're currently in, as in my earlier suggestion.

Kent Cooper, AIA
Message 11 of 11
JCprog
in reply to: Kent1Cooper

Nicely done!!!!! it even execute faster. Thanks Kent!!!!! 🙂

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

Post to forums  

Autodesk Design & Make Report

”Boost