Lisp lệnh DT trong chủ đề “Đổi tên Block được chọn !”
Xin lỗi các bạn hôm trước mình đã sửa rồi quên up lại nên bị sai tí chút
code fix
(defun c:dt () (setq oldos (getvar "osmode")) (setvar "osmode" 0) (command "undo" "be") (setq ten (cdr(assoc 2 (entget(car(entsel "\nChon block dien hinh: ")))))) (princ "\nChon block can doi ten: ") (setq ssc (ssget (list(cons 0 "INSERT")(cons 2 ten))) tm (getstring "\nNhap ten moi: ") ) (command "rename" "b" ten tm) (command "copyclip" ssc "") (command "block" "block_temp" "0,0" ssc "") (command "insert" "block_temp" "0,0" 1 1 0) (setq el (entlast)) (setq pt (car(acet-ent-geomextents el))) (command "undo" "e" "undo" 1) (command "erase" ssc "") (command "pasteclip" pt) (setvar "osmode" oldos) )
Bạn cho hiển thị tên block cũ vào trước đoạn chèn tên block mới thì tốt, dễ kiểm soát (xem hình đính kèm)

Mình trích
(vl-load-com)
(or *kpblc-activedoc*
(setq *kpblc-activedoc* (vla-get-activedocument (vlax-get-acad-object)))
) ;_ end of or
(defun c:vla-rename (/ ent name)
(vla-startundomark *kpblc-activedoc*)
(if
(and (not (vl-catch-all-error-p
(vl-catch-all-apply
'(lambda ()
(setq ent (car (entsel "\nSelect a block to be renamed")))
) ;_ end of lambda
) ;_ end of vl-catch-all-apply
) ;_ end of vl-catch-all-error-p
) ;_ end of not
ent
(= (cdr (assoc 0 (entget ent))) "INSERT")
(/= (substr (cdr (assoc 2 (entget ent))) 1 2) "*U")
(not (vl-catch-all-error-p
(vl-catch-all-apply
'(lambda ()
(setq name (getstring t
(strcat "\nEnter new name <"
(cdr (assoc 2 (entget ent)))
"> : "
) ;_ end of strcat
) ;_ end of getstring
) ;_ end of setq
) ;_ end of lambda
) ;_ end of vl-catch-all-apply
) ;_ end of vl-catch-all-error-p
) ;_ end of not
(/= (vl-string-trim " " name))
) ;_ end of and
(if (vl-catch-all-error-p
(vl-catch-all-apply
'(lambda ()
(vla-put-name
(vla-item (vla-get-blocks *kpblc-activedoc*)
(cdr (assoc 2 (entget ent)))
) ;_ end of vla-item
name
) ;_ end of vla-put-name
) ;_ end of lambda
) ;_ end of vl-catch-all-apply
) ;_ end of vl-catch-all-error-p
(princ (strcat "\nCan't rename a block "
(cdr (assoc 2 (entget ent)))
" with new name "
name
) ;_ end of strcat
) ;_ end of princ
) ;_ end of if
(princ (strcat "\nA error has been catched:"
"\nSelection error | Selected entity isn't a block "
"| It's a unnamed or dynamic block"
) ;_ end of strcat
) ;_ end of princ
) ;_ end of if
(vla-endundomark *kpblc-activedoc*)
(princ)
) ;_ end of defun
đoạn code có tính năng đó lên cho bạn tham khảo:
- Tải lisp
- 288122_dt.lsp
Cách dùng
- Tải tệp 288122_dt.lsp về máy.
- 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ẽ.
- Gõ lệnh DT để chạy lisp.
Mã nguồn 288122_dt.lsp
;; free lisp from cadviet.com
;;; this lisp was downloaded from https://www.cadviet.com/forum/topic/45803-%C4%91%E1%BB%95i-t%C3%AAn-block-%C4%91%C6%B0%E1%BB%A3c-ch%E1%BB%8Dn/
(defun c:dt ()
(setq oldos (getvar "osmode"))
(setvar "osmode" 0)
(command "undo" "be")
(setq ten (cdr(assoc 2 (entget(car(entsel "\nChon block dien hinh: "))))))
(princ "\nChon block can doi ten: ")
(setq ssc (ssget (list(cons 0 "INSERT")(cons 2 ten)))
tm (getstring "\nNhap ten moi: ")
)
(command "rename" "b" ten tm)
(command "copyclip" ssc "")
(command "block" "block_temp" "0,0" ssc "")
(command "insert" "block_temp" "0,0" 1 1 0)
(setq el (entlast))
(setq pt (car(acet-ent-geomextents el)))
(command "undo" "e"
"undo" 1)
(command "erase" ssc "")
(command "pasteclip" pt)
(setvar "osmode" oldos)
)Thảo luận gốc
Lisp được chia sẻ trong chủ đề Đổi tên Block được chọn !. Xem đúng bài viết #288122 để đọc hướng dẫn và trao đổi thêm.