Python inheritance Rhino.Geometry.BoundingBox

Hi,
once again this might be quite a stupid question, but I will ask it anyways.

If I try to create a class inherited from a Rhino.Geometry.BoundingBox and I try this

class BBox(Rhino.Geometry.BoundingBox):

    def __init__(self, geometry):

        Rhino.Geometry.BoundingBox.__init__(self, geometry)

and then I try it I get the following:

bbox = BBox(lst)

cannot derive from Rhino.Geometry.BoundingBox because it is a value type

Yet, when I try

class MyLine(Rhino.Geometry.LineCurve):

    def __init__(self, pt_a, pt_b):

        Rhino.Geometry.LineCurve.__init__(self, pt_a, pt_b)

it’s working fine.
I did my reserach on the value types, but I am stuck here.

Thanks,
T.

Hi @tobias.stoltmann,

BoundingBox is a structure, not a class. A structure type can’t inherit from other class or structure type and it can’t be the base of a class.

More on structures

You haven’t mentioned why you want to inherit from BoundingBox. You might consider creating a new class that includes a BoundingBox as a data member.

– Dale

@dale, thanks.
Okay, I understand (or to be honest, I am starting to understand).

So, what you suggest is something like this?

class BBox(object):
    
    def __init__(self, geometries):

        self.BBox = self._CreateAroundItems()

    def _CreateAroundItems(self):
        bbox = Rhino.Geometry.BoundingBox.Empty
        for geo in geometries:
            obj_box = geo.GetBoundingBox(Rhino.Geometry.Plane.WorldXY)
            bbox = Rhino.Geometry.BoundingBox.Union(bbox, obj_box)

        return bbox

@dale, the reason I want to inherit is, that I usually use boundingboxes properties and then process them.
So I thought I might adapt this to my needs quite easily.

Hi @tobias.stoltmann,

How about something like this?

import Rhino

# Fancy bounding box class
class MyBoundingBox(object):
    def __init__(self): 
        self.m_bbox = Rhino.Geometry.BoundingBox()
    
    # Set this bounding box from a Rhino bounding box
    def Set(self, bbox):
        self.m_bbox = bbox
    
    # Set this bounding box from a GetObject
    def SetFromGetObject(self, go):
        if go:
            for objref in go.Objects():
                rc, bbox = Rhino.DocObjects.RhinoObject.GetTightBoundingBox([objref.Object()])
                if rc:
                    self.m_bbox.Union(bbox)
    
    # Set this bounding box from a list of geometry
    def SetFromGeomtryList(self, list):
        for geo in list:
            bbox = geo.GetBoundingBox(True)
            self.m_bbox.Union(bbox)
    
    # Unions a Rhino bounding box to this bounding box
    def Union(self, bbox):
        self.m_bbox.Union(bbox)
    
    # Returns this bounding box as a Rhino bounding box
    def Get(self):
        return self.m_bbox
    
    # Get the minimum corner point
    @property
    def Min(self):
        return self.m_bbox.Min
    
    # Get the maximum corner point
    @property
    def Max(self):
        return self.m_bbox.Max

# Test
def test():
    go = Rhino.Input.Custom.GetObject()
    go.SetCommandPrompt('Select curves')
    go.GeometryFilter = Rhino.DocObjects.ObjectType.Curve
    go.GetMultiple(1, 0)
    if go.CommandResult() == Rhino.Commands.Result.Success:
        bbox = MyBoundingBox()
        bbox.SetFromGetObject(go)
        print(bbox.Min)
        print(bbox.Max)

if __name__=="__main__":
    test()

– Dale

@dale, perfect. Thanks.