Results 1 to 8 of 8

Thread: Getting Block Attributes/Values (for dummies)

  1. #1
    Design Visualization Moderator stusic's Avatar
    Join Date
    2004-10
    Location
    Denver, Colorado
    Posts
    1,515
    Login to Give a bone
    0

    Question Getting Block Attributes/Values (for dummies)

    Can someone throw me a bone and explain how to drill down and get to the attributes and attribute values of a block and/or blocks? I find a lot of examples, but there's not much consistency between them and they are generally a part of other functions, so it becomes hard to discern what's getting the values of the attributes and what's doing other things. I've even looked at Lee Mac's GetAttributeValues code snippets, but can't figure out enough of what's going on to apply that to multiple blocks.

    A little hand-holding and slow walking would be greatly appreciated.

    Thankyouhaveagreatday.

  2. #2
    All AUGI, all the time
    Join Date
    2010-06
    Posts
    962
    Login to Give a bone
    0

    Default Re: Getting Block Attributes/Values (for dummies)

    Hi .

    I don't think there is simpler than this one ..

    Code:
    (defun c:TesT (/ ss n e x)
      ;; Tharwat 21. Sep. 2012 ;;
      (while
        (setq ss (ssget "_+.:S" '((0 . "INSERT") (66 . 1))))
         (setq n (entnext (ssname ss 0)))
         (while (not (eq (cdr (assoc 0 (setq e (entget n)))) "SEQEND"))
           (if (not (eq (cdr (assoc 1 e)) nil))
             (print (cdr (assoc 1 e)))
           )
           (setq n (entnext n))
         )
      )
      (princ)
    )

  3. #3
    Administrator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    5,719
    Login to Give a bone
    0

    Default Re: Getting Block Attributes/Values (for dummies)

    (from your other thread)... What's confusing?

    Quote Originally Posted by RenderMan View Post
    Code:
    (defun c:FOO (/ *error* adoResults ss pageNo data)
    
      (defun *error* (msg)
        (if (and ss (= 'VLA-OBJECT (type ss)))
          (vla-delete ss)
        )
        (if acDoc
          (vla-endundomark acDoc)
        )
        (cond ((not msg))                                                   ; Normal exit
              ((member msg '("Function cancelled" "quit / exit abort")))    ; <esc> or (quit)
              ((princ (strcat "\n** Error: " msg " ** ")))                  ; Fatal error, display it
        )
        (princ)
      )
    
      ;; Represents the dataset for the ADOLisp results for each pageNo
      (setq adoResults '((1 . "6") (2 . "10") (3 . "5") (4 . "7")))
    
      ;; Main code
      (if (setq
            ss (ssget "_x" '((0 . "INSERT") (2 . "TITLE_BLOCK") (66 . 1)))
          )
        (progn
    
          ;; Get page_no attribute values from blocks
          (vlax-for x (setq ss (setq acDoc
                                      (vla-get-activeselectionset
                                        (vla-get-activedocument
                                          (vlax-get-acad-object)
                                        )
                                      )
                               )
                      )
            (foreach att (vlax-invoke x 'getattributes)
              (if (= "PAGE_NO" (vla-get-tagstring att))
                (progn
                  (setq pageNo (atoi (vla-get-textstring att)))
                  (setq data (cons (cons x pageNo) data))
                )
              )
            )
          )
    
          ;; Put revision attribute values into blocks
          (foreach item data
            (foreach att (vlax-invoke (car item) 'getattributes)
              (if (= "REV" (vla-get-tagstring att))
                (vla-put-textstring
                  att
                  
                  ;; This is a pseudo ADO query
                  (cdr (assoc (cdr item) adoResults))
                )
              )
            )
          )
          (*error* nil)
        )
      )
    )
    "How we think determines what we do, and what we do determines what we get."

    Sincpac C3D ~ Autodesk Exchange Apps

    Computer Specs:
    Dell Precision 3660, Core i9-12900K 5.2GHz, 64GB DDR5 RAM, PCIe 4.0 M.2 SSD (RAID 0), 16GB NVIDIA RTX A4000

  4. #4
    Design Visualization Moderator stusic's Avatar
    Join Date
    2004-10
    Location
    Denver, Colorado
    Posts
    1,515
    Login to Give a bone
    0

    Default Re: Getting Block Attributes/Values (for dummies)

    Well, in this case your code fits with what I'm trying to do, and Tharwat's code is simple. I'm just trying to wrap my head around it. The differences I see in the two examples are:

    1. Tharwat's code is LISP, while Renderman's is VLISP
    2. Tharwat's gets all attributes, while Renderman gets a specific value?
    3. Tharwat's list is held in the variable "e"? Renderman's list is held in variable "data"? With the pairs in variable "pageNo"?


    This seems like this is the heart of Renderman's code after I grab the selection set. Is that right?

    Code:
              (if (= "PAGE_NO" (vla-get-tagstring att))
                (progn
                  (setq pageNo (atoi (vla-get-textstring att)))
                  (setq data (cons (cons x pageNo) data))
                )
              )
    I'm having a hard time wrappign my brain around Tharwat's code. I *kinda* see what's going on, like the (while (not "SEQEND")), but I'm not getting the "(assoc 0 (e)), (assoc 1 e))".

    I don't have my book and, although the online literature is available, it's rather dry and without compassion to my feelings.

    Code:
    (setq n (entnext (ssname ss 0)))
         (while (not (eq (cdr (assoc 0 (setq e (entget n)))) "SEQEND"))
           (if (not (eq (cdr (assoc 1 e)) nil))
             (print (cdr (assoc 1 e)))
           )
           (setq n (entnext n))
    Sorry guys, I feel like I'm trying to count stars on a roller coaster.

  5. #5
    Administrator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    5,719
    Login to Give a bone
    0

    Default Re: Getting Block Attributes/Values (for dummies)

    I understand the confusion; not so long ago, I knew nothing of programming at all.

    I haven't been around much this week (it's good to be busy!), but I'll try to post back soon, as clearly as I can to try and help... Later!
    "How we think determines what we do, and what we do determines what we get."

    Sincpac C3D ~ Autodesk Exchange Apps

    Computer Specs:
    Dell Precision 3660, Core i9-12900K 5.2GHz, 64GB DDR5 RAM, PCIe 4.0 M.2 SSD (RAID 0), 16GB NVIDIA RTX A4000

  6. #6
    Design Visualization Moderator stusic's Avatar
    Join Date
    2004-10
    Location
    Denver, Colorado
    Posts
    1,515
    Login to Give a bone
    0

    Default Re: Getting Block Attributes/Values (for dummies)

    I am doing something horribly wrong. If I snip out all of the (what I believe to be unneeded) lines, I get the wrong list. I want it to print out the list of values to verify I'm actually getting the right values, but I get results like:

    Command: foo
    ((#<VLA-OBJECT IAcadBlockReference2 28de439c> . 3) (#<VLA-OBJECT
    IAcadBlockReference2 28de4324> . 3) (#<VLA-OBJECT IAcadBlockReference2
    28de42ac> . 5) (#<VLA-OBJECT IAcadBlockReference2 28de4234> . 96) (#<VLA-OBJECT
    IAcadBlockReference2 28de41bc> . 7) (#<VLA-OBJECT IAcadBlockReference2
    28de4144> . 3) (#<VLA-OBJECT IAcadBlockReference2 28de40cc> . 3) (#<VLA-OBJECT
    IAcadBlockReference2 28de4054> . 3)
    ...
    ...
    ...
    (#<VLA-OBJECT IAcadBlockReference2 17b7c80c> . 3) (#<VLA-OBJECT
    IAcadBlockReference2 17b7c884> . 3) (#<VLA-OBJECT IAcadBlockReference2
    17b7c8fc> . 4) (#<VLA-OBJECT IAcadBlockReference2 17b7c974> . 4) (#<VLA-OBJECT
    IAcadBlockReference2 17b7c9ec> . 4) (#<VLA-OBJECT IAcadBlockReference2
    17b7ca64> . 4) (#<VLA-OBJECT IAcadBlockReference2 17b7cadc> . 4) (#<VLA-OBJECT
    IAcadBlockReference2 17b7cb54> . 4) (#<VLA-OBJECT IAcadBlockReference2
    17b7cbcc> . 3))nil

    Here's the snipped code:
    Code:
    (vl-load-com)
    
    (defun c:FOO (/ blk ss pageNo data)
    
    (setq blk "*Balloon*")
      
      ;; Main code
      (if (setq
            ss
    	 (ssget "_A"
                    (list
    		   '(0 . "INSERT")
                       '(66 . 1)
    		    (cons 2 (strcat "`*U*," blk))
                        (cons 410 (getvar 'CTAB))
                    )
                )
          )
        (progn
    
          ;; Get page_no attribute values from blocks
          (vlax-for x (setq ss (setq acDoc
                                      (vla-get-activeselectionset
                                        (vla-get-activedocument
                                          (vlax-get-acad-object)
                                        )
                                      )
                               )
                      )
            (foreach att (vlax-invoke x 'getattributes)
              (if (= "PART_NUM" (vla-get-tagstring att))
                (progn
                  (setq pageNo (atoi (vla-get-textstring att)))
                  (setq data (cons (cons x pageNo) data))
                )
              )
            )
          )
    
          ;; Don't need to put the attributes, so I replace the "foreach" statement and just instruct to print the list made above
          
          (princ (vl-princ-to-string data))
          
          (*error* nil)
        )
      )
    )

  7. #7
    Design Visualization Moderator stusic's Avatar
    Join Date
    2004-10
    Location
    Denver, Colorado
    Posts
    1,515
    Login to Give a bone
    0

    Default Re: Getting Block Attributes/Values (for dummies)

    Is it because it's a DB?

  8. #8
    Administrator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    5,719
    Login to Give a bone
    0

    Default Re: Getting Block Attributes/Values (for dummies)

    Quote Originally Posted by stusic View Post
    I am doing something horribly wrong. If I snip out all of the (what I believe to be unneeded) lines, I get the wrong list. I want it to print out the list of values to verify I'm actually getting the right values, but I get results like:

    Code:
    Command: foo
    ((#<VLA-OBJECT IAcadBlockReference2 28de439c> . 3) (#<VLA-OBJECT 
    IAcadBlockReference2 28de4324> . 3) (#<VLA-OBJECT IAcadBlockReference2 
    28de42ac> . 5) (#<VLA-OBJECT IAcadBlockReference2 28de4234> . 96) (#<VLA-OBJECT 
    IAcadBlockReference2 28de41bc> . 7) (#<VLA-OBJECT IAcadBlockReference2 
    28de4144> . 3) (#<VLA-OBJECT IAcadBlockReference2 28de40cc> . 3) (#<VLA-OBJECT 
    IAcadBlockReference2 28de4054> . 3)
    
    <snip>
    To explain the output you received, see the 'item' description noted in this adaptation of my earlier offering:

    Code:
    (defun c:FOO (/ *error* adoResults ss pageNo data)
    
      (defun *error* (msg)
        (if (and ss (= 'VLA-OBJECT (type ss)))
          (vla-delete ss)
        )
        (if acDoc
          (vla-endundomark acDoc)
        )
        (cond ((not msg))                                                   ; Normal exit
              ((member msg '("Function cancelled" "quit / exit abort")))    ; <esc> or (quit)
              ((princ (strcat "\n** Error: " msg " ** ")))                  ; Fatal error, display it
        )
        (princ)
      )
    
      ;; Represents the dataset for the ADOLisp results for each pageNo
      (setq adoResults '((1 . "6") (2 . "10") (3 . "5") (4 . "7")))
    
      ;; Main code
      (if (setq
            ss (ssget "_x" '((0 . "INSERT") (2 . "TITLE_BLOCK") (66 . 1)))
          )
        (progn
    
          ;; Get page_no attribute values from blocks
          (vlax-for x (setq ss (setq acDoc
                                      (vla-get-activeselectionset
                                        (vla-get-activedocument
                                          (vlax-get-acad-object)
                                        )
                                      )
                               )
                      )
            (foreach att (vlax-invoke x 'getattributes)
              (if (= "PAGE_NO" (vla-get-tagstring att))
                (progn
                  (setq pageNo (atoi (vla-get-textstring att)))
                  (setq data (cons (cons x pageNo) data))
                )
              )
            )
          )
    
    ;;;      ;; Put revision attribute values into blocks
    ;;;      (foreach item data
    ;;;        (foreach att (vlax-invoke (car item) 'getattributes)
    ;;;          (if (= "REV" (vla-get-tagstring att))
    ;;;            (vla-put-textstring
    ;;;              att
    ;;;              
    ;;;              ;; This is a pseudo ADO query
    ;;;              (cdr (assoc (cdr item) adoResults))
    ;;;            )
    ;;;          )
    ;;;        )
    ;;;      )
    
          ;; for each item in data (a list of grouped pairs,
          ;; where the first element is the attribute object,
          ;; and the second element is the attribute object's
          ;; textstring property value converted to an integer
          (foreach item data
    
            ;; print 
            (print
    
              ;; the second element of the association list returned
              ;; by a query against the ADO by searching for the second
              ;; element of the item's grouped pair (an integer)
              (cdr (assoc (cdr item) adoResults))
              )
          )
          (*error* nil)
        )
      )
    )
    "How we think determines what we do, and what we do determines what we get."

    Sincpac C3D ~ Autodesk Exchange Apps

    Computer Specs:
    Dell Precision 3660, Core i9-12900K 5.2GHz, 64GB DDR5 RAM, PCIe 4.0 M.2 SSD (RAID 0), 16GB NVIDIA RTX A4000

Similar Threads

  1. Dynamic block with attributes that display X, Y, and Z values
    By sammyz28 in forum Dynamic Blocks - Technical
    Replies: 25
    Last Post: 2015-12-03, 05:37 AM
  2. Block Count via attributes values
    By cadconcepts in forum AutoLISP
    Replies: 21
    Last Post: 2015-02-10, 06:35 PM
  3. Replies: 21
    Last Post: 2011-11-16, 04:38 PM
  4. Assign Attributes & Block to Values
    By asma.waqar in forum AutoCAD General
    Replies: 0
    Last Post: 2011-02-15, 06:21 PM
  5. Extract Dynamic Block Attributes, values change as Block changes
    By dave.buckberry in forum Dynamic Blocks - Technical
    Replies: 11
    Last Post: 2006-09-05, 04:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •