Has anyone got a ValueInput using 'percent' units to work properly?

OceanHydroAU
Collaborator

Has anyone got a ValueInput using 'percent' units to work properly?

OceanHydroAU
Collaborator
Collaborator

I've got assorted input boxes that work OK, accepting stuff with units like 'N' (newtons), 'mm', 'mps' (meters per second) and so on.

 

One however is behaving oddly - I've got 'percent' for the unit type.

 

Has anyone used this before?  Is there a trick to make it work?

 

Sometimes I can type in a number, which is doesn't wipe out, but my code only gets "0.0" when grabbing the value to validate. 

 

Screen Shot 2020-11-30 at 8.43.30 am.png

 

Other times, any number I type in gets erased and replaced with "0.00 %" either immediately, or some random number of moments later:

e.g. I type this:

Screen Shot 2020-11-30 at 8.43.00 am.png

Press TAB and it turns into this:

Screen Shot 2020-11-30 at 8.42.50 am.png

 

Oddly, actually typing anything with a % on the end turns red:

Screen Shot 2020-11-30 at 8.42.39 am.png

Screen Shot 2020-11-30 at 8.42.06 am.png

    

See also Screencast: https://autode.sk/3fLRr8B

 

 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0 Likes
Reply
484 Views
3 Replies
Replies (3)

markSJVQS
Participant
Participant

I am running into many issues when using "percent" or "%" as a unit in the Fusion API.

 

1) The Units and Value Display preference do not behave as I would expect for ValueInputs with percent units.

 

☐ Display abbreviations for unit
☐ Display symbols for unit
ValueInput shows "50 percent"

 

☑︎ Display abbreviations for unit
☐ Display symbols for unit
ValueInput shows "50 %"

 

☐ Display abbreviations for unit
☑︎ Display symbols for unit
ValueInput shows "50 percent"

 

☑︎ Display abbreviations for unit
☑︎ Display symbols for unit

ValueInput shows "50 %"

 

2) When a ValueInput can initially show a "%" but as soon as the user starts to edit the field it turns red, and the only thing the user can do is to replace the "%" with "percent" or to remove the "%" to make the ValueInput happy.

Even if the user makes no changes, validation of this field eventually fails.

 

3) Many APIs behave inconsistently or don't work with percent units.

# Initial value can't always be set.
input = inputs.addValueInput(
  'name', 'Description',
  'percent', adsk.core.ValueInput.createByString('50 %'))  # initial value is "0.00 %"
input = inputs.addValueInput(
  'name', 'Description',
  'percent', adsk.core.ValueInput.createByString('50 percent'))  # initial value is "0.00 %"
input = inputs.addValueInput(
  'name', 'Description',
  'percent', adsk.core.ValueInput.createByString('50'))  # initial value is "0.00 %"
input = inputs.addValueInput(
  'name', 'Description',
  'percent', adsk.core.ValueInput.createByReal(0.5))  # initial value is "50.00 %"

# ValueInput.maximumValue behaves inconsistently. Neither of these value work in all cases:
input.maximumValue = 1
input.maximumValue = 100

unitsManager = adsk.core.Application.get().activeProduct.unitsManager
unitsManager.evaluateExpression('50 %', '%') # FAILS
unitsManager.evaluateExpression('50 %', 'percent') # FAILS
unitsManager.evaluateExpression('50 percent', 'percent') # works
unitsManager.evaluateExpression('50', 'percent') # works
unitsManager.formatUnits('%') # FAILS
unitsManager.formatUnits('percent') -> '%'
unitsManager.standardizeExpression('50 %') # FAILS
unitsManager.standardizeExpression('50 percent') # FAILS

# All of the below FAIL.
customFeatureInput.addCustomParameter('name', 'Description', adsk.core.ValueInput.createByString('50 %'), '%') # FAILS
customFeatureInput.addCustomParameter('name', 'Description', adsk.core.ValueInput.createByString('50 percent'), 'percent') # FAILS
customFeatureInput.addCustomParameter('name', 'Description', adsk.core.ValueInput.createByString('50'), 'percent') # FAILS
customFeatureInput.addCustomParameter('name', 'Description', adsk.core.ValueInput.createByReal(50), 'percent') # FAILS


As is, percentage units are unusable.
Hey Autodesk, can you fix this please?

0 Likes

drisler8109
Participant
Participant

Percent % is a “unit” and Fusion360 won’t work in equations where you want to scale a “length mm” variable. But you can create a dummy “percent %” variable set to unity (100%) and then when you need to use a “percent %” in an equation, you can cancel out the unit:

a = 50%
b = 100%
c = 10 mm

 

result = (a/b) * c

 

result is set to 5mm (no parameter error)

 

But “percent%” should behave like “no unit” in my mind percentages are always ratios of quantities of the same unit, and should therefore be “no unit”

0 Likes

BrianEkins
Mentor
Mentor

I don't think this is an API bug, but it is a problem with the units system in Fusion. If I interactively create a user parameter and choose 'Percentage' as the unit type, the 'Unit' column remains empty, like it's a unitless value. The expression also does not show the unit type. If I try to change the expression and use a '%' sign, it turns red.

 

BrianEkins_0-1681940131168.png

 

It will probably be better to report this on the Support forum and not mention the API. I'm guessing if it's fixed internally, the API will start working as you expect.

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