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: 

getting center of mass in mm

2 REPLIES 2
Reply
Message 1 of 3
fusion360_Admin4QV4K
107 Views, 2 Replies

getting center of mass in mm

Hello.
I am trying to write a script to parse through all components and write their XYZ coordinates to a file. So far everything works except the XYZ coordinates. What I get are insanely large values. How can I convert these values to say.. MM?
Example:
if I use "x = child.physicalProperties.centerOfMass.x"
I will get a HUGE number like -0.2487257213245866
(the actual value in mm is 26.5002)
I am trying to get the center of mass.
2 REPLIES 2
Message 2 of 3

You might want to look at the User Manual topic on units. When using the API, all distance values are in centimeters. The value you're seeing is in centimeters. The value (-0.2487257213245866) is not big. There's a decimal point at the beginning so it's less than 1/4 of the centimeter or -2.487 mm. There are a lot of digits because Fusion is doing the calculations using double-precision floating point numbers. Using formatting options, you can always choose to round to a specific precision. You can also use the UnitsManager object to convert the value into a string that uses the user's current units and the precision they've specified in their preferences.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 3 of 3

Hi @fusion360_Admin4QV4K .

 

Fusion360's internal unit of length is "Cm".
It is recommended to use the UnitsManager.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-GUID-9c2d17c1-70c6-4c2d-ae9e-c6f6f283b1a9 

 

If you have set the unit of length to "mm", it will look like this.

# Fusion360API Python script

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

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

        unitsMgr: core.UnitsManager = des.unitsManager

        occ: fusion.Occurrence = None
        for occ in root.allOccurrences:
            props: fusion.PhysicalProperties = occ.physicalProperties
            cog: core.Point3D = props.centerOfMass

            infos = [
                f'** {occ.name} - Center Of Mass**',
                f'X:{unitsMgr.formatInternalValue(cog.x)}',
                f'Y:{unitsMgr.formatInternalValue(cog.y)}',
                f'Z:{unitsMgr.formatInternalValue(cog.z)}',
            ]
            app.log('\n'.join(infos))

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

 

If you want to specify the unit, you can change it to this.

                f'X:{unitsMgr.formatInternalValue(cog.x, "mm")}',
                f'Y:{unitsMgr.formatInternalValue(cog.y, "mm")}',
                f'Z:{unitsMgr.formatInternalValue(cog.z, "mm")}',

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

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report