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

Community Tip - Need to share some code when posting a question or reply? Make sure to use the "Insert code sample" menu option. Learn more! X

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

tleati
10-Marble

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:

1.png

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


This thread is inactive and closed by the PTC Community Management Team. If you would like to provide a reply and re-open this thread, please notify the moderator and reference the thread. You may also use "Start a topic" button to ask a new question. Please be sure to include what version of the PTC product you are using so another community member knowledgeable about your version may be able to assist.
22 REPLIES 22
TomU
23-Emerald IV
(To:tleati)

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.

KenFarley
21-Topaz I
(To:TomU)

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.

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!!

Hi
I was looking for such relation, I found your reply working and and understood how it works. Such relation is very complicated so I would like to write it down in my evernote diary for future refernce, yesterday I saved another note  Transcript of real numbers to string

which resembles to our case, and found another solution for this approach. It is quite simple and easy graspable.

Untitled.png

/*easy approach

Mass2=itos(MASS:FID_MASS)+"."+itos(ceil(MASS:FID_MASS*100)-floor(MASS:FID_MASS)*100)+" Kg"

Have anyone solution for weight of components in assembly for creo similar to Assembly visualization in SolidWorks.

Thanks everyone.

Kulbir Sandhu

Hi Kulbir,

thanks for your reply, your solution could be ok but my original problem was more complex: if you see the previous two posts, I had to find a script which satisfied several conditions:

original needs:

"

- 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

"

final solution 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 for this I wrote that script with that logic.

It all depends on what you want (or have) to display, in my case we have parts weighting more than 1 kg and others less, so for them is better a differentiated visualization (units kg and units g).

in addition to this there are some complications derived from the limitations of the string operators available in Creo.

bye

another simple way: Re: Getting part weights to show up in title block is totally automatic.

TomU
23-Emerald IV
(To:KenFarley)

Kenneth,


I've finally got around to testing your sample code. I noticed a couple of things.


1.) You're missing a ")" on the second line after (number + 1). It should read:

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


2.) There is a logical issue with numbers larger than 1. For example, 123,456 outputs 123,2 and 1,94 outputs 2,9.

You need to Extract the last digit in the string, not the second.

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

3.) Values that should round up, do not do so correctly. For number less than one that should round to one, no one is added. For numbers greater than one, the first ITOS will round anything decimal portion ,5 or larger up by one, but the decimal portion doesn't round up until much later. Here are some examples:

25,94 outputs 26,9 but it should be 25,9

0,994 outputs 0,00 but it should be 1,0

1,55 outputs 2,6 but it should be 1.6

Hopefully this exact code isn't in production anywhere...

KenFarley
21-Topaz I
(To:TomU)

I typed that off the cuff, just as I thought it should be. I guess I should have copied and pasted an actual example from a model file, like the following, which adds the bolt length to a screw description:

IF ismetric

description = description + ITOS ( floor ( lenunderhead ) ) + "MM"

ELSE

description = description + ITOS ( floor ( lenunderhead ) )

IF lenunderhead < 1.0

description = description + "0"

ENDIF

description = description + "." + EXTRACT ( ITOS ( floor ( 100 * ( 1 + lenunderhead - floor ( lenunderhead ) ) ) ), 2, 2 )

ENDIF

tleati
10-Marble
(To:TomU)

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

KenFarley
21-Topaz I
(To:tleati)

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.

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)

KenFarley
21-Topaz I
(To:tleati)

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.

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

thanks anyway, bye

TomU
23-Emerald IV
(To:tleati)

Sorry for the late reply. No, I'm not using MatLab. I'm using the Creo relations editor just like everyone else. The difference is, when I copy code samples in the reply window, I'm using the advanced editor's "syntax highlighting" function.

30.PNG

31.PNG

It makes whatever you type

look like this. :-)

tleati
10-Marble
(To:TomU)

Ah ok perfect, thanks a lot for the information...indeed it is more readable, above all for code sharing like in this case...

KenFarley
21-Topaz I
(To:tleati)

I looked, but I don't seem to have the "advanced editor" option. That probably means that either (a) It's offered in the newest version of the program, Creo 3, which I haven't installed, or (b) it's from an older version (I'm using Creo 2) and has been removed at some point. Either way, sad to see I don't have it. It definitely makes things more readable.

TomU
23-Emerald IV
(To:KenFarley)

No, no, no. Not in Creo, here on the Community. This website. The pictures above are from this dialog I'm typing this reply in right now.

dschenken
21-Topaz I
(To:TomU)

You got my hopes up that PTC put in an advanced editor for relations. Sigh.

dschenken
21-Topaz I
(To:tleati)

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)

That won't work, because of the idiosyncratic behavior of ITOS and the math in general:

(1) ITOS ( 0 ) = <blank string>

(2) ITOS ( 0.01 * 100 ) = ITOS ( 1 ) = "1", not what you're looking for.

I missed the previous replies, which should be marked Correct by now, as the interface didn't expand all the comments when I asked to show more comments.

Thanks for fixing that. I'm new to CAD.

Ok. Yes now the post is marked as Solved.

Top Tags