本帖最后由 fangmin723 于 2024-4-17 08:12 编辑
单行文字的宽度因子好说,直接在属性面板中可批量修改。
但是多行文字的话,不知道能否通过文字样式批量修改,为了修改多行文字样式而去创建文字样式,未免过于繁琐。
通常情况下只能一个个进入文字格式编辑器中修改,也过于麻烦,于是就有了批量修改文字宽度因子的想法,并实现!
代码如下:
- ;;说明:批量修改CAD文字宽度因子 by 702099480 @ q q . com 2023.3.29
- (defun C:CTW(/ edata ei ent n newwid newwidstr si ss str tpy widstr)
- (if (setq ss (ssget '((0 . "*TEXT"))))
- (progn
- (if (= nil (setq newwid (getreal "\n 请输入新的文字宽度因子,默认<0.7>:"))) (setq newwid 0.7))
- (if (> newwid 10) (setq newwid 10.0))
- (setq n -1)
- (while (setq ent (ssname ss (setq n (1+ n))))
- (setq edata (entget ent) tpy (cdr (assoc 0 edata)))
- (if (equal tpy "TEXT")
- (entmod (subst (cons 41 newwid) (assoc 41 edata) edata))
- (progn
- (setq str (cdr (assoc 1 edata)))
- ;;2024.4.9修改匹配项至小数点后四位
- (if (wcmatch str "*`\\W##;*,*`\\W#;*,*`\\W#.#;*,*`\\W#.##;*,*`\\W#.###;*,*`\\W#.####;*")
- (progn
- (setq si (1+ (vl-string-search "\\W" str)))
- (setq ei (1+ (vl-string-search ";" str (1+ si))))
- (setq widstr (substr str si (1+ (- ei si))))
- (setq newwidstr (strcat "\\W" (rtos newwid 2 2) ";"))
- (setq str (vl-string-subst newwidstr widstr str))
- (entmod (subst (cons 1 str) (assoc 1 edata) edata))
- )
- (if (wcmatch str "{*}")
- (progn
- (setq newwidstr (strcat "{\\W" (rtos newwid 2 2) ";"))
- (setq str (vl-string-subst newwidstr "{" str))
- (entmod (subst (cons 1 str) (assoc 1 edata) edata))
- )
- (progn
- (setq str (strcat "{\\W" (rtos newwid 2 2) ";" str "}"))
- (entmod (subst (cons 1 str) (assoc 1 edata) edata))
- )
- )
- )
- )
- )
- )
- )
- (alert "请选择文字对象后再行尝试!")
- )
- (prin1)
- )
(常规方式修改) 多行文字存在多个宽度因子时统一修改为输入值 2024.4.17
- ;;说明:批量修改CAD文字宽度因子 by 702099480 @ q q . com 2023.3.29
- ;; 多行文字存在多个宽度因子时统一修改为输入值 (常规方式修改) 2024.4.17
- (defun C:CTW(/ edata ei endstr ent ischanged n newwid newwidstr si ss str tpy widstr)
- (if (setq ss (ssget '((0 . "*TEXT"))))
- (progn
- (if (= nil (setq newwid (getreal "\n 请输入新的文字宽度因子,默认<0.7>:"))) (setq newwid 0.7))
- (if (> newwid 10) (setq newwid 10.0))
- (setq n -1)
- (while (setq ent (ssname ss (setq n (1+ n))))
- (setq edata (entget ent) tpy (cdr (assoc 0 edata)))
- (if (equal tpy "TEXT")
- (entmod (subst (cons 41 newwid) (assoc 41 edata) edata))
- (progn
- (setq endstr (cdr (assoc 1 edata)) si 1 ei (strlen endstr) str "" ischanged nil)
- ;;2024.4.9修改匹配项至小数点后四位
- (while (wcmatch endstr "*`\\W##;*,*`\\W#;*,*`\\W#.#;*,*`\\W#.##;*,*`\\W#.###;*,*`\\W#.####;*")
- (setq si (1+ (vl-string-search "\\W" endstr)))
- (setq ei (1+ (vl-string-search ";" endstr (1+ si))))
- (setq widstr (substr endstr si (1+ (- ei si))))
- (setq newwidstr (strcat "\\W" (rtos newwid 2 2) ";"))
- (setq str (strcat str (substr endstr 1 ei)))
- (setq str (vl-string-subst newwidstr widstr str))
- (setq endstr (substr endstr (1+ ei)))
- (setq ischanged t)
- )
- (if (null ischanged)
- (if (wcmatch str "{*}")
- (progn
- (setq newwidstr (strcat "{\\W" (rtos newwid 2 2) ";"))
- (setq str (vl-string-subst newwidstr "{" str))
- (entmod (subst (cons 1 str) (assoc 1 edata) edata))
- )
- (progn
- (setq str (strcat "{\\W" (rtos newwid 2 2) ";" str "}"))
- (entmod (subst (cons 1 str) (assoc 1 edata) edata))
- )
- )
- (progn
- (setq str (strcat str endstr))
- (entmod (subst (cons 1 str) (assoc 1 edata) edata))
- )
- )
- )
- )
- )
- )
- (alert "请选择文字对象后再行尝试!")
- )
- (prin1)
- )
(正则方式修改) 多行文字存在多个宽度因子时统一修改为输入值 2024.4.17
- ;;说明:批量修改CAD文字宽度因子 by 702099480 @ q q . com 2023.3.29
- ;; 多行文字存在多个宽度因子时统一修改为输入值 (正则方式修改) 2024.4.17
- (defun C:CTW(/ edata ent mat n newwid newwidstr reg ss str tpy)
- (if (setq ss (ssget '((0 . "*TEXT"))))
- (progn
- (if (= nil (setq newwid (getreal "\n 请输入新的文字宽度因子,默认<0.7>:"))) (setq newwid 0.7))
- (if (> newwid 10) (setq newwid 10.0))
- (setq reg (vlax-create-object "vbscript.regexp")) ;创建正则表达式
- (vlax-put-property reg 'global 1) ;是否匹配全部
- (vlax-put-property reg 'Multiline 1);是否多行匹配
- (vlax-put-property reg 'IgnoreCase 1);是否忽略大小写
- (setq n -1)
- (while (setq ent (ssname ss (setq n (1+ n))))
- (setq edata (entget ent) tpy (cdr (assoc 0 edata)))
- (if (equal tpy "TEXT")
- (entmod (subst (cons 41 newwid) (assoc 41 edata) edata))
- (progn
- (setq str (cdr (assoc 1 edata)))
- (vlax-put-property reg 'Pattern "\\\\W\\d+\.?\\d*;")
- (setq mat (vlax-invoke reg 'Execute str))
- (if (> (vlax-get-property mat 'count) 0)
- (progn
- (setq newwidstr (strcat "\\W" (rtos newwid 2 2) ";"))
- (vlax-for x mat
- (vlax-put-property reg 'Pattern (strcat "\\" (vla-get-value x)))
- (setq str (vlax-invoke reg 'Replace str newwidstr))
- )
- (entmod (subst (cons 1 str) (assoc 1 edata) edata))
- )
- (if (wcmatch str "{*}")
- (progn
- (setq newwidstr (strcat "{\\W" (rtos newwid 2 2) ";"))
- (setq str (vl-string-subst newwidstr "{" str))
- (entmod (subst (cons 1 str) (assoc 1 edata) edata))
- )
- (progn
- (setq str (strcat "{\\W" (rtos newwid 2 2) ";" str "}"))
- (entmod (subst (cons 1 str) (assoc 1 edata) edata))
- )
- )
- )
- )
- )
- )
- (vlax-release-object mat)
- (vlax-release-object reg)
- )
- (alert "请选择文字对象后再行尝试!")
- )
- (prin1)
- )
应部分人要求,上传源码文件:文件内代码和上面给出代码一样
|