Lisp lệnh TE trong chủ đề “Xin hỗ trợ lisp tự động click điểm vào các mắt lưới”
18 phút trước, monavamonava đã nói:Cho em hỏi thêm chút. Lisp này có giới hạn phiên bản autocad hay j nửa không nhỉ.
Em xài trên cad 2005 nó báo lỗi ; error: no function definition: ACET-SS-TO-LIST. Có thể khắc phục nó được không?
Do k có Expresstool, bạn dùng bản này
(defun @Inside (PIQ Object / ClosestPoint ClosestParam Sample Start End P1 P2 P a1 a2 Defl ) (setq Sample 0.2) (vl-load-com) (or (= (type @delta) 'SUBR) (defun @delta (a1 a2) (cond ((> a1 (+ a2 pi)) (+ a2 pi pi (- a1)) ) ((> a2 (+ a1 pi)) (- a2 a1 pi pi) ) (1 (- a2 a1)) ) ) ) (and (cond ((not Object) (prompt " No object provided.") ) ((= (type Object) 'VLA-Object)) ((= (type Object) 'Ename) (setq Object (vlax-ename->vla-object Object)) ) (1 (prompt " Improper object type.")) ) (or (and (< 1 (vl-list-length PIQ) 4) (vl-every 'numberp PIQ) ) (prompt " Improper point value.") ) (or (not (vl-catch-all-error-p (setq Start (vl-catch-all-apply 'vlax-curve-getStartPoint (list Object) ) ) ) ) (prompt " Object is not a curve.") ) (or (equal Start (vlax-curve-getendpoint Object) 1e-10) (prompt " Curve is not closed.") ) (setq P (trans PIQ 1 0)); PIQ in WCS (setq ClosestPoint (vlax-curve-getclosestpointto Object P) ; In WCS ) (not (equal P ClosestPoint 1e-10)); in WCS (setq ClosestParam (vlax-curve-getparamatpoint Object ClosestPoint)) (setq ClosestPoint (trans ClosestPoint 0 1)) ; convert to UCS (setq End (vlax-curve-getEndparam Object)) (setq P1 0.0 P2 Sample Defl 0.0 ) (setq a1 (angle PIQ (trans Start 0 1))) ; in UCS (while (<= P2 End) (setq P2 (min P2 End)) (if (< P1 ClosestParam P2) (setq a2 (angle PIQ ClosestPoint) Defl (+ Defl (@delta a1 a2)) a1 a2 ) ) (while (not (setq P (vlax-curve-getPointAtParam Object P2))) (setq P2 (+ P2 Sample)) ) (setq a2 (angle PIQ (trans P 0 1)) ; in UCS Defl (+ Defl (@delta a1 a2)) a1 a2 P1 P2 P2 (+ P2 Sample) ) ) (> (abs Defl) 4) ) ) ;;;;;;************* (defun C:te (/ ss1 p1 p2 p3 ss t1 lst pt) (vl-load-com) (Prompt "\nQu\U+00E9t v\U+00F9ng ch\U+1EE9a c\U+00E1c \U+0111i\U+1EC3m giao") (setq ss (CV:ss-to-list (ssget (list (cons 0 "LINE,LWPOLYLINE"))) nil)) (if (and ss) (progn (setq t1 0) (mapcar '(lambda (x) (if (> (vlax-curve-getendparam x) t1) (progn (setq ent x) (setq t1 (vlax-curve-getendparam x)))) ) ss) (setq ss (vl-remove ent ss)) (setq ss (vl-sort ss '(lambda (x y) (< (vla-get-length (vlax-ename->vla-object x)) (vla-get-length (vlax-ename->vla-object y)))))) (setq ss1 (list (car ss)) ss (cdr ss)) (setq p1 (vlax-curve-getpointatparam (car ss1) (vlax-curve-getstartparam (car ss1))) p2 (vlax-curve-getpointatparam (car ss1) (+ 1 (vlax-curve-getstartparam (car ss1)) )) p3 (list (/ (+ (car p1) (car p2)) 2) (/ (+ (cadr p1) (cadr p2)) 2) (/ (+ (caddr p1) (caddr p2)) 2))) (setq lst (getlstpoint ss)) (foreach pt lst (if (@Inside Pt ent)(progn (foreach obj ss1 (vla-Move (vla-Copy (vlax-ename->vla-object obj)) (vlax-3d-point p3) (vlax-3d-point pt)) ) )) ) )) (princ) ) (defun getlstpoint (ss / ent1 lst_pt lst ) (setq lst_pt nil) (while (> (length ss) 1) (setq ent1 (car ss)) (setq lst (apply 'append (mapcar '(lambda (x) (vla-inters ent1 x 0)) (cdr ss)))) (setq lst_pt (append lst_pt lst)) (setq ss (cdr ss)) ) lst_pt ) (defun vla-inters (ent1 ent2 mode / lst1 lst2) (setq lst1 (vlax-invoke (vlax-ename->vla-object ent1) 'intersectwith (vlax-ename->vla-object ent2) (cond ((= mode 0) acextendnone) ((= mode 1) acextendthisentity) ((= mode 2) acextendotherentity) ((= mode 3) acextendboth) ))) (repeat (/ (length lst1) 3) (setq lst2 (cons (list (car lst1) (cadr lst1) (caddr lst1)) lst2) lst1 (cdddr lst1) ) ) (reverse lst2) ) (defun CV:ss-to-list (ss vla / n e l) (if ss (progn (setq n (sslength ss)) (while (setq e (ssname ss (setq n (1- n)))) (setq l (cons (if vla (vlax-ename->vla-object e) e) l)) ) ) ) )
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 437206_te.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 TE để chạy lisp.
Mã nguồn 437206_te.lsp (trích từ bài viết gốc)
(defun @Inside (PIQ Object / ClosestPoint
ClosestParam Sample Start End P1
P2 P a1 a2 Defl
)
(setq Sample 0.2)
(vl-load-com)
(or (= (type @delta) 'SUBR)
(defun @delta (a1 a2)
(cond
((> a1 (+ a2 pi))
(+ a2 pi pi (- a1))
)
((> a2 (+ a1 pi))
(- a2 a1 pi pi)
)
(1 (- a2 a1))
)
)
)
(and
(cond
((not Object)
(prompt " No object provided.")
)
((= (type Object) 'VLA-Object))
((= (type Object) 'Ename)
(setq Object (vlax-ename->vla-object Object))
)
(1 (prompt " Improper object type."))
)
(or
(and
(< 1 (vl-list-length PIQ) 4)
(vl-every 'numberp PIQ)
)
(prompt " Improper point value.")
)
(or
(not
(vl-catch-all-error-p
(setq Start
(vl-catch-all-apply
'vlax-curve-getStartPoint
(list Object)
)
)
)
)
(prompt " Object is not a curve.")
)
(or
(equal Start (vlax-curve-getendpoint Object) 1e-10)
(prompt " Curve is not closed.")
)
(setq P (trans PIQ 1 0)); PIQ in WCS
(setq ClosestPoint
(vlax-curve-getclosestpointto Object P) ; In WCS
)
(not (equal P ClosestPoint 1e-10)); in WCS
(setq ClosestParam (vlax-curve-getparamatpoint Object ClosestPoint))
(setq ClosestPoint (trans ClosestPoint 0 1)) ; convert to UCS
(setq End (vlax-curve-getEndparam Object))
(setq P1 0.0
P2 Sample
Defl 0.0
)
(setq a1 (angle PIQ (trans Start 0 1))) ; in UCS
(while (<= P2 End)
(setq P2 (min P2 End))
(if (< P1 ClosestParam P2)
(setq a2 (angle PIQ ClosestPoint)
Defl (+ Defl (@delta a1 a2))
a1 a2
)
)
(while (not (setq P (vlax-curve-getPointAtParam Object P2)))
(setq P2 (+ P2 Sample))
)
(setq a2 (angle PIQ (trans P 0 1)) ; in UCS
Defl (+ Defl (@delta a1 a2))
a1 a2
P1 P2
P2 (+ P2 Sample)
)
)
(> (abs Defl) 4)
)
)
;;;;;;*************
(defun C:te (/ ss1 p1 p2 p3 ss t1 lst pt)
(vl-load-com)
(Prompt "\nQu\U+00E9t v\U+00F9ng ch\U+1EE9a c\U+00E1c \U+0111i\U+1EC3m giao")
(setq ss (CV:ss-to-list (ssget (list (cons 0 "LINE,LWPOLYLINE"))) nil))
(if (and ss) (progn
(setq t1 0)
(mapcar '(lambda (x) (if (> (vlax-curve-getendparam x) t1) (progn (setq ent x) (setq t1 (vlax-curve-getendparam x)))) ) ss)
(setq ss (vl-remove ent ss))
(setq ss (vl-sort ss '(lambda (x y) (< (vla-get-length (vlax-ename->vla-object x)) (vla-get-length (vlax-ename->vla-object y))))))
(setq ss1 (list (car ss))
ss (cdr ss))
(setq p1 (vlax-curve-getpointatparam (car ss1) (vlax-curve-getstartparam (car ss1)))
p2 (vlax-curve-getpointatparam (car ss1) (+ 1 (vlax-curve-getstartparam (car ss1)) ))
p3 (list (/ (+ (car p1) (car p2)) 2) (/ (+ (cadr p1) (cadr p2)) 2) (/ (+ (caddr p1) (caddr p2)) 2)))
(setq lst (getlstpoint ss))
(foreach pt lst
(if (@Inside Pt ent)(progn
(foreach obj ss1
(vla-Move (vla-Copy (vlax-ename->vla-object obj)) (vlax-3d-point p3) (vlax-3d-point pt))
)
))
)
))
(princ)
)
(defun getlstpoint (ss / ent1 lst_pt lst )
(setq lst_pt nil)
(while (> (length ss) 1)
(setq ent1 (car ss))
(setq lst (apply 'append (mapcar '(lambda (x) (vla-inters ent1 x 0)) (cdr ss))))
(setq lst_pt (append lst_pt lst))
(setq ss (cdr ss))
)
lst_pt
)
(defun vla-inters (ent1 ent2 mode / lst1 lst2)
(setq lst1 (vlax-invoke (vlax-ename->vla-object ent1) 'intersectwith (vlax-ename->vla-object ent2)
(cond
((= mode 0) acextendnone)
((= mode 1) acextendthisentity)
((= mode 2) acextendotherentity)
((= mode 3) acextendboth)
)))
(repeat (/ (length lst1) 3)
(setq lst2 (cons (list (car lst1) (cadr lst1) (caddr lst1)) lst2)
lst1 (cdddr lst1)
)
)
(reverse lst2)
)
(defun CV:ss-to-list (ss vla / n e l)
(if ss (progn
(setq n (sslength ss))
(while (setq e (ssname ss (setq n (1- n))))
(setq l (cons (if vla (vlax-ename->vla-object e) e) l))
)
) )
)Thảo luận gốc
Lisp được chia sẻ trong chủ đề Xin hỗ trợ lisp tự động click điểm vào các mắt lưới. Xem đúng bài viết #437206 để đọc hướng dẫn và trao đổi thêm.