Copy Attribute Value as Field

Copy Attribute Value as Field

christopher.l.evans
Advocate Advocate
234 Views
3 Replies
Message 1 of 4

Copy Attribute Value as Field

christopher.l.evans
Advocate
Advocate

Does anyone know of a routine to copy a block attribute to multiple user selected block attributes as a field (%<\\AcObjProp Object(%<\\_ObjId #########>%).TextString>%)?

 

@Lee_MacCopyFieldV1-1.lsp is the closest routine I've come across to what I'm looking for.  However the initial attribute selection has to be a field.  I would like to select a blocks text/mtext attribute value initially, I would also like to be able to switch to other layouts for placement selections.

 

 

;;--------------------------=={ Copy Field }==--------------------------;;
;;                                                                      ;;
;;  This program enables the user to copy a field expression from a     ;;
;;  selected source object to multiple destination objects in a         ;;
;;  drawing.                                                            ;;
;;                                                                      ;;
;;  Upon issuing the command syntax 'copyfield' at the AutoCAD          ;;
;;  command-line, the user is prompted to select an annotation object   ;;
;;  (Text, MText, Attribute, Multileader, Dimension) containing a       ;;
;;  field expression to be copied.                                      ;;
;;                                                                      ;;
;;  Following a valid response, the user may then copy the field to     ;;
;;  multiple selected destination objects in the drawing.               ;;
;;----------------------------------------------------------------------;;
;;  Author:  Lee Mac, Copyright © 2013  -  www.lee-mac.com              ;;
;;----------------------------------------------------------------------;;
;;  Version 1.0    -    2013-07-14                                      ;;
;;                                                                      ;;
;;  - First release.                                                    ;;
;;----------------------------------------------------------------------;;
;;  Version 1.1    -    2017-06-13                                      ;;
;;                                                                      ;;
;;  - Updated LM:fieldcode function to account for field expressions    ;;
;;    greater than 250 characters in length.                            ;;
;;----------------------------------------------------------------------;;
 
(defun c:copyfield ( / *error* select src )
 
    (defun *error* ( msg )
        (LM:endundo (LM:acdoc))
        (if (not (wcmatch (strcase msg t) "*break,*cancel*,*exit*"))
            (princ (strcat "\nError: " msg))
        )
        (princ)
    )
 
    (defun select ( msg fun / ent rtn )
        (while
            (progn (setvar 'errno 0) (setq ent (nentsel msg))
                (cond
                    (   (= 7 (getvar 'errno))
                        (princ "\nMissed, try again.")
                    )
                    (   (= 'list (type ent))
                        (cond
                            (   (progn
                                    (if (= 4 (length ent))
                                        (setq ent (last (last ent)))
                                        (setq ent (car ent))
                                    )
                                    (not (wcmatch (cdr (assoc 0 (entget ent))) "TEXT,MTEXT,ATTRIB,MULTILEADER,*DIMENSION"))
                                )
                                (princ "\nInvalid object selected.")
                            )
                            (   (not (setq rtn ((eval fun) ent))))
                        )
                    )
                )
            )
        )
        rtn
    )
 
    (if
        (setq src
            (select "\nSelect source field: "
                (function
                    (lambda ( ent )
                        (cond ((LM:fieldcode ent)) ((not (princ "\nSelected object does not contain a field."))))
                    )
                )
            )
        )
        (progn
            (LM:startundo (LM:acdoc))
            (select "\nSelect destination object <Exit>: "
                (function
                    (lambda ( ent / obj )
                        (cond
                            (   (null (vlax-write-enabled-p (setq obj (vlax-ename->vla-object ent))))
                                (princ "\nSelected object is on a locked layer.")
                            )
                            (   (vlax-property-available-p obj 'textoverride t)
                                (vla-put-textoverride obj src)
                                (command "_.updatefield" ent "")
                            )
                            (   (vlax-property-available-p obj 'textstring t)
                                (vla-put-textstring obj src)
                                (command "_.updatefield" ent "")
                            )
                        )
                        nil
                    )
                )
            )
            (LM:endundo (LM:acdoc))
        )
    )
    (princ)
)
 
;; Field Code  -  Lee Mac
;; Returns the field expression associated with an entity
 
(defun LM:fieldcode ( ent / replacefield replaceobject fieldstring enx )
 
    (defun replacefield ( str enx / ent fld pos )
        (if (setq pos (vl-string-search "\\_FldIdx" (setq str (replaceobject str enx))))
            (progn
                (setq ent (assoc 360 enx)
                      fld (entget (cdr ent))
                )
                (strcat
                    (substr str 1 pos)
                    (replacefield (fieldstring fld) fld)
                    (replacefield (substr str (1+ (vl-string-search ">%" str pos))) (cdr (member ent enx)))
                )
            )
            str
        )
    )
 
    (defun replaceobject ( str enx / ent pos )
        (if (setq pos (vl-string-search "ObjIdx" str))
            (strcat
                (substr str 1 (+ pos 5)) " "
                (LM:ObjectID (vlax-ename->vla-object (cdr (setq ent (assoc 331 enx)))))
                (replaceobject (substr str (1+ (vl-string-search ">%" str pos))) (cdr (member ent enx)))
            )
            str
        )
    )
 
    (defun fieldstring ( enx / itm )
        (if (setq itm (assoc 3 enx))
            (strcat (cdr itm) (fieldstring (cdr (member itm enx))))
            (cond ((cdr (assoc 2 enx))) (""))
        )
    )
    
    (if (and (wcmatch  (cdr (assoc 0 (setq enx (entget ent)))) "TEXT,MTEXT,ATTRIB,MULTILEADER,*DIMENSION")
             (setq enx (cdr (assoc 360 enx)))
             (setq enx (dictsearch enx "ACAD_FIELD"))
             (setq enx (dictsearch (cdr (assoc -1 enx)) "TEXT"))
        )
        (replacefield (fieldstring enx) enx)
    )
)
 
;; 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
                (and
                    (vl-string-search "64" (getenv "PROCESSOR_ARCHITECTURE"))
                    (vlax-method-applicable-p (vla-get-utility (LM:acdoc)) 'getobjectidstring)
                )
                (list 'vla-getobjectidstring (vla-get-utility (LM:acdoc)) 'obj ':vlax-false)
               '(itoa (vla-get-objectid obj))
            )
        )
    )
    (LM:ObjectID obj)
)
 
;; 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:: CopyField.lsp | Version 1.1 | \\U+00A9 Lee Mac "
        (menucmd "m=$(edtime,0,yyyy)")
        " www.lee-mac.com ::"
        "\n:: Type \"copyfield\" to Invoke ::"
    )
)
(princ)
 
;;----------------------------------------------------------------------;;
;;                             End of File                              ;;
;;----------------------------------------------------------------------;;

 

 

0 Likes
235 Views
3 Replies
Replies (3)
Message 2 of 4

paullimapa
Mentor
Mentor

have you found/tried this thread:

Copy Attribute values between blocks. - Autodesk Community - AutoCAD


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 3 of 4

christopher.l.evans
Advocate
Advocate

Didn't seem to have what I was aiming for.

 

What I'm doing is associating one or more block attribute values as a field from a source block attribute.  So when a callout is changed, the field value would too after a regen.

0 Likes
Message 4 of 4

paullimapa
Mentor
Mentor

see if you can put the following code together with the others to make it work:

; place at start of code:

 (vl-load-com)

(defun Get-ObjectIDx64 (obj / util)(setq util (vla-get-Utility (vla-get-activedocument (vlax-get-acad-object))))(if (= (type obj) 'ENAME)(setq obj (vlax-ename->vla-object obj)))(if (= (type obj) 'VLA-OBJECT)(if (> (vl-string-search "x64" (getvar "platform")) 0)(vlax-invoke-method util "GetObjectIdString" obj :vlax-False)(rtos (vla-get-objectid obj) 2 0)))

) ;  defun Get-ObjectIDx64

; then use the following to select

(setq en(car(nentsel))) ; select 1st block's attributes you want to create a field from
(setq ed (entget en)); get the dxf
(setq txt(cdr(assoc 1 ed))) ; get the text string
(setq txtnew (strcat "%<\\AcObjProp.16.2 Object (%<\\_ObjId " (Get-ObjectIDx64 en) ">%).TextString>%")) ; create new text string as field
(setq en1(car(nentsel))) ; select other block's attributes you want to place the field into
(vla-put-TextString (vlax-ename->vla-object en1) txtnew) ; replace with new text string as field
(command"_.UpdateField" en1 "") ; update the block attribute
 

Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes