How to get a OrientedBoundingBox3D from more faces?

How to get a OrientedBoundingBox3D from more faces?

maurizio_manzi
Advocate Advocate
150 Views
2 Replies
Message 1 of 3

How to get a OrientedBoundingBox3D from more faces?

maurizio_manzi
Advocate
Advocate

Hello,
with this code I get the OrientedBoundingBox3D of the first selected face (item 0).

But how can I get the OrientedBoundingBox3D of ALL the selected faces?

Best regards

Maurizio

 

 

brep_face = adsk.fusion.BRepFace.cast(ui.activeSelections.item(0).entity)

# Vector acquisition from eye direction. Just for test
        camera: core.Camera = app.activeViewport.camera

        vecLength: core.Vector3D = camera.eye.vectorTo(
            camera.target
        )
        vecLength.normalize()

        vecWidth: core.Vector3D = vecLength.crossProduct(
            camera.upVector
        )
        vecWidth.normalize()
 
measMgr: core.MeasureManager = app.measureManager
        orientedBBox: core.OrientedBoundingBox3D = measMgr.getOrientedBoundingBox(
            brep_face,
            vecLength,
            vecWidth,
        )
0 Likes
Accepted solutions (1)
151 Views
2 Replies
Replies (2)
Message 2 of 3

kandennti
Mentor
Mentor
Accepted solution

Hi @maurizio_manzi -san.


I created a temporary UnionBody.

# Fusion360API Python script

import traceback
import adsk.core as core
import adsk.fusion as fusion

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

        body: fusion.BRepBody = root.bRepBodies[0]

        # target faces
        faces = list(body.faces)[0:3]

        # Vector acquisition from eye direction.
        camera: core.Camera = app.activeViewport.camera

        vecLength: core.Vector3D = camera.eye.vectorTo(
            camera.target
        )
        vecLength.normalize()

        vecWidth: core.Vector3D = vecLength.crossProduct(
            camera.upVector
        )

        # OrientedBoundingBox
        measMgr: core.MeasureManager = app.measureManager
        orientedBBox: core.OrientedBoundingBox3D = measMgr.getOrientedBoundingBox(
            create_union_body(faces),
            vecLength,
            vecWidth,
        )

        # dump
        app.log("*********")
        app.log(f"CenterPoint:{orientedBBox.centerPoint.asArray()}")
        app.log(f"LengthDirection:{orientedBBox.lengthDirection.asArray()}")
        app.log(f"Length:{orientedBBox.length}")
        app.log(f"WidthDirection:{orientedBBox.widthDirection.asArray()}")
        app.log(f"Width:{orientedBBox.width}")
        app.log(f"HeightDirection:{orientedBBox.heightDirection.asArray()}")
        app.log(f"Height:{orientedBBox.height}")

        DumpBoundingBox(orientedBBox)
        ui.activeSelections.clear()
        [ui.activeSelections.add(f) for f in faces]

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


def create_union_body(faces: list) -> fusion.BRepBody:
    if len(faces) < 1:
        return None
    
    tmpMgr: fusion.TemporaryBRepManager = fusion.TemporaryBRepManager.get()
    tmpBody: fusion.BRepBody = tmpMgr.copy(faces[0])

    if len(faces) < 2:
        return tmpBody

    for face in faces[1:]:
        tmpMgr.booleanOperation(
            tmpBody,
            tmpMgr.copy(face),
            fusion.BooleanTypes.UnionBooleanType,
        )

    return tmpBody


def DumpBoundingBox(bBox: core.OrientedBoundingBox3D):
    tmpMgr: fusion.TemporaryBRepManager = fusion.TemporaryBRepManager.get()
    tmpBody: fusion.BRepBody = tmpMgr.createBox(bBox)

    app: core.Application = core.Application.get()
    des: fusion.Design = app.activeProduct
    root: fusion.Component = des.rootComponent

    baseFeat: fusion.BaseFeature = None
    if des.designType == fusion.DesignTypes.ParametricDesignType:
        baseFeat = root.features.baseFeatures.add()

    bodies: fusion.BRepBodies = root.bRepBodies
    resBody: fusion.BRepBody = None
    if baseFeat:
        baseFeat.startEdit()
        try:
            bodies.add(tmpBody, baseFeat)
        except:
            pass
        finally:
            baseFeat.finishEdit()
            resBody = baseFeat.bodies[0]
    else:
        resBody = bodies.add(tmpBody)

    resBody.opacity = 0.5
0 Likes
Message 3 of 3

maurizio_manzi
Advocate
Advocate

Thank you very much. This is very helpfull.
Best regards
Maurizio