ParametricText icon indicating copy to clipboard operation
ParametricText copied to clipboard

Suppress decimals if not needed

Open evilC opened this issue 2 years ago • 2 comments

Is your feature request related to a problem? Please describe. No

Describe the solution you'd like If a parameter is an integer, generate an integer
If a parameter is a decimal, generate a decimal
ie, currently: if the parameter is 2, then 2.0 is generated (I want 2 instead)
if the parameter is 2.5, then generate 2.5 to put it another way, I want the text to appear EXACTLY how the user parameter appears

Describe alternatives you've considered None - not aware of any alternatives. I have a User Parameter that is sometimes an integer, and sometimes a decimal

evilC avatar Oct 25 '22 01:10 evilC

Hi, this is actually much harder than you think.

First, what you can do today:

Expression Example Result Explanation
{d10:.3f} 10.000 Parameter value with 3 decimals
{d10:.0f} 10 Parameter value without decimals

From: https://parametrictext.readthedocs.io/en/stable/parameters.html

Second, the problem:

The add-in cannot discern how many decimals the user wants. Try bringing up the Console (Ctrl+Alt+C), select "Py" and enter this line: print('\n'.join([str((param.name,param.value,param.unit)) for param in adsk.core.Application.get().activeProduct.userParameters]))

It will print all the currently entered parameter's names, values and units (sry, old screenshot, but it shows values and units). image

The values are in the internal base unit of centimeters, as floating point numbers, with no information of the original number of decimals.

While writing this reply, I started looking some more and saw that it is possible to read the "expression":

print('\n'.join([str((param.name,param.value,param.unit,param.expression)) for param in adsk.core.Application.get().activeProduct.userParameters]))

image

I guess if the expression is split by the space character (" "), the add-in could get the first part. Two complications:

  • Formulas won't show up as a number, but rather the formula instead (e.g. b + 10 mm).
  • No formatting can be done ({a:.2f}) as the value will be read as a text string, not a number.

Funnily, parameter.expr is already part of the add-in, but it includes the whole string, not the first number up to the first space.

Soo, maybe this can be done.

Third, what has been done so far:

As a "sane" fallback, I just round numbers to 10 digits (trailing zeros get dropped, so 42 becomes 42 for example) Issue: https://github.com/thomasa88/ParametricText/issues/11

thomasa88 avatar Oct 26 '22 05:10 thomasa88

Thanks for the exhaustive explanation! The suggested workaround works perfectly for me, so as far as I am concerned, nothing more needs to be done - close it if you wish For anyone else coming across this issue, and a little confused by the formatting system (Took be a bit to wrap my head around it, you need to use 10g, eg {MyParameter:.10g}

evilC avatar Oct 27 '22 14:10 evilC

TODO: Investigate g. Add to docs.

Reference: https://docs.python.org/3.4/library/string.html#format-specification-mini-language

thomasa88 avatar Dec 02 '22 05:12 thomasa88

I tried to explain g in the documentation, but it is quite complex, as it switches to scientific notation if the numbers get many digits. So I will leave the documentation as it is for now.

If there are more problem reports in this area, I might look into cutting up the expression text or doing something smarter when processing the numbers.

thomasa88 avatar Jan 25 '24 06:01 thomasa88