Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Bounding Box of a Block definition

11 REPLIES 11
SOLVED
Reply
Message 1 of 12
ProfWolfMan
4169 Views, 11 Replies

Bounding Box of a Block definition

Hi all,

 

How i can calculate bounding box of a BLOCK definition ?

 

I know after inserting a block, we can get bounding box of a block reference.

But how can i get bounding box before insert?

 

assuming that i never going to scale or rotate it.

 

Kindly explore any of your thoughts related to this.

 

Thanks & Regards,

Ganesan M

Thanks & Regards,
G
Tags (1)
11 REPLIES 11
Message 2 of 12
_Tharwat
in reply to: ProfWolfMan

My version ..... Smiley Happy

 

This would prompt the bounding box first and second points to the command line ...

(defun c:test (/ name e bo) (vl-load-com)
  ;; Tharwat 09. Nov. 2011 ;;
  (if (and (setq name (getstring T "\n Enter name of Block :"))
           (tblsearch "BLOCK" name)
      )
    (progn
      (setq e (entmakex (list '(0 . "INSERT")
                              (cons 2 name)
                              (cons 10 '(0. 0. 0.))
                              '(41 . 1.)
                              '(42 . 1.)
                              '(43 . 1.)
                        )
              )
      )
      (setq bo (vla-getboundingbox (vlax-ename->vla-object e) 'a 'b))
      (princ (vlax-safearray->list a))
      (princ (vlax-safearray->list b))
      (entdel e)
    )
  )
  (princ)
)

 Tharwat

Message 3 of 12
ProfWolfMan
in reply to: _Tharwat

Hi Tharwat,

 

Thanks for your cute solution.

 

Though , my situation not allowed to insert that block which trigers some other program not in my control.

 


Thanks & Regards,
G
Message 4 of 12
devitg
in reply to: ProfWolfMan

Do the Block , belong to the DWG or it is a external DWG??
Message 5 of 12
Lee_Mac
in reply to: ProfWolfMan

See my method in this program.

Message 6 of 12
devitg
in reply to: Lee_Mac

Hi Lee , the OP state.

 

 

Though , my situation not allowed to insert that block which trigers some other program not in my control.

Message 7 of 12
Lee_Mac
in reply to: devitg

Did you look at my code devitg?

 

My function retrieves the BoundingBox of the Block Definition before the block is inserted, then transforms this BoundingBox using a matrix based on the scale, normal, rotation, and position of the inserted block.

 

Here is the function to retrieve the Bounding Box of the Block Definition, simplified for the purposes of the OP:

 

;; Block Definition BoundingBox  -  Lee Mac
;; Args:
;; blocks - Block Collection in which block resides
;; block  - Name of Block for which to return BoundingBox
;; Returns lower-left and upper-right corners of BoundingBox

(defun LM:BlockDefinitionBoundingBox ( blocks block / l1 l2 ll ur )
    (vlax-for obj (vla-item blocks block)
        (if (vlax-method-applicable-p obj 'getboundingbox)
            (if (not
                    (vl-catch-all-error-p
                        (vl-catch-all-apply 'vla-getboundingbox (list obj 'll 'ur))
                    )
                )
                (setq l1 (cons (vlax-safearray->list ll) l1)
                      l2 (cons (vlax-safearray->list ur) l2)
                )
            )
        )
    )
    (if l1
        (list
            (apply 'mapcar (cons 'min l1))
            (apply 'mapcar (cons 'max l2))
        )
    )
)

 

 

 

Message 8 of 12
Kent1Cooper
in reply to: ProfWolfMan


@ProfWolfMan wrote:

.... 

I know after inserting a block, we can get bounding box of a block reference.

But how can i get bounding box before insert?

 

assuming that i never going to scale or rotate it.

....


Another way, assuming a Block defined in the current drawing:

 

(defun BDBB (blkname / gbb ell eur ent); = Block Definition's Bounding Box
  (setq
    ent (tblobjname "block" blkname)
    LL nil ; eliminate prior, if any
    UR nil
  ); setq
  (while (setq ent (entnext ent))
    (vla-getboundingbox (vlax-ename->vla-object ent) 'mi 'ma)
    (setq
      ell (vlax-safearray->list mi); = Entity's Lower Left
      eur (vlax-safearray->list ma); = Entity's Upper Right
      LL (if LL (mapcar 'min LL ell) ell)
      UR (if UR (mapcar 'max UR eur) eur)
    ); setq
  ); while
); defun

 

Usage: (BDBB "ABlockName")

 

That leaves the Lower Left corner of the overall Block definition's bounding box in the LL variable, and the Upper Right corner in the UR variable.  That would be in relation to the Block's defined insertion point, and as if at scales of 1 and rotation of 0.

 

If you went on to do something with those corners within a larger routine, so you didn't need them retained, you could localize those variables, and not need to set them to nil in the middle there.

 

EDIT: shortened routine a little [incorporated a subroutine that really wasn't necessary to define separately]

EDIT2:  the above doesn't like Attributes -- if those are involved, try this:
 

(defun BDBB (blkname / gbb ell eur ent); = Block Definition's Bounding Box
  (setq
    ent (tblobjname "block" blkname)
    LL nil ; eliminate prior, if any
    UR nil
  ); setq
  (while (setq ent (entnext ent))
    (if (/= (cdr (assoc 0 (entget ent))) "ATTDEF"); they don't have extents
      (progn
        (vla-getboundingbox (vlax-ename->vla-object ent) 'mi 'ma)
        (setq
          ell (vlax-safearray->list mi); = Entity's Lower Left
          eur (vlax-safearray->list ma); = Entity's Upper Right
          LL (if LL (mapcar 'min LL ell) ell)
          UR (if UR (mapcar 'max UR eur) eur)
        ); setq
      ); progn
    ); if
  ); while
); defun

Kent Cooper, AIA
Message 9 of 12
Kent1Cooper
in reply to: Kent1Cooper


@Kent1Cooper wrote:
.... 

(defun BDBB (blkname / gbb ell eur ent); = Block Definition's Bounding Box
....


[You can dump the gbb in that localized variables list -- that was the subroutine I eliminated from the initial version.]

 

Also, it occurred to me to wonder whether it would handle nested Blocks correctly, which it seems to do, in limited testing, even if they're rotated or scaled peculiarly in relation to the parent Block.

Kent Cooper, AIA
Message 10 of 12
devitg
in reply to: Lee_Mac

Lee , pardon me, I put my fingers moving first , before my brain.
Message 11 of 12
ProfWolfMan
in reply to: devitg

Hi all,

 

Both solves my problem. It opens a new door in my current programming logic.

I did not test with nested blocks/attributes which is not needed on my current situation.

 

Thanks to Lee, Kent and devitg.

Thanks & Regards,
G
Message 12 of 12
Lee_Mac
in reply to: ProfWolfMan


devitg wrote:
Lee , pardon me, I put my fingers moving first , before my brain.
No worries devitg Smiley Wink

ProfWolfMan wrote:

Hi all,

 

Both solves my problem. It opens a new door in my current programming logic.

I did not test with nested blocks/attributes which is not needed on my current situation.

 

Thanks to Lee, Kent and devitg.


You're very welcome, happy to help! Smiley Happy

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost