Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Script - Discrepancy in the precision length, height and width

3 REPLIES 3
Reply
Message 1 of 4
YouElz
172 Views, 3 Replies

Script - Discrepancy in the precision length, height and width

Hello everyone,

I've recently begun experimenting with scripting for Fusion 360, focusing on simple tasks like extracting component or body dimensions such as Length, Height, and Width. I think I've successfully retrieved these dimensions, but I've noticed a discrepancy in the precision of the values displayed by the script (see attached image) compared to when they are inspected directly within the Fusion 360 design.

Here are the body dimensions (in Design):

  • Length: 200.0 mm
  • Width: 150.00 mm
  • Height: 300.00 mm

@BrianEkins 

import traceback
import adsk.fusion
import adsk.core

def run(context):
    ui = 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

        mMgr:adsk.core.MeasureManager = app.measureManager

        OBBox: adsk.core.OrientedBoundingBox3D = mMgr.getOrientedBoundingBox(
                                                        comp.bRepBodies[0],
                                                        comp.yConstructionAxis.geometry.direction,
                                                        comp.xConstructionAxis.geometry.direction)

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

        msg = [
            f'length : {OBBox.length * ratio} {defLenUnit}',
            f'width : {OBBox.width * ratio} {defLenUnit}',
            f'height : {OBBox.height * ratio} {defLenUnit}',
        ]

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

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
Tags (2)
3 REPLIES 3
Message 2 of 4
kandennti
in reply to: YouElz

Hi @YouElz -San.

 

The possibility of incorrect representation of real numbers is possible in any programming language.

・・・
        msg = [
            f'length : {round(OBBox.length * ratio, 3)} {defLenUnit}',
            f'width : {round(OBBox.width * ratio, 3)} {defLenUnit}',
            f'height : {round(OBBox.height * ratio, 3)} {defLenUnit}',
        ]
・・・
Message 3 of 4
YouElz
in reply to: kandennti

Hi @kandennti 

 

I am wondering if it has to do with the OrientedBoundingBox:

 

oBBox: adsk.core.OrientedBoundingBox3D = mMgr.getOrientedBoundingBox(
                                                        comp.bRepBodies[0],
                                                        comp.yConstructionAxis.geometry.direction,
                                                        comp.xConstructionAxis.geometry.direction)

 

 

looking at the API I found formatValue method of UnitsManger

 

 

        Given a floating point number this method evaulates it as a value of a specific unit type
        and returns an appropriate string. By default, the current unit settings defined
        in the user preferences is used, but you can set the method arguments to override
        the defaults to specify the formatting you want. The input value always uses internal
        units, which are centimeters for length, radians for angles, and mass is in kilograms.
        
        This method is useful whenever you have a value you've gotten from Fusion or computed on your
        own and need to display it to the user as a string. This method does the conversion and also
        takes into account the units and the formatting the user has specified in their preferences.

 

 

I used it this way and it seems the value is being displayed correctly, however, I am not sure if this is the right way to do it. 

 

 

    unitsMgr: adsk.core.UnitsManager = design.unitsManager
    length = unitsMgr.formatValue(OBBox.length)
    width  = unitsMgr.formatValue(OBBox.width)
    height  = unitsMgr.formatValue(OBBox.height)

 

Message 4 of 4
kandennti
in reply to: YouElz

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Technology Administrators