Report the number of attributes for each block

Report the number of attributes for each block

Anonymous
Not applicable
870 Views
4 Replies
Message 1 of 5

Report the number of attributes for each block

Anonymous
Not applicable

Hi all,

 

Please refer to me to the right topic if this question has been asked before. At least I didn't find anything.

 

I am trying to write the name and number of attributes of each block in my current drawing in a *.txt file (don't care about nested blocks).

 

I have come this far (for the counting part of task) but I am stuck at this stage:

 

(defun test1 (VlaObj TagNames TagValues / AttObj TagNam TagVal OutList)
  (if
    (and
      (= 'VLA-OBJECT (type VlaObj))
      (= (vla-get-hasattributes VlaObj) :vlax-true)
    )
    (foreach ObjFor (vlax-invoke VlaObj 'GetAttributes)
      (if   
        (and
          (wcmatch (setq TagNam (vla-get-TagString  ObjFor))           (strcase TagNames))
          (wcmatch (strcase (setq TagVal (vla-get-TextString ObjFor))) (strcase TagValues))
        )
        (setq OutList (cons (list TagNam TagVal (vla-get-ObjectID ObjFor) ObjFor) OutList))
      )
    )
  )
  OutList
)

I can test singular block with:

 

(length (test1 (vlax-ename->vla-object (car (entsel))) "*" "*"))

But I can't get it to go through the loop and count the number of attributes for all of the blocks.

 

I guess reporting the results into a *.txt file shouldn't be a problem (hopefully Smiley Tongue)

0 Likes
871 Views
4 Replies
Replies (4)
Message 2 of 5

dbroad
Mentor
Mentor

It looks like you are trying to do more than just count attributes in the function.  It looks like you are filtering for specific tagnames and tagvalues.  If that is what you want to do, it might help if you provide some sample calls to test1 including the tagnames and tagvalues lists.

 

That said, if you just want a text file of block names and attributes in a text file, there are built-in commands to do that:  dataextraction and attext.

Architect, Registered NC, VA, SC, & GA.
0 Likes
Message 3 of 5

Anonymous
Not applicable

Thanks for the reply and apologies for my late response. I wasn't aware of 'dataextraction' command in AutoCAD. IT's great for what I need to achieve. The only problem I have is I can't ask it to filiter the blocks with certain number of attributes. Hence it reads all the blocks with atributes. Having many blocks with one, two or three attributes makes my machine to 'freeze' when AutoCAD tries to display the extracted information.

 

Any solution for that?

 

 

AttEXT is no good because I don't know the name of the blocks in the drawing pile.

0 Likes
Message 4 of 5

Anonymous
Not applicable

This code ignores anonymous blocknames. If a block has visibilty state, this code only counts attributes at its default state. If you will use a dynamic block, with a couple of changes, it can be done.

 

 

(princ
  (strcat
    "\n..:: ATTNUM.lsp :: \\U+00A9 Zeynelabidin BOSTAN :: 2017 :: www.202mimarlik.com.tr ::..";
    "\n::                                                                              ::"       ;
    "\n:: ATTNUM: Gives the blocknames and number of attributes in a block             ::"       ;
    "\n::                                                                              ::"       ;
    "\n::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::"       ;
    )
  )



(defun c:attnum ()
  (setq bllist nil)
  (setq secim (ssget "_:L" '((0 . "INSERT"))))
  (foreach objevla (mapcar 'vlax-ename->vla-object(vl-remove-if 'listp (mapcar 'cadr (ssnamex secim)))) ;_ end of mapcar
    (setq blockname (vla-get-effectivename objevla))
    (if (= (member blockname bllist) nil)
      (progn
        (setq bllist (cons blockname bllist))
        (setq attribute (LM:vl-getattributes objevla))
        (setq attlistblk (mapcar '(lambda (x) (car x)) attribute))
        (setq len (length attlistblk))
        (princ (strcat "\n" blockname " has " (itoa len) " attribute(s)"))
        )
      )
    )
  (princ)
  )


;; Get Attributes  -  Lee Mac
;; Returns an association list of attributes present in the supplied block.
;; blk - [vla] VLA Block Reference Object
;; Returns: [lst] Association list of ((<Tag> . <Value>) ... )

(defun LM:vl-getattributes ( blk )
    (mapcar '(lambda ( att ) (cons (vla-get-tagstring att) (vla-get-textstring att)))
        (vlax-invoke blk 'getattributes)
    )
)
    
    
    
0 Likes
Message 5 of 5

Anonymous
Not applicable

Exporting to a file can be like that

 

(princ
  (strcat
    "\n..:: ATTNUM.lsp :: \\U+00A9 Zeynelabidin BOSTAN :: 2017 :: www.202mimarlik.com.tr ::..";
    "\n::                                                                              ::"       ;
    "\n:: ATTNUM: Gives the blocknames and number of attributes in a block             ::"       ;
    "\n::                                                                              ::"       ;
    "\n::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::"       ;
    )
  )



(defun c:attnum ()
  (setq bllist nil)
  (setq file (getfiled "Select or give a name to export file" "" "txt" 1))
  (setq file(open file "w"))
  (setq secim (ssget "_:L" '((0 . "INSERT"))))
  (foreach objevla (mapcar 'vlax-ename->vla-object(vl-remove-if 'listp (mapcar 'cadr (ssnamex secim)))) ;_ end of mapcar
    (setq blockname (vla-get-effectivename objevla))
    (if (= (member blockname bllist) nil)
      (progn
        (setq bllist (cons blockname bllist))
        (setq attribute (LM:vl-getattributes objevla))
        (setq attlistblk (mapcar '(lambda (x) (car x)) attribute))
        (setq len (length attlistblk))
        (write-line (strcat "\n" blockname " has " (itoa len) " attribute(s)") file)
        )
      )
    )
  (close file)
  (princ)
  )


;; Get Attributes  -  Lee Mac
;; Returns an association list of attributes present in the supplied block.
;; blk - [vla] VLA Block Reference Object
;; Returns: [lst] Association list of ((<Tag> . <Value>) ... )

(defun LM:vl-getattributes ( blk )
    (mapcar '(lambda ( att ) (cons (vla-get-tagstring att) (vla-get-textstring att)))
        (vlax-invoke blk 'getattributes)
    )
)
0 Likes