Jump to content

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


Bạn dùng lisp này, tên lệnh DC (đo cạnh):

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 18675_dc.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 DC để chạy lisp.

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

;;;-------------------------------------------------------------
(defun Etype (e) (cdr (assoc 0 (entget e)))) ;;;Entity type
;;;-------------------------------------------------------------
(defun GetMid(p1 p2) (polar p1 (angle p1 p2) (/ (distance p1 p2) 2))) ;;;Midpoint
;;;-------------------------------------------------------------
(defun getVert (e / i L) ;;;Return vertexs list of pline e
(setq i 0 L nil)
(vl-load-com)
(repeat (fix (+ (vlax-curve-getEndParam e) 1))
   (setq L (append L (list (vlax-curve-getPointAtParam e i))))
   (setq i (1+ i))
)
L
)
;;;-------------------------------------------------------------
(defun wtxtpa (txt p a vj hj / sty d h LV vcode LH hcode)
;;;Write text. Arguments: txt, point, angle, ver justify, hor justify
(setq
   sty (getvar "textstyle")
   d (tblsearch "style" sty)
   h (cdr (assoc 40 d))
   LV (list "" "B" "M" "T") ;;;Bottom, Middle, Top
   vcode (vl-position vj LV)
   LH (list "L" "C" "R")  ;;;Left, Center, Right
   hcode (vl-position hj LH)
)
(if (= h 0) (setq h (cdr (assoc 42 d))))
(entmake
   (list (cons 0 "TEXT") (cons 7 sty) (cons 1 txt) (cons 10 p) (cons 11 p)
   (cons 40 h) (assoc 41 d) (cons 72 hcode) (cons 73 vcode) (cons 50 a))
)
)
;;;-------------------------------------------------------------
(defun C:DC( / e Lp i p1 p2 L p a) ;;;Do Canh pline
(setq e (car (entsel "\nSelect polyline:")))
(if (/= (etype e) "LWPOLYLINE") (progn (alert "Object is not a polyline!") (exit)))
(setq Lp (getVert e) i 0)
(repeat (- (length Lp) 1)
   (setq
       p1 (nth i Lp)
       p2 (nth (1+ i) Lp)
       L (distance p1 p2)
       p (getmid p1 p2)
       a (angle p1 p2)
       i (1+ i)  
   )
   (if (and (> a (* 0.5 pi)) (< a (* 1.5 pi))) (setq a (+ a pi)))
   (wtxtpa (rtos L) p a "B" "C")
)
(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 #18675 để đọc hướng dẫn và trao đổi thêm.

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