Lisp lệnh FIXLINE HATCHKIN trong chủ đề “Nhờ viết Lisp Hatch vùng kín của các đối tượng giao nhau.”
Em sửa được rồi. Tuyệt quá. Ko biết bác có cách nào để khắc phục vấn đề những chỗ hở 1 chút thì ko hatch được ko ah?
Vì hình như khi Hatch cũng có tùy chọn để ta hatch những hình bị hở đúng ko nhỉ? Nếu giải quyết thêm được vấn đề này thì tuyệt quá.Không biết phải làm sao để cám ơn các bác trên diễn đàn. Em xin gửi lời cám ơn chân thành tới các bác :)
(defun LM:Intersections ( 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)
)
)
(reverse r)
)
(defun LM:IntersectionsInSet ( ss / a b i j l )
(repeat (setq i (sslength ss))
(setq a (vlax-ename->vla-object (ssname ss (setq i (1- i)))))
(repeat (setq j i)
(setq b (vlax-ename->vla-object (ssname ss (setq j (1- j))))
l (cons (LM:Intersections a b acextendnone) l)
)
)
)
(apply 'append (reverse l))
)
(defun LM:IntersectionsInSetboth ( ss / a b i j l )
(repeat (setq i (sslength ss))
(setq a (vlax-ename->vla-object (ssname ss (setq i (1- i)))))
(repeat (setq j i)
(setq b (vlax-ename->vla-object (ssname ss (setq j (1- j))))
l (cons (LM:Intersections a b acextendboth) l)
)
)
)
(apply 'append (reverse l))
)
(defun DDH:pointtolsppoint (pointchen lsppointchen / vitrichen ii)
(setq iii 0)
(repeat (- (length lsppointchen) 1)
(if (and (> (distance pointchen (nth (+ iii 1) lsppointchen)) 0.001)
(> (distance pointchen (nth iii lsppointchen)) 0.001)
)
(progn
(if (<= (abs (- (distance (nth iii lsppointchen) (nth (+ iii 1) lsppointchen))
(+ (distance pointchen (nth (+ iii 1) lsppointchen))
(distance pointchen (nth iii lsppointchen))
)
))
0.001)
(setq vitrichen (+ iii 1))
)
)
)
(setq iii (+ iii 1))
)
(setq lsppointchen (LM:InsertNth pointchen vitrichen lsppointchen))
)
(defun LM:InsertNth ( x n l )
((lambda ( k )
(apply 'append
(mapcar '(lambda ( a ) (if (= n (setq k (1+ k))) (list x a) (list a))) l)
)
)
-1
)
)
(defun LM:Uniqueline ( l )
(if l (cons (car l)
(LM:Uniqueline
(vl-remove-if '(lambda (x) (or (and (equal (car x) (car (car l)))
(equal (cadr x) (cadr (car l))))
(and (equal (car x) (cadr (car l)))
(equal (cadr x) (car (car l))))
)
) (cdr l))
))))
(defun taopolyline (lst layer mau / x )
(entmakex
(append (list (cons 0 "LWPOLYLINE")
(cons 100 "AcDbEntity")
(cons 100 "AcDbPolyline")
(cons 90 (length lst))
(cons 70 0)
(cons 66 1)
(cons 8 layer)
(cons 62 mau)
)
(mapcar (function (lambda (x) (cons 10 x))) lst)
)
)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun c:fixline(/)
(setq khoangfix (getreal "\nNhap Khang Cach Max De Xet Fix Doi Tuong <1.00>:"))
(if (= khoangfix nil)
(setq khoangfix 1.00)
)
(princ "\nChon Cac Line, Polyline, Arc, Circle, Spline Hoac Ellipse De Tao Polyline Kin")
(if (setq chonlinepolyline (ssget '((-4 . "<OR")(0 . "LINE")(0 . "CIRCLE")(0 . "SPLINE")(0 . "ARC")(0 . "ELLIPSE")(0 . "*POLYLINE")(-4 . "OR>"))))
(progn
(setq chonpolyline (ssadd))
(setq i 0)
(repeat (sslength chonlinepolyline)
(taopolyline (ACET-GEOM-OBJECT-POINT-LIST (ssname chonlinepolyline i)
(/ (vlax-curve-getdistatparam (ssname chonlinepolyline i) (vlax-curve-getendparam (ssname chonlinepolyline i)))
10000)) "0" 1)
(ssadd (entlast) chonpolyline)
(setq i (+ i 1))
)
(setq lspgiaodiem (LM:IntersectionsInSetboth chonpolyline))
(setq i 0)
(repeat (sslength chonpolyline)
(setq toado (ACET-GEOM-OBJECT-POINT-LIST (ssname chonpolyline i)
(/ (vlax-curve-getdistatparam (ssname chonpolyline i) (vlax-curve-getendparam (ssname chonpolyline i)))
10000)))
(foreach diemgiao lspgiaodiem
(if (and (<= (distance (car toado) diemgiao) khoangfix)
(> (distance (car toado) diemgiao) 0.001))
(entmakex (list '(0 . "LINE")
(cons 10 (car toado))
(cons 11 diemgiao)
))
)
)
(foreach diemgiao lspgiaodiem
(if (and (<= (distance (last toado) diemgiao) khoangfix)
(> (distance (last toado) diemgiao) 0.001))
(entmakex (list '(0 . "LINE")
(cons 10 (last toado))
(cons 11 diemgiao)
))
)
)
(entdel (ssname chonpolyline i))
(setq i (+ i 1))
)
)
)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun c:hatchkin(/)
(vl-load-com)
(princ "\nChon Cac Line, Polyline, Arc, Circle, Spline Hoac Ellipse De Tao Polyline Kin")
(if (setq chonlinepolyline (ssget '((-4 . "<OR")(0 . "LINE")(0 . "CIRCLE")(0 . "SPLINE")(0 . "ARC")(0 . "ELLIPSE")(0 . "*POLYLINE")(-4 . "OR>"))))
(progn
(setq chonpolyline (ssadd))
(setq lsppolyline nil)
(setq i 0)
(repeat (sslength chonlinepolyline)
(taopolyline (ACET-GEOM-OBJECT-POINT-LIST (ssname chonlinepolyline i)
(/ (vlax-curve-getdistatparam (ssname chonlinepolyline i) (vlax-curve-getendparam (ssname chonlinepolyline i)))
10000)) "0" 1)
(ssadd (entlast) chonpolyline)
(setq lsppolyline (append lsppolyline (list (ACET-GEOM-OBJECT-POINT-LIST (ssname chonlinepolyline i)
(/ (vlax-curve-getdistatparam (ssname chonlinepolyline i) (vlax-curve-getendparam (ssname chonlinepolyline i)))
10000)))))
(setq i (+ i 1))
)
(setq lspgiaodiem (LM:IntersectionsInSet chonpolyline))
(setq chonline (ssadd))
(setq lspchonline nil)
(setq lsp2diemline nil)
(foreach lsp lsppolyline
(foreach diemgiao lspgiaodiem
(setq lsp (DDH:pointtolsppoint diemgiao lsp))
)
(setq i 0)
(repeat (- (length lsp) 1)
(setq lsp2diemline (append lsp2diemline (list (list (nth i lsp) (nth (+ i 1) lsp)))))
(setq i (+ i 1))
)
)
(setq lsp2diemline (LM:Uniqueline lsp2diemline))
(foreach line lsp2diemline
(entmakex (list '(0 . "LINE")
(cons 10 (car line))
(cons 11 (cadr line))
))
(ssadd (entlast) chonline)
(setq lspchonline (append lspchonline (list (vlax-ename->vla-object (entlast)))))
)
(or acDoc (setq acDoc (vla-get-activedocument (vlax-get-acad-object))))
(setq ms (vlax-get acDoc (if (= 1 (getvar 'cvport)) 'paperspace 'modelspace)))
(if (setq lsptongregion (vlax-invoke ms 'AddRegion lspchonline))
(progn
(command "ERASE" chonline "")
(command "ERASE" chonpolyline "")
(setq lspxetbien nil)
(foreach tungregion lsptongregion
(setq lspxetbien (append lspxetbien (list (list (vlax-vla-object->ename tungregion) (vlax-get-property tungregion 'area)))))
)
(setq lspxetbien (vl-sort lspxetbien (function (lambda (e1 e2) (> (cadr e1) (cadr e2))))))
(setq dientichtong 0.00)
(foreach dientich (cdr lspxetbien)
(setq dientichtong (+ dientichtong (cadr dientich)))
)
(if (<= (abs (- (cadr (car lspxetbien)) dientichtong)) 0.0001)
(entdel (car (car lspxetbien)))
)
(setq mau 1)
(foreach xetpl (cdr lspxetbien)
(if (>= mau 255)
(setq mau 1)
)
(command "hatch" "" "" "" (car xetpl) "")
(entdel (car xetpl))
(command "change" (entlast) "" "p" "c" mau "")
(setq mau (+ mau 1))
)
)
)
)
)
)
thực hiện lệnh fixline để đóng những khoảng hở trước

- Tải lisp
- 277827_fixline_hatchkin.lsp
Cách dùng
- Tải tệp 277827_fixline_hatchkin.lsp về máy.
- 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 FIXLINE HATCHKIN để chạy lisp.
Mã nguồn 277827_fixline_hatchkin.lsp
;; free lisp from cadviet.com
;;; this lisp was downloaded from https://www.cadviet.com/forum/topic/93584-nh%E1%BB%9D-vi%E1%BA%BFt-lisp-hatch-v%C3%B9ng-k%C3%ADn-c%E1%BB%A7a-c%C3%A1c-%C4%91%E1%BB%91i-t%C6%B0%E1%BB%A3ng-giao-nhau/?page=2
(defun LM:Intersections ( 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)
)
)
(reverse r)
)
(defun LM:IntersectionsInSet ( ss / a b i j l )
(repeat (setq i (sslength ss))
(setq a (vlax-ename->vla-object (ssname ss (setq i (1- i)))))
(repeat (setq j i)
(setq b (vlax-ename->vla-object (ssname ss (setq j (1- j))))
l (cons (LM:Intersections a b acextendnone) l)
)
)
)
(apply 'append (reverse l))
)
(defun LM:IntersectionsInSetboth ( ss / a b i j l )
(repeat (setq i (sslength ss))
(setq a (vlax-ename->vla-object (ssname ss (setq i (1- i)))))
(repeat (setq j i)
(setq b (vlax-ename->vla-object (ssname ss (setq j (1- j))))
l (cons (LM:Intersections a b acextendboth) l)
)
)
)
(apply 'append (reverse l))
)
(defun DDH:pointtolsppoint (pointchen lsppointchen / vitrichen ii)
(setq iii 0)
(repeat (- (length lsppointchen) 1)
(if (and (> (distance pointchen (nth (+ iii 1) lsppointchen)) 0.001)
(> (distance pointchen (nth iii lsppointchen)) 0.001)
)
(progn
(if (<= (abs (- (distance (nth iii lsppointchen) (nth (+ iii 1) lsppointchen))
(+ (distance pointchen (nth (+ iii 1) lsppointchen))
(distance pointchen (nth iii lsppointchen))
)
))
0.001)
(setq vitrichen (+ iii 1))
)
)
)
(setq iii (+ iii 1))
)
(setq lsppointchen (LM:InsertNth pointchen vitrichen lsppointchen))
)
(defun LM:InsertNth ( x n l )
((lambda ( k )
(apply 'append
(mapcar '(lambda ( a ) (if (= n (setq k (1+ k))) (list x a) (list a))) l)
)
)
-1
)
)
(defun LM:Uniqueline ( l )
(if l (cons (car l)
(LM:Uniqueline
(vl-remove-if '(lambda (x) (or (and (equal (car x) (car (car l)))
(equal (cadr x) (cadr (car l))))
(and (equal (car x) (cadr (car l)))
(equal (cadr x) (car (car l))))
)
) (cdr l))
))))
(defun taopolyline (lst layer mau / x )
(entmakex
(append (list (cons 0 "LWPOLYLINE")
(cons 100 "AcDbEntity")
(cons 100 "AcDbPolyline")
(cons 90 (length lst))
(cons 70 0)
(cons 66 1)
(cons 8 layer)
(cons 62 mau)
)
(mapcar (function (lambda (x) (cons 10 x))) lst)
)
)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun c:fixline(/)
(setq khoangfix (getreal "\nNhap Khang Cach Max De Xet Fix Doi Tuong <1.00>:"))
(if (= khoangfix nil)
(setq khoangfix 1.00)
)
(princ "\nChon Cac Line, Polyline, Arc, Circle, Spline Hoac Ellipse De Tao Polyline Kin")
(if (setq chonlinepolyline (ssget '((-4 . "<OR")(0 . "LINE")(0 . "CIRCLE")(0 . "SPLINE")(0 . "ARC")(0 . "ELLIPSE")(0 . "*POLYLINE")(-4 . "OR>"))))
(progn
(setq chonpolyline (ssadd))
(setq i 0)
(repeat (sslength chonlinepolyline)
(taopolyline (ACET-GEOM-OBJECT-POINT-LIST (ssname chonlinepolyline i)
(/ (vlax-curve-getdistatparam (ssname chonlinepolyline i) (vlax-curve-getendparam (ssname chonlinepolyline i)))
10000)) "0" 1)
(ssadd (entlast) chonpolyline)
(setq i (+ i 1))
)
(setq lspgiaodiem (LM:IntersectionsInSetboth chonpolyline))
(setq i 0)
(repeat (sslength chonpolyline)
(setq toado (ACET-GEOM-OBJECT-POINT-LIST (ssname chonpolyline i)
(/ (vlax-curve-getdistatparam (ssname chonpolyline i) (vlax-curve-getendparam (ssname chonpolyline i)))
10000)))
(foreach diemgiao lspgiaodiem
(if (and (<= (distance (car toado) diemgiao) khoangfix)
(> (distance (car toado) diemgiao) 0.001))
(entmakex (list '(0 . "LINE")
(cons 10 (car toado))
(cons 11 diemgiao)
))
)
)
(foreach diemgiao lspgiaodiem
(if (and (<= (distance (last toado) diemgiao) khoangfix)
(> (distance (last toado) diemgiao) 0.001))
(entmakex (list '(0 . "LINE")
(cons 10 (last toado))
(cons 11 diemgiao)
))
)
)
(entdel (ssname chonpolyline i))
(setq i (+ i 1))
)
)
)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun c:hatchkin(/)
(vl-load-com)
(princ "\nChon Cac Line, Polyline, Arc, Circle, Spline Hoac Ellipse De Tao Polyline Kin")
(if (setq chonlinepolyline (ssget '((-4 . "<OR")(0 . "LINE")(0 . "CIRCLE")(0 . "SPLINE")(0 . "ARC")(0 . "ELLIPSE")(0 . "*POLYLINE")(-4 . "OR>"))))
(progn
(setq chonpolyline (ssadd))
(setq lsppolyline nil)
(setq i 0)
(repeat (sslength chonlinepolyline)
(taopolyline (ACET-GEOM-OBJECT-POINT-LIST (ssname chonlinepolyline i)
(/ (vlax-curve-getdistatparam (ssname chonlinepolyline i) (vlax-curve-getendparam (ssname chonlinepolyline i)))
10000)) "0" 1)
(ssadd (entlast) chonpolyline)
(setq lsppolyline (append lsppolyline (list (ACET-GEOM-OBJECT-POINT-LIST (ssname chonlinepolyline i)
(/ (vlax-curve-getdistatparam (ssname chonlinepolyline i) (vlax-curve-getendparam (ssname chonlinepolyline i)))
10000)))))
(setq i (+ i 1))
)
(setq lspgiaodiem (LM:IntersectionsInSet chonpolyline))
(setq chonline (ssadd))
(setq lspchonline nil)
(setq lsp2diemline nil)
(foreach lsp lsppolyline
(foreach diemgiao lspgiaodiem
(setq lsp (DDH:pointtolsppoint diemgiao lsp))
)
(setq i 0)
(repeat (- (length lsp) 1)
(setq lsp2diemline (append lsp2diemline (list (list (nth i lsp) (nth (+ i 1) lsp)))))
(setq i (+ i 1))
)
)
(setq lsp2diemline (LM:Uniqueline lsp2diemline))
(foreach line lsp2diemline
(entmakex (list '(0 . "LINE")
(cons 10 (car line))
(cons 11 (cadr line))
))
(ssadd (entlast) chonline)
(setq lspchonline (append lspchonline (list (vlax-ename->vla-object (entlast)))))
)
(or acDoc (setq acDoc (vla-get-activedocument (vlax-get-acad-object))))
(setq ms (vlax-get acDoc (if (= 1 (getvar 'cvport)) 'paperspace 'modelspace)))
(if (setq lsptongregion (vlax-invoke ms 'AddRegion lspchonline))
(progn
(command "ERASE" chonline "")
(command "ERASE" chonpolyline "")
(setq lspxetbien nil)
(foreach tungregion lsptongregion
(setq lspxetbien (append lspxetbien (list (list (vlax-vla-object->ename tungregion) (vlax-get-property tungregion 'area)))))
)
(setq lspxetbien (vl-sort lspxetbien (function (lambda (e1 e2) (> (cadr e1) (cadr e2))))))
(setq dientichtong 0.00)
(foreach dientich (cdr lspxetbien)
(setq dientichtong (+ dientichtong (cadr dientich)))
)
(if (<= (abs (- (cadr (car lspxetbien)) dientichtong)) 0.0001)
(entdel (car (car lspxetbien)))
)
(setq mau 1)
(foreach xetpl (cdr lspxetbien)
(if (>= mau 255)
(setq mau 1)
)
(command "hatch" "" "" "" (car xetpl) "")
(entdel (car xetpl))
(command "change" (entlast) "" "p" "c" mau "")
(setq mau (+ mau 1))
)
)
)
)
)
)Thảo luận gốc
Lisp được chia sẻ trong chủ đề Nhờ viết Lisp Hatch vùng kín của các đối tượng giao nhau.. Xem đúng bài viết #277827 để đọc hướng dẫn và trao đổi thêm.