Jump to content

Lisp tính tổng độ dài đoạn thẳng.

-Mình tìm trên diễn đàn có lisp tính độ dài các đoạn thẳng và có chỉnh sửa lại theo nhu cầu công việc. Nhưng có 1 điểm không biết phải làm sao để làm tròn số lên 0.5m.
VD: Tổng độ dài các đoạn thẳng là 19213m mình muốn làm tròn lên 19500m.Còn 19501m thì làm tròn lên 20000m. Tóm lại là làm tròn từ 001--->499 = 500 và từ 501--->999 =1000

;; free lisp from cadviet.com
;;; this lisp was downloaded from https://www.cadviet.com/forum/index.php?showtopic=3778
(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))
)
)
(setq p (getpoint "\n Chon diem nhap ket qua" ))
(setq i (getreal "\n Chieu cao ha xuong" ))
(setq h (getreal "\n Nhap chieu cao text ket qua "))
(command "text" p h "0" (strcat(rtos tot_len 2 0)"/" (rtos i 2 0)))
;(prompt (strcat "\nTotal length is: " (rtos tot_len 2 2)))
(princ)
)
http://www.cadviet.c...3/35974_cad.dwg
Xin cám ơn mong mọi người giúp đỡ.

Cách dùng

  1. Tải tệp 195561_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 195561_tg.lsp

;; free lisp from cadviet.com
;;; this lisp was downloaded from https://www.cadviet.com/forum/topic/63090-nh%E1%BB%9D-ch%E1%BB%89nh-s%E1%BB%ADalisp-t%C3%ADnh-t%E1%BB%95ng-%C4%91%E1%BB%99-d%C3%A0i-%C4%91o%E1%BA%A1n-th%E1%BA%B3ng/
;; free lisp from cadviet.com
;;; this lisp was downloaded from https://www.cadviet.com/forum/index.php?showtopic=3778
(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))
)
)
(setq p (getpoint "\n Chon diem nhap ket qua" ))
(setq i (getreal "\n Chieu cao ha xuong" ))
(setq h (getreal "\n Nhap chieu cao text ket qua "))
(command "text" p h "0" (strcat(rtos tot_len 2 0)"/" (rtos i 2 0)))
;(prompt (strcat "\nTotal length is: " (rtos tot_len 2 2)))
(princ)
)

Thảo luận gốc

Lisp được chia sẻ trong chủ đề [Nhờ chỉnh sửa]Lisp tính tổng độ dài đoạn thẳng.. Xem đúng bài viết #195561 để đọc hướng dẫn và trao đổi thêm.

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