Jump to content

Leading zero's in attribute value


Recommended Posts

Posted

Hello All,

 

I am currently working on a block program for Cable ID Tags. The tags use a sequential number at the end. They start out at 001, 002, 003 & so on. When the program inserts each block the number is to increase by 1. The problem I am having is the leading zero's in the number do not show up after the block is inserted. I thought changing dimzin would work, But it did'nt happen.

See image below.

 

Is there a variable for this or do I need to use some sort of special method to achieve this?

 

Any help is much appreciated.

Thanks

Document1.JPG

Posted

Have you tried the vl-string-left-trim function?

 

[edit]

(vl-string-left-trim "0" "01-N-AV-001")
_$
"1-N-AV-001"
_$

[/edit]

Posted

(defun AT:NumFix (s n)
 ;; Fix number string with leading zeros
 ;; s - Number string to fix
 ;; n - Number of characters for final string
 ;; Alan J. Thompson, 10.29.09
 (if (< (strlen s) n)
   (AT:NumFix (strcat "0" s) n)
   s
 )
)

 

(AT:NumFix "3" 4) -> "0003"

  • Like 1
Posted

Thanks RenderMan, But I am not familar with VL. I was looking more towards a vanilla solution. Below is the function I am currently using. It works with the exception of the leading zeros.

 

(defun COMM_SEQN ()
 (setq ATAG (strcat "01-N-"OUSE"-"SEQN""))
 (setq SS (ssget "_l" (list (cons 0 "INSERT")(cons 66 1))))
 (if
   (/= SS nil)
   (progn
     (setq INDEX 0)
     (setq ENAME (ssname SS INDEX))
     (setq ELIST (entget ENAME))
     (while
       (/= (cdr (assoc 0 ELIST)) "SEQEND")
       (setq ELIST (entget ENAME))
       (if
         (= "ID-TAG" (cdr (assoc 2 ELIST)))
         (progn
           (entmod (subst (cons 1 ATAG)(assoc 1 ELIST) ELIST))
           (entupd ENAME)))
         (setq ENAME (entnext ENAME)))))
 (princ))
(princ)

Posted

Alan, thats interesting. It looks like I need to provide two arguments, a string and a number if I understand it correctly.

Posted

My bad, Buzzard.

 

Alan's given you a great alternative! (...another one for the trophy shelf, ehh, Alan!? 8) )

Posted
My bad, Buzzard.

 

Alan's given you a great alternative! (...another one for the trophy shelf, ehh, Alan!? 8) )

 

No problem, I appreciate the help all the same.

Posted
Alan, thats interesting. It looks like I need to provide two arguments, a string and a number if I understand it correctly.

Correct. First is the number string and the second is the desired length of the string

eg. (AT:NumFix "1" 3) would yield "001".

 

My bad, Buzzard.

 

Alan's given you a great alternative! (...another one for the trophy shelf, ehh, Alan!? 8) )

Thanks. :)
Posted
Correct. First is the number string and the second is the desired length of the string

eg. (AT:NumFix "1" 3) would yield "001".

 

Thanks. :)

 

Ok, So as the sequence increases, I would need to use a conditional statement. ex: 001-009 & 010-099 as these would change due to fewer leading zeros when they reach these points.

Posted (edited)

Alan, you code inspired me to have some fun with the concept :)

 

(defun PadLeft&Right ( string char left right )

 (defun PadLeft ( string left right )
   (cond
     ( (< 0  left) (PadRight (strcat char string) (1- left) right) )
     ( (< 0 right) (PadRight string left right) )
     ( string )
   )
 )

 (defun PadRight ( string left right )
   (cond
     ( (< 0 right) (PadLeft (strcat string char) left (1- right)) )
     ( (< 0  left) (PadLeft string left right) )
     ( string )
   )
 )

 (PadLeft string left right)
)

I love the TRACE call:

 

Entering (PADLEFT&RIGHT "1" "0" 8 5)
 Entering (PADLEFT "1" 8 5)
   Entering (PADRIGHT "01" 7 5)
     Entering (PADLEFT "010" 7 4)
       Entering (PADRIGHT "0010" 6 4)
         Entering (PADLEFT "00100" 6 3)
           Entering (PADRIGHT "000100" 5 3)
             Entering (PADLEFT "0001000" 5 2)
               Entering (PADRIGHT "00001000" 4 2)
                 Entering (PADLEFT "000010000" 4 1)
[10] Entering (PADRIGHT "0000010000" 3 1)
[11]   Entering (PADLEFT "00000100000" 3 0)
[12]     Entering (PADRIGHT "000000100000" 2 0)
[13]       Entering (PADLEFT "000000100000" 2 0)
[14]         Entering (PADRIGHT "0000000100000" 1 0)
[15]           Entering (PADLEFT "0000000100000" 1 0)
[16]             Entering (PADRIGHT "00000000100000" 0 0)
[16]             Result:  "00000000100000"
[15]           Result:  "00000000100000"
[14]         Result:  "00000000100000"
[13]       Result:  "00000000100000"
[12]     Result:  "00000000100000"
[11]   Result:  "00000000100000"
[10] Result:  "00000000100000"
                 Result:  "00000000100000"
               Result:  "00000000100000"
             Result:  "00000000100000"
           Result:  "00000000100000"
         Result:  "00000000100000"
       Result:  "00000000100000"
     Result:  "00000000100000"
   Result:  "00000000100000"
 Result:  "00000000100000"
Result:  "00000000100000"

Edited by Lee Mac
Posted
Ok, So as the sequence increases, I would need to use a conditional statement. ex: 001-009 & 010-099 as these would change due to fewer leading zeros when the reach these points.

It will account for any string

(AT:NumFix "123" 3) -> "123"

(AT:NumFix "12" 3) -> "012"

Posted

If you don't mind, Buzzard... have you ever considered something like this:

 

(defun COMM_SEQM (ouse seqn / ss index elist)
 (if (setq ss (ssget "_L" '((0 . "INSERT") (66 . 1))))
   (progn
     ;; ...code
     (entmod 
       (subst 
         (cons 1 (strcat "01-N-" ouse "-" seqn ""))
         (assoc 1 elist)
         elist))
     ;; ...code
     ))
 (princ))

 

 

This localizes some of the variables, and I believe the others are arguments (?), also, it removes a couple of steps.

Posted
Alan, you code inspired me to have some fun with the concept :)

Nice... :)

Posted
If you don't mind, Buzzard... have you ever considered something like this:

 

(defun COMM_SEQM (ouse seqn / ss index elist)
(if (setq ss (ssget "_L" '((0 . "INSERT") (66 . 1))))
(progn
;; ...code
(entmod 
(subst 
(cons 1 (strcat "01-N-" ouse "-" seqn ""))
(assoc 1 elist)
elist))
;; ...code
))
(princ))

 

 

This localizes some of the variables, and I believe the others are arguments (?), also, it removes a couple of steps.

 

Thanks, But I do have them them localized. The function that was shown is only a small part of the code.

Posted
Thanks, But I do have them them localized. The function that was shown is only a small part of the code.

 

I suspected that, but wanted to be sure, my friend. :wink:

Posted (edited)

If you are curious to see what I have done.

 

Attached is the code and dcl. Type COMM to start. Select the ID Tag radio button and click enter. Note the program will go into a loop after each insertion.

Edited by The Buzzard
Posted (edited)

Ok, I got my leading zeros now. Just a few odd quirky things going on that I will need to fix, But I am closer now than before.

 

Thanks Alan.

Edited by The Buzzard
Posted
Ok, I got my leading zeros now. Just a few odd quirky things going on that I will need to fix, But I am closer now than before.

 

Thanks Alan.

You're welcome. :)
Posted

To All, Please note,

 

This code is still work in progress and not complete. I plan to reduce the size of the Block Definition & Show Image functions. Also I plan to fix the looping as well. I will repost later at some point when it is completed.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...