Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

GetAttributes via lisp

15 REPLIES 15
SOLVED
Reply
Message 1 of 16
XIJIANGWOO
2052 Views, 15 Replies

GetAttributes via lisp

Hello guys,

 

         I am trying to find a lisp that would get an attribute value (tag name = NBR_5) . I'm trying to run a simple routine that would let the user place the value from the titleblock attribute as text on a drawing. I see a ton of articles regarding getting attributes but I am not well versed in programming.

 

Any help is greatly appreciated. 

 

Thanks in advance. 

 

15 REPLIES 15
Message 2 of 16
alanjt_
in reply to: XIJIANGWOO

(defun _findMatchingAttributeValue (block tagstring)
  (vl-some (function (lambda (att)
                       (if (eq (vla-get-tagstring att) (strcase tagstring))
                         (vla-get-textstring att)
                       )
                     )
           )
           (vlax-invoke block 'GetAttributes)
  )
)


(_findMatchingAttributeValue <vla-object> <tagstring>)

 

Message 3 of 16
XIJIANGWOO
in reply to: alanjt_

Thanks Alan,

 

I am not familiar with Vlisp. I received this error "; error: bad argument type: VLA-OBJECT nil". What am I doing wrong? I know your answer is going to be obvious, isn't it?

 

thanks again.

Message 4 of 16
BlackBox_
in reply to: XIJIANGWOO

The last line of Alan's code is an example, where you would replace <vla-object>, and <tagstring> with actual values.

 

Consider this pseduo-code:

 

(vl-load-com)

(defun c:FOO (/ _findMatchingAttributeValue ss value)

  (defun _findMatchingAttributeValue (block tagstring)
    (vl-some (function
               (lambda (att)
                 (if (eq (vla-get-tagstring att) (strcase tagstring))
                   (vla-get-textstring att)
                 )
               )
             )
             (vlax-invoke block 'GetAttributes)
    )
  )

  (if (and (setq ss (ssget ":S:E" '((0 . "INSERT") (66 . 1))))
           (setq value (_findMatchingAttributeValue
                         (vlax-ename->vla-object (ssname ss 0))         ;<-- <vla-object>
                         "NBR_5"                                        ;<-- <tagstring>
                       )
           )
      )
    (prompt (strcat "\n[Attribute Value] : " value))
    (prompt "\n** Invalid selection ** ")
  )
  (princ)
)

 



"How we think determines what we do, and what we do determines what we get."

Message 5 of 16
dbroad
in reply to: XIJIANGWOO

It hardly worth a program.  Use ddatte. Pick the titleblock. Highlight the text. CTRL+C.  Then, CTRL+V into the drawing.

Architect, Registered NC, VA, SC, & GA.
Message 6 of 16
alanjt_
in reply to: BlackBox_

BlackBox answered your question about converting to a vla-object. However, just for completeness, here's a non VLISP version:

 

(defun _findMatchingAttributeValue (block tagstring / data string)
  (while (and (not (eq (cdr (assoc 0 (setq data (entget (setq block (entnext block)))))) "SEQEND"))
              (not string)
         )
    (if (and (eq (cdr (assoc 0 data)) "ATTRIB") (eq (cdr (assoc 2 data)) (strcase tagstring)))
      (setq string (cdr (assoc 1 data)))
    )
  )
  string
)

 

Message 7 of 16
BlackBox_
in reply to: alanjt_

FWIW -

 

I think you'll find both of these adaptations to be *slightly* faster:

 

(defun _setq (block tagstring)
  (setq tagstring (strcase tagstring))
  (vl-some (function
             (lambda (att)
               (if (eq (vla-get-tagstring att) tagstring)
                 (vla-get-textstring att)
               )
             )
           )
           (vlax-invoke block 'GetAttributes)
  )
)

(defun _cond (block tagstring)
  (vl-some (function
             (lambda (att)
               (if (eq (vla-get-tagstring att)
                       (cond (tagstring)
                             ((setq tagstring (strcase tagstring)))
                       )
                   )
                 (vla-get-textstring att)
               )
             )
           )
           (vlax-invoke block 'GetAttributes)
  )
)

 

... Speed test of 1000 iterations (same block, same tag😞

 

_FINDMATCHINGATTRIBUTEVALUE
Elapsed: 312
Average: 0.3120

_SETQ
Elapsed: 281
Average: 0.2810

_COND
Elapsed: 280
Average: 0.2800

 

Minor advantage; not worth recoding multiple functions, etc., IMO. Just thought I'd mention it... Again, only FWIW.

 

Cheers



"How we think determines what we do, and what we do determines what we get."

Message 8 of 16
XIJIANGWOO
in reply to: BlackBox_

Thanks BlackBox,

 

   How would I revise the code to skip the block selection as it is always the same block "TitleBlockMfg" and have that attribute value placed as text ?

 

This is actually part of a macro that I use when I cycle through many drawings tagging specific objects with the "NBR" tag value loacate in the titleblock. 

 

It would make life so much easier here.

 

Thanks for all of the help everyone !

 

 

Message 9 of 16
alanjt_
in reply to: XIJIANGWOO

(ssget "_X" '( (0 . "INSERT") (2 . "TitleBlockMfg")))

Message 10 of 16
alanjt_
in reply to: alanjt_

Good catch, BBox. I should have change the case of the string before hand. 

Message 11 of 16
alanjt_
in reply to: alanjt_

BTW, your _cond won't work, since it only checks if 'tagstring is an assigned variable, which it is. You need to setq a different variable for _cond.

 

eg.

(defun _cond (block tagstring / upcase)
  (vl-some (function
             (lambda (att)
               (if (eq (vla-get-tagstring att)
                       (cond (upcase)
                             ((setq upcase (strcase tagstring)))
                       )
                   )
                 (vla-get-textstring att)
               )
             )
           )
           (vlax-invoke block 'GetAttributes)
  )
)

 However, I'd just stick with upcasing the string before running anything, IMO.

Message 12 of 16
BlackBox_
in reply to: alanjt_

Yes, of course... What a silly mistake (not changing the name)... Keen eye, as always.

 

Cheers



"How we think determines what we do, and what we do determines what we get."

Message 13 of 16
BlackBox_
in reply to: XIJIANGWOO


@XIJIANGWOO wrote:

Thanks BlackBox,

 

   How would I revise the code to skip the block selection as it is always the same block "TitleBlockMfg" and have that attribute value placed as text ?

 

This is actually part of a macro that I use when I cycle through many drawings tagging specific objects with the "NBR" tag value loacate in the titleblock. 

 

It would make life so much easier here.

 

Thanks for all of the help everyone !

 

 


Alan's answered your question already, about how to revise the SSGET call... Did you get your macro working they way you wanted?

 

Cheers



"How we think determines what we do, and what we do determines what we get."

Message 14 of 16
alanjt_
in reply to: BlackBox_


@BlackBox_ wrote:

Yes, of course... What a silly mistake (not changing the name)... Keen eye, as always.

 

Cheers


You caught my coding boo boo first. 🙂

Message 15 of 16
XIJIANGWOO
in reply to: BlackBox_

Thank Everyone. After playing with the code supplied by Blackbox, I was able to get my macro running.

 

I still have a lot to learn. I'm not in full time CAD like I used to be. 

 

 

Xi

 

Message 16 of 16
BlackBox_
in reply to: XIJIANGWOO


@XIJIANGWOO wrote:

Thank Everyone. After playing with the code supplied by Blackbox, I was able to get my macro running.

 


That is kind of you to say; I'm happy to help. :beer:

 


@XIJIANGWOO wrote:

I still have a lot to learn. I'm not in full time CAD like I used to be. 

 


Don't we all... And you're not alone; I was just busted down to half-time myself. *Looking*



"How we think determines what we do, and what we do determines what we get."

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

”Boost