Cancel
Showing results for
Did you mean:

# Variables with if/then rules

Gears Esteemed Contributor

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?

****

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
SE2019 MP5 - Insight - Win10 - K4200
22 REPLIES 22

# Re: Variables with if/then rules

Gears Phenom

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.5B = ABS( ShaftDIa < 20 ) * 2.75HoleDia = 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

Phenom

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

# Re: Variables with if/then rules

Gears Esteemed Contributor

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
SE2019 MP5 - Insight - Win10 - K4200

# Re: Variables with if/then rules

Gears Esteemed Contributor

Fiorini,

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

Bruce Shand
SE2019 MP5 - Insight - Win10 - K4200

# Re: Variables with if/then rules

Gears Phenom

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

Legend

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
ST10 certified

# Re: Variables with if/then rules

Gears Esteemed Contributor

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

Thanks again.

Bruce Shand
SE2019 MP5 - Insight - Win10 - K4200

# Re: Variables with if/then rules

Gears Honored Contributor

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

~Tushar

www.SurfAndCode.in

# Re: Variables with if/then rules

Gears Esteemed Contributor

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