Get Bounding Box Coordinates in FreeCAD Using Python

This article explains how to retrieve and utilize the bounding box coordinates of a 3D object in FreeCAD using its built-in Python API. By accessing an object's topological shape and querying its BoundBox property, you can quickly extract minimum and maximum spatial limits, center points, and dimensions for automated modeling, collision detection, or export preparation.

Accessing the Bounding Box

In FreeCAD, any geometric object derived from the Part workbench contains a Shape attribute. This shape features a BoundBox property, which represents an axis-aligned bounding box (AABB) surrounding the geometry.

To access the bounding box of an object, open the FreeCAD Python console (View > Panels > Python console) and run the following commands:

import FreeCAD as App

# Get the active document
doc = App.ActiveDocument

# Access the object by its internal name
obj = doc.getObject("Box")  # Replace "Box" with your object's name

# Retrieve the bounding box
bbox = obj.Shape.BoundBox

Alternatively, if you want to inspect whatever object is currently highlighted in the tree view or 3D view, use FreeCADGui:

import FreeCADGui as Gui

selected_objs = Gui.Selection.getSelection()
if selected_objs:
    bbox = selected_objs[0].Shape.BoundBox

Reading Coordinate Values

The BoundBox object provides several read-only attributes that define its extent along the X, Y, and Z axes:

  • Extreme Coordinates:
    • bbox.XMin, bbox.XMax
    • bbox.YMin, bbox.YMax
    • bbox.ZMin, bbox.ZMax
  • Dimensions:
    • bbox.XLength, bbox.YLength, bbox.ZLength
  • Center Position:
    • bbox.Center (returns a Base.Vector representing (X, Y, Z))

Complete Script Example

Below is a complete script that prints the bounding box limits, dimensions, and center coordinates of a selected object in a readable format:

import FreeCAD as App
import FreeCADGui as Gui

def print_bounding_box():
    selection = Gui.Selection.getSelection()
    if not selection:
        App.Console.PrintError("No object selected.\n")
        return

    obj = selection[0]
    if not hasattr(obj, "Shape"):
        App.Console.PrintError("Selected object has no Shape attribute.\n")
        return

    bbox = obj.Shape.BoundBox

    print(f"Bounding Box for: {obj.Label} ({obj.Name})")
    print(f"X: Min = {bbox.XMin:.2f}, Max = {bbox.XMax:.2f}, Length = {bbox.XLength:.2f}")
    print(f"Y: Min = {bbox.YMin:.2f}, Max = {bbox.YMax:.2f}, Length = {bbox.YLength:.2f}")
    print(f"Z: Min = {bbox.ZMin:.2f}, Max = {bbox.ZMax:.2f}, Length = {bbox.ZLength:.2f}")
    print(f"Center: ({bbox.Center.x:.2f}, {bbox.Center.y:.2f}, {bbox.Center.z:.2f})")

print_bounding_box()

Handling Transformed Objects

If an object has been moved or rotated using its Placement property, obj.Shape.BoundBox returns the bounding box aligned with the global coordinate system, accounting for that placement. If you need the bounding box relative to the object's local, untransformed coordinates, calculate it from an unplaced copy of the shape:

local_shape = obj.Shape.copy()
local_shape.Placement = App.Placement()
local_bbox = local_shape.BoundBox