Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

Convert Mass from internal units to user units

ebunn3
Advocate

Convert Mass from internal units to user units

ebunn3
Advocate
Advocate

Is there a way to change this line of code to work for mass units?  For example converting kg to lbmass?   I can determine how to do it for length units but not mass units.  

 

Thanks,

Eric

 

convertVal = design.unitsManager.convert(val, design.unitsManager.internalUnits,design.unitsManager.defaultLengthUnits )

   

0 Likes
Reply
Accepted solutions (1)
379 Views
3 Replies
Replies (3)

kandennti
Mentor
Mentor

Hi @ebunn3 .

 

I tried it, and was able to convert.

# Fusion360API Python script

import traceback
import adsk.fusion
import adsk.core

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

        value = 1
        kg = des.unitsManager.formatUnits('kg')
        lbm = des.unitsManager.formatUnits('lbmass')
        res = des.unitsManager.convert(
            value,
            kg,
            lbm,
        )

        ui.messageBox(f'{value} {kg} -> {res} {lbm}')

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

 

It seems to be possible to convert directly by using a string as a parameter.

        res = des.unitsManager.convert(
            value,
            'kg',
            'lbmass',
        )
1 Like

BrianEkins
Mentor
Mentor
Accepted solution

In the user interface, the only setting available to the user is to set the active unit for the distance, as shown below. That's the reason the API only exposes that setting.

 

ActiveUnits.png

 

However, I just noticed that other units types are changed based on that setting.  For example, below are various settings for the distance and the unit type that mass is reported in.  It seems like it would be best to give the user the choice of their mass unit instead of tying it to the length but that's what it does today. Because of the current behavior, it would be useful for the API to expose this but you can also have your own table so you know what units to associate with the current active length.

 

Inch - Ounces

Feet - Pounds

Centimeter - Grams

Meter - Kilograms  

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
2 Likes

ebunn3
Advocate
Advocate

Thanks for the clarity.   I can work with this based on user defined units.  

Eric

0 Likes