Wednesday, December 28, 2011

Compare values with iLogic–MinofMany and MaxOfMany



There are several functions in iLogic that help you compare values and allow you to make rule statements based on the evaluation.

Let’s start with a function that lets you find the minimum (or maximum) value in a list and act on that value


I want to evaluate the length and width of this model, determine which of those values is the smallest and use that value to drive the diameter of the hole based on a particular material value.



The “gotcha” in this process is that there may be occasions where the length is the smaller value, but the reverse can also be true.

As always, there are several ways we can write the code to make the proper determination using an “IF” statement or a “CASE” statement:

If Length < Width Then
   dia = ....
Else
   dia = ....

Or perhaps

Select Case
Case Length < Width
    dia = ....

Both of the above could get us where we want to go with the given scenario but could get very complex very quickly as we throw more variables in the mix. What if we wanted to throw additional variables in the evaluation like the value for thickness or a parameter from several other features or parts?

Believe me you could end up spending a great deal of time writing out conditional statements in “long hand” only to get frustrated and confused. Remember, we want to make the material of the model part of the equation as well so we could end up with a bunch of sub arguments to account for that variable.

This situation is a perfect example for using a “MinOfMany” statement as a simple and efficient method to find the value we need.

The function syntax looks like MinOfMany( ,,, ) and returns the lesser of many arguments so the string MinOfMany(2,4,3,6,7,8) = 2 or MinOfMany(9,4,5,67,3,5) = 3 or MinOfMany(Sqrt(2), Sqrt(3), Sin(PI/2)) = 1

Conversely “MaxOfMany” would return the largest of many arguments. MaxOfMany(2,4,3,6,7,8) = 8. or MaxOfMany(9,4,5,67,3,5) = 67 or MaxOfMany(Sqrt(2), Sqrt(3), Sin(PI/ignored>/2)) = 1.73205.....

An iLogic rule contains the following code:

'''set the material iProperty
iProperties.Material = Material
'''declare a variable to use in the code
smaller_side = MinOfMany(Length, Width)
If Material = "Steel" Then
    dia = smaller_side - .25 in
ElseIf Material = "Aluminum-6061" Then
    dia = smaller_side - .5 in
End If
'''message box to clarify change to user
MessageBox.Show("Distance from Hole Edge to Edge of Part = " & (smaller_side - dia)/2, "Material Cross Section Measurement")

The first line sets the Material iProperty value to the selected value in the parameters multi-value list. The second line simply declares “smaller_side” as a variable I can use in the body of the code.

The code will now look for the material selected and change the diameter of the hole to be equal to the value of Length or Width (whichever is the minimum value) minus a hardcoded fraction.


I closed the code with a little message box to confirm the change and inform the user of the new value.This can be a useful and very powerful bit of code to compare values in parts and assemblies.


I’d love to see some examples and other ideas for MinOfMany and MaxOfMany.

Download the sample part here and use the iLogic form to make some changes.
Let me know how it works out!

3 comments: