Lisp lệnh C2P trong chủ đề “Chia đất!!!”
Bài toán của anh có thể giải quyết bằng lisp "chia đất" này. Trình tự thực hiện như sau, tham khảo file ssg đã chạy ví dụ:
https://www.cadviet.com/upfiles/TinhDT159B.zip
1. Là hình nguyên thuỷ theo bản vẽ của anh
2. Copy 1/2 hình, dùng lisp ConvertToPline (cái này ssg đã post ở đâu đó rồi, tiện thể post lại ở đây), lệnh C2P. Khi chương trình hỏi "Delete source object? Y/N" thì chọn "Y", xoá luôn cái spline cho đỡ lằng nhằng!
Thực hiện lần lượt cho 2 spline. Kết quả: 2 đường spline biến thành 2 pline riêng biệt
3. Lấy đối xứng, gọi lệnh Pedit với tuỳ chọn j (joint) -> toàn bộ thành 1 pline khép kín duy nhất. Tính được:
- Diện tích toàn phần S = 1286.9223
- Diện tích giới hạn bởi đường y=18 (không phải y=23.5 như anh đã nêu, có lẽ nhầm?): S1 = 915.9525
- Hệ số k = S1/S = 0.711739
4. Vẽ 1 line có góc nghiêng tuỳ ý, cắt pline kín ở 2 điểm, dùng cái lisp "chia đất" (lisp đầu tiên của topic này), chạy lệnh DL.
Đường chia dừng lại ở vị trí có S1 = 915.9528 -> sai số rất nhỏ so với các tính toán kỹ thuật thông thường
Tương tự như vậy, anh có thể thực hiện với nhiều đường chia có góc nghiêng theo ý muốn.
- Tải lisp
- 48987_c2p.lsp
Cách dùng
- Tải tệp 48987_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 48987_c2p.lsp
;; free lisp from cadviet.com
;;; this lisp was downloaded from https://www.cadviet.com/forum/topic/8950-chia-%C4%91%E1%BA%A5t/
;;;***********************************************************
;;;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) ;;;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
(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
)
(initget "Y N") ;;;Init keywords
(setq ans (getkword "\nDelete source objects? [Yes/No] :")) ;;;Get answer from user
(if (= ans "Y") (command "erase" ss "")) ;;;Erase source objects if ans = "y" or "Y"
(setvar "osmode" oldos) ;;;Reset osmode
(princ) ;;;Silent quit
)
;;;-------------------------------------------------------------
Thảo luận gốc
Lisp được chia sẻ trong chủ đề Chia đất!!!. Xem đúng bài viết #48987 để đọc hướng dẫn và trao đổi thêm.