I hate doing things I can make my computer do for me. My adventures in customizing and automating computers. Primarily with AutoCAD and Microsoft Office.

Wednesday, April 01, 2009

Get block insertions with objectdbx

Routine: GetBlockInserts  
Purpose:   Get a list of all insertions of specified blocks.
Arguments: doc vla-document object (or objectdbx document). If nil, then the active document is used.           
  bnames a list of strings, the block names for the subject blocks.
  ReturnEnames boolean, non-nil returns entity names, nil returns vla-objects.
Returns:   A list of all the insertions of the subject blocks. -1 if the block is not defined. Nil if the block is defined but has no insertions.

Calling GetBlockInserts thus:

(getblockinserts2 nil '("test1" "test2" "test3") nil)

Might return something that looks like this:

(("test1"
   #<VLA-OBJECT IAcadBlockReference 0b4b55c4>       ;These may be enames or vla-objects depending on ReturnEnames
   #<VLA-OBJECT IAcadBlockReference 0b4b52f4>
   #<VLA-OBJECT IAcadBlockReference 0b4b56b4>
   #<VLA-OBJECT IAcadBlockReference 0b4b563c>
   #<VLA-OBJECT IAcadBlockReference 0b4b518c>
   #<VLA-OBJECT IAcadBlockReference 0b4b554c>
   #<VLA-OBJECT IAcadBlockReference 0b4b54d4>
  )
  ("test2"
   #<VLA-OBJECT IAcadBlockReference 0b4b57a4>
   #<VLA-OBJECT IAcadBlockReference 0b4b53e4>
   #<VLA-OBJECT IAcadBlockReference 0b4b536c>
   #<VLA-OBJECT IAcadBlockReference 0b4b545c>
  )
  ("test3"
    #<VLA-OBJECT IAcadBlockReference 0b4b527c>
    #<VLA-OBJECT IAcadBlockReference 0b4b5204>
    #<VLA-OBJECT IAcadBlockReference 0b4b4dcc>
    #<VLA-OBJECT IAcadBlockReference 0b4b4fac>
    #<VLA-OBJECT IAcadBlockReference 0b4b509c>
   )
("test4")                                           ;There are no insertions of this block
("test5" . –1)                                      ;This block is not defined
)

Note that (cdr(assoc “test4” results)) will return nil.

 

(defun GetBlockInserts2    (doc bnames ReturnEnames / objBlockDef InsList Blocks)
  (
setq    doc    (if doc
         doc
        
(vla-get-activedocument (vlax-get-acad-object))
           )
   
blocks (vla-get-blocks doc)
  )
  (
mapcar (function (lambda (bname)
              (
setq inslist nil)
              (
cons bname
               
(if    (vl-catch-all-error-p
                 
(setq    objBlockDef
                    
(vl-catch-all-apply 'vla-item (list blocks bname))
                  )
                )
                 
-1    ; Block doesn't exist
                  (setq InsList (vl-remove-if-not
                     
(function
                       
(lambda (x) (and (= 331 (car x)) (entget (cdr x))))
                          )
                          (
member '(102 . "{BLKREFS")
                              (
entget (vlax-vla-object->ename objBlockDef))
                          )
                        )
                   
inslist (if    ReturnEnames
                         
(mapcar (function (lambda (x) (cdr x))) inslist)
                          (
mapcar
                       
(function (lambda (x) (vlax-ename->vla-object (cdr x)))
                        )
                       
inslist
                         
)
                        )
                  )
                )
              )
            )
      )
     
bnames
 

)
)

Modified 4/24/2009

No comments: