Author Topic: Get attribute tagstring from block  (Read 6007 times)

0 Members and 1 Guest are viewing this topic.

GDF

  • Water Moccasin
  • Posts: 2081
Get attribute tagstring from block
« on: July 08, 2011, 02:08:29 PM »
How do I modify the code below to return the tagstring value "AttVal" of the attributed tag name?

Code: [Select]
(defun UpdateBlocks  (BlockName AttTag AttVal / ssBlocks ssObjects Block)
  (vl-load-com)
  (setq ssBlocks (ssget "x" (list (cons 2 BlockName))))
  (if ssBlocks
    (progn (setq ssObjects
                  (vla-get-ActiveSelectionSet
                    (vla-get-ActiveDocument (vlax-get-acad-object))))
           (vlax-for
                  Block  ssObjects
             (foreach
                    Attribute  (vlax-invoke Block "GetAttributes")
               (if (= (vla-get-TagString Attribute) AttTag)
                 (vla-put-TextString Attribute AttVal))
               (vla-update Attribute)))))
  (princ))
[code/]
Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2020x64 Windows 10x64

Lee Mac

  • Seagull
  • Posts: 12926
  • London, England
Re: Get attribute tagstring from block
« Reply #1 on: July 08, 2011, 03:04:40 PM »
I'm not sure that I completely understand the question - do you want to return the value of the variable 'AttVal'?

If so, just make the variable the last evaluated expression:

Code: [Select]
(defun UpdateBlocks ( BlockName AttTag AttVal / ss )
  (if (ssget "_X" (list (cons 2 BlockName) (cons 66 1)))
    (progn
      (vlax-for obj (setq ss (vla-get-ActiveSelectionSet (vla-get-ActiveDocument (vlax-get-acad-object))))
        (foreach att (vlax-invoke obj 'GetAttributes)
          (if (eq (vla-get-TagString att) AttTag)
            (vla-put-TextString att AttVal)
          )
        )
      )
      (vla-delete ss)
      attval
    )
  )
)

GDF

  • Water Moccasin
  • Posts: 2081
Re: Get attribute tagstring from block
« Reply #2 on: July 08, 2011, 03:16:16 PM »
Thanks Lee.

I want to read the existing tag value and then edit it...as per code below.
The code below does not print "NEWV1" to a dtext edit box, this exiting value for editing...which is my goal.

I hope this makes it clearer.


Code: [Select]
(defun C:SARIT  (/ NEWV1 NEWV2)
  (prompt "\n* To Rename Title Block Attribute *")
  (setq NEWV1 (getstring t "\n* Enter New Sheet Title <enter to leave unchanged> :"))
  (if (/= NEWV1 "")(UpdateBlocks "2436TBA" "SHT_TTL" NEWV1))
  (prompt "\n* Updating Sheet Number completed *")
  (setq NEWV2 (substr (getvar "dwgname") 1 (- (strlen (getvar "dwgname")) 4)))
  (setq NEWV2 (vl-string-subst "." "-" NEWV2))
  (UpdateBlocks "2436TBA" "A-01" NEWV2)
  (princ))
[code/]
Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2020x64 Windows 10x64

Lee Mac

  • Seagull
  • Posts: 12926
  • London, England
Re: Get attribute tagstring from block
« Reply #3 on: July 08, 2011, 03:42:56 PM »
Hi Gary,

How about something along these lines:

Code: [Select]
(defun c:sarit ( / new obj ss )
  (if
    (and
      (setq ss  (ssget "_X" '((0 . "INSERT") (2 . "2436TBA") (66 . 1))))
      (setq obj (vlax-ename->vla-object (ssname ss 0)))
      (setq new (LM:EditBox (cond ((LM:GetAttributeValue obj "SHT_TTL")) (""))))
    )
    (progn
      (LM:SetAttributeValue obj "SHT_TTL" new)
      (prompt "\n* Updating Sheet Number completed *")
      (LM:SetAttributeValue obj "A-01" (vl-string-translate "-" "." (vl-filename-base (getvar 'DWGNAME))))
    )
  )
  (princ)
)

(defun LM:EditBox ( string / id )
  (and
    (< 0 (setq id (load_dialog "ACAD")))
    (new_dialog "acad_txtedit" id)
    (set_tile    "text_edit"  string)
    (action_tile "text_edit" "(setq string $value)")
    (if (zerop (start_dialog)) (setq string nil))
  )
  (if (< 0 id) (unload_dialog id))
  string
)

(defun LM:GetAttributeValue ( block tag ) (setq tag (strcase tag))
  (vl-some
    (function
      (lambda ( attrib )
        (if (eq tag (strcase (vla-get-Tagstring attrib)))
          (vla-get-TextString attrib)
        )
      )
    )
    (vlax-invoke block 'GetAttributes)
  )
)

(defun LM:SetAttributeValue ( block tag value ) (setq tag (strcase tag))
  (vl-some
    (function
      (lambda ( attrib )
        (if (eq tag (strcase (vla-get-TagString attrib)))
          (progn (vla-put-TextString attrib value) value)
        )
      )
    )
    (vlax-invoke block 'GetAttributes)
  )
)

I have taken the attribute functions from here. I assumed you only wanted a single block updated, but it could be easily modified to update a set of blocks.

Lee

GDF

  • Water Moccasin
  • Posts: 2081
Re: Get attribute tagstring from block
« Reply #4 on: July 08, 2011, 03:54:15 PM »
Lee

I have a beer with your name on it. Saying Thank You just is not good enough. I tried for an hour to hack out a solution. Now you have gotten me spoiled. I will study your solution and learn from it.

Gary

 :-)
Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2020x64 Windows 10x64

Lee Mac

  • Seagull
  • Posts: 12926
  • London, England
Re: Get attribute tagstring from block
« Reply #5 on: July 08, 2011, 04:31:04 PM »
I have a beer with your name on it. Saying Thank You just is not good enough. I tried for an hour to hack out a solution. Now you have gotten me spoiled. I will study your solution and learn from it.

Good stuff Gary, I like beer  :lol:

You're very welcome, and of course, if you have any questions about any part of the code, I would be happy to provide clarification  :-)

EDIT: Looking back at the code, a faster way to approach it might be to first collect an association list of attribute objects with their tag string, then 'assoc' from this list when updating and reading the attribute values.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2149
  • class keyThumper<T>:ILazy<T>
Re: Get attribute tagstring from block
« Reply #6 on: July 08, 2011, 05:35:23 PM »

EDIT: Looking back at the code, a faster way to approach it might be to first collect an association list of attribute objects with their tag string, then 'assoc' from this list when updating and reading the attribute values.

That's what I do ... Slurp everything into an accoc list and go from there
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

Lee Mac

  • Seagull
  • Posts: 12926
  • London, England
Re: Get attribute tagstring from block
« Reply #7 on: July 08, 2011, 08:33:26 PM »
Perhaps this is a little more succinct  :-)

Code: [Select]
(defun c:sarit ( / _get _set lst new ss )

  (defun _get ( tag alist / ass )
    (if (setq ass (cdr (assoc tag alist))) (vla-get-textstring ass))
  )

  (defun _set ( tag value alist / ass )
    (if (setq ass (cdr (assoc tag alist))) (vla-put-textstring ass value))
  )
 
  (if
    (and
      (setq ss  (ssget "_X" '((0 . "INSERT") (2 . "2436TBA") (66 . 1))))
      (setq lst
        (mapcar'(lambda ( x ) (cons (vla-get-tagstring x) x))
          (vlax-invoke (vlax-ename->vla-object (ssname ss 0)) 'getattributes)
        )
      )
      (setq new (LM:EditBox (cond ((_get "SHT_TTL" lst)) (""))))
    )
    (progn
      (_set "SHT_TTL" new lst)
      (_set "A-01" (vl-string-translate "-" "." (vl-filename-base (getvar 'DWGNAME))) lst)
    )
  )
  (princ)
)

(defun LM:EditBox ( string / id )
  (and
    (< 0 (setq id (load_dialog "ACAD")))
    (new_dialog "acad_txtedit" id)
    (set_tile    "text_edit"  string)
    (action_tile "text_edit" "(setq string $value)")
    (if (zerop (start_dialog)) (setq string nil))
  )
  (if (< 0 id) (unload_dialog id))
  string
)

GDF

  • Water Moccasin
  • Posts: 2081
Re: Get attribute tagstring from block
« Reply #8 on: July 11, 2011, 11:51:13 AM »
Thanks again Lee
Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2020x64 Windows 10x64

airportman

  • Newt
  • Posts: 37
  • i customize when required
Re: Get attribute tagstring from block
« Reply #9 on: December 18, 2012, 11:16:09 AM »
Lee Mac:

I have tried your code (modified it to suit my block and atts)...I get nothing. I'm not gettting a dialogue / DCL popping up either.
Am I missing something?
Perception is a state of mind  -/ Twitter = @hochanz -/ Intel i7 - 3.07GHz -/ 12GB Ram -/ Nvidia Quadro FX 1800

Lee Mac

  • Seagull
  • Posts: 12926
  • London, England
Re: Get attribute tagstring from block
« Reply #10 on: December 18, 2012, 01:55:38 PM »
Lee Mac:

I have tried your code (modified it to suit my block and atts)...I get nothing. I'm not gettting a dialogue / DCL popping up either.
Am I missing something?

Which code are you using and how have you modified it?