ninja37 发表于 2021-6-24 19:55:45

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

求各位大师帮修改一下 ,修改文本,现在有一个程序,只能修改一个文字,现在要求能替换 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)

start4444 发表于 2021-6-24 19:55:46

最简单就是在后面加一段,要改多少个就加多少段




ninja37 发表于 2021-6-25 09:16:15

start4444 发表于 2021-6-25 00:17
最简单就是在后面加一段,要改多少个就加多少段

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

start4444 发表于 2021-6-25 09:34:15

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

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

ninja37 发表于 2021-6-25 13:40:47

start4444 发表于 2021-6-25 09:34
你看看原来加载的那个命令代码在不在,不行把最后那句删了

你好 谢谢了事情解决了我把代码中的defuntt5 改成了 chgtext就好了

书院扫地僧 发表于 2023-3-12 09:27:21

start4444 发表于 2021-6-24 19:55
最简单就是在后面加一段,要改多少个就加多少段

能不能支持属性快文字
页: [1]
查看完整版本: 求 修改文本替换命令,可以替换2种文字