Visual LISP, AutoLISP and General Customization

Reply
Valued Contributor
ProfWolfMan
Posts: 86
Registered: ‎04-21-2010
Message 1 of 12 (746 Views)
Accepted Solution

Bounding Box of a Block definition

746 Views, 11 Replies
11-08-2011 06:21 PM

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
Distinguished Mentor
_Tharwat
Posts: 574
Registered: ‎07-02-2010
Message 2 of 12 (734 Views)

Re: Bounding Box of a Block definition

11-08-2011 11:32 PM in reply to: ProfWolfMan

My version ..... :smileyhappy:

 

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

____________________________________________________
Get learn and learn and practice , to become experienced guy .
Valued Contributor
ProfWolfMan
Posts: 86
Registered: ‎04-21-2010
Message 3 of 12 (727 Views)

Re: Bounding Box of a Block definition

11-09-2011 01:02 AM 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
Mentor
devitg
Posts: 1,692
Registered: ‎03-14-2004
Message 4 of 12 (714 Views)

Re: Bounding Box of a Block definition

11-09-2011 04:11 AM in reply to: ProfWolfMan
Do the Block , belong to the DWG or it is a external DWG??
*Expert Elite*
Lee_Mac
Posts: 1,108
Registered: ‎12-29-2009
Message 5 of 12 (710 Views)

Re: Bounding Box of a Block definition

11-09-2011 04:25 AM in reply to: ProfWolfMan

See my method in this program.

Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)
Expert Elite
With Mathematics there is the possibility of perfect rigour, so why settle for less?
Mentor
devitg
Posts: 1,692
Registered: ‎03-14-2004
Message 6 of 12 (704 Views)

Re: Bounding Box of a Block definition

11-09-2011 04:51 AM 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.

*Expert Elite*
Lee_Mac
Posts: 1,108
Registered: ‎12-29-2009
Message 7 of 12 (699 Views)

Re: Bounding Box of a Block definition

11-09-2011 05:06 AM 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))
        )
    )
)

 

 

 

Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)
Expert Elite
With Mathematics there is the possibility of perfect rigour, so why settle for less?
*Expert Elite*
Kent1Cooper
Posts: 5,242
Registered: ‎09-13-2004
Message 8 of 12 (692 Views)

Re: Bounding Box of a Block definition

11-09-2011 05:44 AM 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
*Expert Elite*
Kent1Cooper
Posts: 5,242
Registered: ‎09-13-2004
Message 9 of 12 (675 Views)

Re: Bounding Box of a Block definition

11-09-2011 07:24 AM 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
Mentor
devitg
Posts: 1,692
Registered: ‎03-14-2004
Message 10 of 12 (672 Views)

Re: Bounding Box of a Block definition

11-09-2011 07:32 AM in reply to: Lee_Mac
Lee , pardon me, I put my fingers moving first , before my brain.

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community