face.boundingBox viewed from the direction of a known adsk.core.Vector3D

face.boundingBox viewed from the direction of a known adsk.core.Vector3D

maurizio_manzi
Advocate Advocate
475 Views
6 Replies
Message 1 of 7

face.boundingBox viewed from the direction of a known adsk.core.Vector3D

maurizio_manzi
Advocate
Advocate

Hello,

face.boundingBox provides a bounding box that originates from the global coordinate system and is aligned with the global coordinate system. Is there a way to obtain a bounding box that originates from the global coordinate system but is viewed from the direction of a known adsk.core.Vector3D?

 

Best regards
Maurizio

0 Likes
Accepted solutions (1)
476 Views
6 Replies
Replies (6)
Message 2 of 7

kandennti
Mentor
Mentor

Hi @maurizio_manzi -San.

 

BoundingBox3D relies on the orientation of the global coordinate system.
The size in any vector direction can be obtained with the MeasureManager.getOrientedBoundingBox method, but it will be OrientedBoundingBox3D, not BoundingBox3D.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-DF2F5C84-57C7-4F61-BA05-D2978FFF2724 

 

The following sample outputs the size of the first face of the first body of the root component in the screen direction.

# 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]
        face: fusion.BRepFace = body.faces[0]

        bBox: core.BoundingBox3D = face.boundingBox

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

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

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

        # OrientedBoundingBox
        measMgr: core.MeasureManager = app.measureManager
        orientedBBox: core.OrientedBoundingBox3D = measMgr.getOrientedBoundingBox(
            face,
            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}")

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

 

0 Likes
Message 3 of 7

maurizio_manzi
Advocate
Advocate

Thank you very much. This is really helpful.

The Vector3D is acquired from the camera view:

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

Is there a possibility to use an existing adsk.core.Vector3D variable instead of the camera view? Maybe with also a 3D-Line for the length direction?

I also noticed that even when I use the camera, only the values for "CenterPoint" are correct. All other values are only displayed as fractions of the actual length.
For example, for a length or width of 3.5 cm, only 0.15 cm is shown.

Best regards
Maurizio

0 Likes
Message 4 of 7

kandennti
Mentor
Mentor

@maurizio_manzi -San.

 

I have not checked because I am busy, but I forgot to make vecLength a unit vector and that may be the reason.

 

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

 

0 Likes
Message 5 of 7

maurizio_manzi
Advocate
Advocate

Hello,

thank you very much.
I add:
vecLength.normalize()
vecWidth.normalize()
but if I use simply vectors as vecLength = adsk.core.Vector3D.create(1,0,0) and vecWidth = adsk.core.Vector3D.create(0,1,0)
it works fine. But if I use the camera.eye values for the vectors, the values of the orientated bounding box are all very wrong.

 

Best regards
Maurizio

0 Likes
Message 6 of 7

kandennti
Mentor
Mentor
Accepted solution

@maurizio_manzi -San.

Fixed to add a block for easier checking.

# 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]
        face: fusion.BRepFace = body.faces[0]

        bBox: core.BoundingBox3D = face.boundingBox

        #*****
        # 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(
            face,
            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(face)

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


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



It is possible that I misunderstood what you are asking for as well, but the sample script works like this.



0 Likes
Message 7 of 7

maurizio_manzi
Advocate
Advocate

Hello,
I made a mistake. It works.
Thank you very much.
Best regards
Maurizio