Lisp lệnh TB trong chủ đề “[Yêu cầu] viết lisp tính chiều dài trung bình của nhiều đoạn thẳng”
Bạn kiểm tra lại với lisp này nhé !
Mình check ok mà
(defun add_mline ()
(foreach e_record_sub e_record
(cond ((= 10 (car e_record_sub))
(setq pt1 (cdr e_record_sub)
mline_len 0.0
)
)
((= 11 (car e_record_sub))
(setq pt2 (cdr e_record_sub)
mline_len (+ mline_len (distance pt2 pt1))
pt1 pt2
)
)
)
)
(setq tot_len (+ tot_len mline_len))
(ssdel e_name ss)
)
(defun C:tb (/ tot_len ss e_name e_record e_type)
(grtext -1 "Free from cadviet.com @ketxu")
(setq k (getvar "dimlfac"))
(setq tot_len 0.0)
(setq ss (ssget))
(setq len (sslength ss))
(if (null ss)
(exit)
)
(while (> (sslength ss) 0)
(setq e_name (ssname ss 0))
(setq e_record (entget e_name))
(setq e_type (cdr (assoc '0 e_record)))
(cond ((wcmatch e_type "LINE,ARC,CIRCLE,POLYLINE,LWPOLYLINE,ELLIPSE,SPLINE")
(command "lengthen" e_name "")
(setq tot_len (+ tot_len (getvar "PERIMETER")))
(ssdel e_name ss)
)
((wcmatch e_type "MLINE") (add_mline))
(e_type (ssdel e_name ss))
)
)
(setq tot_len (* k tot_len))
(setq tbinh (/ tot_len len))
;--------------------------------------------
(setq Kieu (strcase (getstring "\nGhi ra Text co san <Co>/Khong: ")))
(Cond
((/= Kieu "K")
(setq elst (entget (car (entsel "\n Thay cho so: "))))
(setq elst (subst (cons 1 (rtos tbinh 2 2)) (assoc 1 elst) elst))
;; doan ma lisp chuyen mau ket qua tinh
(if (assoc 62 elst)
(setq elst (subst (cons 62 80) (assoc 62 elst) elst))
(setq elst (append elst (list (cons 62 80))))
)
(entmod elst)
(princ)
)
((= Kieu "K")
;--------------------------------------------
(setq point (getpoint "\n Chon diem ghi ket qua: "))
(setq th (getvar "textsize"))
(setq th (getstring (strcat "\nChieu cao chu <"(rtos th)"> :")))
(command "TEXT" point th 0 (rtos tbinh 2 2))
(alert (strcat "Chieu dai trung binh cac doan vua chon: "(rtos tbinh 2 2)))
;---------------------------------------------
)
)
(princ)
)
Lệnh: tb nếu chọn ghi ra text có sẵn: bỏ qua là có, nếu không ghi K
Mình check ok mà
(defun add_mline ()
(foreach e_record_sub e_record
(cond ((= 10 (car e_record_sub))
(setq pt1 (cdr e_record_sub)
mline_len 0.0
)
)
((= 11 (car e_record_sub))
(setq pt2 (cdr e_record_sub)
mline_len (+ mline_len (distance pt2 pt1))
pt1 pt2
)
)
)
)
(setq tot_len (+ tot_len mline_len))
(ssdel e_name ss)
)
(defun C:tb (/ tot_len ss e_name e_record e_type)
(grtext -1 "Free from cadviet.com @ketxu")
(setq k (getvar "dimlfac"))
(setq tot_len 0.0)
(setq ss (ssget))
(setq len (sslength ss))
(if (null ss)
(exit)
)
(while (> (sslength ss) 0)
(setq e_name (ssname ss 0))
(setq e_record (entget e_name))
(setq e_type (cdr (assoc '0 e_record)))
(cond ((wcmatch e_type "LINE,ARC,CIRCLE,POLYLINE,LWPOLYLINE,ELLIPSE,SPLINE")
(command "lengthen" e_name "")
(setq tot_len (+ tot_len (getvar "PERIMETER")))
(ssdel e_name ss)
)
((wcmatch e_type "MLINE") (add_mline))
(e_type (ssdel e_name ss))
)
)
(setq tot_len (* k tot_len))
(setq tbinh (/ tot_len len))
;--------------------------------------------
(setq Kieu (strcase (getstring "\nGhi ra Text co san <Co>/Khong: ")))
(Cond
((/= Kieu "K")
(setq elst (entget (car (entsel "\n Thay cho so: "))))
(setq elst (subst (cons 1 (rtos tbinh 2 2)) (assoc 1 elst) elst))
;; doan ma lisp chuyen mau ket qua tinh
(if (assoc 62 elst)
(setq elst (subst (cons 62 80) (assoc 62 elst) elst))
(setq elst (append elst (list (cons 62 80))))
)
(entmod elst)
(princ)
)
((= Kieu "K")
;--------------------------------------------
(setq point (getpoint "\n Chon diem ghi ket qua: "))
(setq th (getvar "textsize"))
(setq th (getstring (strcat "\nChieu cao chu <"(rtos th)"> :")))
(command "TEXT" point th 0 (rtos tbinh 2 2))
(alert (strcat "Chieu dai trung binh cac doan vua chon: "(rtos tbinh 2 2)))
;---------------------------------------------
)
)
(princ)
)
Lệnh: tb nếu chọn ghi ra text có sẵn: bỏ qua là có, nếu không ghi K
- Tải lisp
- 221020_tb.lsp
Cách dùng
- Tải tệp 221020_tb.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 TB để chạy lisp.
Mã nguồn 221020_tb.lsp
;; free lisp from cadviet.com
;;; this lisp was downloaded from https://www.cadviet.com/forum/topic/38453-y%C3%AAu-c%E1%BA%A7u-vi%E1%BA%BFt-lisp-t%C3%ADnh-chi%E1%BB%81u-d%C3%A0i-trung-b%C3%ACnh-c%E1%BB%A7a-nhi%E1%BB%81u-%C4%91o%E1%BA%A1n-th%E1%BA%B3ng/
(defun add_mline ()
(foreach e_record_sub e_record
(cond ((= 10 (car e_record_sub))
(setq pt1 (cdr e_record_sub)
mline_len 0.0
)
)
((= 11 (car e_record_sub))
(setq pt2 (cdr e_record_sub)
mline_len (+ mline_len (distance pt2 pt1))
pt1 pt2
)
)
)
)
(setq tot_len (+ tot_len mline_len))
(ssdel e_name ss)
)
(defun C:tb (/ tot_len ss e_name e_record e_type)
(grtext -1 "Free from cadviet.com @ketxu")
(setq k (getvar "dimlfac"))
(setq tot_len 0.0)
(setq ss (ssget))
(setq len (sslength ss))
(if (null ss)
(exit)
)
(while (> (sslength ss) 0)
(setq e_name (ssname ss 0))
(setq e_record (entget e_name))
(setq e_type (cdr (assoc '0 e_record)))
(cond ((wcmatch e_type "LINE,ARC,CIRCLE,POLYLINE,LWPOLYLINE,ELLIPSE,SPLINE")
(command "lengthen" e_name "")
(setq tot_len (+ tot_len (getvar "PERIMETER")))
(ssdel e_name ss)
)
((wcmatch e_type "MLINE") (add_mline))
(e_type (ssdel e_name ss))
)
)
(setq tot_len (* k tot_len))
(setq tbinh (/ tot_len len))
;--------------------------------------------
(setq Kieu (strcase (getstring "\nGhi ra Text co san <Co>/Khong: ")))
(Cond
((/= Kieu "K")
(setq elst (entget (car (entsel "\n Thay cho so: "))))
(setq elst (subst (cons 1 (rtos tbinh 2 2)) (assoc 1 elst) elst))
;; doan ma lisp chuyen mau ket qua tinh
(if (assoc 62 elst)
(setq elst (subst (cons 62 80) (assoc 62 elst) elst))
(setq elst (append elst (list (cons 62 80))))
)
(entmod elst)
(princ)
)
((= Kieu "K")
;--------------------------------------------
(setq point (getpoint "\n Chon diem ghi ket qua: "))
(setq th (getvar "textsize"))
(setq th (getstring (strcat "\nChieu cao chu <"(rtos th)"> :")))
(command "TEXT" point th 0 (rtos tbinh 2 2))
(alert (strcat "Chieu dai trung binh cac doan vua chon: "(rtos tbinh 2 2)))
;---------------------------------------------
)
)
(princ)
)
Thảo luận gốc
Lisp được chia sẻ trong chủ đề [Yêu cầu] viết lisp tính chiều dài trung bình của nhiều đoạn thẳng. Xem đúng bài viết #221020 để đọc hướng dẫn và trao đổi thêm.