Bounding Box

Geometric definition


A bounding box is the smallest axis-aligned box that contains a given set of points or a geometric object. In 3D space, it’s a rectangular box, and in 2D space, it’s a rectangle. The sides (or edges) of a bounding box are aligned with the axes of the coordinate system.

Here’s an example of how to create a bounding box in Rhino

rhinoscriptsyntax

import rhinoscriptsyntax as rs

# Prompt the user to select an object
object_id = rs.GetObject("Select an object")

# Get the bounding box of the object
bbox = rs.BoundingBox(object_id)

# If the bounding box was successfully created, add it to the document
if bbox:
    rs.AddBox(bbox)

RhinoCommons

import Rhino
import scriptcontext as sc

# Prompt the user to select an object
object_id = sc.doc.Objects.GetObject("Select an object").Id

# Get the geometric object
geo = sc.doc.Objects.Find(object_id).Geometry

# Get the bounding box of the object
bbox = geo.GetBoundingBox(True)

# If the bounding box was successfully created, add it to the document
if bbox.IsValid:
    box = Rhino.Geometry.Box(bbox)
    sc.doc.Objects.AddBox(box)
    sc.doc.Views.Redraw()

Properties


Here are some properties of the BoundingBox struct and how to implement them

import Rhino

# Create a bounding box
bbox = Rhino.Geometry.BoundingBox(-1, -1, -1, 1, 1, 1)

# Get the point in the corner with the smallest coordinates
min_point = bbox.Min

# Get the point in the corner with the largest coordinates
max_point = bbox.Max

# Get the point in the center of the bounding box
center_point = bbox.Center

# Get the total area of the bounding box
area = bbox.Area

# Get the volume of the bounding box
volume = bbox.Volume

Methods


Here are some methods of the BoundingBox struct and how to implement them

import Rhino

# Create a bounding box
bbox = Rhino.Geometry.BoundingBox(-1, -1, -1, 1, 1, 1)

# Inflate the bounding box by a certain amount
bbox.Inflate(0.5)

# Convert the bounding box to a Brep
brep = bbox.ToBrep()

# Get the point at the given coordinates within the bounding box
point = bbox.PointAt(0.5, 0.5, 0.5)


🛠 Exercise


00 :🐍⬇️⬇️⬇️ Download the script here ⬇️⬇️⬇️🐍 00 :🦏⬇️⬇️⬇️ Download the 3dm here ⬇️⬇️⬇️🦏

01 :🐍⬇️⬇️⬇️ Download the script here ⬇️⬇️⬇️🐍

01 :🦏⬇️⬇️⬇️ Download the 3dm here ⬇️⬇️⬇️🦏


Solution

00 :🐍⬇️⬇️⬇️ Download the script here ⬇️⬇️⬇️🐍 01 :🐍⬇️⬇️⬇️ Download the script here ⬇️⬇️⬇️🐍