Lisp lệnh LENFIELD trong chủ đề “nhờ kết hợp 2 lisp”
Nói chung không có ý tranh luận làm gì, đơn giản là thấy field khó xài hơn reactor nên dùng reactor.
Nếu có ai rành về field và lisp thì làm thử bài toán trên để tôi được mở rộng tầm mắt.
Search nhẹ cái ra ngay này Tot77, ket post luôn code nhé ^^
;;--------------------=={ Length Field }==--------------------;;
;; ;;
;; Creates an MText Field referencing the sum of the lengths ;;
;; of selected objects. ;;
;;------------------------------------------------------------;;
;; Author: Lee Mac, Copyright © 2011 - www.lee-mac.com ;;
;;------------------------------------------------------------;;
(defun c:LenField ( / acdoc acspc format pt ss )
(setq format "%lu6%qf1") ;; Field Formatting
(setq acdoc (vla-get-activedocument (vlax-get-acad-object))
acspc (vlax-get-property acdoc (if (= 1 (getvar 'CVPORT)) 'Paperspace 'Modelspace))
)
(if
(and
(ssget '((0 . "LINE,*POLYLINE")))
(setq pt (getpoint "\nPick Point for Field: "))
)
(
(lambda ( ss fld )
(vlax-for obj ss
(setq fld
(strcat fld "%<\\AcObjProp Object(%<\\_ObjId "
(LM:GetObjectID acdoc obj) ">%).Length>% + "
)
)
)
(vla-addMText acspc (vlax-3D-point (trans pt 1 0)) 0.
(setq fld
(strcat
(substr fld 1
(- (strlen fld) (if (< 1 (vla-get-Count ss)) 3 5))
)
" \\f \"" format "\">%"
)
)
)
(vla-delete ss)
)
(setq ss (vla-get-ActiveSelectionSet acdoc))
(if (< 1 (vla-get-Count ss)) "%<\\AcExpr " "")
)
)
(princ)
)
;;-------------------=={ Get ObjectID }==---------------------;;
;; ;;
;; Returns the ObjectID string for the supplied VLA-Object ;;
;;------------------------------------------------------------;;
;; Author: Lee Mac, Copyright © 2011 - www.lee-mac.com ;;
;;------------------------------------------------------------;;
;; Arguments: ;;
;; doc - VLA Document Object (req'd for 64-bit systems) ;;
;; obj - VLA Object to query ;;
;;------------------------------------------------------------;;
;; Returns: ObjectID string for VLA-Object ;;
;;------------------------------------------------------------;;
(defun LM:GetObjectID ( doc obj )
(if (vl-string-search "64" (getenv "PROCESSOR_ARCHITECTURE"))
(vlax-invoke-method (vla-get-Utility doc) 'GetObjectIdString obj :vlax-false)
(itoa (vla-get-Objectid obj))
)
)
- Tải lisp
- 311805_lenfield.lsp
Cách dùng
- Tải tệp 311805_lenfield.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 LENFIELD để chạy lisp.
Mã nguồn 311805_lenfield.lsp
;; free lisp from cadviet.com
;;; this lisp was downloaded from https://www.cadviet.com/forum/topic/108785-nh%E1%BB%9D-k%E1%BA%BFt-h%E1%BB%A3p-2-lisp/
;;--------------------=={ Length Field }==--------------------;;
;; ;;
;; Creates an MText Field referencing the sum of the lengths ;;
;; of selected objects. ;;
;;------------------------------------------------------------;;
;; Author: Lee Mac, Copyright © 2011 - www.lee-mac.com ;;
;;------------------------------------------------------------;;
(defun c:LenField ( / acdoc acspc format pt ss )
(setq format "%lu6%qf1") ;; Field Formatting
(setq acdoc (vla-get-activedocument (vlax-get-acad-object))
acspc (vlax-get-property acdoc (if (= 1 (getvar 'CVPORT)) 'Paperspace 'Modelspace))
)
(if
(and
(ssget '((0 . "LINE,*POLYLINE")))
(setq pt (getpoint "\nPick Point for Field: "))
)
(
(lambda ( ss fld )
(vlax-for obj ss
(setq fld
(strcat fld "%<\\AcObjProp Object(%<\\_ObjId "
(LM:GetObjectID acdoc obj) ">%).Length>% + "
)
)
)
(vla-addMText acspc (vlax-3D-point (trans pt 1 0)) 0.
(setq fld
(strcat
(substr fld 1
(- (strlen fld) (if (< 1 (vla-get-Count ss)) 3 5))
)
" \\f \"" format "\">%"
)
)
)
(vla-delete ss)
)
(setq ss (vla-get-ActiveSelectionSet acdoc))
(if (< 1 (vla-get-Count ss)) "%<\\AcExpr " "")
)
)
(princ)
)
;;-------------------=={ Get ObjectID }==---------------------;;
;; ;;
;; Returns the ObjectID string for the supplied VLA-Object ;;
;;------------------------------------------------------------;;
;; Author: Lee Mac, Copyright © 2011 - www.lee-mac.com ;;
;;------------------------------------------------------------;;
;; Arguments: ;;
;; doc - VLA Document Object (req'd for 64-bit systems) ;;
;; obj - VLA Object to query ;;
;;------------------------------------------------------------;;
;; Returns: ObjectID string for VLA-Object ;;
;;------------------------------------------------------------;;
(defun LM:GetObjectID ( doc obj )
(if (vl-string-search "64" (getenv "PROCESSOR_ARCHITECTURE"))
(vlax-invoke-method (vla-get-Utility doc) 'GetObjectIdString obj :vlax-false)
(itoa (vla-get-Objectid obj))
)
)Thảo luận gốc
Lisp được chia sẻ trong chủ đề nhờ kết hợp 2 lisp. Xem đúng bài viết #311805 để đọc hướng dẫn và trao đổi thêm.