Lisp lệnh TAOVUNGPOLYLINEKIN trong chủ đề “Nhờ viết Lisp Hatch vùng kín của các đối tượng giao nhau.”
Mọi người test nhé

(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 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:SubstNth ( a n l / i )
(setq i -1)
(mapcar '(lambda ( x ) (if (= (setq i (1+ i)) n) a x)) l)
)
(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 entmakex-hatchvatlieu (Lsp layer mau kihieuhatch)
(entmakex
(apply 'append
(list (list '(0 . "HATCH")
'(100 . "AcDbEntity")
'(410 . "Model")
'(100 . "AcDbHatch")
'(10 0.0 0.0 0.0)
'(210 0.0 0.0 1.0)
(cons 2 kihieuhatch)
'(70 . 1)
'(71 . 0)
(cons 91 (length Lsp))
(cons 8 layer)
(cons 62 mau)
)
(apply 'append
(mapcar '(lambda (a)
(apply 'append
(list (list '(92 . 7) '(72 . 0) '(73 . 1) (cons 93 (length a)))
(mapcar '(lambda (b) (cons 10 b)) a)
'((97 . 0))))) Lsp))
'((75 . 0)
(76 . 1)
(47 . 1.)
(98 . 2)
(10 0. 0. 0.0)
(10 0. 0. 0.0)
(451 . 0)
(460 . 0.0)
(461 . 0.0)
(452 . 1)
(462 . 1.0)
(453 . 2)
(463 . 0.0)
(463 . 1.0)
(470 . "LINEAR")
)
)
)
)
)
(defun create_layer (Layer Color)
(if (not (tblsearch "Layer" Layer))
(entmakex
(list
(cons 0 "LAYER")
(cons 100 "AcDbSymbolTableRecord")
(cons 100 "AcDbLayerTableRecord")
(cons 70 0)
(cons 2 Layer)
(cons 62 Color)
)
)
)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun c:taovungpolylinekin(/)
(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
(create_layer "PoLyline Tao Vung Kin" 3)
(setq lsplinepolyline nil)
(setq i 0)
(repeat (sslength chonlinepolyline)
(setq lsplinepolyline (append lsplinepolyline (list (ACET-GEOM-OBJECT-POINT-LIST (ssname chonlinepolyline i) 1e-3))))
(setq i (+ i 1))
)
(setq lspgiaodiem (LM:IntersectionsInSet chonlinepolyline))
(setq i 0)
(foreach lsptungline lsplinepolyline
(foreach diemgiao lspgiaodiem
(setq lsptungline (DDH:pointtolsppoint diemgiao lsptungline))
)
(setq lsplinepolyline (LM:SubstNth lsptungline i lsplinepolyline))
(setq i (+ i 1))
)
(setq lsptongline nil)
(foreach lsptunglinepolyline lsplinepolyline
(setq i 0)
(repeat (- (length lsptunglinepolyline) 1)
(entmakex (list '(0 . "LINE")
(cons 10 (list (car (nth i lsptunglinepolyline)) (cadr (nth i lsptunglinepolyline))))
(cons 11 (list (car (nth (+ i 1) lsptunglinepolyline)) (cadr (nth (+ i 1) lsptunglinepolyline))))
))
(setq lsptongline (append lsptongline (list (vlax-ename->vla-object (entlast)))))
(setq i (+ i 1))
)
)
(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 lsptongline))
(progn
(foreach lsptungline lsptongline
(entdel (vlax-vla-object->ename lsptungline))
)
(setq lsppolylinetong nil)
(foreach tungregion lsptongregion
(command "explode" (vlax-vla-object->ename tungregion) "")
(command "PEDIT" "m" (ssget "p") "" "y" "j" "0.00001" "")
(setq lspdoi (entget (entlast)))
(entmod (subst (cons 8 "PoLyline Tao Vung Kin") (assoc 8 lspdoi) lspdoi))
(setq lsppolylinetong (append lsppolylinetong (list (list (entlast) (vlax-curve-getarea (entlast))))))
)
(setq lsppolylinetong (vl-sort lsppolylinetong (function (lambda (e1 e2) (> (cadr e1) (cadr e2))))))
(setq dientichtong 0.00)
(foreach dientich (cdr lsppolylinetong)
(setq dientichtong (+ dientichtong (cadr dientich)))
)
(if (<= (abs (- (cadr (car lsppolylinetong)) dientichtong)) 0.0001)
(entdel (car (car lsppolylinetong)))
)
(setq mau 1)
(foreach xetpl (cdr lsppolylinetong)
(if (>= mau 255)
(setq mau 1)
)
(entmakex-hatchvatlieu (list (cdr (ACET-GEOM-OBJECT-POINT-LIST (car xetpl) 1e-3))) "PoLyline Tao Vung Kin" mau "SLOID")
(setq mau (+ mau 12))
)
)
)
)
)
)
- Tải lisp
- 277121_taovungpolylinekin.lsp
Cách dùng
- Tải tệp 277121_taovungpolylinekin.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 TAOVUNGPOLYLINEKIN để chạy lisp.
Mã nguồn 277121_taovungpolylinekin.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/
(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 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:SubstNth ( a n l / i )
(setq i -1)
(mapcar '(lambda ( x ) (if (= (setq i (1+ i)) n) a x)) l)
)
(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 entmakex-hatchvatlieu (Lsp layer mau kihieuhatch)
(entmakex
(apply 'append
(list (list '(0 . "HATCH")
'(100 . "AcDbEntity")
'(410 . "Model")
'(100 . "AcDbHatch")
'(10 0.0 0.0 0.0)
'(210 0.0 0.0 1.0)
(cons 2 kihieuhatch)
'(70 . 1)
'(71 . 0)
(cons 91 (length Lsp))
(cons 8 layer)
(cons 62 mau)
)
(apply 'append
(mapcar '(lambda (a)
(apply 'append
(list (list '(92 . 7) '(72 . 0) '(73 . 1) (cons 93 (length a)))
(mapcar '(lambda (b) (cons 10 b)) a)
'((97 . 0))))) Lsp))
'((75 . 0)
(76 . 1)
(47 . 1.)
(98 . 2)
(10 0. 0. 0.0)
(10 0. 0. 0.0)
(451 . 0)
(460 . 0.0)
(461 . 0.0)
(452 . 1)
(462 . 1.0)
(453 . 2)
(463 . 0.0)
(463 . 1.0)
(470 . "LINEAR")
)
)
)
)
)
(defun create_layer (Layer Color)
(if (not (tblsearch "Layer" Layer))
(entmakex
(list
(cons 0 "LAYER")
(cons 100 "AcDbSymbolTableRecord")
(cons 100 "AcDbLayerTableRecord")
(cons 70 0)
(cons 2 Layer)
(cons 62 Color)
)
)
)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun c:taovungpolylinekin(/)
(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
(create_layer "PoLyline Tao Vung Kin" 3)
(setq lsplinepolyline nil)
(setq i 0)
(repeat (sslength chonlinepolyline)
(setq lsplinepolyline (append lsplinepolyline (list (ACET-GEOM-OBJECT-POINT-LIST (ssname chonlinepolyline i) 1e-3))))
(setq i (+ i 1))
)
(setq lspgiaodiem (LM:IntersectionsInSet chonlinepolyline))
(setq i 0)
(foreach lsptungline lsplinepolyline
(foreach diemgiao lspgiaodiem
(setq lsptungline (DDH:pointtolsppoint diemgiao lsptungline))
)
(setq lsplinepolyline (LM:SubstNth lsptungline i lsplinepolyline))
(setq i (+ i 1))
)
(setq lsptongline nil)
(foreach lsptunglinepolyline lsplinepolyline
(setq i 0)
(repeat (- (length lsptunglinepolyline) 1)
(entmakex (list '(0 . "LINE")
(cons 10 (list (car (nth i lsptunglinepolyline)) (cadr (nth i lsptunglinepolyline))))
(cons 11 (list (car (nth (+ i 1) lsptunglinepolyline)) (cadr (nth (+ i 1) lsptunglinepolyline))))
))
(setq lsptongline (append lsptongline (list (vlax-ename->vla-object (entlast)))))
(setq i (+ i 1))
)
)
(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 lsptongline))
(progn
(foreach lsptungline lsptongline
(entdel (vlax-vla-object->ename lsptungline))
)
(setq lsppolylinetong nil)
(foreach tungregion lsptongregion
(command "explode" (vlax-vla-object->ename tungregion) "")
(command "PEDIT" "m" (ssget "p") "" "y" "j" "0.00001" "")
(setq lspdoi (entget (entlast)))
(entmod (subst (cons 8 "PoLyline Tao Vung Kin") (assoc 8 lspdoi) lspdoi))
(setq lsppolylinetong (append lsppolylinetong (list (list (entlast) (vlax-curve-getarea (entlast))))))
)
(setq lsppolylinetong (vl-sort lsppolylinetong (function (lambda (e1 e2) (> (cadr e1) (cadr e2))))))
(setq dientichtong 0.00)
(foreach dientich (cdr lsppolylinetong)
(setq dientichtong (+ dientichtong (cadr dientich)))
)
(if (<= (abs (- (cadr (car lsppolylinetong)) dientichtong)) 0.0001)
(entdel (car (car lsppolylinetong)))
)
(setq mau 1)
(foreach xetpl (cdr lsppolylinetong)
(if (>= mau 255)
(setq mau 1)
)
(entmakex-hatchvatlieu (list (cdr (ACET-GEOM-OBJECT-POINT-LIST (car xetpl) 1e-3))) "PoLyline Tao Vung Kin" mau "SLOID")
(setq mau (+ mau 12))
)
)
)
)
)
)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 #277121 để đọc hướng dẫn và trao đổi thêm.