Lệnh Trim mở rộng
Xuất phát từ nhu cầu Trim cần có nhiều lựa chọn hơn nữa so với lệnh Trim gốc của Cad, tôi viết lisp này để phục vụ bà con lối xóm.
...........................
;Co 3 kieu Trim:;1). Trim theo Phia: pick diem phia nao thi Trim phia do (tuong tu offset).;2). Trim doan Ngan: Trim phan ngan.;3). Trim doan Dai: Trim phan dai.;Khong Trim cac truong hop: doi tuong la duong kin; giao nhau tai hon 1 diem; giao nhau bieu kien.(defun C:HA( / ent0 ent ent2 ss typ p ento lstg len1 len2)(vl-load-com) (command "undo" "be") (setq cmd (getvar "cmdecho")) (setvar "cmdecho" 0)(if(and(setq ent0 (car (entsel "\nChon 1 doi tuong dao cat: ")))(princ "\nChon cac doi tuong bi cat...")(setq ss (ssget '((0 . "Line,Polyline,Lwpolyline,Spline,Arc")))))(progn(initget "P N D")(setq typ (getkword "\nChon kieu Trim <P>: "))(if (not typ) (setq typ "P"))(if (= typ "P")(progn(initget 65)(setq p (getdist (GetP (vlax-curve-getStartPoint ent0) (vlax-curve-getEndPoint ent0) (/ (HA:LenCur ent0) 2) ent0) "\nPick chon phia can Trim: "))(command "offset" 1E-3 ent0 p "")(setq ento (entlast))))(foreach ent1 (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss)))(setq lstg (HA:Giao (vlax-ename->vla-object ent0) (vlax-ename->vla-object ent1) acExtendNone))(if (and (= (length lstg) 1) (not (equal (car lstg) (vlax-curve-getStartPoint ent1) 1E-3)) (not (equal (car lstg) (vlax-curve-getEndPoint ent1) 1E-3)))(progn(setq ent (entlast))(command ".break" ent1 "_non" (car lstg) "_non" (car lstg))(setq ent2 (car (HA:GetNewEnts ent)))(setq len1 (HA:LenCur ent1) len2 (HA:LenCur ent2))(cond((or (and (= typ "N") (> len1 len2)) (and (= typ "D") (< len1 len2))) (entdel ent2))((or (and (= typ "N") (< len1 len2)) (and (= typ "D") (> len1 len2))) (entdel ent1))((= typ "P")(if (HA:Giao (vlax-ename->vla-object ento) (vlax-ename->vla-object ent1) acExtendNone)(entdel ent1)(entdel ent2)))))))(if ento (entdel ento))))(command "undo" "e") (setvar "cmdecho" cmd) (princ))(defun GetP (pg ph kc cur / dg dh dp)(setq dg (vlax-curve-getDistAtPoint cur pg))(setq dh (vlax-curve-getDistAtPoint cur ph))(if (> dh dg)(setq dp (+ dg kc))(setq dp (- dg kc)))(vlax-curve-getPointAtDist cur dp))(defun HA:GetNewEnts (ename / new) (while (setq ename (entnext ename)) (if (entget ename) (setq new (cons ename new)))) new)(defun HA:LenCur(ent)(vlax-curve-getDistAtParam ent (vlax-curve-getEndParam ent)))(defun HA:Giao(obj1 obj2 mode / l r)(setq l (vlax-invoke obj1 'intersectwith obj2 mode))(repeat (/ (length l) 3)(setq r (cons (list (car l) (cadr l) (caddr l)) r) l (cdddr l)))r)
Hàm này : (defun HA:GetNewEnts (ename / new) (while (setq ename (entnext ename)) (if (entget ename) (setq new (cons ename new)))) new)
gặp (setq ss (ssget '((0 . "Line,Polyline,Lwpolyline,Spline,Arc")))))
bởi "entnext" của POLYLINE ra Vertext.
nên thay, nên thay -> Nếu gặp đối tượng là POLYLINE: Lisp sẽ lỗi ngay..............
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 236275_ha.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 HA để chạy lisp.
Mã nguồn 236275_ha.lsp (trích từ bài viết gốc)
;Co 3 kieu Trim:;1). Trim theo Phia: pick diem phia nao thi Trim phia do (tuong tu offset).;2). Trim doan Ngan: Trim phan ngan.;3). Trim doan Dai: Trim phan dai.;Khong Trim cac truong hop: doi tuong la duong kin; giao nhau tai hon 1 diem; giao nhau bieu kien.(defun C:HA( / ent0 ent ent2 ss typ p ento lstg len1 len2) (vl-load-com) (command "undo" "be") (setq cmd (getvar "cmdecho")) (setvar "cmdecho" 0) (if (and (setq ent0 (car (entsel "\nChon 1 doi tuong dao cat: "))) (princ "\nChon cac doi tuong bi cat...") (setq ss (ssget '((0 . "Line,Polyline,Lwpolyline,Spline,Arc"))))) (progn (initget "P N D") (setq typ (getkword "\nChon kieu Trim [theo_Phia/doan_Ngan/doan_Dai] <P>: ")) (if (not typ) (setq typ "P")) (if (= typ "P") (progn(initget 65) (setq p (getdist (GetP (vlax-curve-getStartPoint ent0) (vlax-curve-getEndPoint ent0) (/ (HA:LenCur ent0) 2) ent0) "\nPick chon phia can Trim: ")) (command "offset" 1E-3 ent0 p "") (setq ento (entlast)))) (foreach ent1 (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss))) (setq lstg (HA:Giao (vlax-ename->vla-object ent0) (vlax-ename->vla-object ent1) acExtendNone)) (if (and (= (length lstg) 1) (not (equal (car lstg) (vlax-curve-getStartPoint ent1) 1E-3)) (not (equal (car lstg) (vlax-curve-getEndPoint ent1) 1E-3))) (progn (setq ent (entlast)) (command ".break" ent1 "_non" (car lstg) "_non" (car lstg)) (setq ent2 (car (HA:GetNewEnts ent))) (setq len1 (HA:LenCur ent1) len2 (HA:LenCur ent2)) (cond ((or (and (= typ "N") (> len1 len2)) (and (= typ "D") (< len1 len2))) (entdel ent2)) ((or (and (= typ "N") (< len1 len2)) (and (= typ "D") (> len1 len2))) (entdel ent1)) ((= typ "P") (if (HA:Giao (vlax-ename->vla-object ento) (vlax-ename->vla-object ent1) acExtendNone) (entdel ent1) (entdel ent2))))))) (if ento (entdel ento)))) (command "undo" "e") (setvar "cmdecho" cmd) (princ))(defun GetP (pg ph kc cur / dg dh dp) (setq dg (vlax-curve-getDistAtPoint cur pg)) (setq dh (vlax-curve-getDistAtPoint cur ph)) (if (> dh dg) (setq dp (+ dg kc)) (setq dp (- dg kc))) (vlax-curve-getPointAtDist cur dp))(defun HA:GetNewEnts (ename / new) (while (setq ename (entnext ename)) (if (entget ename) (setq new (cons ename new)))) new)(defun HA:LenCur(ent) (vlax-curve-getDistAtParam ent (vlax-curve-getEndParam ent)))(defun HA:Giao(obj1 obj2 mode / l r) (setq l (vlax-invoke obj1 'intersectwith obj2 mode)) (repeat (/ (length l) 3) (setq r (cons (list (car l) (cadr l) (caddr l)) r) l (cdddr l))) r) Thảo luận gốc
Lisp được chia sẻ trong chủ đề [Đã xong] Lệnh Trim mở rộng. Xem đúng bài viết #236275 để đọc hướng dẫn và trao đổi thêm.