Lisp lệnh C2P trong chủ đề “nối nhiều spline!!!!”
Đầu tiên, cho phép Tue_NV gửi lời cảm ơn đến bác Nguyễn Hoành, bác ssg cùng các bác trên diễn đàn. Các bác đã đặt những viên gạch đầu tiên vô cùng quý giá. Nếu hiểu đúng và đầy đủ bản chất của lệnh Lisp thì ta có thể làm rất nhiều việc lắm, tạo nên những ứng dụng cực kì hiệu quả cho công việc của mỗi chúng ta.
Chào các bạn Nani, tucdrom, haanh :
Đoạn Code sau Tue_NV đã cải tiến dựa trên Lisp của bác ssg và cải tiến dựa trên Code của bác Nguyễn Hoành đã trả lời cho bài viết Làm thế nào để tìm số đối tượng sinh ra bởi lệnh Boundary
Dựa vào ý của bạn tucdrom mình đã biến cái Spline thành Pline sau đó nối chúng lại với nhau thành 1 Pline duy nhất trong cùng một đọan Lisp
Chúc các bạn thành công
- Tải lisp
- 54747_c2p.lsp
Cách dùng
- Tải tệp 54747_c2p.lsp về máy.
- 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 C2P để chạy lisp.
Mã nguồn 54747_c2p.lsp
;; free lisp from cadviet.com
;;; this lisp was downloaded from https://www.cadviet.com/forum/topic/10030-n%E1%BB%91i-nhi%E1%BB%81u-spline/
;;;***********************************************************
;;;CONVERT TO PLINES PROGRAM WITH FULL COMMENTS!
;;;Convert all objects: Line, Pline, Spline, Arc, Circle, Ellipse_
;;;to Plines. Length of 1 segment is specified by user
;;;Copy & Paste to Notepad, Saveas *.lsp, Appload then Type C2P to run
;;;Happy New Year 2008!
;;;Written by ssg - January 2008 - www.cadviet.com
;;;***********************************************************
;;;-------------------------------------------------------------
(defun makepl ( e d1 / ps pe d d2 p2 tr ss1 cur frome toe) ;;;Make pline along curve e. Length of 1 segment = d1
(vl-load-com) ;;;Load Visual LISP extensions before use vlax-xxxx functions
(setq
ps (vlax-curve-getStartPoint e) ;;;Start point
pe (vlax-curve-getEndPoint e) ;;;End point
d (vlax-curve-getDistAtParam e (vlax-curve-getEndParam e)) ;;;Length of curve e
d2 d1 ;;;Init variable distance
)
(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 curve
(command p2) ;;;Continue pline command from current point to p2
(setq d2 (+ d2 d1)) ;;;Increase distance d2 by d1
) ;;;End while
(command pe "") ;;;Pline to pe and finish command
)
;;;-------------------------------------------------------------
(defun C:C2P( / d1 ss oldos i e ans) ;;;Convert to Plines
(if (not d0) (setq d0 0.5)) ;;;Init dividual distance, global variable
(setq d1 (getreal (strcat "\nLength of 1 segment <" (rtos d0) ">:"))) ;;;Input distance
(if d1 (setq d0 d1) (setq d1 d0)) ;;;Reset or get distance
(setq
ss (ssget '((0 . "LINE,LWPOLYLINE,SPLINE,ARC,CIRCLE,ELLIPSE"))) ;;;Selection set
oldos (getvar "osmode") ;;;Save osmode
i 0 ;;;Init counter
)
(setvar "osmode" 0) ;;;Disable osmode
(setq frome (entlast))
(repeat (sslength ss) ;;;Repeat for all entities in ss
(setq e (ssname ss i)) ;;;Set e for entity with ordinal i in selection set ss
(makepl e d1) ;;;Use makepl function. Make pline along e
(setq i (1+ i)) ;;;Increase counter
)
(setq toe (entlast))
(setq cur frome
ss1 (ssadd)
)
(while (not (eq cur toe)) ;; chon cac doi tuong tu frome den toe
(setq cur (entnext cur)
ss1 (ssadd cur ss1)))
(initget "Y N") ;;;Init keywords
(setq ans (getkword "\nDelete source objects? [Yes/No] <N>:")) ;;;Get answer from user
(if (= ans "Y") (command "erase" ss "")) ;;;Erase source objects if ans = "y" or "Y"
(initget "C K") ;;;Init keywords
(setq ans (getkword "\nBan co muon noi cac Pline vua tao khong [C/K] <K>:")) ;;;Get answer from user
(if (= ans "C") (Command "Pedit" "M" ss1 "" "j" "0.5" "")) ;;;join Pline
(setvar "osmode" oldos) ;;;Reset osmode
(princ) ;;;Silent quit
)
;;;-------------------------------------------------------------
Thảo luận gốc
Lisp được chia sẻ trong chủ đề nối nhiều spline!!!!. Xem đúng bài viết #54747 để đọc hướng dẫn và trao đổi thêm.