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
Solved! Go to Solution.
Solved by Kent1Cooper. Go to Solution.
Solved by Lee_Mac. Go to Solution.
My version .....
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
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.
Hi Lee , the OP state.
Though , my situation not allowed to insert that block which trigers some other program not in my control.
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)) ) ) )
@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
@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.
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.
No worries devitg
devitg wrote:
Lee , pardon me, I put my fingers moving first , before my brain.
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!
Can't find what you're looking for? Ask the community or share your knowledge.