Author Topic: LM:blockreferenceboundingbox  (Read 5779 times)

0 Members and 1 Guest are viewing this topic.

domenicomaria

  • Swamp Rat
  • Posts: 724
LM:blockreferenceboundingbox
« on: February 26, 2021, 05:05:10 PM »
I am using the
(for me impossible to understand)
LM:blockreferenceboundingbox
routine of the great LEE MAC.

But
if the INSERT is ROTATED
it returns the YELLOW bounding box . . .
. . .
Is there a way
to obtain the GREEN RESULT ?

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: LM:blockreferenceboundingbox
« Reply #1 on: February 26, 2021, 06:53:23 PM »
You can do it with the following steps:

1- Get the current rotation value of the selected block.
2- Set the rotation to 0.0
3- Get the Bounding box of the block.
4- Reset the rotation value back.

domenicomaria

  • Swamp Rat
  • Posts: 724
Re: LM:blockreferenceboundingbox
« Reply #2 on: February 27, 2021, 01:05:01 AM »
2- Set the rotation to 0.0
3- Get the Bounding box of the block.
. . .
if I do so,
I will get the yellow rectangle shown in the image on the right side,
which has nothing to do
with the green rectangle of the image on the left side . . .

I will get exactly what is shown on the right side !

domenicomaria

  • Swamp Rat
  • Posts: 724
Re: LM:blockreferenceboundingbox
« Reply #3 on: February 27, 2021, 01:15:43 AM »
The LEFT YELLOW rectangle,
is the SAME RIGTH YELLOW rectangle

the ONLY DIFFERENCE is that it is ROTATED !

While the GREEN LEFT rectangle,
is the TRUE BOUNDING BOX of the ROTATED BLOCK REFERENCE.


Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: LM:blockreferenceboundingbox
« Reply #4 on: February 27, 2021, 03:57:55 AM »
What's the result with this?
Code: [Select]
(defun ALE_Utl_GetBoundingBox (VlaObj / LowLft UppRgt)
  (vl-catch-all-apply
    'vla-GetBoundingBox (list VlaObj 'LowLft 'UppRgt)
  )
  (if LowLft
    (mapcar 'vlax-safearray->list (list LowLft UppRgt))
  )
)

domenicomaria

  • Swamp Rat
  • Posts: 724
Re: LM:blockreferenceboundingbox
« Reply #5 on: February 27, 2021, 08:19:19 AM »
Marco,
don't you think that maybe there is good reason
to explain the difference between
your code
and LM:blockreferenceboundingbox of Lee Mac ?

I suppose that the Lee Mac code, works also for rotated and not uniformly scaled inserts . . .

Code - Auto/Visual Lisp: [Select]
  1. ;; Block Reference Bounding Box  -  Lee Mac
  2. ;; Returns a WCS point list describing a rectangular frame bounding all geometry of a supplied block reference.
  3. ;; Excludes Text, MText & Attribute Definitions.
  4. ;; ref - [vla] Block Reference Object
  5.  
  6. (defun LM:blockreferenceboundingbox ( ref )
  7.     (
  8.         (lambda ( lst )
  9.             (apply
  10.                 (function
  11.                     (lambda ( m v )
  12.                         (mapcar (function (lambda ( p ) (mapcar '+ (mxv m p) v))) lst)
  13.                     )
  14.                 )
  15.                 (refgeom (vlax-vla-object->ename ref))
  16.             )
  17.         )
  18.         (LM:blockdefinitionboundingbox
  19.             (vla-item
  20.                 (vla-get-blocks (vla-get-document ref))
  21.                 (vla-get-name ref)
  22.             )
  23.         )
  24.     )
  25. )
  26.  
  27. ;; Block Definition Bounding Box  -  Lee Mac
  28. ;; Returns a WCS point list describing a rectangular frame bounding all geometry of a supplied block definition.
  29. ;; Excludes Text, MText & Attribute Definitions.
  30. ;; def - [vla] Block Definition Object
  31.  
  32. (defun LM:blockdefinitionboundingbox ( def / llp lst urp )
  33.     (vlax-for obj def
  34.         (cond
  35.             (   (= :vlax-false (vla-get-visible obj)))
  36.             (   (= "AcDbBlockReference" (vla-get-objectname obj))
  37.                 (setq lst (append lst (LM:blockreferenceboundingbox obj)))
  38.             )
  39.             (   (and (not (wcmatch (vla-get-objectname obj) "AcDbAttributeDefinition,AcDb*Text"))
  40.                      (vlax-method-applicable-p obj 'getboundingbox)
  41.                      (not (vl-catch-all-error-p (vl-catch-all-apply 'vla-getboundingbox (list obj 'llp 'urp))))
  42.                 )
  43.                 (setq lst (vl-list* (vlax-safearray->list llp) (vlax-safearray->list urp) lst))
  44.             )
  45.         )
  46.     )
  47.     (LM:points->boundingbox lst)
  48. )
  49.  
  50. ;; Point to Bounding Box  -  Lee Mac
  51. ;; Returns the rectangular extents of a supplied point list
  52.  
  53. (defun LM:points->boundingbox ( lst )
  54.     (   (lambda ( l )
  55.             (mapcar '(lambda ( a ) (mapcar '(lambda ( b ) ((eval b) l)) a))
  56.                '(
  57.                     (caar   cadar)
  58.                     (caadr  cadar)
  59.                     (caadr cadadr)
  60.                     (caar  cadadr)
  61.                 )
  62.             )
  63.         )
  64.         (mapcar '(lambda ( f ) (apply 'mapcar (cons f lst))) '(min max))
  65.     )
  66. )
  67.  
  68. ;; RefGeom (gile)
  69. ;; Returns a list which first item is a 3x3 transformation matrix (rotation, scales, normal)
  70. ;; and second item the object insertion point in its parent (xref, block or space)
  71. ;; Argument : an ename
  72.  
  73. (defun refgeom ( ent / ang ang mat ocs )
  74.     (setq enx (entget ent)
  75.           ang (cdr (assoc 050 enx))
  76.           ocs (cdr (assoc 210 enx))
  77.     )
  78.     (list
  79.         (setq mat
  80.             (mxm
  81.                 (mapcar '(lambda ( v ) (trans v 0 ocs t))
  82.                    '(
  83.                         (1.0 0.0 0.0)
  84.                         (0.0 1.0 0.0)
  85.                         (0.0 0.0 1.0)
  86.                     )
  87.                 )
  88.                 (mxm
  89.                     (list
  90.                         (list (cos ang) (- (sin ang)) 0.0)
  91.                         (list (sin ang) (cos ang)     0.0)
  92.                        '(0.0 0.0 1.0)
  93.                     )
  94.                     (list
  95.                         (list (cdr (assoc 41 enx)) 0.0 0.0)
  96.                         (list 0.0 (cdr (assoc 42 enx)) 0.0)
  97.                         (list 0.0 0.0 (cdr (assoc 43 enx)))
  98.                     )
  99.                 )
  100.             )
  101.         )
  102.         (mapcar '- (trans (cdr (assoc 10 enx)) ocs 0)
  103.             (mxv mat (cdr (assoc 10 (tblsearch "block" (cdr (assoc 2 enx))))))
  104.         )
  105.     )
  106. )
  107.  
  108. ;; Matrix x Vector - Vladimir Nesterovsky
  109. ;; Args: m - nxn matrix, v - vector in R^n
  110.  
  111. (defun mxv ( m v )
  112.     (mapcar '(lambda ( r ) (apply '+ (mapcar '* r v))) m)
  113. )
  114.  
  115. ;; Matrix Transpose - Doug Wilson
  116. ;; Args: m - nxn matrix
  117.  
  118. (defun trp ( m )
  119.     (apply 'mapcar (cons 'list m))
  120. )
  121.  
  122. ;; Matrix x Matrix - Vladimir Nesterovsky
  123. ;; Args: m,n - nxn matrices
  124.  
  125. (defun mxm ( m n )
  126.     ((lambda ( a ) (mapcar '(lambda ( r ) (mxv a r)) m)) (trp n))
  127. )

domenicomaria

  • Swamp Rat
  • Posts: 724
Re: LM:blockreferenceboundingbox
« Reply #6 on: February 27, 2021, 08:26:46 AM »
Marc'Antonio, your code produces the RED RECTANGLE . . .
 . . .
It is NOT SO EASY as it could seem
« Last Edit: March 01, 2021, 03:42:52 AM by domenicomaria »

domenicomaria

  • Swamp Rat
  • Posts: 724
Re: LM:blockreferenceboundingbox
« Reply #7 on: February 27, 2021, 08:32:39 AM »
while, if i EXPLODE the INSERT,

easily I get the RIGHT BOUNDING BOX (the GREEN RECTANGLE) !

domenicomaria

  • Swamp Rat
  • Posts: 724
Re: LM:blockreferenceboundingbox
« Reply #8 on: February 27, 2021, 11:00:25 AM »
Re: LM:blockreferenceboundingbox works
obviously (but this does't mean it is easy !)
also with NOT UNIFORMLY SCALED insert . . .
. . .
If the insert is ROTATED
it returns the same bounding box (rotated)
of the not rotated insert.

The Lee Mac solution is YELLOW.

While I hope in a GREEN SOLUTION !

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: LM:blockreferenceboundingbox
« Reply #9 on: February 27, 2021, 12:26:06 PM »
Marco,
don't you think that maybe there is good reason to explain the difference between your code
and LM:blockreferenceboundingbox of Lee Mac?
I suppose that the Lee Mac code, works also for rotated and not uniformly scaled inserts...
...
Lee Mac: "I would note that the ActiveX getboundingbox method doesn't work too well where dynamic block references are concerned, since the method doesn't seem to account for invisible objects within the block definition. Hence, you will receive unexpected results when calculating the extents of a modelspace block containing dynamic block references, or if you are processing a block definition containing nested dynamic block references (unlikely, but you never know)."

Original post: http://www.theswamp.org/index.php?topic=48405.msg534887#msg534887

can you attach a file with your block inserted as you wish?

domenicomaria

  • Swamp Rat
  • Posts: 724
Re: LM:blockreferenceboundingbox
« Reply #10 on: February 27, 2021, 01:30:27 PM »
Marco, it is not a dynamic block ...

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: LM:blockreferenceboundingbox
« Reply #11 on: February 27, 2021, 01:45:56 PM »
don't you think that maybe there is good reason
to explain the difference between
your code
and LM:blockreferenceboundingbox of Lee Mac ?

I suppose that the Lee Mac code, works also for rotated and not uniformly scaled inserts . .

If you were to analyse my code, you will notice that my function uses the getboundingbox method to calculate the bounding box of all visible non-text objects found within the block definition (including within the definition of any nested blocks found within the block definition), and then transforms such bounding box relative to the position, scale, rotation & orientation of the block reference.

domenicomaria

  • Swamp Rat
  • Posts: 724
Re: LM:blockreferenceboundingbox
« Reply #12 on: February 27, 2021, 03:39:27 PM »
Lee, thanks for your reply.

I had already carefully read all of your code,
and I understand what it does
. . . and a little, I understand how he does it . . .
. . .because it is not an easy code for everyone to understand . . .

. . . and anyway,
can you suggest us how to do
to get the green "bounding box" ?  8-)

domenicomaria

  • Swamp Rat
  • Posts: 724
Re: LM:blockreferenceboundingbox
« Reply #13 on: March 01, 2021, 03:39:13 AM »
I have found, a very simple solution
to obtain the true bounding box
of a simple insert that doesn't contain nested inserts.

The insert is not uniformly scaled, and rotated.

I explode it, calculate the Bounding Box avoiding hatches (and other "bad" objects)
then UNDO 1 time , and the result is correct . . .

It is not a matematical and elegant solution.

The solution is very limited by certain requirements that the block must have

However, at least, it works.

On the left side there is the result of LM:blockreferenceboundingbox
while on the right side there is the correct result that I get exploding it.

Ciao
« Last Edit: March 01, 2021, 06:12:35 AM by domenicomaria »

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: LM:blockreferenceboundingbox
« Reply #14 on: March 01, 2021, 07:51:39 AM »
Without Explode & Undo try this (it is only an example!):
Code: [Select]
(setq SS_Out (ssadd))
(foreach ObjFor (vlax-invoke (vlax-ename->vla-object EntNam) "Explode")
  (cond
    ( (wcmatch (strcase (vla-get-ObjectName ObjFor)) "*LINE"))
      (ssadd (vlax-vla-object->ename ObjFor) SS_Out)
    )
    ( T (vla-delete ObjFor) )
  )
)