Lisp lệnh LENGFIELD trong chủ đề “Nhờ sữa lisp xuất chiều dài pline ra text”
Dựa theo lisp MIDLEN.lsp của LEEMAC, Thiep ra lisp LENGFIELD.lsp phù hợp với ý của bạn
Nhớ là cách chọn đối tượng là kiểu Fence nhe bạn, mục đích của mình là đối tượng polyline nào "dính" hàng rào trước thì lisp sẽ tạo ra 1 text gắn field trước, như vậy sẽ pick điểm chèn text cho phù hợp theo trật tự.
;;-------------------=={ Length *POLYLINE _ field }==-------------------;; ;; ;; ;; This program prompts the user for a selection of objects to be ;; ;; labelled and proceeds to generate an MText object located at ;; ;; the midpoint of each object displaying a Field Expression ;; ;; referencing the length of the object. ;; ;; ;; ;; The program is compatible for use with *Polylines, and under ;; ;; all UCS & View settings. ;; ;; The MText will have a background mask ;; ;; enabled and will use the active Text Style and Text Height settings ;; ;; at the time of running the program. ;; ;;----------------------------------------------------------------------;; ;; Author: Trân Thiêp base lisp midlen.lsp by Lee Mac, Copyright © 2013 - www.lee-mac.com ;; ;;----------------------------------------------------------------------;; ;; - Modified LM:objectid function to account for 64-bit AutoCAD 2008. ;; ;;----------------------------------------------------------------------;; (defun c:lengfield (/ *error* ent fmt idx ins ocs par sel spc txt typ uxa) (setvar "textsize" (getvar "DIMTXT")) (defun *error* (msg) (LM:endundo (LM:acdoc)) (if (not (wcmatch (strcase msg t) "*break,*cancel*,*exit*")) (princ (strcat "\nError: " msg)) ) (princ) ) (if (setq sel (ssget "F" (acet-ui-fence-select) (list '(0 . "*POLYLINE") '(-4 . "<NOT") '(-4 . "<AND") '(0 . "POLYLINE") '(-4 . "&") '(70 . 80) '(-4 . "AND>") '(-4 . "NOT>") (if (= 1 (getvar 'cvport)) (cons 410 (getvar 'ctab)) '(410 . "Model") ) ) ) ) (progn (setq spc (vlax-get-property (LM:acdoc) (if (= 1 (getvar 'cvport)) 'paperspace 'modelspace ) ) ) (setq ocs (trans '(0.0 0.0 1.0) 1 0 t) uxa (angle '(0.0 0.0) (trans (getvar 'ucsxdir) 0 ocs t)) ) (LM:startundo (LM:acdoc)) (repeat (setq idx (sslength sel)) (setq ent (ssname sel (setq idx (1- idx))) par (vlax-curve-getparamatdist ent (/ (vlax-curve-getdistatparam ent (vlax-curve-getendparam ent)) 2.0) ) ins (vlax-curve-getpointatparam ent par) typ (cdr (assoc 0 (entget ent))) ) (setq txt (vlax-invoke spc 'addmtext ins 0.0 (strcat "L=" "%<\\AcObjProp Object(%<\\_ObjId " (LM:objectid (vlax-ename->vla-object ent)) ">%).Length \\f \"%lu2%pr0%ps%ct8\">%" ) ) ) (vla-put-backgroundfill txt :vlax-true) (vla-put-attachmentpoint txt acattachmentpointmiddlecenter) (vla-put-insertionpoint txt (vlax-3D-point (getpoint "\pick a point for inserttext_field")) ) ) (LM:endundo (LM:acdoc)) ) ) (princ) ) ;; Readable - Lee Mac ;; Returns an angle corrected for text readability. (defun LM:readable ( a ) ( (lambda ( a ) (if (and (< (* pi 0.5) a) (<= a (* pi 1.5))) (LM:readable (+ a pi)) a ) ) (rem (+ a pi pi) (+ pi pi)) ) ) ;; ObjectID - Lee Mac ;; Returns a string containing the ObjectID of a supplied VLA-Object ;; Compatible with 32-bit & 64-bit systems (defun LM:objectid ( obj ) (eval (list 'defun 'LM:objectid '( obj ) (if (wcmatch (getenv "PROCESSOR_ARCHITECTURE") "*64*") (if (vlax-method-applicable-p (vla-get-utility (LM:acdoc)) 'getobjectidstring) (list 'vla-getobjectidstring (vla-get-utility (LM:acdoc)) 'obj ':vlax-false) '(LM:ename->objectid (vlax-vla-object->ename obj)) ) '(itoa (vla-get-objectid obj)) ) ) ) (LM:objectid obj) ) ;; Entity Name to ObjectID - Lee Mac ;; Returns the 32-bit or 64-bit ObjectID for a supplied entity name (defun LM:ename->objectid ( ent ) (LM:hex->decstr (setq ent (vl-string-right-trim ">" (vl-prin1-to-string ent)) ent (substr ent (+ (vl-string-position 58 ent) 3)) ) ) ) ;; Hex to Decimal String - Lee Mac ;; Returns the decimal representation of a supplied hexadecimal string (defun LM:hex->decstr ( hex / foo bar ) (defun foo ( lst rtn ) (if lst (foo (cdr lst) (bar (- (car lst) (if (< 57 (car lst)) 55 48)) rtn)) (apply 'strcat (mapcar 'itoa (reverse rtn))) ) ) (defun bar ( int lst ) (if lst (if (or (< 0 (setq int (+ (* 16 (car lst)) int))) (cdr lst)) (cons (rem int 10) (bar (/ int 10) (cdr lst))) ) (bar int '(0)) ) ) (foo (vl-string->list (strcase hex)) nil) ) ;; Start Undo - Lee Mac ;; Opens an Undo Group. (defun LM:startundo ( doc ) (LM:endundo doc) (vla-startundomark doc) ) ;; End Undo - Lee Mac ;; Closes an Undo Group. (defun LM:endundo ( doc ) (while (= 8 (logand 8 (getvar 'undoctl))) (vla-endundomark doc) ) ) ;; Active Document - Lee Mac ;; Returns the VLA Active Document Object (defun LM:acdoc nil (eval (list 'defun 'LM:acdoc 'nil (vla-get-activedocument (vlax-get-acad-object)))) (LM:acdoc) ) (vl-load-com) (princ (strcat "\n:: Lengfield.lsp | by TranThiep | " (menucmd "m=$(edtime,0,yyyy)") ", based lisp midlen.lsp \\U+00A9 Lee Mac " "\n:: Type \"lengfield\" to Invoke ::" ) ) (princ) ;;----------------------------------------------------------------------;; ;; End of File ;; ;;----------------------------------------------------------------------;;
Thân ái, chúc bạn thành công. TranThiep tel:0918841230
- Tải lisp
- 458786_lengfield.lsp
Cách dùng
- Tải tệp 458786_lengfield.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 LENGFIELD để chạy lisp.
Mã nguồn 458786_lengfield.lsp
;; free lisp from cadviet.com
;;; this lisp was downloaded from https://www.cadviet.com/forum/topic/188463-nh%E1%BB%9D-s%E1%BB%AFa-lisp-xu%E1%BA%A5t-chi%E1%BB%81u-d%C3%A0i-pline-ra-text/
;;-------------------=={ Length *POLYLINE _ field }==-------------------;;
;; ;;
;; This program prompts the user for a selection of objects to be ;;
;; labelled and proceeds to generate an MText object located at ;;
;; the midpoint of each object displaying a Field Expression ;;
;; referencing the length of the object. ;;
;; ;;
;; The program is compatible for use with *Polylines, and under ;;
;; all UCS & View settings. ;;
;; The MText will have a background mask ;;
;; enabled and will use the active Text Style and Text Height settings ;;
;; at the time of running the program. ;;
;;----------------------------------------------------------------------;;
;; Author: Trân Thiêp base lisp midlen.lsp by Lee Mac, Copyright © 2013 - www.lee-mac.com ;;
;;----------------------------------------------------------------------;;
;; - Modified LM:objectid function to account for 64-bit AutoCAD 2008. ;;
;;----------------------------------------------------------------------;;
(defun c:lengfield (/ *error* ent fmt idx ins ocs par sel spc txt typ uxa)
(setvar "textsize" (getvar "DIMTXT"))
(defun *error* (msg)
(LM:endundo (LM:acdoc))
(if (not (wcmatch (strcase msg t) "*break,*cancel*,*exit*"))
(princ (strcat "\nError: " msg))
)
(princ)
)
(if (setq sel (ssget "F"
(acet-ui-fence-select)
(list '(0 . "*POLYLINE")
'(-4 . "<NOT")
'(-4 . "<AND")
'(0 . "POLYLINE")
'(-4 . "&")
'(70 . 80)
'(-4 . "AND>")
'(-4 . "NOT>")
(if (= 1 (getvar 'cvport))
(cons 410 (getvar 'ctab))
'(410 . "Model")
)
)
)
)
(progn (setq spc (vlax-get-property (LM:acdoc)
(if (= 1 (getvar 'cvport))
'paperspace
'modelspace
)
)
)
(setq ocs (trans '(0.0 0.0 1.0) 1 0 t)
uxa (angle '(0.0 0.0) (trans (getvar 'ucsxdir) 0 ocs t))
)
(LM:startundo (LM:acdoc))
(repeat (setq idx (sslength sel))
(setq ent (ssname sel (setq idx (1- idx)))
par (vlax-curve-getparamatdist
ent
(/ (vlax-curve-getdistatparam ent (vlax-curve-getendparam ent)) 2.0)
)
ins (vlax-curve-getpointatparam ent par)
typ (cdr (assoc 0 (entget ent)))
)
(setq txt (vlax-invoke spc
'addmtext
ins
0.0
(strcat "L="
"%<\\AcObjProp Object(%<\\_ObjId "
(LM:objectid (vlax-ename->vla-object ent))
">%).Length \\f \"%lu2%pr0%ps[,0]%ct8[0.1]\">%"
)
)
)
(vla-put-backgroundfill txt :vlax-true)
(vla-put-attachmentpoint txt acattachmentpointmiddlecenter)
(vla-put-insertionpoint
txt
(vlax-3D-point (getpoint "\pick a point for inserttext_field"))
)
)
(LM:endundo (LM:acdoc))
)
)
(princ)
)
;; Readable - Lee Mac
;; Returns an angle corrected for text readability.
(defun LM:readable ( a )
( (lambda ( a )
(if (and (< (* pi 0.5) a) (<= a (* pi 1.5)))
(LM:readable (+ a pi))
a
)
)
(rem (+ a pi pi) (+ pi pi))
)
)
;; ObjectID - Lee Mac
;; Returns a string containing the ObjectID of a supplied VLA-Object
;; Compatible with 32-bit & 64-bit systems
(defun LM:objectid ( obj )
(eval
(list 'defun 'LM:objectid '( obj )
(if (wcmatch (getenv "PROCESSOR_ARCHITECTURE") "*64*")
(if (vlax-method-applicable-p (vla-get-utility (LM:acdoc)) 'getobjectidstring)
(list 'vla-getobjectidstring (vla-get-utility (LM:acdoc)) 'obj ':vlax-false)
'(LM:ename->objectid (vlax-vla-object->ename obj))
)
'(itoa (vla-get-objectid obj))
)
)
)
(LM:objectid obj)
)
;; Entity Name to ObjectID - Lee Mac
;; Returns the 32-bit or 64-bit ObjectID for a supplied entity name
(defun LM:ename->objectid ( ent )
(LM:hex->decstr
(setq ent (vl-string-right-trim ">" (vl-prin1-to-string ent))
ent (substr ent (+ (vl-string-position 58 ent) 3))
)
)
)
;; Hex to Decimal String - Lee Mac
;; Returns the decimal representation of a supplied hexadecimal string
(defun LM:hex->decstr ( hex / foo bar )
(defun foo ( lst rtn )
(if lst
(foo (cdr lst) (bar (- (car lst) (if (< 57 (car lst)) 55 48)) rtn))
(apply 'strcat (mapcar 'itoa (reverse rtn)))
)
)
(defun bar ( int lst )
(if lst
(if (or (< 0 (setq int (+ (* 16 (car lst)) int))) (cdr lst))
(cons (rem int 10) (bar (/ int 10) (cdr lst)))
)
(bar int '(0))
)
)
(foo (vl-string->list (strcase hex)) nil)
)
;; Start Undo - Lee Mac
;; Opens an Undo Group.
(defun LM:startundo ( doc )
(LM:endundo doc)
(vla-startundomark doc)
)
;; End Undo - Lee Mac
;; Closes an Undo Group.
(defun LM:endundo ( doc )
(while (= 8 (logand 8 (getvar 'undoctl)))
(vla-endundomark doc)
)
)
;; Active Document - Lee Mac
;; Returns the VLA Active Document Object
(defun LM:acdoc nil
(eval (list 'defun 'LM:acdoc 'nil (vla-get-activedocument (vlax-get-acad-object))))
(LM:acdoc)
)
(vl-load-com)
(princ
(strcat "\n:: Lengfield.lsp | by TranThiep | "
(menucmd "m=$(edtime,0,yyyy)")
", based lisp midlen.lsp \\U+00A9 Lee Mac "
"\n:: Type \"lengfield\" to Invoke ::"
)
)
(princ)
;;----------------------------------------------------------------------;;
;; End of File ;;
;;----------------------------------------------------------------------;;Thảo luận gốc
Lisp được chia sẻ trong chủ đề Nhờ sữa lisp xuất chiều dài pline ra text. Xem đúng bài viết #458786 để đọc hướng dẫn và trao đổi thêm.