Jump to content

Add leading zero at the number in a text string on end


Recommended Posts

Posted

Hi,

 

i work with the code "AddLeadingZeros" that

write zeros at the number in text string at the

beginning.

 

How should the program look like when i want

the zero at the number in the end of the text string?

e.g:

SO-Ei-Un-1

SO-Ei-Un-01

 

Thx.

Juergen

 

Quote

(defun C:AddLeadingZeros ( / a b ed i z)

  (if (and (not (initget 7))
       (setq z (getint "\nAdd zeros to how many: "))        
       (princ "\nNeed TEXTs or MTEXTs,")
       (setq ss (ssget '((0 . "*TEXT"))))
       )
    (repeat (setq i (sslength ss))
      (setq ed (entget (ssname ss (setq i (1- i)))))
      (entmod (subst (cons 1 (strcat (substr "000000000" 1 (if (>= z (setq b (strlen (itoa (fix (atof (cdr (setq a (assoc 1 ed)))))))))
                                 (- z b)
                                 0))
                     (cdr a)))
             a
             ed))))
  (princ)
)

 

Posted (edited)

This should work

 

;; settings
(setq alz_digits 2)             ;; number of digits

;; substring, like php substr
;; Just because I don't like (substr) enough
(defun substring ( str idx len )
  (substr str (if (< idx 0) (+ 1 (strlen str) idx) (1+ idx)) len)
)

(defun alze (val delim digits / pos rest num numlength)
  ;; example: (vl-string-position (ascii "z") "azbzlmnqc" nil T)
  (setq pos (vl-string-position (ascii delim) val nil T))          ;; the last "-" is on this string position
  (setq rest (substring val 0 (+ pos 1)))                          ;; the rest of the string, including the last -
  (setq num (substring val (+ pos 1) (strlen val)))                ;; the number.  I don't really care if it's a number, I'll just add a zero if the string length is < digits
  (repeat (- digits (strlen num))
    (setq num (strcat "0" num))                                    ;; add zeroes to num as long as num has fewer characters as digits
  )
  (strcat rest num)                                                ;; now stick rest back to num
)

;; Add Leading Zero End
(defun c:alze ( / ss i elm val newval)
  (setq ss (ssget '((0 . "*TEXT"))))
  (setq i 0)
  (repeat (sslength ss)
    (setq elm (ssname ss i))
    ;; text value
    (setq val (cdr (assoc 1 (entget elm))))
    ;;
    (setq newval (alze val "-" alz_digits))
    (entmod (subst (cons 1 newval) (cons 1 val) (entget elm)))
    (setq i (+ i 1))
  )
  (princ)
)

Edited by Emmanuel Delay
  • Like 1
Posted

Hi Emmanuel,

 

thank you for your help.

It work perfect!!!

 

Juergen

  • Like 1

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