TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: xiaxiang on January 26, 2011, 04:27:57 AM

Title: Need help--Get tag,prompt and value of Attribute
Post by: xiaxiang on January 26, 2011, 04:27:57 AM
Is there a lisp can do that: select Attribute,then return it's tag, prompt and value?
And I know that "attributefunctions" From Lee
 http://lee-mac.com/attributefunctions.html (http://lee-mac.com/attributefunctions.html)
But only return tag and value.I want prompt,too.
Thanks to all :angel:
Title: Re: Need help--Get tag,prompt and value of Attribute
Post by: Jeff H on January 26, 2011, 05:36:33 AM
I know nothing about Lisp but I am going to take a guess

vla-get-PromptString
Title: Re: Need help--Get tag,prompt and value of Attribute
Post by: T.Willey on January 26, 2011, 11:19:37 AM
The issue with attributes ( that are associated with an inserts ) is that they don't really relate back to the attributes that are within the block definition.  They seem to be in order, with respect to prompt values, but that doesn't always give you the right item.  Doesn't make sense right?  Yea we know, but that is what is happening.  If you want to get the prompt value, then you have to step through the block definition and get the prompt from the attribute definition, code 3 I think.  You can also get the tag from there, and then get the inserts of said block ( however you want, couple of ways ) then step through those and get the value of each attribute.  Hope that helps.
Title: Re: Need help--Get tag,prompt and value of Attribute
Post by: Lee Mac on January 26, 2011, 11:29:01 AM
Getting at the Attribute Prompts is slightly more difficult than just Tag/Value, since the prompt is stored in the block definition.

An example function might be:

Code: [Select]
;; Get Attribute Tag, Prompt & Value  -  Lee Mac 2011  -  www.lee-mac.com
;;
;; Arguments:
;; VLA Attributed Block Reference Object
;;
;; Returns:
;; List of   ((<Tag> <Prompt> <Value>) ... )

(defun GetAttributeTagPromptValue ( block / lst ) (vl-load-com)

  (vlax-for obj
    (vla-item (vla-get-Blocks (vla-get-Document block))
      (if (vlax-property-available-p block 'EffectiveName)
        (vla-get-EffectiveName block)
        (vla-get-Name block)
      )
    )
    (if (eq "AcDbAttributeDefinition" (vla-get-ObjectName obj))
      (setq lst
        (cons
          (list (vla-get-TagString obj) (vla-get-PromptString obj)) lst
        )
      )
    )
  )

  (mapcar
    (function
      (lambda ( attrib )
        (append (assoc (vla-get-TagString attrib) lst) (list (vla-get-TextString attrib)))
      )
    )
    (vlax-invoke block 'GetAttributes)
  )
)


;; Test Function

(defun c:test ( / ss )

  (if (setq ss (ssget "_+.:S:E" '((0 . "INSERT") (66 . 1))))
    (print
      (GetAttributeTagPromptValue
        (vlax-ename->vla-object (ssname ss 0))
      )
    )
  )

  (princ)
)

EDIT: Tim beat me to it.. I've got to learn to type faster...

Also, bear in mind the above code will return erroneous results for multiple attributes with the same tag - follows from Tim's description of how they are stored.
Title: Re: Need help--Get tag,prompt and value of Attribute
Post by: T.Willey on January 26, 2011, 11:51:57 AM
Lee,

  I just noticed something cool, that I think would make code a little smarter.  You code looks like

Code: [Select]
(defun GetAttributeTagPromptValue ( block / lst )

  (vlax-for obj
    (vla-item
      (vla-get-Blocks
        (vla-get-ActiveDocument (vlax-get-acad-object))
      )
<snip>

Where if the ' block ' that is passed in not in the current drawing will error.  I know, highly unlikely, but I was thinking that there had to be a better way, and there is.

Code: [Select]
(defun GetAttributeTagPromptValue ( block / lst )

  (vlax-for obj
    (vla-item
      (vla-get-Blocks
        (vla-get-Document block)
      )

I'm assuming that it would be the same as with objectDBX.  Just an idea to get the correct document, if you don't pass the document.
Title: Re: Need help--Get tag,prompt and value of Attribute
Post by: Lee Mac on January 26, 2011, 11:55:20 AM
Code: [Select]
(defun GetAttributeTagPromptValue ( block / lst )

  (vlax-for obj
    (vla-item
      (vla-get-Blocks
        (vla-get-Document block)
      )

I'm assuming that it would be the same as with objectDBX.  Just an idea to get the correct document, if you don't pass the document.

Fantastic idea - good thinking Tim :-)

Updated as per your suggestion!
Title: Re: Need help--Get tag,prompt and value of Attribute
Post by: xiaxiang on January 27, 2011, 04:40:43 AM
It worked very well ,Lee.Thanks! I think that Nothing can stop you from writing functions for us.
and same to Tim!
Title: Re: Need help--Get tag,prompt and value of Attribute
Post by: Lee Mac on January 27, 2011, 11:30:44 AM
It worked very well ,Lee.Thanks! I think that Nothing can stop you from writing functions for us.
and same to Tim!

I'm definitely addicted  :lol:

You're welcome xiaxiang  :-)