Jump to content

Lisp Tìm Và Thay Thế Text Theo Mẫu Có Sẵn

Bạn sử dụng lệnh FIND của CAD cũng được, tuy nhiên nhược điểm là gọi lệnh này sẽ hiện Dialog và mất thời gian load. Góp vui cho bạn một LISP tìm và thay thế tự động!

;superstr.lsp l. gabriel 11-11-1996  22:04:42
;
;object: string search and replace. Works for both text and attributes. Program
;        will globally search and replace every text/attribute within the selection set.
;
;Rev 1.0 Added Dimension string search and replace l. gabriel 06.12.08
;
(defun atext (num)
   (cdr (assoc num d))
)
;
(defun echooff ()
  (setq oldecho (getvar "CMDECHO"))
  (setq oldblip (getvar "BLIPMODE"))
  (setq oldosm (getvar "OSMODE"))
  (setvar "CMDECHO" 0)
  (setvar "BLIPMODE" 0)
  (setvar "OSMODE" 0)
  (setq olderror_echo *ERROR*)
  (terpri)
  (defun *ERROR* (msg)
    (princ " \n")
    (princ msg)
    (echoon)
  )
)
;
(defun echoon ()
  (setvar "CMDECHO" oldecho)
  (setvar "BLIPMODE" oldblip)
  (setvar "OSMODE" oldosm)
  (setq *ERROR* olderror_echo)
  (princ)
)
;super search and replace routine
(defun c:FEQ()
    (echooff)
    (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)
    (echoon)
)

Cách dùng

  1. Tải tệp 413281_feq.lsp về máy.
  2. Trong AutoCAD gõ lệnh APPLOAD, chọn tệp vừa tải rồi bấm Load. Cũng có thể kéo thả tệp .lsp vào cửa sổ bản vẽ.
  3. Gõ lệnh FEQ để chạy lisp.

Mã nguồn 413281_feq.lsp

;; free lisp from cadviet.com
;;; this lisp was downloaded from https://www.cadviet.com/forum/topic/167937-xin-lisp-t%C3%ACm-v%C3%A0-thay-th%E1%BA%BF-text-theo-m%E1%BA%ABu-c%C3%B3-s%E1%BA%B5n/
;superstr.lsp l. gabriel 11-11-1996  22:04:42
;
;object: string search and replace. Works for both text and attributes. Program
;        will globally search and replace every text/attribute within the selection set.
;
;Rev 1.0 Added Dimension string search and replace l. gabriel 06.12.08
;
(defun atext (num)
   (cdr (assoc num d))
)
;
(defun echooff ()
  (setq oldecho (getvar "CMDECHO"))
  (setq oldblip (getvar "BLIPMODE"))
  (setq oldosm (getvar "OSMODE"))
  (setvar "CMDECHO" 0)
  (setvar "BLIPMODE" 0)
  (setvar "OSMODE" 0)
  (setq olderror_echo *ERROR*)
  (terpri)
  (defun *ERROR* (msg)
    (princ " \n")
    (princ msg)
    (echoon)
  )
)
;
(defun echoon ()
  (setvar "CMDECHO" oldecho)
  (setvar "BLIPMODE" oldblip)
  (setvar "OSMODE" oldosm)
  (setq *ERROR* olderror_echo)
  (princ)
)
;super search and replace routine
(defun c:FEQ()
    (echooff)
    (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)
    (echoon)
)

Thảo luận gốc

Lisp được chia sẻ trong chủ đề [Xin] Lisp Tìm Và Thay Thế Text Theo Mẫu Có Sẵn. Xem đúng bài viết #413281 để đọc hướng dẫn và trao đổi thêm.

Về Thư viện AutoLisp (trang 216)