Where in Fusion 360 is unitsMgr.defaultLengthUnits pulling from?

Where in Fusion 360 is unitsMgr.defaultLengthUnits pulling from?

MichaelAubry
Autodesk Autodesk
2,018 Views
6 Replies
Message 1 of 7

Where in Fusion 360 is unitsMgr.defaultLengthUnits pulling from?

MichaelAubry
Autodesk
Autodesk

Hello!  I'm building a simple python script and I'm having trouble getting it to output with the correct units.

 

Code and pictures below.  Any quick ideas about where I've gone wrong?

 

Thank you!

Mike

 

2m.jpgoutput in mmjpg.jpgdefault is all mm.jpg

 

 

import adsk.core, adsk.fusion
app = adsk.core.Application.get()
ui = app.userInterface
design = app.activeProduct

 

distance = "2 m"

 

input = distance
createInput = ui.inputBox('Enter distance', 'Distance', input)
if createInput[0]:
(input, isCancelled) = createInput
distance = input

ui.messageBox('Distance: ' + distance)

 

#This is where I'm having trouble getting the units to be correct
unitsMgr = design.unitsManager
realValue = unitsMgr.evaluateExpression(distance, unitsMgr.defaultLengthUnits)
isValid = True

ui.messageBox('real value: ' + str(realValue))

Michael Aubry
Autodesk Fusion 360 Evangelist
0 Likes
Accepted solutions (3)
2,019 Views
6 Replies
Replies (6)
Message 2 of 7

ekinsb
Alumni
Alumni
Accepted solution

I re-wrote the topic on units for the June release.  The previous one (which I also wrote) was not written very well and was difficult to understand.  Hopefully this one is better.

 

http://help.autodesk.com/cloudhelp/ENU/Fusion-360-API/files/Units_UM.htm

 

The basics with units is that lengths will always be in centimeters.  At first this might not seem like it's very good but it actually makes everything much easier because it makes your code completely independent of any user settings.  This way your code doesn't need to do anything special to try and handle all of the different possible units but can be written knowing that everything is in centimeters.  Let's look some more at your specific example.  In the input box the user can enter anything such as "23", "2 m", "2 in + 3 cm", "5 in + d1", etc.  The UnitsManager.evaluateExpression does all of the hard work to take whatever they've entered, evaluate it, and return the value in centimeters.  It uses the current document units (mm in your example) to be able to do this.  For example if you had just entered "23", it's assumed to be 23 of the active units, which are currently millimeters.  If you enter "2 m" then the current units don't really matter because you've specified the unit as part of th string.

 

Now you can do whatever processing you need to do with the value, knowing it is in centimeters.  If you perform some calculation using the value and then need to display the result back to the user then you can use the UnitsManager.formatInternalValue method to create a string to display to the user.  For example, if you replace the last line of your program with this you can see this in action.  In your case the result will be displayed in millimeters, but if you change the document units to inch all of a sudden it will now display in inches, without any changes to your code.

 

# Do some kind of calculation using the value.
half = realValue / 2

# Convert the value to a string using the default length units and display the result.
display = unitsManager.formatInternalValue(half, unitsMgr.defaultLength, true)
ui.MessageBox('Half the length: ' + display)

 

 


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
0 Likes
Message 3 of 7

MichaelAubry
Autodesk
Autodesk

That makes total sense.

 

Thanks Brian.  

 

Here's updated code that converts to mm:

 

 

import adsk.core, adsk.fusion

app = adsk.core.Application.get()

ui = app.userInterface

design = app.activeProduct

 

distance = "2 m"

 

input = distance

createInput = ui.inputBox('Enter distance', 'Distance', input)

if createInput[0]:

(input, isCancelled) = createInput

distance = input

 

ui.messageBox('Distance: ' + distance)

 

#Converts to default coding units ('cm')

unitsMgr = design.unitsManager

realValue = unitsMgr.evaluateExpression(distance, unitsMgr.defaultLengthUnits)

isValid = True

 

ui.messageBox('real value: ' + str(realValue)+ " cm")

 

#Converts the 'real value' to desired units

unitsCvt = design.unitsManager.convert(realValue,"cm", "mm")

ui.messageBox('Converted units: ' + str(unitsCvt)+" mm")

 

ui.messageBox('real value: ' + str(unitsCvt)+ " mm")

 

Michael Aubry
Autodesk Fusion 360 Evangelist
0 Likes
Message 4 of 7

ekinsb
Alumni
Alumni
Accepted solution

The convert method converts a real value in one unit to a real value in another unit.  This is useful whenever you need to convert a value and use the value in some type of computation.  If you just need to display a value to the user then the formatInternalValue method is better because it converts a real value into a string and automatically formats it using the formatting the user has specified in the preferences.  For example, you'll get the specified number of decimal places and optionally you can have it append the correct unit identifier.


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Message 5 of 7

MichaelAubry
Autodesk
Autodesk

That is better.  I'll do that. Thanks for the tip!

Michael Aubry
Autodesk Fusion 360 Evangelist
0 Likes
Message 6 of 7

MichaelAubry
Autodesk
Autodesk
Accepted solution

For those following the thread, here's what the final solution ended up as.

 

Gallery Post: https://fusion360.autodesk.com/projects/horn-of-fibonacci

Code: https://github.com/MichaelAubry/Fusion360.git

Video: 

 

Big thanks to Brian, Xiaohui, and Casey for helping with this!

 

Best,

Mike

 

 

 

 

 

 

 

 

 

Michael Aubry
Autodesk Fusion 360 Evangelist
0 Likes
Message 7 of 7

fleming.kyle.william
Enthusiast
Enthusiast

This may be an old post. But it helped me slow down and clean up a mess today!

 

Thanks guys 👍

0 Likes