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):
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()))
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):
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()))
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}',
]
・・・
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}',
]
・・・
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)
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)
Can't find what you're looking for? Ask the community or share your knowledge.