Lisp lệnh TRIMBLK trong chủ đề “Viết lisp theo yêu cầu [phần 2]”
LISP làm 1 lệnh mà trim được nhiều đoạn cống khác nhau.
Chú ý : Lisp chỉ sử lý trường hợp block hố ga giao với đường ống (Line, Arc, PLine) tại 2 điểm.
Do đó trong file của bạn, lisp không sử lý điểm ngoài cùng.
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 81776_trimblk.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 TRIMBLK để chạy lisp.
Mã nguồn 81776_trimblk.lsp (trích từ bài viết gốc)
(vl-load-com)
(defun C:TRIMBLK (/ ent ipts lstblk lstpts lstptspa obj ss)
;; By : Gia Bach, Copyrightc December 2009 ;;
;; Contact : gia_bach @ www.CadViet.com ;;
(defun BlockIntersObj (Blk Obj / iPts pt_lst)
(foreach memb (vlax-invoke Blk 'Explode)
(if (eq (vla-get-ObjectName memb) "AcDbPolyline")
(setq iPts (vlax-invoke memb 'IntersectWith Obj 0)) )
(vla-delete memb)
)
(if iPts
(repeat (/ (length iPts) 3)
(setq pt_lst (cons (list (car iPts)(cadr iPts)(caddr iPts)) pt_lst)
iPts (cdddr iPts) ) ) )
(if pt_lst (reverse pt_lst))
)
;--------------- main -------------------
(command "undo" "be")
(princ "\nChon doi tuong can Trim : ")
(if (setq ss (ssget (list (cons 0 "*LINE,ARC"))))
(foreach obj (mapcar 'vlax-ename->vla-object (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss))))
(setq lst_pt (TraceObject obj)
ssBlk (ssget "f" lst_pt(list (cons 0 "INSERT") (cons 8 "Hoga"))))
(foreach e (mapcar 'vlax-ename->vla-object (vl-remove-if 'listp (mapcar 'cadr (ssnamex ssBlk))))
(if (and (setq iPts (BlockIntersObj e obj)) (=(vl-list-length iPts)2))
(foreach pt iPts
(setq lstPtsPa (cons (cons (vlax-curve-getParamAtPoint obj pt) pt) lstPtsPa) ) ))
);foreach
(if lstPtsPa
(setq lstPtsPa (vl-sort lstPtsPa '(lambda (x y) (> (car x) (car y))))
lstPts (mapcar 'cdr lstPtsPa) ))
(setq ent (vlax-vla-object->ename obj)
lstPtsPa nil)
(repeat (/ (length lstPts) 2)
(command "._break" ent "_non" (car lstPts) "_non" (cadr lstPts))
(setq lstPts (cddr lstPts)) )
)
)
(command "undo" "e")
(princ)
)
(defun TraceObject (obj / typlst
typ TracePline TraceACE
TraceLine TraceSpline TraceType1Pline
TraceType23Pline
)
(defun ZClosed (lst)
(if (and (vlax-curve-isClosed obj)
(not (equal (car lst) (last lst) 1e-6))
)
(append lst (list (car lst)))
lst
)
)
(defun TracePline (obj / param endparam anginc
tparam pt blg ptlst delta inc
arcparam flag
)
(setq param (vlax-curve-getStartParam obj)
endparam (vlax-curve-getEndParam obj)
anginc (* pi (/ 7.5 180.0))
)
(setq tparam param)
(while (<= param endparam)
(setq pt (vlax-curve-getPointAtParam obj param))
(if (not (equal pt (car ptlst) 1e-12))
(setq ptlst (cons pt ptlst))
)
(if (and (/= param endparam)
(setq blg (abs (vlax-invoke obj 'GetBulge param)))
(/= 0 blg)
)
(progn
(setq delta (* 4 (atan blg)) ;included angle
inc (/ 1.0 (1+ (fix (/ delta anginc))))
arcparam (+ param inc)
)
(while (< arcparam (1+ param))
(setq pt (vlax-curve-getPointAtParam obj arcparam)
ptlst (cons pt ptlst)
arcparam (+ inc arcparam)
)
)
)
)
(setq param (1+ param))
)
(if (and (apply 'and ptlst)
(> (length ptlst) 1)
)
(ZClosed (reverse ptlst))
)
) ;end
(defun TraceACE (obj / startparam endparam
anginc delta div inc pt
ptlst
)
(setq startparam (vlax-curve-getStartParam obj)
endparam (vlax-curve-getEndParam obj)
anginc (* pi (/ 5.0 180.0))
)
(if (equal endparam (* pi 2) 1e-12)
(setq delta endparam)
(setq delta (NormalAngle (- endparam startparam)))
)
(setq div (1+ (fix (/ delta anginc)))
inc (/ delta div)
)
(while (or
(< startparam endparam)
(equal startparam endparam 1e-12)
)
(setq pt (vlax-curve-getPointAtParam obj startparam)
ptlst (cons pt ptlst)
startparam (+ inc startparam)
)
)
(reverse ptlst)
);end defun
(defun TraceLine (obj)
(list (vlax-get obj 'StartPoint)
(vlax-get obj 'EndPoint)
)
)
(defun TraceSpline (obj / startparam endparam
ncpts inc param fd ptlst pt1
pt2 ang1 ang2 a
)
(setq startparam (vlax-curve-getStartParam obj)
endparam (vlax-curve-getEndParam obj)
ncpts (vlax-get obj 'NumberOfControlPoints)
inc (/ (- endparam startparam) (* ncpts 7))
param (+ inc startparam)
fd (vlax-curve-getfirstderiv obj param)
ptlst (cons (vlax-curve-getStartPoint obj) ptlst)
)
(while (< param endparam)
(setq pt1 (vlax-curve-getPointAtParam obj param)
ang1 fd
param (+ param inc)
pt2 (vlax-curve-getPointAtParam obj param)
fd (vlax-curve-getfirstderiv obj param)
ang2 fd
a (abs (3d_angw1w2 ang1 ang2))
)
(if (> a 0.00218166)
(setq ptlst (cons pt1 ptlst))
)
)
(if (not (equal
(setq pt1 (vlax-curve-getEndPoint obj))
(car ptlst)
1e-8
)
)
(setq ptlst (cons pt1 ptlst))
)
(reverse ptlst)
);end defun
(defun TraceType1Pline (obj / ptlst objlst lst)
(setq ptlst (list (vlax-curve-getStartPoint obj))
objlst (vlax-invoke obj 'Explode)
)
(foreach x objlst
(setq lst (TraceACE x))
(if (not (equal (car lst) (last ptlst) 1e-8))
(setq lst (reverse lst))
)
(setq ptlst (append ptlst (cdr lst)))
(vla-delete x)
)
(ZClosed ptlst)
);end defun
(defun TraceType23Pline (obj / objlst ptlst lastpt)
(setq objlst (vlax-invoke obj 'Explode)
lastpt (vlax-get (last objlst) 'EndPoint)
)
(foreach x objlst
(setq ptlst (cons (vlax-get x 'StartPoint) ptlst))
(vla-delete x)
)
(ZClosed (reverse (cons lastpt ptlst)))
);end defun
(defun Trace3DPline (obj / coord ptlst)
(setq coord (vlax-get obj 'Coordinates))
(repeat (/ (length coord) 3)
(setq
ptlst (cons (list (car coord) (cadr coord) (caddr coord))
ptlst
)
)
(setq coord (cdddr coord))
)
(ZClosed (reverse ptlst))
);end defun
(defun NormalAngle (a)
(if (numberp a)
(angtof (angtos a 0 14) 0)
)
)
(defun 3d_angw1w2 (Wekt1 Wekt2 / CosA)
(if (equal (setq CosA (/ (apply '+ (mapcar '* Wekt1 Wekt2))
(distance '(0 0 0) Wekt1)
(distance '(0 0 0) Wekt2)
)
)
-1.0
1e-6
)
Pi
(if (equal CosA 0.0 1e-6)
(* 0.5 PI)
(atan (sqrt (- 1 (* CosA CosA))) CosA)
)
)
)
(setq typlst '("AcDb2dPolyline" "AcDbPolyline"
"AcDb3dPolyline" "AcDbCircle"
"AcDbArc" "AcDbEllipse"
"AcDbSpline" "AcDbLine"
)
)
(or (eq (type obj) 'VLA-OBJECT)
(setq obj (vlax-ename->vla-object obj))
)
(setq typ (vlax-get obj 'ObjectName))
(if (vl-position typ typlst)
(cond
((or (eq typ "AcDb2dPolyline") (eq typ "AcDbPolyline"))
(cond
((or
(not (vlax-property-available-p obj 'Type))
(= 0 (vlax-get obj 'Type))
)
(TracePline obj)
)
((or (= 3 (vlax-get obj 'Type)) (= 2 (vlax-get obj 'Type)))
(TraceType23Pline obj)
)
((= 1 (vlax-get obj 'Type))
(TraceType1Pline obj)
)
)
)
((eq typ "AcDbLine") (TraceLine obj))
((or (eq typ "AcDbCircle")
(eq typ "AcDbArc")
(eq typ "AcDbEllipse")
)
(TraceACE obj)
)
((eq typ "AcDbSpline") (TraceSpline obj))
((eq typ "AcDb3dPolyline") (Trace3DPline obj))
)
)
)Thảo luận gốc
Lisp được chia sẻ trong chủ đề Viết lisp theo yêu cầu [phần 2]. Xem đúng bài viết #81776 để đọc hướng dẫn và trao đổi thêm.