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.
Trên forum đã có vài topic nói về Trim mở rộng, nhưng theo những nhu cầu khác nhau, nằm lẻ tẻ, và hầu như cũng chưa hoàn thiện lắm.
Lệnh Trim mở rộng này có 3 tùy chọn: Trim theo từng phía + Trim đoạn ngắn + Trim đoạn dài.
Đối tượng Trim: Line, Polyline, Lwpolyline, Spline, Arc.
Ai tải về dùng tốt thì nhớ like. Ai thấy chưa ưng bụng thì góp ý để sửa, đừng ném đá.
Hình để xem:

File Cad để test:
https://www.cadviet.com/upfiles/3/67029_trim_nguoc.dwg
File Lsp để dùng:
;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)
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.