Author Topic: Get attribute values and tags from mleader block  (Read 3192 times)

0 Members and 1 Guest are viewing this topic.

cmwade77

  • Swamp Rat
  • Posts: 1443
Get attribute values and tags from mleader block
« on: March 13, 2014, 03:21:05 PM »
I had the need to get all of the attribute tags and values from a block attached to an mLeader. It took me a bit to figure out how to accomplish this and I don't think that I could have without Lee's itemp function, as you can see in the code. There may be even better ways to accomplish this, but this is what I know and it works, given how much it took me to figure this out, I thought it might benefit some others as well and that I would post it here to see if anyone had ideas from improving it.

Code: [Select]
;;-----------------------=={ Itemp }==------------------------;;
;;                                                            ;;
;;  Retrieves the item with index 'item' if present in the    ;;
;;  specified collection, else nil                            ;;
;;------------------------------------------------------------;;
;;  Author: Lee McDonnell, 2010                               ;;
;;                                                            ;;
;;  Copyright � 2010 by Lee McDonnell, All Rights Reserved.   ;;
;;  Contact: Lee Mac @ TheSwamp.org, CADTutor.net             ;;
;;------------------------------------------------------------;;
;;  Arguments:                                                ;;
;;  coll - the VLA Collection Object                          ;;
;;  item - the index of the item to be retrieved              ;;
;;------------------------------------------------------------;;
;;  Returns:  the VLA Object at the specified index, else nil ;;
;;------------------------------------------------------------;;

(defun LM:Itemp ( coll item )
  ;; � Lee Mac 2010
  (if
    (not
      (vl-catch-all-error-p
        (setq item
          (vl-catch-all-apply
            (function vla-item) (list coll item)
          )
        )
      )
    )
    item
  )
)

(defun CW:GetMleaderAttData (Obj / Att AttList Ent Obj Temp)
(setq AttList (LM:Itemp (vla-get-Blocks (vla-get-ActiveDocument (vlax-get-acad-object))) (vla-get-ContentBlockName Obj)))
(vlax-for Att AttList
(if (= (vla-get-objectname Att) "AcDbAttributeDefinition")
(progn
(setq Temp (list Temp (cons (vla-get-tagstring Att) (vla-GetBlockAttributeValue Obj (vla-get-ObjectID Att)))))
)
)
)
Temp
)

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: Get attribute values and tags from mleader block
« Reply #1 on: March 13, 2014, 03:38:09 PM »
Thanks Chris - that's very old code, but I'm glad you found it useful  :-)

Here is another way to write the function, to also account for 64-bit systems:
Code: [Select]
(defun getmleaderatts ( obj / rtn )
    (if
        (and
            (= "AcDbMLeader"  (vla-get-objectname  obj))
            (= acblockcontent (vla-get-contenttype obj))
        )
        (vlax-for sub
             (vla-item
                 (vla-get-blocks (vla-get-document obj))
                 (vla-get-contentblockname obj)
             )
            (if (= "AcDbAttributeDefinition" (vla-get-objectname sub))
                (setq rtn
                    (cons
                        (cons
                            (vla-get-tagstring sub)
                            (vla-getblockattributevalue obj (LM:objectid sub))
                        )
                        rtn
                    )
                )
            )
        )
    )
    (reverse rtn)
)

;; 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
                    (wcmatch (getenv "PROCESSOR_ARCHITECTURE") "*64*")
                    (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)
)

;; 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)

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: Get attribute values and tags from mleader block
« Reply #2 on: March 13, 2014, 04:49:05 PM »
The code might be old, but it does what I need it to.

The code that I posted was tested on a 64-Bit system. But looking at help files, it seems that AutoCAD 2013 fixed these items, so that they run on both 64-Bit and 32-Bit systems, so I suppose the code you posted could be needed on older systems. Although your code definitely has better error handling, such as checking to make sure that the object is an mleader with a block than what I posted, but that is because I needed to handle the error handling before calling this routine for other reasons.

But I do have to say that I am always in awe of the code you come up with and honestly a lot of our routines utilize some code of yours somewhere.