Check block overlapping

Check block overlapping

BestFriendCZ
Advocate Advocate
2,291 Views
8 Replies
Message 1 of 9

Check block overlapping

BestFriendCZ
Advocate
Advocate

Hi all,

 is there easy way how to check if some block are overlapping? I have test DWG like on the picture. All blocks are on top level.... i mean they aren't nested. i tryed use BoundingBoxIntersectWith but it doesn't works. It is possible to use this command in 3D like that?

    //test sample
                    for (int i = 0; i < connectorElements.Count-1; i++)
                    {
                        Entity blk = (Entity)tr.GetObject(db.GetObjectId(false, new Handle(Convert.ToInt64(connectorElements[i].HandleValue)), 0), OpenMode.ForRead);
                        Entity blk2 = (Entity)tr.GetObject(db.GetObjectId(false, new Handle(Convert.ToInt64(connectorElements[i + 1].HandleValue)), 0), OpenMode.ForRead);
                        Point3dCollection points = new Point3dCollection();
                        blk.BoundingBoxIntersectWith(blk2, Intersect.OnBothOperands, points, new IntPtr(0), new IntPtr(0));                   
                        if (points.Count > 0)
                        {
                            Debug.Print("Found");
                        }                     
                   }

info.png

 

 

...
0 Likes
Accepted solutions (1)
2,292 Views
8 Replies
Replies (8)
Message 2 of 9

_gile
Consultant
Consultant

Hi,

 

It seems to me the problem is due to the way you iterate the collection.

As is, your code only checks for intersection between each block and the the follwong one in the collection: (1, 2) (2, 3) (3, 4).

If you want to check any possible combination: (1, 2) (1, 3) (1, 4) (2, 3) (2, 4) (3, 4), you have to do something laike this:

            for (int i = 0; i < connectorElements.Count - 1; i++)
            {
                for (int j = i + 1; j < connectorElements.Count; j++)
                {
                    Entity blk = (Entity)tr.GetObject(db.GetObjectId(false, new Handle(Convert.ToInt64(connectorElements[i].HandleValue)), 0), OpenMode.ForRead);
                    Entity blk2 = (Entity)tr.GetObject(db.GetObjectId(false, new Handle(Convert.ToInt64(connectorElements[j].HandleValue)), 0), OpenMode.ForRead);
                    Point3dCollection points = new Point3dCollection();
                    blk.BoundingBoxIntersectWith(blk2, Intersect.OnBothOperands, points, new IntPtr(0), new IntPtr(0));
                    if (points.Count > 0)
                    {
                        Debug.Print("Found");
                    }
                }
            }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 9

BestFriendCZ
Advocate
Advocate

Hi _gile,

thank you for the information. This loop is just a sample. In fact i only have two blocks in the list and i would like to know if they intersect in 3D and these two block should be intersect for sure

 

 

  //connectorElements list contains only two block
                    for (int i = 0; i < connectorElements.Count - 1; i++)
                    {
                        BlockReference blkRefFirst = (BlockReference)tr.GetObject(db.GetObjectId(false, new Handle(Convert.ToInt64(connectorElements[i].HandleValue)), 0), OpenMode.ForRead);
                        BlockReference blkRefSecond = (BlockReference)tr.GetObject(db.GetObjectId(false, new Handle(Convert.ToInt64(connectorElements[i+1].HandleValue)), 0), OpenMode.ForRead);
                        Debug.Print($"X1min: {blkRefFirst.GeometricExtents.MinPoint.X} Y1min: {blkRefFirst.GeometricExtents.MinPoint.Y} Z1min: { blkRefFirst.GeometricExtents.MinPoint.Z}");
                        Debug.Print($"X1min: {blkRefFirst.GeometricExtents.MaxPoint.X} Y1min: {blkRefFirst.GeometricExtents.MaxPoint.Y} Z1min: { blkRefFirst.GeometricExtents.MaxPoint.Z}");
                        Debug.Print($"-------------------------------------------------");
                        Debug.Print($"X2min: {blkRefSecond.GeometricExtents.MinPoint.X} Y2min: {blkRefSecond.GeometricExtents.MinPoint.Y} Z2min: { blkRefSecond.GeometricExtents.MinPoint.Z}");
                        Debug.Print($"X2min: {blkRefSecond.GeometricExtents.MaxPoint.X} Y2min: {blkRefSecond.GeometricExtents.MaxPoint.Y} Z2min: { blkRefSecond.GeometricExtents.MaxPoint.Z}");
                        Point3dCollection points = new Point3dCollection();
                        blkRefFirst.BoundingBoxIntersectWith(blkRefSecond, Intersect.OnBothOperands, points, new IntPtr(0), new IntPtr(0));
                        if (points.Count > 0)
                        {
                            Debug.Print("Found");
                        }
                        blkRefFirst.IntersectWith(blkRefSecond, Intersect.OnBothOperands, points, new IntPtr(0), new IntPtr(0));
                        if (points.Count > 0)
                        {
                            Debug.Print("Found");
                        }
                    }

X1min: 2256.xxx-xxxxxxxx Y1min: 2963.xxx-xxxxxxxx Z1min: 1874.xxx-xxxxxxxx
X1min: 2440.xxx-xxxxxxxx Y1min: 3140.5918479847 Z1min: 1925.0000697987

X2min: 2264.xxx-xxxxxxxx Y2min: 2983.xxx-xxxxxxxx Z2min: 1874.9999302013
X2min: 2419.xxx-xxxxxxxx Y2min: 3085.xxx-xxxxxxxx Z2min: 1925.xxx-xxxxxxxx

 

1.png2.png

 

 

 

link to my test file, was created ACAD2017

https://www.edisk.cz/stahni/36355/testdwg.dwg_567.85KB.html/

...
0 Likes
Message 4 of 9

BestFriendCZ
Advocate
Advocate

Hi _gile,

I found reason why my BF does not contains any intersect points. Is there any solution for it... maybe use something different like BB?

 

3.png

...
0 Likes
Message 5 of 9

_gile
Consultant
Consultant
Accepted solution

The box are not intersecting.

Look at the X coordinates: blkRefFirst is between 2240 and 2256, blkRefSecond is between 2264 and 2419.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 6 of 9

BestFriendCZ
Advocate
Advocate

Hi _gile,

you are correct.  My problem is that my blocks have different rotation. If i set rotation for both block to 0 and change only their coordinates for example Z,Y so this function works exactly how i need. I need somehow transform BB by their rotations before compare.

 

6.png

 

[ACAD] ScanBlocks(PCM1242)...
X1min: 2317.xxx-xxxxxxxx Y1min: 2983.xxx-xxxxxxxx Z1min: 1834.xxx-xxxxxxxx
X1max: 2472.xxx-xxxxxxxx Y1max: 3085.xxx-xxxxxxxx Z1max: 1885.0000697987
-------------------------------------------------
X2min: 2317.xxx-xxxxxxxx Y2min: 2983.xxx-xxxxxxxx Z2min: 1874.9999302013
X2max: 2472.xxx-xxxxxxxx Y2max: 3085.xxx-xxxxxxxx Z2max: 1925.xxx-xxxxxxxx

 

found point

 

7.png

 

[ACAD] ScanBlocks(PCM1242)...
X1min: 2317.xxx-xxxxxxxx Y1min: 2983.xxx-xxxxxxxx Z1min: 1814.xxx-xxxxxxxx
X1max: 2472.xxx-xxxxxxxx Y1max: 3085.xxx-xxxxxxxx Z1max: 1865.0000697987
-------------------------------------------------
X2min: 2317.xxx-xxxxxxxx Y2min: 2983.xxx-xxxxxxxx Z2min: 1874.9999302013
X2max: 2472.xxx-xxxxxxxx Y2max: 3085.xxx-xxxxxxxx Z2max: 1925.xxx-xxxxxxxx

 

didn't find point

 

 

Thanks for your help

...
0 Likes
Message 7 of 9

_gile
Consultant
Consultant

Sorry but I do not understand what you're trying to achieve.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 8 of 9

BestFriendCZ
Advocate
Advocate

I try to find if some types of blocks are overlapping in space.
This function works correct "IntersectWith".... but only if these blocks have zero rotation. In another case i have to transform these BB somehow

 

 

8.png

...
0 Likes
Message 9 of 9

BestFriendCZ
Advocate
Advocate

i changed function ... and it looks like it works for my purposes

 

 

                        Double AX1 = blkRefFirst.GeometricExtents.MinPoint.X;
                        Double AX2 = blkRefFirst.GeometricExtents.MaxPoint.X;
                        Double AY1 = blkRefFirst.GeometricExtents.MinPoint.Y;
                        Double AY2 = blkRefFirst.GeometricExtents.MaxPoint.Y;
                        Double AZ1 = blkRefFirst.GeometricExtents.MinPoint.Z;
                        Double AZ2 = blkRefFirst.GeometricExtents.MaxPoint.Z;

                        Double BX1 = blkRefSecond.GeometricExtents.MinPoint.X;
                        Double BX2 = blkRefSecond.GeometricExtents.MaxPoint.X;
                        Double BY1 = blkRefSecond.GeometricExtents.MinPoint.Y;
                        Double BY2 = blkRefSecond.GeometricExtents.MaxPoint.Y;
                        Double BZ1 = blkRefSecond.GeometricExtents.MinPoint.Z;
                        Double BZ2 = blkRefSecond.GeometricExtents.MaxPoint.Z;

                        //it works with rotation
                        if  (AX2 < BX1 || BX2 < AX1 || AZ2 < BZ1 || BZ2 < AZ1 || AY2 < BY1|| BY2 < AY1)
                        {
                          
                            Debug.Print(" no intersect");
                        }
                        else
                        {
                            Debug.Print("Found");
                        }

                        //doesn't work with rotation
                        //blkRefFirst.BoundingBoxIntersectWith(blkRefSecond, Intersect.ExtendArgument, points, new IntPtr(0), new IntPtr(0));
                        //if (points.Count > 0)
                        //{
                        //    Debug.Print("Found");
                        //}
                        //blkRefFirst.IntersectWith(blkRefSecond, Intersect.OnBothOperands, points, new IntPtr(0), new IntPtr(0));
                        //if (points.Count > 0)
                        //{
                        //    Debug.Print("Found");
                        //}
...
0 Likes