Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

Simple script to show bounding box length, width & thickness

isocam
Collaborator

Simple script to show bounding box length, width & thickness

isocam
Collaborator
Collaborator

Can anybody help?

 

Can anybody create a simple Python script for me that shows a the length, width & thickness of a part (not an assembly) using the following?

 

L = component.boundingBox.maxPoint.x - component.boundingBox.minPoint.x

W = component.boundingBox.maxPoint.y - component.boundingBox.minPoint.y

H = component.boundingBox.maxPoint.z - component.boundingBox.minPoint.z

 

Many thanks in advance!

 

Darren

0 Likes
Reply
809 Views
2 Replies
Replies (2)

kandennti
Mentor
Mentor

Hi @isocam .

 

# Fusion360API Python script
import traceback
import adsk.fusion
import adsk.core

def run(context):
    ui: adsk.core.UserInterface = adsk.core.UserInterface.cast(None)
    try:
        app: adsk.fusion.Application = adsk.core.Application.get()
        ui = app.userInterface
        des: adsk.fusion.Design = app.activeProduct

        comp: adsk.fusion.Component = des.activeComponent
        bBox: adsk.core.BoundingBox3D = comp.boundingBox

        maxPnt: adsk.core.Point3D = bBox.maxPoint
        minPnt: adsk.core.Point3D = bBox.minPoint

        length = abs(maxPnt.x - minPnt.x)
        width = abs(maxPnt.y - minPnt.y)
        height = abs(maxPnt.z - minPnt.z)

        unitsMgr: adsk.core.UnitsManager = des.unitsManager
        defLenUnit = unitsMgr.defaultLengthUnits
        retio = unitsMgr.convert(1, unitsMgr.internalUnits, defLenUnit)

        msg = [
            f'-- {comp.name} --',
            f'length : {length * retio} {defLenUnit}',
            f'width : {width * retio} {defLenUnit}',
            f'height : {height * retio} {defLenUnit}',
        ]

        ui.messageBox('\n'.join(msg))

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
1 Like

BrianEkins
Mentor
Mentor

What @kandennti posted is a good solution.  However, anytime you want to display a value to the user I would recommend using the UnitsManager.formatInternalValue method. This will take the value and format it using the settings the user has specified in their preferences.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
1 Like