Jump to content

Lisp lệnh LL trong chủ đề “Viết Lisp theo yêu cầu”


I'm sorry! Đọc chưa kỹ yêu cầu của bạn.
Chỉ cần thêm hàm round và sửa 1 dòng lệnh là OK. Không cần set lại Units, bạn cứ để precision 0.0000 cũng được.
Code đã sửa:

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 6523_ll.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 LL để chạy lisp.

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

(Defun round(x i / j) ;;;Round x, i digit after decimal point, return REAL
   (setq j (expt 10 i))
   (/ (float (fix (+ 0.5 (* x j)))) j) 
) 
;;;------------------------------------------------------------------------------------
(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 (rtos (round L 0)) "/" (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 #6523 để đọc hướng dẫn và trao đổi thêm.

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