Lisp lệnh A2XL trong chủ đề “[Nhờ chỉnh sửa] Lisp tính diện tích bằng Pick Điểm”
Chỉnh sửa lại tí nửa theo ý bạn anhemtracdia
Sau khi Pick điểm xác định vùng tính diện tích. Nếu muốn chọn text chứa tên của vùng thì chọn text hoặc mtext. Nếu muốn dùng tên tự động thì chuột phải hoặc enter để tiếp. Tên tự động có dạng Si.
;; free lisp from cadviet.com
;;; this lisp was downloaded from https://www.cadviet.com/forum/topic/70735-nho-chinh-sua-lisp-tinh-dien-tich-bang-pick-diem/
(defun Add_Hatch (poly Htype / mspace)
(vl-load-com)
(setq mspace (vla-get-ModelSpace
(vla-get-ActiveDocument (vlax-get-Acad-Object))
)
)
(setq hatch (vla-AddHatch
mspace
acHatchPatternTypePreDefined
Htype
:vlax-True
)
)
(vlax-invoke hatch 'AppendOuterLoop (list poly))
(vla-evaluate hatch)
)
;==============================================================
(defun MCText (pt string ht / mspace thetext tent alpoint)
(vl-load-com)
(setq mspace (vla-get-modelspace
(vla-get-activedocument (vlax-get-acad-object))
)
)
(setq thetext (vla-AddText mspace string (vlax-3d-point pt) ht))
(setq tent (entget (vlax-vla-object->ename thetext)))
(setq alpoint (cdr (assoc 10 tent)))
(setq tent (subst (cons 73 2) (assoc 73 tent) tent))
(setq tent (subst (cons 72 1) (assoc 72 tent) tent))
(setq tent (subst (cons 11 alpoint) (assoc 11 tent) tent))
(entmod tent)
thetext
)
;==============================================================
(defun MLText (pt string ht / mspace thetext tent alpoint)
(vl-load-com)
(setq mspace (vla-get-modelspace (vla-get-activedocument (vlax-get-acad-object))))
(setq thetext (vla-AddText mspace string (vlax-3d-point pt) ht))
(setq tent (entget (vlax-vla-object->ename thetext)))
(setq alpoint (cdr (assoc 10 tent)))
(setq tent (subst (cons 73 2) (assoc 73 tent) tent))
(setq tent (subst (cons 72 0) (assoc 72 tent) tent))
(setq tent (subst (cons 11 alpoint) (assoc 11 tent) tent))
(entmod tent)
thetext
)
;==============================================================
(defun obj2plist (obj-ename / en timp timl pli)
(if (= (cdr (assoc 0 (entget obj-ename))) "LINE")
(progn
(setq timp (list
(cdr (assoc 10 (entget obj-ename)))
(cdr (assoc 11 (entget obj-ename)))
)
)
)
)
(if (= (cdr (assoc 0 (entget obj-ename))) "POLYLINE")
(progn
(setq en obj-ename)
(while (/= (cdr (assoc 0 (entget en))) "SEQEND")
(if (= (cdr (assoc 0 (entget en))) "VERTEX")
(setq timp (append timp (list (cdr (assoc 10 (entget en))))))
)
(setq en (entnext en))
)
)
)
(if (= (cdr (assoc 0 (entget obj-ename))) "LWPOLYLINE")
(progn
(setq timl (entget obj-ename))
(setq pli 0)
(while (< pli (length timl))
(if (= (car (nth pli timl)) 10)
(setq timp (append timp (list (cdr (nth pli timl)))))
)
(setq pli (1+ pli))
)
)
)
timp
)
;==============================================================
(defun c:A2xl (/ *error* vl ov xlApp xlCells Row pt eLast dtich plist
pgiua)
(vl-load-com)
(defun *error* (msg)
(ObjRel (list xlApp xlCells))
(and ov (mapcar 'setvar vl ov))
(if (not (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*"))
(princ (strcat "\n** Error: " msg " **"))
)
(princ)
)
(setq vl '("CMDECHO" "OSMODE")
ov (mapcar 'getvar vl)
)
(setq xlApp (vlax-get-or-create-object "Excel.Application")
xlCells (vlax-get-property
(vlax-get-property
(vlax-get-property
(vlax-invoke-method
(vlax-get-property xlApp 'Workbooks)
'Add
)
'Sheets
)
'Item
1
)
'Cells
)
Row 1
dem 1
)
(while (setq pt (getpoint "\nPick Area: "))
(mapcar 'setvar vl '(0 0))
(setq eLast (entlast))
(vl-cmdf "_.-boundary" "_a" "_i" "_n" "" "" pt "")
(if (not (eq elast (setq ent (entlast))))
(progn
(setq vitri (car (entsel "\nSelect Area Name text: ")))
(if vitri
(setq vitri (cdr (assoc 1 (entget vitri))))
(setq vitri (strcat "S" (rtos dem 2 0)) dem (1+ dem))
)
(vlax-put-property
xlCells
'Item
row
1
vitri
)
(vlax-put-property
xlCells
'Item
row
2
(setq dtich (rtos (vlax-get-property
(vlax-ename->vla-object ent)
'Area
)
)
)
)
(ADD_HATCH (vlax-ename->vla-object ent) "ANSI31")
(setq plist (OBJ2PLIST ent))
(setq
Pgiua (list
(/ (apply '+ (mapcar '(lambda (x) (car x)) plist))
(length plist)
)
(/ (apply '+ (mapcar '(lambda (x) (cadr x)) plist))
(length plist)
)
)
)
(setq Pstt (polar pgiua (* 0.5 pi) 1.5))
(setq Parea (polar pgiua (* -0.5 pi) 1.5))
(MLText Pstt vitri 1.5)
(MLText Parea dtich 1.5)
(entdel ent)
(setq Row (1+ Row))
)
)
(mapcar 'setvar vl ov)
)
(vlax-put-property xlApp 'Visible :vlax-true)
(ObjRel (list xlApp xlCells))
(gc)
(gc)
(mapcar 'setvar vl ov)
(princ)
)
;==============================================================
(defun ObjRel (lst)
(mapcar
(function
(lambda (x)
(if (and (eq (type x) 'VLA-OBJECT)
(not (vlax-object-released-p x))
)
(vl-catch-all-apply
'vlax-release-object
(list x)
)
)
)
)
lst
)
)
Chúc thành công!
- Tải lisp
- 233448_a2xl.lsp
Cách dùng
- Tải tệp 233448_a2xl.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 A2XL để chạy lisp.
Mã nguồn 233448_a2xl.lsp
;; free lisp from cadviet.com
;;; this lisp was downloaded from https://www.cadviet.com/forum/topic/70735-nh%E1%BB%9D-ch%E1%BB%89nh-s%E1%BB%ADa-lisp-t%C3%ADnh-di%E1%BB%87n-t%C3%ADch-b%E1%BA%B1ng-pick-%C4%91i%E1%BB%83m/
;; free lisp from cadviet.com;;; this lisp was downloaded from https://www.cadviet.com/forum/topic/70735-nho-chinh-sua-lisp-tinh-dien-tich-bang-pick-diem/(defun Add_Hatch (poly Htype / mspace)(vl-load-com)(setq mspace (vla-get-ModelSpace(vla-get-ActiveDocument (vlax-get-Acad-Object))))(setq hatch (vla-AddHatchmspaceacHatchPatternTypePreDefinedHtype:vlax-True))(vlax-invoke hatch 'AppendOuterLoop (list poly))(vla-evaluate hatch));==============================================================(defun MCText (pt string ht / mspace thetext tent alpoint)(vl-load-com)(setq mspace (vla-get-modelspace(vla-get-activedocument (vlax-get-acad-object))))(setq thetext (vla-AddText mspace string (vlax-3d-point pt) ht))(setq tent (entget (vlax-vla-object->ename thetext)))(setq alpoint (cdr (assoc 10 tent)))(setq tent (subst (cons 73 2) (assoc 73 tent) tent))(setq tent (subst (cons 72 1) (assoc 72 tent) tent))(setq tent (subst (cons 11 alpoint) (assoc 11 tent) tent))(entmod tent)thetext);==============================================================(defun MLText (pt string ht / mspace thetext tent alpoint)(vl-load-com)(setq mspace (vla-get-modelspace (vla-get-activedocument (vlax-get-acad-object))))(setq thetext (vla-AddText mspace string (vlax-3d-point pt) ht))(setq tent (entget (vlax-vla-object->ename thetext)))(setq alpoint (cdr (assoc 10 tent)))(setq tent (subst (cons 73 2) (assoc 73 tent) tent))(setq tent (subst (cons 72 0) (assoc 72 tent) tent))(setq tent (subst (cons 11 alpoint) (assoc 11 tent) tent))(entmod tent)thetext);==============================================================(defun obj2plist (obj-ename / en timp timl pli)(if (= (cdr (assoc 0 (entget obj-ename))) "LINE")(progn(setq timp (list(cdr (assoc 10 (entget obj-ename)))(cdr (assoc 11 (entget obj-ename)))))))(if (= (cdr (assoc 0 (entget obj-ename))) "POLYLINE")(progn(setq en obj-ename)(while (/= (cdr (assoc 0 (entget en))) "SEQEND")(if (= (cdr (assoc 0 (entget en))) "VERTEX")(setq timp (append timp (list (cdr (assoc 10 (entget en)))))))(setq en (entnext en)))))(if (= (cdr (assoc 0 (entget obj-ename))) "LWPOLYLINE")(progn(setq timl (entget obj-ename))(setq pli 0)(while (< pli (length timl))(if (= (car (nth pli timl)) 10)(setq timp (append timp (list (cdr (nth pli timl))))))(setq pli (1+ pli)))))timp);==============================================================(defun c:A2xl (/ *error* vl ov xlApp xlCells Row pt eLast dtich plistpgiua)(vl-load-com)(defun *error* (msg)(ObjRel (list xlApp xlCells))(and ov (mapcar 'setvar vl ov))(if (not (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*"))(princ (strcat "\n** Error: " msg " **")))(princ))(setq vl '("CMDECHO" "OSMODE")ov (mapcar 'getvar vl))(setq xlApp (vlax-get-or-create-object "Excel.Application")xlCells (vlax-get-property(vlax-get-property(vlax-get-property(vlax-invoke-method(vlax-get-property xlApp 'Workbooks)'Add)'Sheets)'Item1)'Cells)Row 1dem 1)(while (setq pt (getpoint "\nPick Area: "))(mapcar 'setvar vl '(0 0))(setq eLast (entlast))(vl-cmdf "_.-boundary" "_a" "_i" "_n" "" "" pt "")(if (not (eq elast (setq ent (entlast))))(progn(setq vitri (car (entsel "\nSelect Area Name text: ")))(if vitri(setq vitri (cdr (assoc 1 (entget vitri))))(setq vitri (strcat "S" (rtos dem 2 0)) dem (1+ dem)))(vlax-put-propertyxlCells'Itemrow1vitri)(vlax-put-propertyxlCells'Itemrow2(setq dtich (rtos (vlax-get-property(vlax-ename->vla-object ent)'Area))))(ADD_HATCH (vlax-ename->vla-object ent) "ANSI31")(setq plist (OBJ2PLIST ent))(setqPgiua (list(/ (apply '+ (mapcar '(lambda (x) (car x)) plist))(length plist))(/ (apply '+ (mapcar '(lambda (x) (cadr x)) plist))(length plist))))(setq Pstt (polar pgiua (* 0.5 pi) 1.5))(setq Parea (polar pgiua (* -0.5 pi) 1.5))(MLText Pstt vitri 1.5)(MLText Parea dtich 1.5)(entdel ent)(setq Row (1+ Row))))(mapcar 'setvar vl ov))(vlax-put-property xlApp 'Visible :vlax-true)(ObjRel (list xlApp xlCells))(gc)(gc)(mapcar 'setvar vl ov)(princ));==============================================================(defun ObjRel (lst)(mapcar(function(lambda (x)(if (and (eq (type x) 'VLA-OBJECT)(not (vlax-object-released-p x)))(vl-catch-all-apply'vlax-release-object(list x)))))lst))Thảo luận gốc
Lisp được chia sẻ trong chủ đề [Nhờ chỉnh sửa] Lisp tính diện tích bằng Pick Điểm. Xem đúng bài viết #233448 để đọc hướng dẫn và trao đổi thêm.