Lisp lệnh HATCHKIN trong chủ đề “Nhờ viết Lisp Hatch vùng kín của các đối tượng giao nhau.”
mình up lại đây
lisp nay mình test rồi nó ổn khi các đối tượng phải giao nhau hết (không tính được như hình mẫu ở trên trường hợp line và spline nó không giao nhau còn các hình còn lại thì ok)
File mẫu phải khai báo lại tỉ lệ hatch mặc định lớn lên vd=10 nếu không nó không có hatch đượ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 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 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: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) (foreach lsp lsppolyline (foreach diemgiao lspgiaodiem (setq lsp (DDH:pointtolsppoint diemgiao lsp)) ) (setq i 0) (repeat (- (length lsp) 1) (entmakex (list '(0 . "LINE") (cons 10 (nth i lsp)) (cons 11 (nth (+ i 1) lsp)) )) (ssadd (entlast) chonline) (setq lspchonline (append lspchonline (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 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) "") (command "change" (entlast) "" "p" "c" mau "") (setq mau (+ mau 1)) ) ) ) ) ) )
Em còn gặp lỗi là nếu ta quét chọn nhiều hình cùng lúc (như file em gửi là em quét chọn cả 6 khối hình 1 lúc) thì sau khi Hatchkin có một số vùng Hatch được tạo ra bao toàn bộ tất cả các hình. Mà em chỉ muốn nó ra kết quả Hatch ở mỗi vùng riêng biệt thôi. Và Bác có thể chỉnh cho em cái phần sau khi hatch xong thì nó mất cái phần Region được tạo ra đó ko ah?
Nói chung Lisp đã rất tuyệt rồi. Bác chỉnh giúp em mấy cái này nữa nhá. Em cám ơn các Bác nhiều lắm :) https://www.cadviet.com/upfiles/3/64997_test_hatch_2.dwg
- Tải lisp
- 277305_hatchkin.lsp
Cách dùng
- Tải tệp 277305_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 HATCHKIN để chạy lisp.
Mã nguồn 277305_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/
(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: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 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: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)
(foreach lsp lsppolyline
(foreach diemgiao lspgiaodiem
(setq lsp (DDH:pointtolsppoint diemgiao lsp))
)
(setq i 0)
(repeat (- (length lsp) 1)
(entmakex (list '(0 . "LINE")
(cons 10 (nth i lsp))
(cons 11 (nth (+ i 1) lsp))
))
(ssadd (entlast) chonline)
(setq lspchonline (append lspchonline (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 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) "")
(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 #277305 để đọc hướng dẫn và trao đổi thêm.
