(defun c:modtext (/ process-number ss ent txt pos start end num-str new-num result remaining) (vl-load-com) ; 定义处理单个数字字符串的函数 (defun process-number (str / int-part dec-part dec-len last-two num new-dec-str result carry new-int new-dec) (if (and (wcmatch str "*#`.*#*") (setq pos (vl-string-search "." str)) (> pos 0) (< pos (1- (strlen str))) (setq dec-part (substr str (+ pos 2))) (>= (strlen dec-part) 2)) (progn (setq int-part (substr str 1 pos)) (setq dec-len (strlen dec-part)) (setq last-two (substr dec-part (- dec-len 1) 2)) (setq num (atof last-two)) (cond ((> num 90) (setq new-dec-str (substr dec-part 1 (1- dec-len))) (setq result (increment-string new-dec-str)) (setq carry (car result)) (setq new-dec (cadr result)) (if carry (strcat (itoa (1+ (atoi int-part))) ".0") (strcat int-part "." new-dec) ) ) ((and (<= num 90) (> (rem num 10) 0)) (setq new-dec-str (substr dec-part 1 (1- dec-len))) (setq result (increment-string new-dec-str)) (setq carry (car result)) (setq new-dec (cadr result)) (if carry (strcat (itoa (1+ (atoi int-part))) "." new-dec) (strcat int-part "." new-dec) ) ) (T (strcat int-part "." (substr dec-part 1 (1- dec-len))) ) ) ) str ) ) ; 主程序 (if (setq ss (ssget '((0 . "TEXT")))) (repeat (setq i (sslength ss)) (setq ent (ssname ss (setq i (1- i)))) (setq txt (cdr (assoc 1 (entget ent)))) (setq start 1) (setq result "") ; 安全遍历文本 (while (and (> (strlen txt) 0) ; 确保字符串非空 (setq pos (vl-string-search "." txt start)) (<= pos (- (strlen txt) 2))) ; 安全定位数字范围 (setq start (find-number-start txt pos)) (setq end (find-number-end txt pos)) ; 严格校验所有位置 (cond ((not (and (>= start 1) (<= end (strlen txt)) (<= start end) (> (1+ (- end start)) 0))) (setq result (strcat result txt)) (setq txt "") ; 强制退出循环 ) (T ; 安全截取子字符串 (setq num-str (substr txt start (1+ (- end start)))) (setq new-num (process-number num-str)) ; 安全拼接结果 (setq prefix (substr txt 1 (1- start))) (setq result (strcat result prefix new-num)) ; 安全截取剩余字符串 (setq remaining-start (1+ end)) (setq txt (if (<= remaining-start (strlen txt)) (substr txt remaining-start) "" ) ) (setq start 1) ) ) ) ; 拼接最终结果 (setq result (strcat result txt)) ; 更新实体 (entmod (subst (cons 1 result) (assoc 1 (entget ent)) (entget ent))) ) ) (princ) ) ; 辅助函数:安全查找数字起始位置 (defun find-number-start (str pos / ch) (while (and (>= pos 1) (setq ch (substr str pos 1)) (or (<= 48 (ascii ch) 57) (eq ch "."))) (setq pos (1- pos)) ) (max 1 (1+ pos)) ; 强制返回有效位置 ) ; 辅助函数:安全查找数字结束位置 (defun find-number-end (str pos / len ch) (setq len (strlen str)) (while (and (<= (1+ pos) len) (setq ch (substr str (1+ pos) 1)) (or (<= 48 (ascii ch) 57) (eq ch "."))) (setq pos (1+ pos)) ) (min len pos) ; 强制不越界 ) ; 进位逻辑函数 (defun increment-string (str) (if (= str "") (list t "1") (progn (setq len (strlen str)) (setq carry 1) (setq result "") (while (> len 0) (setq ch (substr str len 1)) (setq digit (- (ascii ch) 48)) (setq sum (+ digit carry)) (if (>= sum 10) (progn (setq sum (- sum 10)) (setq carry 1) ) (setq carry 0) ) (setq result (strcat (itoa sum) result)) (setq len (1- len)) ) (if (= carry 1) (list t (strcat "1" result)) (list nil result) ) ) ) ) (princ "\n命令: modtext") (princ)