Jump to content

Lisp lệnh TG trong chủ đề “Routine tính tổng chiều dài các đối tượng”

Nhờ sự giúp đỡ của bác Nguyễn Hoành em có viết 1 đoạn code nhằm tính tổng chiều dài của các đối tượng chọn (*Line, Arc, Circle, Elippse). Các bác dùng thử và cho ý kiến.

Một lần nữa xin cảm ơn bác Hoành về sự nhiệt tình giúp đỡ anh em.

Chúc cả nhà luôn vui..

 

(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:tg (/ tot_len ss e_name e_record e_type)
 (setq tot_len 0.0)
 (setq ss (ssget))
 (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))
   )
 )
 (prompt (strcat "\nTotal length is: " (rtos tot_len 2 2)))
 (princ)
)

đoạn lisp này giúp cho mình thống kê vật liệu đơn giản hơn rất nhiều, cám ơn bạn lắm lắm!

Cách dùng

  1. Tải tệp 11042_tg.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 TG để chạy lisp.

Mã nguồn 11042_tg.lsp

;; free lisp from cadviet.com
;;; this lisp was downloaded from https://www.cadviet.com/forum/topic/763-routine-t%C3%ADnh-t%E1%BB%95ng-chi%E1%BB%81u-d%C3%A0i-c%C3%A1c-%C4%91%E1%BB%91i-t%C6%B0%E1%BB%A3ng/?tab=comments#comment-2481
(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:tg (/ tot_len ss e_name e_record e_type)
 (setq tot_len 0.0)
 (setq ss (ssget))
 (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))
   )
 )
 (prompt (strcat "\nTotal length is: " (rtos tot_len 2 2)))
 (princ)
)

Thảo luận gốc

Lisp được chia sẻ trong chủ đề Routine tính tổng chiều dài các đối tượng. Xem đúng bài viết #11042 để đọc hướng dẫn và trao đổi thêm.

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