Lisp lệnh DT trong chủ đề “Viết Lisp theo yêu cầu”
Lấy dt hatch cũng như lấy dt polyline mà ( lệnh area trong CAD cho phép chọn cả hatch)
Còn đây là lisp: Tên lệnh "DT", chọn các đối tượng --> chọn lỗ thủng (đối tượng cần trừ dt) --> Chọn text hoặc chỉ điểm chèn kết quả
Chú ý với Tiếng Việt ở Command line
Tệp .lsp gốc không còn trên máy chủ. Hãy chép mã nguồn bên dưới (lấy từ bài viết gốc) vào một tệp văn bản, lưu với đuôi .lsp.
Cách dùng
- Chép mã nguồn, lưu thành tệp 64849_dt.lsp.
- 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 64849_dt.lsp (trích từ bài viết gốc)
(defun myerror (s)
(cond
((= s "quit / exit abort") (princ))
((/= s "Function cancelled") (princ (strcat "\nError: " s)))
)
(setvar "cmdecho" CMD) ; Restore saved modes
(setvar "osmode" OSM)
(setq *error* OLDERR) ; Restore old *error* handler
(princ)
)
(defun bocchu (ss1 c)
(setq ob (entget (ssname ss1 c)))
(setq ts (assoc 1 ob))
(setq a (cdr ts))
)
(defun sothanhchuint (num)
(if (> num 0)
(strcat "+" (rtos num 2 2))
(rtos num 2 2)
)
)
(defun sothanhchuintreal (num) (rtos num 2 2))
(defun sothanhchuintreal1 (num) (rtos num 2 0))
(defun Noichu (Ob newstr key)
(setq txtstr (assoc 1 Ob))
(setq newstr (cons 1 (strcat (cdr txtstr) key newstr)))
(entmod (subst newstr txtstr Ob))
)
(defun thaychu (Ob newstr / obtmp)
(setq txtstr (assoc 1 Ob))
(setq newstr (cons 1 newstr)
obtmp (entmod (subst newstr txtstr Ob))
)
(entupd (cdr (assoc -1 obtmp)))
)
(defun chonchu (dongnhac)
(prompt dongnhac)
(ssget
'((-4 . " (0 . "text")
(0 . "mtext")
(0 . "ATTRIB")
(-4 . "OR>")
)
)
)
(defun chon1chu (dongnhac / obj objtype)
(if (setq obj (nentsel dongnhac))
(setq obj (entget (car obj))
objtype (cdr (assoc 0 obj))
)
)
(if (member objtype '("ATTRIB" "MTEXT" "TEXT"))
(setq obj obj)
)
)
(defun chon (str) (ssget '((cons (0 str)))))
(defun bamchon (st) (entget (car (entsel st))))
(defun bocdt (ss1 c) (entget (ssname ss1 c)))
;;;;================================================
;;;======================================================
;;;Tinh dien tich bang cach chon doi tuong
(defun C:dt (/ pt1 Objare objtxt dtich dtich1 ss1 ss2 ss3 lacol)
(command "UCS" "W" "")
(command "Undo" "Mark")
(setq oldos (getvar "OsMODE"))
(princ "\nChän ®èi tîng cÇn lÊy diÖn tÝch")
(setq Objare (car (nentsel)))
(while
(not
(member (cdr (assoc 0 (entget objare)))
'("POLYLINE" "LWPOLYLINE" "SPLINE" "CIRCLE" "HATCH")
)
)
(prompt
"\n§èi tîng kh«ng ®óng kiÓu! Chän l¹i ®èi tîng cÇn lÊy diÖn tÝch ..."
)
(setq objare (car (nentsel))
)
)
(command "area" "o" Objare)
(setq dtich (getvar "Area"))
(princ "\nChän ®èi tîng cÇn lÊy diÖn tÝch tiÕp theo (Enter ®Ó dõng l¹i)")
(setq Objare (car (nentsel)))
(while (/= Objare nil)
(if
(not
(member (cdr (assoc 0 (entget objare)))
'("POLYLINE" "LWPOLYLINE" "SPLINE" "CIRCLE" "HATCH")
)
)
(prompt
"\n§èi tîng kh«ng ®óng kiÓu! Chän l¹i ®èi tîng cÇn lÊy diÖn tÝch ..."
)
(progn
(command "area" "o" Objare)
(setq dtich1 (getvar "Area")
dtich (+ dtich dtich1)
)
)
)
(princ "\nChän ®èi tîng cÇn lÊy diÖn tÝch tiÕp theo (Enter ®Ó dõng l¹i)")
(setq Objare (car (nentsel)))
)
(princ "\nChän ®èi tîng lç thñng (cÇn trõ diÖn tÝch)...")
(setq Objare (car (nentsel)))
(while (/= Objare nil)
(if
(not
(member (cdr (assoc 0 (entget objare)))
'("POLYLINE" "LWPOLYLINE" "SPLINE" "CIRCLE" "HATCH")
)
)
(prompt
"\n§èi tîng kh«ng ®óng kiÓu! Chän l¹i ®èi tîng ..."
)
(progn
(command "area" "o" Objare)
(setq dtich1 (* -1 (getvar "Area"))
dtich (+ dtich dtich1)
)
)
)
(princ "\nChän ®èi tîng lç thñng tiÕp theo (Enter ®Ó dõng l¹i)")
(setq Objare (car (nentsel)))
)
(setq objtxt (chon1chu "\nChän text chøa kÕt qu¶ diÖn tÝch (Enter ®Ó t¹o text míi)"))
(if (/= objtxt nil)
(progn
(setq st (sothanhchuintreal dtich))
(thaychu objtxt st)
)
(progn
(setq st (sothanhchuintreal dtich))
(setq pt1 (getpoint "\n Diem dat text: "))
(command "text" pt1 "" "" st)
)
)
)Thảo luận gốc
Lisp được chia sẻ trong chủ đề Viết Lisp theo yêu cầu. Xem đúng bài viết #64849 để đọc hướng dẫn và trao đổi thêm.