Lisp lệnh DOPL trong chủ đề “[Nhờ sửa lisp] Đo đường polyline”
Cái này chắc khá hơn.
(defun c:dopl(/ E LA OLDOS P1 P2 PT3) (defun angf(d1 d2 ang) (angle d1 (inters d1 (polar d1 ang 1) d2 (polar d2 (+ ang (* pi 0.5)) 1) nil)) ) (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) 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) a1 (atan (/ (cadr v1) (car v1))) a2 (atan (/ (cadr v2) (car v2))) p3 (polar p1 (angf pt1 pt3 (+ a1 (/ pi 2))) h) p4 (polar p2 (angf pt1 pt3 (+ a2 (/ pi 2))) h) p5 (polar p3 (angle p1 p3) 0.5) p6 (polar p4 (angle p2 p4) 0.5) ) (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)) ) ) ) ;;; (setq oldos (getvar "osmode")) (setvar "osmode" 15359) (setvar "cmdecho" 0) (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) (setvar "cmdecho" 1) (princ) )
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 337839_dopl.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 DOPL để chạy lisp.
Mã nguồn 337839_dopl.lsp (trích từ bài viết gốc)
(defun c:dopl(/ E LA OLDOS P1 P2 PT3)
(defun angf(d1 d2 ang)
(angle d1 (inters d1 (polar d1 ang 1) d2 (polar d2 (+ ang (* pi 0.5)) 1) nil))
)
(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)
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)
a1 (atan (/ (cadr v1) (car v1)))
a2 (atan (/ (cadr v2) (car v2)))
p3 (polar p1 (angf pt1 pt3 (+ a1 (/ pi 2))) h)
p4 (polar p2 (angf pt1 pt3 (+ a2 (/ pi 2))) h)
p5 (polar p3 (angle p1 p3) 0.5)
p6 (polar p4 (angle p2 p4) 0.5)
)
(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))
)
)
)
;;;
(setq oldos (getvar "osmode"))
(setvar "osmode" 15359) (setvar "cmdecho" 0)
(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) (setvar "cmdecho" 1)
(princ)
)
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 #337839 để đọc hướng dẫn và trao đổi thêm.