Lisp thao tác trong 3D
Em đã sửa lại mấy cái lisp trên kia một chút (lisp 2 và lisp 3), bây giờ nó sẽ cho phép em chọn nhiều trục ống, vẽ đồng thời một lúc. Tại từ trước tới giờ em chỉ quen thao tác với entsel, bây giờ biết thêm lệnh ssget(cái này là nhờ em ngâm cứu lại một cái lisp em nhờ anh ketxu viết hộ, cảm ơn anh í một lần nữa
), nên em cải tiến chúng một chút như sau, công việc nhanh hơn hẳn 
Lisp 2: vẽ ống trụ:
Lisp 2: vẽ ống trụ:
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 148817_ovu.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 OVU để chạy lisp.
Mã nguồn 148817_ovu.lsp (trích từ bài viết gốc)
;;; Ve ong hinh tru co truc cho truoc
;;; By pdle
(defun c:otr (/ ent pt1 pt2 ent1 ent2 Routerpre Rinnerpre)
;; Tat che do bat diem
(setq OldOS (getvar "osmode"))
(setvar "osmode" 0)
;; Nhap ban kinh ngoai
(if (= Router nil) (setq Routerpre 0.04) (setq Routerpre Router))
(setq Router (getreal (strcat "\nBan kinh ngoai < " (rtos Routerpre) " > : ")))
(if (= Router nil) (setq Router Routerpre))
;; Nhap ban kinh trong
(if (= Rinner nil) (setq Rinnerpre 0.03) (setq Rinnerpre Rinner))
(setq Rinner (getreal (strcat "\nBan kinh trong < " (rtos Rinnerpre) " > : ")))
(if (= Rinner nil) (setq Rinner Rinnerpre))
(prompt "\nNhap truc ong: ")
(foreach ent (vl-remove-if 'listp (mapcar 'cadr (ssnamex (ssget '((0 . "LINE"))))))
(setq pt1 (acet-dxf 10 (entget ent)) pt2 (acet-dxf 11 (entget ent)))
;; Ve hinh tru va thuc hien lenh subtract
(command "cylinder" pt1 Router "a" pt2)
;; Neu ban kinh trong khac 0, thi thuc hien ve hinh tru voi ban kinh trong va subtract
(if (/= Rinner 0)
(progn
(setq ent1 (entlast))
(command "cylinder" pt1 Rinner "a" pt2)
(setq ent2 (entlast))
(command "subtract" ent1 "" ent2 "")
))
)
;; Tra lai che do bat diem
(setvar "osmode" OldOS)
)
;; Ve ong hinh vuong co truc cho truoc, co canh vuong go voi 1 trong 3 truc toa do cua WCS
;; By pdle
(defun c:ovu(/ pt1 pt2 pt3 ct con1 con2 i j k ent ent1 ent2 pt2new OldOS Dimepre Thickpre Orenpre X1 Y1 Z1 X2 Y2 Z2 Outer Inner)
;; Tat che do bat diem
(setq OldOS (getvar "osmode"))
(setvar "osmode" 0)
;; Dua he truc toa do ve WCS
(command "ucs" "w")
;; Kich thuoc ngoai cua ong vuong
(if (= Dime nil) (setq Dimepre 0.1) (setq Dimepre Dime))
(setq Dime (getreal (strcat "\nKich thuoc ngoai cua ong < " (rtos Dimepre) " > : ")))
(if (= Dime nil) (setq Dime Dimepre))
;; Do day thanh ong
(if (= Thick nil) (setq Thickpre 0.006) (setq Thickpre Thick))
(setq Thick (getreal (strcat "\nDo day cua ong < " (rtos Thickpre) " > : ")))
(if (= Thick nil) (setq Thick Thickpre))
;; Tinh toan kich thuoc mat ngoai va mat trong ong
(setq Outer (* 0.5 Dime) Inner (- Outer Thick))
;; Xac dinh truc dinh huong cua ong
(if (= Oren nil) (setq Orenpre "Z") (setq Orenpre Oren))
(setq Oren (strcase (getstring (strcat "\nTruc dinh huong (X,Y,Z) < " Orenpre " >: "))))
(if (= Oren "") (setq Oren Orenpre))
(prompt "\nNhap truc cua ong: ")
(foreach ent (vl-remove-if 'listp (mapcar 'cadr (ssnamex (ssget '((0 . "LINE"))))))
(setq
pt1 (acet-dxf 10 (entget ent))
pt2 (acet-dxf 11 (entget ent))
X1 (nth 0 pt1) Y1 (nth 1 pt1) Z1 (nth 2 pt1)
X2 (nth 0 pt2) Y2 (nth 1 pt2) Z2 (nth 2 pt2)
i (cond ((= Oren "X") 1) ((= Oren "Y") 0) ((= Oren "Z") 0))
j (cond ((= Oren "X") 0) ((= Oren "Y") 1) ((= Oren "Z") 0))
k (cond ((= Oren "X") 0) ((= Oren "Y") 0) ((= Oren "Z") 1))
pt3 ( list (+ X1 (* k(- Y2 Y1)) (* j (- Z2 Z1))) (+ Y1 (* i(- Z2 Z1)) (* k (- X2 X1))) (+ Z1 (* i(- Y2 Y1)) (* j (- X2 X1))))
)
;; Dua ho truc toa do ve truc cua ong
(command "ucs" "3" pt1 pt2 pt3)
(setq
pt2new (trans pt2 0 1)
ct (list (* 0.5 (nth 0 pt2new)) (* 0.5 (nth 1 pt2new)) (* 0.5 (nth 2 pt2new)))
con1 (list 0 Outer Outer)
con2 (list 0 Inner Inner)
)
;; Ve 2 hinh hop va dung lenh subtract de tao ong
(command "box" "c" ct con1) (setq ent1 (entlast))
(command "box" "c" ct con2) (setq ent2 (entlast))
(command "subtract" ent1 "" ent2 "")
(command "ucs" "w")
)
;; tra lai che do bat diem hien hanh
(setvar "osmode" OldOS)
)Thảo luận gốc
Lisp được chia sẻ trong chủ đề [Hỏi] Lisp thao tác trong 3D. Xem đúng bài viết #148817 để đọc hướng dẫn và trao đổi thêm.