Jump to content

viết lisp tính chiều dài trung bình của nhiều đoạn thẳng

Kiểu đại loại thế này :

(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 1000))
 (alert (strcat "Chieu dai trung binh cac doan vua chon là : "(rtos tbinh 2 2)))
) 

 

bạn ơi muốn thêm đuôi

mà sao mình load list vào đánh lệnh tb thì nó toàn lên cái bảng Insert Table là sao :unsure:

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

  1. Chép mã nguồn, lưu thành tệp 155670_tb.lsp.
  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 TB để chạy lisp.

Mã nguồn 155670_tb.lsp (trích từ bài viết gốc)

(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 1000))
 (alert (strcat "Chieu dai trung binh cac doan vua chon là : "(rtos tbinh 2 2)))
)

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 #155670 để đọc hướng dẫn và trao đổi thêm.

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