Calling BoundingBox as union from Python

I’m trying to use BoundingBox() from python, but i can’t set it to Union, as I can with the canvas, so i get a Singular Box for every point, instead of a single bounding box.

Any quick way around this?

/J

Hi,
Would you mind sharing some code so I can see exactly what you’re trying to do?

when you say canvas, do you mean from within a GHPython component?
if it’s points, a “quick” way might be to run a polylinecurve through all of your points and get the bounding box from that. The API documentation has some info that might help:
https://developer.rhino3d.com/api/RhinoCommon/html/M_Rhino_Geometry_GeometryBase_GetBoundingBox_3.htm

Here is a quick and dirty example in a GHPython component. (ripped from the method linked above)

import Rhino

view = Rhino.RhinoDoc.ActiveDoc.Views.ActiveView
plane = view.ActiveViewport.ConstructionPlane()

crv = Rhino.Geometry.PolylineCurve(x)

bbox = crv.GetBoundingBox(plane)

# Print the min and max box coordinates in cplane coordinates
print "CPlane min:", bbox.Min
print "CPlane max:", bbox.Max

box = Rhino.Geometry.BoundingBox(bbox.Min, bbox.Max)
a = box.ToBrep()

x needs to be set to list access, type hint point3d (expects a bunch of points)

1 Like

Hi Alain

I got it solved now using rs.boundingbox instead as also suggested elsewhere, but I remade the problem. The standard BBox component has the Union/Per Object selection, which is not available via ghpythonlibs.components.

import ghpythonlib.components as ghcomp
import Grasshopper as gh
import rhinoscriptsyntax as rs

a = []

def listToTree(list):
    
    """ Convert a nested python iterable to a datatree """
    
    dt = gh.DataTree[object]()
    for i,l in enumerate(list):
        dt.AddRange(l,gh.Kernel.Data.GH_Path(i))
        
    return dt

a = listToTree(ghcomp.BoundingBox(x,rs.WorldXYPlane()))

Hey Chris.
I figured it almost like you describe here. Thank you so much for the input. I was hoping to do a shortcut with the ghpythonlib components, but the “other way around” is quicker and lighter anyway so win-win.

/J

cool! also, if you are on Rhino 6, you can import
from ghpythonlib import treehelpers as th
and call the list to tree, or, tree to list functions directly.

lastly, I’m away from my workstations, but I think it might be a little faster if you dumped the points into a point3dlist, then got the bounding of that list. The Rhinocommon sdk mentions that getting the bounding box of a point3dlist is a little slower than other geometry types because it has to process each point.
https://developer.rhino3d.com/api/RhinoCommon/html/P_Rhino_Collections_Point3dList_BoundingBox.htm

1 Like

Didn’t know about these. Really cool.

Makes sense, will try tomorrow, and report back.

I don’t know python but in C# its like this, maybe its the same process in python.

BoundingBox unionBox = BoundingBox.Unset;

for (int i = 0; i < geometry.Count; i++)
{
  BoundingBox bBox = geometry[i].GetBoundingBox(plane);
  unionBox.Union(bBox);
}

Also, if it is just for points there is this method that takes a list of Point3d: https://developer.rhino3d.com/5/api/RhinoCommon/html/M_Rhino_Geometry_BoundingBox__ctor_1.htm

2 Likes

Just needed this, works similarly in python.

unionBox = rg.BoundingBox.Unset

for i in range(len(geometry)):
    bBox = geometry[i].GetBoundingBox(plane)
    unionBox.Union(bBox)
1 Like

Hey Kane

Can you share the whole script?

Hi @Michael_Pryor


Var worldBox= new Box();
BoundingBox unionBox = BoundingBox.Unset;
for (int i = 0; i < geometry.Count; i++) 
{ BoundingBox bBox = geometry[i].GetBoundingBox(plane,out worldBox);
 unionBox.Union(worldBox); }


What if we want to union the output of worldBoxes? (when the plan is other than xy)? Because they are Box type and give error!!