Juergen Posted October 10, 2019 Share Posted October 10, 2019 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) ) Quote Link to comment Share on other sites More sharing options...
Emmanuel Delay Posted October 10, 2019 Share Posted October 10, 2019 (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 October 10, 2019 by Emmanuel Delay 1 Quote Link to comment Share on other sites More sharing options...
Juergen Posted October 11, 2019 Author Share Posted October 11, 2019 Hi Emmanuel, thank you for your help. It work perfect!!! Juergen 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.