Chuyển đến nội dung
Diễn đàn CADViet
Đăng nhập để thực hiện theo  
taipham

Nhờ Sửa Lisp Replace Text Nhanh

Các bài được khuyến nghị

(defun c:rpl()
    (setq olsosmode (getvar "OSMODE"))
    (setvar "OSMODE" 0)
    (setq p (ssget))  
    (if p
 (progn
            (setq osl (strlen (setq os (getstring "\nOld string: " t))))
            (setq nsl (strlen (setq ns (getstring "\nNew string: " t))))
     (setq l 0 chm 0 n (sslength p))
     (setq adj
  (cond
      ((/= osl nsl) (- nsl osl))
      (T nsl)
  )
     )
 (while (< l n)                  
     (setq d (entget (setq e (ssname p l))))
     (if (and (= (atext 0) "INSERT")(= (atext 66) 1))
  (progn
      (setq e (entnext e))
      (while e
   (setq d (entget e))
   (cond
       ((= (atext 0) "ATTRIB")
    (setq chf nil si 1)
    (setq s (cdr (setq as (assoc 1 d))))
    (while (= osl (setq sl (strlen
        (setq st (substr s si osl)))))
        (cond
     ((= st os)
         (setq s (strcat (substr s 1 (1- si)) ns
         (substr s (+ si osl))))
         (setq chf t)
         (setq si (+ si adj))
     )
        )
    (setq si (1+ si))
       )
       (if chf
    (progn       
        (setq d (subst (cons 1 s) as d))
        (entmod d)       
        (entupd e)       
        (setq chm (1+ chm))
    )
       )
       (setq e (entnext e))
       )
       ((= (atext 0) "SEQEND")
    (setq e nil))
       (T (setq e (entnext e)))
                        )
      )
  )
     )
            (if (= "MTEXT"            ; Look for MTEXT entity type (group 0)
               (cdr (assoc 0 (setq e (entget (ssname p l))))))
                  (progn
                     (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))
               ))
            )
         )
     (if (= "DIMENSION"            ; Look for DIMENSION entity type (group 0)
               (cdr (assoc 0 (setq e (entget (ssname p l))))))
                  (progn
                     (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))
               ))
            )
         )
     (if (= "TEXT"            ; Look for TEXT entity type (group 0)
               (cdr (assoc 0 (setq e (entget (ssname p l))))))
                  (progn
                     (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))
               ))
            )
         )
     (setq l (1+ l))
 )
 )
    )
    (if (> chm 1)
       (princ (strcat "\nUpdated " (itoa chm) " text strings"))
       (princ (strcat "\nUpdated " (itoa chm) " text string"))
    )
    (setvar "OSMODE" oldosmode)
    (terpri)
)
;
(defun atext (num)
   (cdr (assoc num d))
)

Nhờ các anh chị sửa giúp đoạn lisp trên, thay vì khi điền Old text và New text thì có thêm phần pick chọn text có sẵn trong bản vẽ và lấy nội dung đó để replace luôn. Mong được các anh chị giúp đỡ. Xin cảm ơn!

Chia sẻ bài đăng này


Liên kết tới bài đăng
Chia sẻ trên các trang web khác

Không có file để thử, nhưng bạn thử thay dòng:

(setq nsl (strlen (setq ns (getstring "\nNew string: " t))))
bằng:

(setq nsl (strlen (setq ns (H:get-string))))

Và thêm đoạn:

(defun H:get-string( / str text)
 (cond ((> (strlen (setq str (getstring "\nNew string <Pick>: " T))) 0) str)
    ((while (not text)
    (prompt "\nPick: ")
    (setq text (ssget "+.:E:S" '((0 . "*TEXT"))))
    )
   (setq str (cdr (assoc 1 (entget (ssname text 0))))))
 )
)

vào cuối lisp

==>>> Khi dùng, muốn chọn text thì enter

Chia sẻ bài đăng này


Liên kết tới bài đăng
Chia sẻ trên các trang web khác

Không có file để thử, nhưng bạn thử thay dòng:

(setq nsl (strlen (setq ns (getstring "\nNew string: " t))))

bằng:

(setq nsl (strlen (setq ns (H:get-string))))

Và thêm đoạn:

(defun H:get-string( / str text)

 (cond ((> (strlen (setq str (getstring "\nNew string <Pick>: " T))) 0) str)

    ((while (not text)

    (prompt "\nPick: ")

    (setq text (ssget "+.:E:S" '((0 . "*TEXT"))))

    )

   (setq str (cdr (assoc 1 (entget (ssname text 0))))))

 )

)

vào cuối lisp

==>>> Khi dùng, muốn chọn text thì enter

Cảm ơn anh, em làm theo hướng dẫn của anh, nhưng khi chạy lisp thì bị đứng. Và em muốn là phần Old text và New text đều pick chọn từ text có sẵn luôn anh ạ!

Chia sẻ bài đăng này


Liên kết tới bài đăng
Chia sẻ trên các trang web khác

Có thể lỗi do code cũ hay ... mình không muốn "đột nhập" :D

Chỉ có thể sửa cho bạn thế này thôi :

(defun c:rpl2()
    (setq olsosmode (getvar "OSMODE"))
    (setvar "OSMODE" 0)
    (setq p (ssget))  
    (if p
 (progn
            (setq osl (strlen (setq os (H:get-string "Old string "))))
            (setq nsl (strlen (setq ns (H:get-string "New string "))))
     (setq l 0 chm 0 n (sslength p))
     (setq adj
  (cond
      ((/= osl nsl) (- nsl osl))
      (T nsl)
  )
     )
 (while (< l n)                  
     (setq d (entget (setq e (ssname p l))))
     (if (and (= (atext 0) "INSERT")(= (atext 66) 1))
  (progn
      (setq e (entnext e))
      (while e
   (setq d (entget e))
   (cond
       ((= (atext 0) "ATTRIB")
    (setq chf nil si 1)
    (setq s (cdr (setq as (assoc 1 d))))
    (while (= osl (setq sl (strlen
        (setq st (substr s si osl)))))
        (cond
     ((= st os)
         (setq s (strcat (substr s 1 (1- si)) ns
         (substr s (+ si osl))))
         (setq chf t)
         (setq si (+ si adj))
     )
        )
    (setq si (1+ si))
       )
       (if chf
    (progn       
        (setq d (subst (cons 1 s) as d))
        (entmod d)       
        (entupd e)       
        (setq chm (1+ chm))
    )
       )
       (setq e (entnext e))
       )
       ((= (atext 0) "SEQEND")
    (setq e nil))
       (T (setq e (entnext e)))
                        )
      )
  )
     )
            (if (= "MTEXT"            ; Look for MTEXT entity type (group 0)
               (cdr (assoc 0 (setq e (entget (ssname p l))))))
                  (progn
                     (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))
               ))
            )
         )
     (if (= "DIMENSION"            ; Look for DIMENSION entity type (group 0)
               (cdr (assoc 0 (setq e (entget (ssname p l))))))
                  (progn
                     (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))
               ))
            )
         )
     (if (= "TEXT"            ; Look for TEXT entity type (group 0)
               (cdr (assoc 0 (setq e (entget (ssname p l))))))
                  (progn
                     (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))
               ))
            )
         )
     (setq l (1+ l))
 )
 )
    )
    (if (> chm 1)
       (princ (strcat "\nUpdated " (itoa chm) " text strings"))
       (princ (strcat "\nUpdated " (itoa chm) " text string"))
    )
    (setvar "OSMODE" oldosmode)
    (terpri)
)
;
(defun atext (num)
   (cdr (assoc num d))
)
;;==================
(defun H:get-string(show /  str text)
 (cond ((> (strlen (setq str (getstring (strcat "\n" show " <Pick>: ") T))) 0) str)
    ((while (not text)
    (prompt "\nPick: ")
    (setq text (ssget "+.:E:S" '((0 . "*TEXT"))))
    )
   (setq str (cdr (assoc 1 (entget (ssname text 0))))))
 )
)
  • Vote tăng 2

Chia sẻ bài đăng này


Liên kết tới bài đăng
Chia sẻ trên các trang web khác

 

Có thể lỗi do code cũ hay ... mình không muốn "đột nhập" :D

Chỉ có thể sửa cho bạn thế này thôi :

(defun c:rpl2()
    (setq olsosmode (getvar "OSMODE"))
    (setvar "OSMODE" 0)
    (setq p (ssget))  
    (if p
 (progn
            (setq osl (strlen (setq os (H:get-string "Old string "))))
            (setq nsl (strlen (setq ns (H:get-string "New string "))))
     (setq l 0 chm 0 n (sslength p))
     (setq adj
  (cond
      ((/= osl nsl) (- nsl osl))
      (T nsl)
  )
     )
 (while (< l n)                  
     (setq d (entget (setq e (ssname p l))))
     (if (and (= (atext 0) "INSERT")(= (atext 66) 1))
  (progn
      (setq e (entnext e))
      (while e
   (setq d (entget e))
   (cond
       ((= (atext 0) "ATTRIB")
    (setq chf nil si 1)
    (setq s (cdr (setq as (assoc 1 d))))
    (while (= osl (setq sl (strlen
        (setq st (substr s si osl)))))
        (cond
     ((= st os)
         (setq s (strcat (substr s 1 (1- si)) ns
         (substr s (+ si osl))))
         (setq chf t)
         (setq si (+ si adj))
     )
        )
    (setq si (1+ si))
       )
       (if chf
    (progn       
        (setq d (subst (cons 1 s) as d))
        (entmod d)       
        (entupd e)       
        (setq chm (1+ chm))
    )
       )
       (setq e (entnext e))
       )
       ((= (atext 0) "SEQEND")
    (setq e nil))
       (T (setq e (entnext e)))
                        )
      )
  )
     )
            (if (= "MTEXT"            ; Look for MTEXT entity type (group 0)
               (cdr (assoc 0 (setq e (entget (ssname p l))))))
                  (progn
                     (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))
               ))
            )
         )
     (if (= "DIMENSION"            ; Look for DIMENSION entity type (group 0)
               (cdr (assoc 0 (setq e (entget (ssname p l))))))
                  (progn
                     (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))
               ))
            )
         )
     (if (= "TEXT"            ; Look for TEXT entity type (group 0)
               (cdr (assoc 0 (setq e (entget (ssname p l))))))
                  (progn
                     (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))
               ))
            )
         )
     (setq l (1+ l))
 )
 )
    )
    (if (> chm 1)
       (princ (strcat "\nUpdated " (itoa chm) " text strings"))
       (princ (strcat "\nUpdated " (itoa chm) " text string"))
    )
    (setvar "OSMODE" oldosmode)
    (terpri)
)
;
(defun atext (num)
   (cdr (assoc num d))
)
;;==================
(defun H:get-string(show /  str text)
 (cond ((> (strlen (setq str (getstring (strcat "\n" show " <Pick>: ") T))) 0) str)
    ((while (not text)
    (prompt "\nPick: ")
    (setq text (ssget "+.:E:S" '((0 . "*TEXT"))))
    )
   (setq str (cdr (assoc 1 (entget (ssname text 0))))))
 )
)

OKE, thế này là quá oke rồi anh, cảm ơn anh nhiều nhé! chúc anh và diễn đàn luôn thành công! :))

Chia sẻ bài đăng này


Liên kết tới bài đăng
Chia sẻ trên các trang web khác

Tạo một tài khoản hoặc đăng nhập để nhận xét

Bạn cần phải là một thành viên để lại một bình luận

Tạo tài khoản

Đăng ký một tài khoản mới trong cộng đồng của chúng tôi. Điều đó dễ mà.

Đăng ký tài khoản mới

Đăng nhập

Bạn có sẵn sàng để tạo một tài khoản ? Đăng nhập tại đây.

Đăng nhập ngay
Đăng nhập để thực hiện theo  

×