[python] How to create a box from two points (diagonal)

Is there a single method that does that but I cannot find due to it’s weird name or I just have to do it in several steps?

Thanks in advance.

Call this:

https://developer.rhino3d.com/5/api/RhinoCommonWin/html/M_Rhino_Geometry_Box__ctor.htm

With this:

https://developer.rhino3d.com/5/api/RhinoCommonWin/html/M_Rhino_Geometry_BoundingBox__ctor.htm

Hi @AndersDeleuran,

This is what I did:

bbox_json = [[64396.8984375,4499.89892578125,14999.900390625],[73620.1015625,4518.10107421875,18000.099609375]]
#p1 = Rhino.Geometry.Point3d(bbox_json[0][0],bbox_json[0][1],bbox_json[0][2])
#p2 = Rhino.Geometry.Point3d(bbox_json[1][0],bbox_json[1][1],bbox_json[1][2])
#bbox = Rhino.Geometry.BoundingBox(p1,p2)
#brep = bbox.ToBrep()
p1 = rs.AddPoints(bbox_json)
bbb = rs.BoundingBox(p1)
print bbb

But I can’t see the box appear in Rhino. Can’t figure it out.
The print returns only some points I assume the corner points of the box

lol I figured it out

rs.AddBox(bbb)

That uses a substantially more complicated logic than directly creating the bounding box using the “two corners” overload above:

def BoundingBox(objects, view_or_plane=None, in_world_coords=True):
    """Returns either world axis-aligned or a construction plane axis-aligned
    bounding box of an object or of several objects
    Parameters:
      objects = The identifiers of the objects
      view_or_plane[opt] = Title or id of the view that contains the
          construction plane to which the bounding box should be aligned -or-
          user defined plane. If omitted, a world axis-aligned bounding box
          will be calculated
      in_world_coords[opt] = return the bounding box as world coordinates or
          construction plane coordinates. Note, this option does not apply to
          world axis-aligned bounding boxes.
    Returns:
      Eight 3D points that define the bounding box. Points returned in counter-
      clockwise order starting with the bottom rectangle of the box.
      None on error
    """
    def __objectbbox(object, xform):
        geom = rhutil.coercegeometry(object, False)
        if not geom:
            pt = rhutil.coerce3dpoint(object, True)
            return Rhino.Geometry.BoundingBox(pt,pt)
        if xform: return geom.GetBoundingBox(xform)
        return geom.GetBoundingBox(True)

    xform = None
    plane = rhutil.coerceplane(view_or_plane)
    if plane is None and view_or_plane:
        view = view_or_plane
        modelviews = scriptcontext.doc.Views.GetStandardRhinoViews()
        for item in modelviews:
            viewport = item.MainViewport
            if type(view) is str and viewport.Name==view:
                plane = viewport.ConstructionPlane()
                break
            elif type(view) is System.Guid and viewport.Id==view:
                plane = viewport.ConstructionPlane()
                break
        if plane is None: return scriptcontext.errorhandler()
    if plane:
        xform = Rhino.Geometry.Transform.ChangeBasis(Rhino.Geometry.Plane.WorldXY, plane)
    bbox = Rhino.Geometry.BoundingBox.Empty
    if type(objects) is list or type(objects) is tuple:
        for object in objects:
            objectbbox = __objectbbox(object, xform)
            bbox = Rhino.Geometry.BoundingBox.Union(bbox,objectbbox)
    else:
        objectbbox = __objectbbox(objects, xform)
        bbox = Rhino.Geometry.BoundingBox.Union(bbox,objectbbox)
    if not bbox.IsValid: return scriptcontext.errorhandler()

    corners = list(bbox.GetCorners())
    if in_world_coords and plane is not None:
        plane_to_world = Rhino.Geometry.Transform.ChangeBasis(plane, Rhino.Geometry.Plane.WorldXY)
        for pt in corners: pt.Transform(plane_to_world)
    return corners

And I know how you like to keep things simple and terse :wink:

1 Like

What is the point in technological development if it doesn’t make things simpler? :stuck_out_tongue_winking_eye:

Wrapping a bunch of needless complexity into a function isn’t really making things simpler though, now is it :nerd_face:

1 Like

Philosophical question:
Does complexity matter when it’s hard to consume (comprehend) and remember?

Code should be the simplest possible, use the KISS principle: Keep It Simple, Stupid.

Simple is better than complex
Complex is better than complicated
! -_RunPythonScript "import this"