Author Topic: Get block insertions from objectdbx doc w/o iterating all objects  (Read 2415 times)

0 Members and 1 Guest are viewing this topic.

mkweaver

  • Bull Frog
  • Posts: 352
This is currently working, I'm just looking for suggestions for improvments?
Code: [Select]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;  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, thus: ;;;
;;;-----------------------------------------------------------------------------;;;
;;;(("tes1" ;insertions of tes1 ;;;
;;;   #<VLA-OBJECT IAcadBlockReference 0f306ad4> ;;;
;;;   #<VLA-OBJECT IAcadBlockReference 242f08e4> ;;;
;;;   #<VLA-OBJECT IAcadBlockReference 242ede74> ;;;
;;;   #<VLA-OBJECT IAcadBlockReference 242f0724> ;;;
;;;   #<VLA-OBJECT IAcadBlockReference 242edee4> ;;;
;;;   #<VLA-OBJECT IAcadBlockReference 242efb54> ;;;
;;;  ) ;;;
;;;  ("tes2") ;there are no insertions of tes2;;;
;;;  ("tes3" . -1) ;tes3 doesn't exist ;;;
;;;  ("LOGO" #<VLA-OBJECT IAcadExternalReference 0e83b194>);insertions of logo ;;;
;;;  ("BRD_TTL" #<VLA-OBJECT IAcadBlockReference 242edb64>);insertions of brd_ttl;;
;;;) ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun GetBlockInserts(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
(cond
  ;;the block doesn't exist
  ((vl-catch-all-error-p
     (setq objBlockDef (vl-catch-all-apply 'vla-item (list blocks bname)))
     )
   -1
   )
  (T
   (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
    )
  )

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Get block insertions from objectdbx doc w/o iterating all objects
« Reply #1 on: April 01, 2009, 01:54:14 PM »
Maybe just these quick little changes.  No need to use ' cond ' since you only test two ideas.  And if block doesn't exist within the drawing, return it the same way as the rest; ie ("test3" -1) instead of ("test3" . -1).  Change the formatting a little also.

Code: [Select]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;  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, thus: ;;;
;;;-----------------------------------------------------------------------------;;;
;;;(("tes1" ;insertions of tes1 ;;;
;;;   #<VLA-OBJECT IAcadBlockReference 0f306ad4> ;;;
;;;   #<VLA-OBJECT IAcadBlockReference 242f08e4> ;;;
;;;   #<VLA-OBJECT IAcadBlockReference 242ede74> ;;;
;;;   #<VLA-OBJECT IAcadBlockReference 242f0724> ;;;
;;;   #<VLA-OBJECT IAcadBlockReference 242edee4> ;;;
;;;   #<VLA-OBJECT IAcadBlockReference 242efb54> ;;;
;;;  ) ;;;
;;;  ("tes2") ;there are no insertions of tes2;;;
;;;  ("tes3" -1) ;tes3 doesn't exist ;;;
;;;  ("LOGO" #<VLA-OBJECT IAcadExternalReference 0e83b194>);insertions of logo ;;;
;;;  ("BRD_TTL" #<VLA-OBJECT IAcadBlockReference 242edb64>);insertions of brd_ttl;;
;;;) ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun GetBlockInserts(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
    )
  )
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

mkweaver

  • Bull Frog
  • Posts: 352
Re: Get block insertions from objectdbx doc w/o iterating all objects
« Reply #2 on: April 01, 2009, 02:49:30 PM »
Good suggestions.

Thanks