ParametricText
ParametricText copied to clipboard
Suppress decimals if not needed
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
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).
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]))
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
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}
TODO: Investigate g
. Add to docs.
Reference: https://docs.python.org/3.4/library/string.html#format-specification-mini-language
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.