Lisp lệnh S2P trong chủ đề “Em muốn viết một Lisp để chuyển Spline thành Pline”
Mình hiểu thủ thuật của bạn, chỉ cần dùng lệnh pline như dưới đây là OK. Tuy nhiên, cách làm như vậy không hay lắm. Phải "nhờ vả" các lệnh của AutoCAD như measure, divide... để lấy toạ độ điểm là việc làm bất đắc dĩ, khi không còn cách nào khác.Bạn dùng thử trình sau. Tất cả các dòng code mình đều có comment. Nếu còn lơ mơ thì lật Help ra xem, thường họ có kèm ví dụ, đọc là hiểu ngay.
Cái "đinh" của chương trình dưới là dùng họ function vlax-curve-xxxx để xử lý. Cái này hay lắm, dùng cho cả line, pline, spline, circle, arc...
Về cách dùng command, bạn đọc code chắc là hiểu chứ? Bản chất của vấn đề là bạn có thể "chen ngang" các biểu thức lisp nhưng vẫn không huỷ bỏ quá trình đang thực hiện dở dang của command (giống như phương thức transparent vậy).
Có gì vướng mắc bạn cứ nêu, mình sẽ giải thích thêm.
(defun C:S2P( / d1 e ps pe d d2 oldos p2 ans) ;;;Convert Spline to Pline (if (not d0) (setq d0 0.50)) ;;;Init dividual distance, global variable (setq d1 (getreal (strcat "\nLength of 1 segment <" (rtos d0 2 2) ">:"))) ;;;Input distance (if d1 (setq d0 d1) (setq d1 d0)) ;;;Reset or get distance (vl-load-com) ;;;Load Visual LISP extensions before use vlax-xxxx functions (setq e (car (entsel "\nSelect spline:")) ;;;Spline entity ps (vlax-curve-getStartPoint e) ;;;Start point pe (vlax-curve-getEndPoint e) ;;;End point d (vlax-curve-getDistAtPoint e pe) ;;;Length of spline e d2 d1 ;;;Init variable distance oldos (getvar "osmode") ;;;Save osmode ) (setvar "osmode" 0) ;;;Disable osmode (command "pline") ;;;Call pline command (command ps) ;;;Start point (while (<= d2 d) ;;;While not over end point pe (setq p2 (vlax-curve-getPointAtDist e d2)) ;;;Variable point at d2 = length along spline (command p2) ;;;Continue pline command from current point to p2 (setq d2 (+ d2 d1)) ;;;Increment distance d2 by d1 ) (command pe) ;;;Pline command to end point pe (command "") ;;;Finish pline command (initget "Y N") ;;;Init key words (setq ans (getkword "\nErase original spline <N>:")) ;;;Get answer from user (if (= ans "Y") (command "erase" e "")) ;;;Erase spline if ans = "yes" (setvar "osmode" oldos) ;;;Reset osmode (princ) ;;;Silent quit )
Anh ssg ơi,
Thế nếu đối tượng chọn không phải là spline mà là arc hay circle thì phải làm thế nào ạ?
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 11041_s2p.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 S2P để chạy lisp.
Mã nguồn 11041_s2p.lsp (trích từ bài viết gốc)
(defun C:S2P( / d1 e ps pe d d2 oldos p2 ans)
;;;Convert Spline to Pline
(if (not d0) (setq d0 0.50)) ;;;Init dividual distance, global variable
(setq d1 (getreal (strcat "\nLength of 1 segment <" (rtos d0 2 2) ">:"))) ;;;Input distance
(if d1 (setq d0 d1) (setq d1 d0)) ;;;Reset or get distance
(vl-load-com) ;;;Load Visual LISP extensions before use vlax-xxxx functions
(setq
e (car (entsel "\nSelect spline:")) ;;;Spline entity
ps (vlax-curve-getStartPoint e) ;;;Start point
pe (vlax-curve-getEndPoint e) ;;;End point
d (vlax-curve-getDistAtPoint e pe) ;;;Length of spline e
d2 d1 ;;;Init variable distance
oldos (getvar "osmode") ;;;Save osmode
)
(setvar "osmode" 0) ;;;Disable osmode
(command "pline") ;;;Call pline command
(command ps) ;;;Start point
(while (<= d2 d) ;;;While not over end point pe
(setq p2 (vlax-curve-getPointAtDist e d2)) ;;;Variable point at d2 = length along spline
(command p2) ;;;Continue pline command from current point to p2
(setq d2 (+ d2 d1)) ;;;Increment distance d2 by d1
)
(command pe) ;;;Pline command to end point pe
(command "") ;;;Finish pline command
(initget "Y N") ;;;Init key words
(setq ans (getkword "\nErase original spline <N>:")) ;;;Get answer from user
(if (= ans "Y") (command "erase" e "")) ;;;Erase spline if ans = "yes"
(setvar "osmode" oldos) ;;;Reset osmode
(princ) ;;;Silent quit
)Thảo luận gốc
Lisp được chia sẻ trong chủ đề Em muốn viết một Lisp để chuyển Spline thành Pline. Xem đúng bài viết #11041 để đọc hướng dẫn và trao đổi thêm.