明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 749|回复: 5

求 修改文本替换命令,可以替换2种文字

[复制链接]
发表于 2021-6-24 19:55 | 显示全部楼层 |阅读模式
10明经币
求各位大师帮修改一下 ,修改文本,现在有一个程序,只能修改一个文字,现在要求能替换 2种文字
1,模具费 几个字 改成  修改模具费用    这个功能已经有了
2,零件费用   改成  模具配件      ,把零件费用4个字  改成模具配件4个字    增加这一个功能
现在用的程序如下:
(defun chgterr (s / p)
   (if (/= s "Function cancelled")   ; If an error (such as CTRL-C) occurs
      (princ (strcat "\nError: " s)) ; while this command is active...
   )
   (setq p nil)                      ; Free selection set
   (setq *error* olderr)             ; Restore old *error* handler
   (princ)
)

(defun C:CHGTEXT (/ p l n e os as ns st s nsl osl sl si chf chm olderr)
   (setq olderr  *error*             ; Initialize variables
         *error* chgterr
         chm     0)
   (setq p (ssget "all"))                  ; Select objects
   (if p (progn                      ; If any objects selected
;      (while (= 0 (setq osl (strlen (setq os (getstring t "\nOld string: ")))))
      (while (= 0 (setq osl (strlen (setq os "模具费"))))
            (princ "Null input invalid")
      )
;      (setq nsl (strlen (setq ns (getstring t "\nNew string: "))))
      (setq nsl (strlen (setq ns "修改模具费用")))
      (setq l 0 n (sslength p))
      (while (< l n)                 ; For each selected object...
         (setq e (entget (ssname p l)))
         (cond ((= "TEXT" (cdr (assoc 0 e)))              ; Look for TEXT entity type (group 0)  
                    (setq chf nil si 1)
                    (setq s (cdr (setq as (assoc 1 e))))
                    (while (= osl (setq sl (strlen
                                  (setq st (substr s si osl)))))
                       (if (= st os)
                           (progn
                             (setq s (strcat (substr s 1 (1- si)) ns
                                             (substr s (+ si osl))))
                             (setq chf t) ; Found old string
                             (setq si (+ si nsl))
                           )
                           (setq si (1+ si))
                       )
                    )
                    (if chf (progn        ; Substitute new string for old
                       (setq e (subst (cons 1 s) as e))
                       (entmod e)         ; Modify the TEXT entity
                       (setq chm (1+ chm))
                    ))
               )                      ; Treat TEXT entity type (group 0)  
               ((= "MTEXT" (cdr (assoc 0 e)))              ; Look for MTEXT entity type (group 0)  
                    (setq chf nil si 1)
                    (setq s (cdr (setq as (assoc 1 e))))
                    (while (= osl (setq sl (strlen
                                  (setq st (substr s si osl)))))
                       (if (= st os)
                           (progn
                             (setq s (strcat (substr s 1 (1- si)) ns
                                             (substr s (+ si osl))))
                             (setq chf t) ; Found old string
                             (setq si (+ si nsl))
                           )
                           (setq si (1+ si))
                       )
                    )
                    (if chf (progn        ; Substitute new string for old
                       (setq e (subst (cons 1 s) as e))
                       (entmod e)         ; Modify the MTEXT entity
                       (setq chm (1+ chm))
                    ))
               )                      ; Treat MTEXT entity type (group 0)  
               ((= "ATTDEF" (cdr (assoc 0 e)))              ; Look for Attrib entity type (group 0)  
                    (setq chf nil si 1)
                    (setq s (cdr (setq as (assoc 2 e))))
                    (while (= osl (setq sl (strlen
                                  (setq st (substr s si osl)))))
                       (if (= st os)
                           (progn
                             (setq s (strcat (substr s 1 (1- si)) ns
                                             (substr s (+ si osl))))
                             (setq chf t) ; Found old string
                             (setq si (+ si nsl))
                           )
                           (setq si (1+ si))
                       )
                    )
                    (if chf (progn        ; Substitute new string for old
                       (setq e (subst (cons 2 s) as e))
                       (entmod e)         ; Modify the TEXT entity
                       (setq chm (1+ chm))
                    ))
               )                      ; Treat TEXT entity type (group 0)  
         )        ;cond
         (setq l (1+ l))
      )
   ))
   (princ "Changed ")                ; Print total lines changed
   (princ chm)
   (princ " text lines.")
   (terpri)
   (setq *error* olderr)             ; Restore old *error* handler
   (princ)
)
(defun c:jiamu2cege()
  (C:CHGTEXT)(princ)
)
(princ)

最佳答案

查看完整内容

最简单就是在后面加一段,要改多少个就加多少段
发表于 2021-6-24 19:55 | 显示全部楼层
最简单就是在后面加一段,要改多少个就加多少段




本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x

评分

参与人数 1金钱 +10 收起 理由
tigcat + 10 很给力!解决了多年难题

查看全部评分

回复

使用道具 举报

 楼主| 发表于 2021-6-25 09:16 | 显示全部楼层
start4444 发表于 2021-6-25 00:17
最简单就是在后面加一段,要改多少个就加多少段

大师,你好,我这边cad是2016的  测试了一下  好像出错了,不知道怎么回事
显示是这样的      错误: no function definition: C:CHGTEXT
回复

使用道具 举报

发表于 2021-6-25 09:34 | 显示全部楼层
ninja37 发表于 2021-6-25 09:16
大师,你好,我这边cad是2016的  测试了一下  好像出错了,不知道怎么回事
显示是这样的      错误: no  ...

你看看原来加载的那个命令代码在不在,不行把最后那句删了
回复

使用道具 举报

 楼主| 发表于 2021-6-25 13:40 | 显示全部楼层
start4444 发表于 2021-6-25 09:34
你看看原来加载的那个命令代码在不在,不行把最后那句删了

你好 谢谢了事情解决了  我把代码中的defun  tt5 改成了 chgtext就好了
回复

使用道具 举报

发表于 2023-3-12 09:27 来自手机 | 显示全部楼层
start4444 发表于 2021-6-24 19:55
最简单就是在后面加一段,要改多少个就加多少段

能不能支持属性快文字
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-3-28 21:31 , Processed in 0.246973 second(s), 26 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表