Reply
Solved! Go to solution

Variables with if/then rules

[ Edited ]

Using variables is it possible to have if/then rules in formulas?

For instance, if volume is > X density is Y.

 

Tushar,

In your post "VB in the Variable Table - Sky's the limit" you have an if/then/else function. What is the format of a .bas file. Not the coding itself but is there some header or footer info required? 

One of your examples:

****

Function HoleDia(ByVal ShaftDia As Double) As Double
If ShaftDia >= 20 Then
HoleDia = 4.5
ElseIf ShaftDia < 20 Then
HoleDia = 2.75
End If
End Function

****

Is that all that's needed in a .bas file?

 

 

Bruce Shand
ST9 MP8 - Insight - Win10 - K4200
18 REPLIES
Solution
Solution
Accepted by topic author bshand
‎08-26-2015 04:32 AM

Re: Variables with if/then rules

To create and IF statement just with variables you need to use the function "ABS".

 

for example ABS(X>=20) will return 1 if it's true and 0 if it's false

 

so the Tushar function will became something like:

 

A = ABS( ShaftDia >= 20 ) * 4.5
B = ABS( ShaftDIa < 20 ) * 2.75
HoleDia = A + B

 or in short:

 

HoleDia = ABS( ShaftDia >= 20 ) * 4.5 + ABS( ShaftDia < 20 ) * 2.75

 

 As you can see this is difficult to understand in a quick way, so if you need to create a lot of condition I would use the .bas solution.

 

 

Re: Variables with if/then rules

heres a presentation i did at SEU14...  where i talked thru creating a VB sub...

 

heres the youtube link that starts right at this topic:  https://www.youtube.com/watch?v=l1_z58sQA2c&feature=player_detailpage#t=1631

 

Re: Variables with if/then rules

Thanks to both of you. I will look at your video later, Matt. But I think I prefer, if possible, not needing an external .bas file to account for.

Bruce Shand
ST9 MP8 - Insight - Win10 - K4200

Re: Variables with if/then rules

Fiorini,

 

I don't understand why you add the calculated variables A and B. It's suppose to be an either/or.

Bruce Shand
ST9 MP8 - Insight - Win10 - K4200

Re: Variables with if/then rules

[ Edited ]

If Dia is >= 20 A will be 4.5 and B will be 0

If Dia is <20 A will be 0 and B will be 2.75

 

I add them to always get a valid result

 

Your original request will be something like this:

 

Density = ABS(Volume > X) * Y

 

but in this case it will be 0 if Volume is not greater than X

 

Re: Variables with if/then rules

The key is that conditional statements are made with comparitives (<, = or >) which return a 1 or 0. Correct? Are they processed first? or do you need parentheses around them?

 

There is a comments field in the variable table, this might be a good place to note what you are doing.

Using SE since V12, 2002

Re: Variables with if/then rules

OK, since one variable will be zero. I was reading it as meaning a positive number plus a positive number.

Thanks again.

Bruce Shand
ST9 MP8 - Insight - Win10 - K4200

Re: Variables with if/then rules

[ Edited ]

That's right Bruce

No header or references are needed, Solid Edge has in-built capability to read and evaluate VB syntax inside a .BAS file and older VB 6.0 editor or simply Notepad can be used to write the functions.

 

Solutions suggested by Fiorini and lking no doubt would execute faster since they reside within the Part file in Solid Edge unlike external BAS files, IMO they are difficult to interpret and maintain unless accompanied by good documentation.

.BAS files on the other hand are written in natural language, easy to read and make changes.

 

The bottom line is, IF you need a If-Then rule, THEN use expression or function that uses those keywords and nothing ELSE  Robot Happy

 

~Tushar

www.SurfAndCode.in

Re: Variables with if/then rules

Hi Matt,



scripts are a very fine possibility no doubt, but it would be better, if the script lines could be within the SE document rather than have it saved in a text file.

 

This would be a big enhancement, isn't it?

 

regards

Wolfgang

 



regards
Wolfgang