Jump to content

Lệnh Trim mở rộng

Hình tôi khoanh tròn nhé!

https://www.cadviet.com/upfiles/3/67029_trim_loi.dwg

 

Tue_NV đã có tính tới "vòi bấu víu không được chọn" mà chưa tính tới "vòi bấu víu" được chọn nên mới xảy ra tình trạng này.

Đã sửa lại code. Bác DVH và các bạn thử xem :

@KetXu: Có TH như bạn nêu. Nên chăng sử dụng Offset dẫn hướng rồi trim. Ý bạn thế nào?

 

 
(defun c:mtr(/ ssdao sscat pt Tue-list-ss Tue-geom-inters dgiao lst-cat lst-cat2 XYmin XYmax Lx Ly)
;;;;;write by Tue_NV
  (defun Tue-ss-list (L-ss-vlaobj / n L Lst ssg vlaobj)
  (mapcar 'set '(ssg vlaobj) L-ss-vlaobj)
  (setq L (sslength ssg))
  (Repeat L
        (setq ename (ssname ssg (setq L (1- L))))
(setq Lst (cons (if vlaobj (vlax-ename->vla-object ename) ename) Lst))
  ) 
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 
(defun Tue-geom-inters(e1 e2 flag / Lst_tong Lst);;;Tue-list-tach
  (if (= (type e1) 'ENAME) (setq e1 (vlax-ename->vla-object e1)))
  (if (= (type e2) 'ENAME) (setq e2 (vlax-ename->vla-object e2)))
  (vlax-invoke e1 'IntersectWith e2 flag)
)
(command "undo" "be")
(setvar "cmdecho" 0)
  (princ "\nChon cac doi tuong bi cat :")
   (setq sscat (ssget '((0 . "*line,Arc"))))
  (command ".draworder" sscat "" "F")
  (princ "\nChon Dao cat :")
  (setq ssdao (ssget ":S" '((0 . "*Line,Arc,ELLIPSE,CIRCLE"))))
  (sssetfirst nil ssdao)
  (setq sscat2 (ssadd))
  (foreach x (vl-remove (ssname ssdao 0) (Tue-ss-list (list sscat)))
     (if (Tue-geom-inters x (ssname ssdao 0) 0) 
(setq lst-cat (append lst-cat (list x)))
(setq lst-cat2 (append lst-cat2 (list x)))
    )
  )
(foreach x (Tue-ss-list (list sscat))
(vla-getboundingbox (vlax-ename->vla-object x) 'minp 'maxp)
(setq LX (append LX (list (car(safearray-value minp)))
   (list (car(safearray-value maxp)))))
(setq LY (append LY (list (cadr(safearray-value minp)))
   (list (cadr(safearray-value maxp)))))
)
   (setq XYmin (list (apply 'min LX) (apply 'min LY) 0.0))
   (setq XYmax (list (apply 'max LX) (apply 'max LY) 0.0))
 
  (initget "N D")
  (setq pt (getpoint "\nPick chon theo phia /doanNgan/doanDai] : "))
(command "zoom" XYmin XYmax)
(if (eq (type pt) 'LIST)
  (progn (mapcar 'entdel lst-cat2)
  (foreach x lst-cat
       (if (Tue-geom-inters
   (setq entps (entmakex (list (cons 0 "LINE") (cons 10 pt) (cons 11 (vlax-curve-getendpoint x)) (cons 60 1) )));
           (ssname ssdao 0) 0)
 (Command "trim" ssdao x "" (vlax-curve-getstartpoint x) "")
 (Command "trim" ssdao x "" (vlax-curve-getendpoint x) "")
)
        (entdel entps) 
   )
(mapcar 'entdel lst-cat2)
  )
 )
(if (= pt "N")
  (progn (mapcar 'entdel lst-cat2)
  (foreach x lst-cat
    (if (= (length (setq dgiao (Tue-geom-inters (ssname ssdao 0) x 0))) 3)
       (if (< (vlax-curve-getparamatpoint x (Tue-geom-inters (ssname ssdao 0) x 0))
     (/ (vlax-curve-getendparam x) 2.0))
 (Command "trim" ssdao x "" (vlax-curve-getstartpoint x) "")
 (Command "trim" ssdao x "" (vlax-curve-getendpoint x) "")
)
      )
   )
(mapcar 'entdel lst-cat2)
  )
 )
 (if (= pt "D")
  (progn (mapcar 'entdel lst-cat2)
  (foreach x lst-cat
    (if (= (length (setq dgiao (Tue-geom-inters (ssname ssdao 0) x 0))) 3)
       (if (> (vlax-curve-getparamatpoint x (Tue-geom-inters (ssname ssdao 0) x 0))
     (/ (vlax-curve-getendparam x) 2.0))
 (Command "trim" ssdao x "" (vlax-curve-getstartpoint x) "")
 (Command "trim" ssdao x "" (vlax-curve-getendpoint x) "")
)
      )
   )
(mapcar 'entdel lst-cat2)
  )
 )
(command "zoom" "p")
(command "undo" "end")
 )

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

  1. Chép mã nguồn, lưu thành tệp 236581_mtr.lsp.
  2. 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ẽ.
  3. Gõ lệnh MTR để chạy lisp.

Mã nguồn 236581_mtr.lsp (trích từ bài viết gốc)

 
(defun c:mtr(/ ssdao sscat pt Tue-list-ss Tue-geom-inters dgiao lst-cat lst-cat2 XYmin XYmax Lx Ly)
;;;;;write by Tue_NV
  (defun Tue-ss-list (L-ss-vlaobj / n L Lst ssg vlaobj)
  (mapcar 'set '(ssg vlaobj) L-ss-vlaobj)
  (setq L (sslength ssg))
  (Repeat L
        (setq ename (ssname ssg (setq L (1- L))))
(setq Lst (cons (if vlaobj (vlax-ename->vla-object ename) ename) Lst))
  ) 
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 
(defun Tue-geom-inters(e1 e2 flag / Lst_tong Lst);;;Tue-list-tach
  (if (= (type e1) 'ENAME) (setq e1 (vlax-ename->vla-object e1)))
  (if (= (type e2) 'ENAME) (setq e2 (vlax-ename->vla-object e2)))
  (vlax-invoke e1 'IntersectWith e2 flag)
)
(command "undo" "be")
(setvar "cmdecho" 0)
  (princ "\nChon cac doi tuong bi cat :")
   (setq sscat (ssget '((0 . "*line,Arc"))))
  (command ".draworder" sscat "" "F")
  (princ "\nChon Dao cat :")
  (setq ssdao (ssget ":S" '((0 . "*Line,Arc,ELLIPSE,CIRCLE"))))
  (sssetfirst nil ssdao)
  (setq sscat2 (ssadd))
  (foreach x (vl-remove (ssname ssdao 0) (Tue-ss-list (list sscat)))
     (if (Tue-geom-inters x (ssname ssdao 0) 0) 
(setq lst-cat (append lst-cat (list x)))
(setq lst-cat2 (append lst-cat2 (list x)))
    )
  )
(foreach x (Tue-ss-list (list sscat))
(vla-getboundingbox (vlax-ename->vla-object x) 'minp 'maxp)
(setq LX (append LX (list (car(safearray-value minp)))
   (list (car(safearray-value maxp)))))
(setq LY (append LY (list (cadr(safearray-value minp)))
   (list (cadr(safearray-value maxp)))))
)
   (setq XYmin (list (apply 'min LX) (apply 'min LY) 0.0))
   (setq XYmax (list (apply 'max LX) (apply 'max LY) 0.0))
 
  (initget "N D")
  (setq pt (getpoint "\nPick chon theo phia /doanNgan/doanDai] : "))
(command "zoom" XYmin XYmax)
(if (eq (type pt) 'LIST)
  (progn (mapcar 'entdel lst-cat2)
  (foreach x lst-cat
       (if (Tue-geom-inters
   (setq entps (entmakex (list (cons 0 "LINE") (cons 10 pt) (cons 11 (vlax-curve-getendpoint x)) (cons 60 1) )));
           (ssname ssdao 0) 0)
 (Command "trim" ssdao x "" (vlax-curve-getstartpoint x) "")
 (Command "trim" ssdao x "" (vlax-curve-getendpoint x) "")
)
        (entdel entps) 
   )
(mapcar 'entdel lst-cat2)
  )
 )
(if (= pt "N")
  (progn (mapcar 'entdel lst-cat2)
  (foreach x lst-cat
    (if (= (length (setq dgiao (Tue-geom-inters (ssname ssdao 0) x 0))) 3)
       (if (< (vlax-curve-getparamatpoint x (Tue-geom-inters (ssname ssdao 0) x 0))
     (/ (vlax-curve-getendparam x) 2.0))
 (Command "trim" ssdao x "" (vlax-curve-getstartpoint x) "")
 (Command "trim" ssdao x "" (vlax-curve-getendpoint x) "")
)
      )
   )
(mapcar 'entdel lst-cat2)
  )
 )
 (if (= pt "D")
  (progn (mapcar 'entdel lst-cat2)
  (foreach x lst-cat
    (if (= (length (setq dgiao (Tue-geom-inters (ssname ssdao 0) x 0))) 3)
       (if (> (vlax-curve-getparamatpoint x (Tue-geom-inters (ssname ssdao 0) x 0))
     (/ (vlax-curve-getendparam x) 2.0))
 (Command "trim" ssdao x "" (vlax-curve-getstartpoint x) "")
 (Command "trim" ssdao x "" (vlax-curve-getendpoint x) "")
)
      )
   )
(mapcar 'entdel lst-cat2)
  )
 )
(command "zoom" "p")
(command "undo" "end")
 )

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 #236581 để đọc hướng dẫn và trao đổi thêm.

Về Thư viện AutoLisp (trang 132)