sunduke 发表于 2016-1-29 16:21:12

关于lisp中改变多行文本文字的宽度比例系数

大家好:
      最近在学习用autolisp编小程序来方便改图画图,说下我的问题吧,我要实现的很简单,就是格式刷的简化功能,我只需要修改文字属性,cad自带的格式刷大面积刷的时候难免会改变图的线条的图层,我要的只是修改文字的情况,一个个找字刷也太费劲了。
      运用像素数据修改,已经实现了对单行文本的这个功能,但是多行文本的像素数据的文字宽度比例居然在1.“”文本内容中,这个我真不知道如何能实现了,求教高手!

hoopert 发表于 2016-2-1 20:24:49

sunduke 发表于 2016-2-1 08:14 static/image/common/back.gif
我去试试,可能我没有清除多重文字的宽度因子,直接替换失败

初步看了一下正则表达式,一时半会儿也掌握不了。
以最基本的筛选方法做了一个程序,会有一些BUG,比如文字中原来如果含有“\W”等文本会出错,但基本上能满足通常文本的需要。
下面的程序只是对MTEXT的宽度因子根据TEXT的文字宽度进行了设置,其它高度、颜色、图层等请自行设置。

(defun c:TT ()
    (setq ed1 (entget (car (entsel "请选择TEXT实体:"))))
    (setq ed2 (entget (car (entsel "请选择MTEXT实体:"))))
    (setq wid (rtos (cdr (assoc 41 ed1))))
    (setq aa (cdr (assoc 1 ed2)))

    ;;去除宽度因子
    (while (setq StrP (vl-string-search "\\W" aa))
        (setq
          a1 (substr (substr aa (1+ StrP))
                     (+ 2 (vl-string-search ";" (substr aa (1+ StrP))))
                     ) ;_ end of substr
          ) ;_ end of setq
        (setq aa (strcat (substr aa 1 strp) a1))
        ) ;_ end of while
    ;;去除高度因子
    (while (setq StrP (vl-string-search "\\H" aa))
        (setq
          a1 (substr (substr aa (1+ StrP))
                     (+ 2 (vl-string-search ";" (substr aa (1+ StrP))))
                     ) ;_ end of substr
          ) ;_ end of setq
        (setq aa (strcat (substr aa 1 strp) a1))
        ) ;_ end of while
    ;;去除字体定义
    (while (setq StrP (vl-string-search "\\f" aa))
        (setq
          a1 (substr (substr aa (1+ StrP))
                     (+ 2 (vl-string-search ";" (substr aa (1+ StrP))))
                     ) ;_ end of substr
          ) ;_ end of setq
        (setq aa (strcat (substr aa 1 strp) a1))
        ) ;_ end of while
    ;;去除下划线
    (while (setq StrP (vl-string-search "\\L" aa))
        (setq
          a1 (substr (substr aa (1+ StrP))
                     (+ 2 (vl-string-search ";" (substr aa (1+ StrP))))
                     ) ;_ end of substr
          ) ;_ end of setq
        (setq aa (strcat (substr aa 1 strp) a1))
        ) ;_ end of while
    ;;去除上划线
    (while (setq StrP (vl-string-search "\\O" aa))
        (setq
          a1 (substr (substr aa (1+ StrP))
                     (+ 2 (vl-string-search ";" (substr aa (1+ StrP))))
                     ) ;_ end of substr
          ) ;_ end of setq
        (setq aa (strcat (substr aa 1 strp) a1))
        ) ;_ end of while

    ;;去除颜色定义
    (while (setq StrP (vl-string-search "\\C" aa))
        (setq
          a1 (substr (substr aa (1+ StrP))
                     (+ 2 (vl-string-search ";" (substr aa (1+ StrP))))
                     ) ;_ end of substr
          ) ;_ end of setq
        (setq aa (strcat (substr aa 1 strp) a1))
        ) ;_ end of while
    ;;去除字体倾斜因子
    (while (setq StrP (vl-string-search "\\Q" aa))
        (setq
          a1 (substr (substr aa (1+ StrP))
                     (+ 2 (vl-string-search ";" (substr aa (1+ StrP))))
                     ) ;_ end of substr
          ) ;_ end of setq
        (setq aa (strcat (substr aa 1 strp) a1))
        ) ;_ end of while
    ;;去除字间距因子
    (while (setq StrP (vl-string-search "\\T" aa))
        (setq
          a1 (substr (substr aa (1+ StrP))
                     (+ 2 (vl-string-search ";" (substr aa (1+ StrP))))
                     ) ;_ end of substr
          ) ;_ end of setq
        (setq aa (strcat (substr aa 1 strp) a1))
        ) ;_ end of while

    (while (setq StrP (vl-string-search "{" aa))
        (setq aa (strcat (substr aa 1 strp) (substr aa (+ 2 strp))))
        ) ;_ end of while
    (while (setq StrP (vl-string-search "}" aa))
        (setq aa (strcat (substr aa 1 strp) (substr aa (+ 2 strp))))
        ) ;_ end of while
    (setq aa (strcat "{\\W" wid";" aa "}"))
    ;;下面只是重新设置了MTEXT整体的宽度因子,其它需要设置
    (entmod (subst (cons 1 aa) (assoc 1 ed2) ed2))
    (princ)
    ) ;_ end of defun

kozmosovia 发表于 2016-1-29 19:17:28

多重文本允许不同字符有不同的宽度因子,因此使用程序可能无法预知具体的字符并相应调整。
简单的方式是整个多重文本设定一个统一的宽度因子,实现方式为
1)清除多重文字内已有的宽度因子定义(或连带其他所有定义),可在网站搜索使用正则表达式的处理代码实现
2)统一设定宽度因子:将 (strcat "{\\W1.x;" 多重文字内容 "}")重新写入多重文字内容

sunduke 发表于 2016-2-1 08:14:08

kozmosovia 发表于 2016-1-29 19:17 static/image/common/back.gif
多重文本允许不同字符有不同的宽度因子,因此使用程序可能无法预知具体的字符并相应调整。
简单的方式是整 ...

我去试试,可能我没有清除多重文字的宽度因子,直接替换失败
页: [1]
查看完整版本: 关于lisp中改变多行文本文字的宽度比例系数