Jump to content

Đo đường polyline

Sửa tạm cho bạn về việc thuận nghịch, còn những cái khác bạn bổ túc thêm.

(defun c:dopl(/ E LA OLDOS P1 P2 PT3)
(setq oldos (getvar "osmode"))
(setvar "osmode" 15359)
(setq la (getvar "clayer"))
(command "undo" "be")
(setq e (car (entsel "\n Chon polyline can do "))
        p1 (getpoint "\n Chon diem bat dau ")
        p2 (getpoint "\n Chon diem ket thuc ")
pt3 (getpoint "\n Chon diem dat ")
)
(tiep e p1 p2 pt3) (setq p1 p2)
(while (setq p2 (getpoint "\n Chon diem ket thuc ")) 
(tiep e p1 p2 pt3) (setq p1 p2)
)
(command "undo" "e")
(setvar "osmode" oldos)
(princ)
)
 
(defun tiep (e p1 p2 pt3 / A1 A2 A3 E1 GOC H LA LEN P3 P4 P5 P6 P7 P8 PA1 PA2 PT1 V1 V2 V3)
(setq p1 (vlax-curve-getclosestpointto e p1)
p2 (vlax-curve-getclosestpointto e p2))
(setq pa1 (vlax-curve-getparamatpoint e p1)
        pa2 (vlax-curve-getparamatpoint e p2)
        v1 (vlax-curve-getfirstderiv e pa1)
        v2 (vlax-curve-getfirstderiv e pa2)
        len (- (vlax-curve-getdistatpoint e p2) (vlax-curve-getdistatpoint e p1))
 pt1 (vlax-curve-getClosestPointTo e pt3)
 h (distance pt3 pt1)
) 
  (setq a1 (atan (/ (cadr v1) (car v1)))
     a2 (atan (/ (cadr v2) (car v2)))
     p3 (polar p1 (+ a1 (/ pi 2)) h)
     p4 (polar p2 (+ a2 (/ pi 2)) h)
     p5 (polar p3 (+ a1 (/ pi 2))  (/ (* 0.5 h) h))
     p6 (polar p4 (+ a2 (/ pi 2))  (/ (* 0.5 h) h))
) 
(command "offset" h e "non" pt3 "")
(setq e1 (entlast)) 
  (command "break" e1 (if (> len 0) p4 p3) (vlax-curve-getendpoint e1))
(command "break" e1 (if (> len 0) p3 p4) (vlax-curve-getstartpoint e1))
(command "change" e1 "" "p" "la" la "")
(command "line" "non" p1 "non" p5 "")
(command "line" "non" p2 "non" p6 "")
(command "pline" "non" p3 "w" 0 0.25 "non" (polar p3 a1 0.75) "") 
(command "pline" "non" p4 "w" 0 0.25 "non" (polar p4 (+ a2 pi) 0.75) "")
 
(if (setq p7 (vlax-curve-getpointatdist e1 (/ (abs len) 2)))
(progn
(setq v3 (vlax-curve-getfirstderiv e1 (vlax-curve-getparamatpoint e1 p7))
a3 (atan (/ (cadr v3) (car v3)))
 goc (+ a3 (/ pi 2))
 p8 (polar p7 goc 0.5))
(command "text" "non" "J" "C" p8 1 (* 180 (+ a3 (/ pi 2))) (rtos (abs len) 2 2))
)
)
)

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 336866_dopl.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 DOPL để chạy lisp.

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

(defun c:dopl(/ E LA OLDOS P1 P2 PT3)
(setq oldos (getvar "osmode"))
(setvar "osmode" 15359)
(setq la (getvar "clayer"))
(command "undo" "be")
(setq e (car (entsel "\n Chon polyline can do "))
        p1 (getpoint "\n Chon diem bat dau ")
        p2 (getpoint "\n Chon diem ket thuc ")
pt3 (getpoint "\n Chon diem dat ")
)
(tiep e p1 p2 pt3) (setq p1 p2)
(while (setq p2 (getpoint "\n Chon diem ket thuc ")) 
(tiep e p1 p2 pt3) (setq p1 p2)
)
(command "undo" "e")
(setvar "osmode" oldos)
(princ)
)
 
(defun tiep (e p1 p2 pt3 / A1 A2 A3 E1 GOC H LA LEN P3 P4 P5 P6 P7 P8 PA1 PA2 PT1 V1 V2 V3)
(setq p1 (vlax-curve-getclosestpointto e p1)
p2 (vlax-curve-getclosestpointto e p2))
(setq pa1 (vlax-curve-getparamatpoint e p1)
        pa2 (vlax-curve-getparamatpoint e p2)
        v1 (vlax-curve-getfirstderiv e pa1)
        v2 (vlax-curve-getfirstderiv e pa2)
        len (- (vlax-curve-getdistatpoint e p2) (vlax-curve-getdistatpoint e p1))
 pt1 (vlax-curve-getClosestPointTo e pt3)
 h (distance pt3 pt1)
) 
  (setq a1 (atan (/ (cadr v1) (car v1)))
     a2 (atan (/ (cadr v2) (car v2)))
     p3 (polar p1 (+ a1 (/ pi 2)) h)
     p4 (polar p2 (+ a2 (/ pi 2)) h)
     p5 (polar p3 (+ a1 (/ pi 2))  (/ (* 0.5 h) h))
     p6 (polar p4 (+ a2 (/ pi 2))  (/ (* 0.5 h) h))
) 
(command "offset" h e "non" pt3 "")
(setq e1 (entlast)) 
  (command "break" e1 (if (> len 0) p4 p3) (vlax-curve-getendpoint e1))
(command "break" e1 (if (> len 0) p3 p4) (vlax-curve-getstartpoint e1))
(command "change" e1 "" "p" "la" la "")
(command "line" "non" p1 "non" p5 "")
(command "line" "non" p2 "non" p6 "")
(command "pline" "non" p3 "w" 0 0.25 "non" (polar p3 a1 0.75) "") 
(command "pline" "non" p4 "w" 0 0.25 "non" (polar p4 (+ a2 pi) 0.75) "")
 
(if (setq p7 (vlax-curve-getpointatdist e1 (/ (abs len) 2)))
(progn
(setq v3 (vlax-curve-getfirstderiv e1 (vlax-curve-getparamatpoint e1 p7))
a3 (atan (/ (cadr v3) (car v3)))
 goc (+ a3 (/ pi 2))
 p8 (polar p7 goc 0.5))
(command "text" "non" "J" "C" p8 1 (* 180 (+ a3 (/ pi 2))) (rtos (abs len) 2 2))
)
)
)

Thảo luận gốc

Lisp được chia sẻ trong chủ đề [Nhờ sửa lisp] Đo đường polyline. Xem đúng bài viết #336866 để đọc hướng dẫn và trao đổi thêm.

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