Lisp lệnh LL trong chủ đề “Viết Lisp theo yêu cầu”
Mình không bao giờ bỏ qua bước test khi lập trình. Tuy nhiên, cũng có thể là chưa lường hết các trường hợp có thể xảy ra. Nghe bạn phản hồi, mình đã test lại, kết quả vẫn đúng! Chả hiểu trên máy bạn thế nào!
Thôi được, bạn hãy delete toàn bộ "tàn tích" của các code trước đây, thay bằng đoạn sau. Hy vọng là sẽ đúng ý trong mọi trường hợp:
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 6859_ll.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 LL để chạy lisp.
Mã nguồn 6859_ll.lsp (trích từ bài viết gốc)
;;;-------------------------------------------------------------------------------
(Defun rnd(x) (fix (+ 0.5 x)) ) ;;;Round x, return INT
;;;-------------------------------------------------------------------------------
(Defun round(x i / j) ;;;Round x, i digit after decimal point, return INT
(setq j (expt 10 i))
(/ (float (fix (+ 0.5 (* x j)))) j)
) ;;;return REAL
;;;------------------------------------------------------------------------------------
(defun getTw() ;;;Get current text width factor
(cdr (assoc 41 (tblsearch "style" (getvar "textstyle"))))
)
;;;------------------------------------------------------------------------------------
(defun getTh( / Th) ;;;Get current textheight or textsize
(if (= (setq Th (cdr (assoc 40 (tblsearch "style" (getvar "textstyle"))))) 0) (getvar "textsize") Th)
)
;;;------------------------------------------------------------------------------------
(defun emkT (S p)
;;;Write text S at point p by entmake function
;;;Text style, heigh and width factor get from current values
(entmake (list (cons 0 "TEXT") (cons 10 p) (cons 40 (getTh))
(cons 41 (getTw)) (cons 1 S) (cons 7 (getvar "textstyle"))))
)
;;;------------------------------------------------------------------------------------
(defun calcL (e) ;;;Calculate length of Line, Pline, 3dPoly, Spline, Circle, Arc, Polygon
(vlax-curve-getDistAtParam e (vlax-curve-getEndParam e))
)
;;;------------------------------------------------------------------------------------
(defun C:LL( / Opt S1 S2 e p L)
(vl-load-com)
(if (not preT) (setq preT "L="))
(if (not sufT) (setq sufT ""))
(setq S1 preT S2 sufT)
(prompt (strcat "\nMeasure and write length. Current prefix:[" preT "]\tSuffix:[" sufT "]"))
(initget "Y N")
(setq Opt (getkword "\nChange prefix and suffix? [Yes/No] :"))
(if (not Opt) (setq Opt "N"))
(if (= Opt "Y") (progn
(setq
S1 (getstring "\nPrefix :") preT S1
S2 (getstring "\nSuffix :") sufT S2
)
))
(while (setq e (car (entsel "\nSelect Object or :")))
(setq
p (getpoint "\nBase point: ")
L (calcL e)
)
(emkT (strcat S1 (itoa (rnd L)) "/" (rtos (round (* L 0.97) 2)) S2) p)
)
(princ)
)
;;;------------------------------------------------------------------------------------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 #6859 để đọc hướng dẫn và trao đổi thêm.