cancel
Showing results for
Did you mean:
cancel
Showing results for
Did you mean:

## how to report a number with conditionally-determined decimal places in a drawing

Hello,

I am experiencing a problem with reporting a number parameter (in my case a parameter of mass) with specific decimal places in a drawing.:

- I would like to plot the parameter with 1 decimal places if the mass is higher or equal to 1 kg.

- If instead the mass is lower than 1 kg, I would like to report it with 2 decimal places

I tried to set a conditional script (in the relations field) with functions like "if...ceil(),floor()" : but the problem is that in the drawing cell where I report the mass, I cannot set a conditional-formatted number of decimal places. I can set only 1 or 2, fixed: and therefore when the mass is higher than 1 kg (for example 2,6 kg) and I set 2 decimal places in the cell property window, it reports for example 2.60 kg (with a useless zero).

(and vice-versa if I set 1 dec. place with a mass lower than 1 kg, the system cuts out a decimal place).

thanks

22 REPLIES 22

## Re: how to report a number with conditionally-determined decimal places in a drawing

The only way I know to do this is to create a single cell repeat region, add the appropriate relations to convert the mass to text, and then manipulate accordingly.

The repeat region parameter will initially be set to &mdl.param.value and a "by rule" filter will need to be added for your specific mass parameter. Something like &mdl.param.name == massa. This should display only the value for the mass in the repeat region. Next, add relations to the repeat region:

```IF mdl_param_value >= 1
IF FLOOR(mdl_param_value) == FLOOR(mdl_param_value,1)
WEIGHT = ITOS(mdl_param_value) + ",0"
ELSE
WEIGHT = ITOS(FLOOR(mdl_param_value)) + "," + ITOS((FLOOR(mdl_param_value,1) - FLOOR(mdl_param_value)) * 10)
ENDIF
ELSE
IF mdl_param_value >= 0.10
WEIGHT = "0," + ITOS(FLOOR(mdl_param_value * 100))
ELSE
IF FLOOR(mdl_param_value,2) == 0
WEIGHT = "0,00"
ELSE
WEIGHT = "0,0" + ITOS(FLOOR(mdl_param_value * 100))
ENDIF
ENDIF
ENDIF
```

Finally, switch the repeat region cell parameter to point to your newly created relation parameter (WEIGHT in my example). &rpt.rel.WEIGHT

Note: The sample relations above always round down. You would need to modify them with additional IF statements if you want to round up and down.

## Re: how to report a number with conditionally-determined decimal places in a drawing

Hey, I do a lot of this type of stuff for building part numbers, descriptions and the like using dimensions. This is the slightly more tricky way I have to get floating point numbers into text strings, with decimal places the way you want them:

IF FLOOR ( number ) < 1

WEIGHT = "0," + EXTRACT ( ITOS ( 100 * ( number + 1 ), 2, 2 )

ELSE

WEIGHT = ITOS ( number ) + "," + EXTRACT ( ITOS ( 10 * number ), 2, 1 )

ENDIF

The addition of 1 to "number" in the first IF clause ensures that multiplying it by 100 gets a number of the format 1XX, so extraction of the two digits of interest is guaranteed.

As stated in Tom's reply, you've got to add a bit of logic if you want to round up or down.

What I wonder is how a program can be more than 20 years old and still not have the functionality built in for converting floating point numbers to strings.

## Re: how to report a number with conditionally-determined decimal places in a drawing

Hi,

thanks for your suggestions, they were both useful.

I inserted this script (massa_disegno is the parameter to print in the drawing):

if pro_mp_mass<1 & pro_mp_mass >= 0.0005

massa_disegno=itos(1000*pro_mp_mass) + " g"
else

if pro_mp_mass-floor(pro_mp_mass) < 0.05

massa_disegno=itos(floor(pro_mp_mass)) + ",0" + " kg"
endif

if pro_mp_mass-floor(pro_mp_mass) >= 0.95
massa_disegno=itos(ceil(pro_mp_mass)) + ",0" + " kg"
else

massa_disegno=itos(floor(pro_mp_mass)) + ","+ itos(10*(pro_mp_mass-floor(pro_mp_mass))) + " kg"
endif

if pro_mp_mass < 0.0005
massa_disegno="< 1 g"
endif

endif

Therefore at the end we decided to print the mass with this logic:

- part with mass lower than 1 g as --> " < 1 g"

- part with mass lower than 1 kg but higher than 1 gram as --> " xxx g "

- part with mass higher than 1 kg as --> " xxx,x kg "

and the script works!!

and...yes it's incredible how Creo is missing a function like this too, and above all there is no possibility to use "else if" so I have to make strange nestings!!

## Re: how to report a number with conditionally-determined decimal places in a drawing

Hi,

just for curiosity, you wrote the code in Matlab didn't you?

because if not, I guess it'd be more comfortable to use that interface in Creo instead of the default relation panel...

Bye

## Re: how to report a number with conditionally-determined decimal places in a drawing

No, we don't use MatLab. I wrote that stuff in the relations part of Creo. And yes, as a programming language, it's pretty primitive, about the level of Fortran.

## Re: how to report a number with conditionally-determined decimal places in a drawing

And so how did you manage to have that interface in the relations panel? (I was saying the contrary, that is it's more comfortable the interface you are using)

## Re: how to report a number with conditionally-determined decimal places in a drawing

Oh, I think you meant to be asking Tom. I'd say he *is* using something else to write equations. Probably MatLab, but I don't know. Sorry.

## Re: how to report a number with conditionally-determined decimal places in a drawing

yes I first asked him but probably he didn't see it yet...

thanks anyway, bye

## Re: how to report a number with conditionally-determined decimal places in a drawing

PTC doesn't have conditional formatting. It also doesn't have a Real->String conversion, though it does have ITOS, so you could craft a set of relations to convert the Real to Whole / Fraction to String.

I think this should work, though some more effort may be required as I don't know what PTC does with INT->REAL conversions; if it truncates this works ok; if it rounds, then some extra work is required:

/*WHOLE and FRACTION are type INTEGER, MASSTEXT is type STRING

WHOLE = MASS

IF WHOLE > 1

FRACTION = (MASS-WHOLE)*10

ELSE

FRACTION =(MASS-WHOLE)*100

ENDIF

MASSTEXT = ITOS(WHOLE)+"."+ITOS(FRACTION)

Announcements