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 cho bạn. Tớ chỉ thêm các hàm con bên trên và thêm vào khoảng 4 câu lệnh trong hàm cũ của bạn. Mấy hàm con copy của ông Demigod bên trạm X.vn. Hy vọng giải quyết được yêu cầu của bạn. Tuy nhiên chưa điền tỷ lệ hatch và chiều cao text hợp lý. Kiểu Hatch tớ chọn ANSI31 và chiều cao chữ 1.5.
(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 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
)
(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
(vlax-put-property
xlCells
'Item
row
1
(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))
)
)
(MCText Pgiua 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
)
)
- Tải lisp
- 233291_a2xl.lsp
Cách dùng
- Tải tệp 233291_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 233291_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/
(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 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)'Item1)'Cells)Row 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(vlax-put-propertyxlCells'Itemrow1(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))))(MCText Pgiua 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 #233291 để đọc hướng dẫn và trao đổi thêm.