by Luke Davenport
Problem:
You are creating some clever iLogic models, but when using the ‘Feature.IsActive’ iLogic snippet to switch between different features, you are finding that your code is lengthy and repetitive.
Solution:
We can hugely reduce those dozens of lines of code by simply looping through a multi-value list and only activating one feature based on the current value of the parameter. If that is confusing – read on…
Explanation:
You’ll often have a number of possible features in a part, and need to activate only one of them depending on your required configuration. To achieve this you’d typically create a multi-value parameter, and depending on the value selected for that parameter, suppress all the features except the single feature you want.
The code for this can get quite cumbersome and lengthy – allow me to demonstrate with a quick example:
We have a Cadline logo plaque (as shown in picture above) and we need to switch between 8 different logo formats, called TypeA, TypeB, TypeC etc. These are all separate features in the model and so we’ll always have 7 of these features suppressed and just 1 of them activated. We also want the option of switching ALL the features off if we don’t want any logo at all.
So we’ve created a text parameter called ‘CadlineLogo’, we’ve made it multi-value, and we’ve set the 9 possible values to be:
TypeA, TypeB, TypeC, TypeD, TypeE, TypeF, TypeG, TypeH, NONE
(Notice the 9th possible value of ‘NONE’ to dictate suppression of all the features).
So using this parameter, In order to accomplish our feature suppression, here is the code we’d need to activate just one of these features – brace yourself – I’m going to paste the whole lot!
If CadlineLogo = "TypeA" Then
Feature.IsActive("TypeA") = True
Feature.IsActive("TypeB") = False
Feature.IsActive("TypeC") = False
Feature.IsActive("TypeD") = False
Feature.IsActive("TypeE") = False
Feature.IsActive("TypeF") = False
Feature.IsActive("TypeG") = False
Feature.IsActive("TypeH") = False
ElseIf CadlineLogo = "TypeB"
Feature.IsActive("TypeA") = False
Feature.IsActive("TypeB") = True
Feature.IsActive("TypeC") = False
Feature.IsActive("TypeD") = False
Feature.IsActive("TypeE") = False
Feature.IsActive("TypeF") = False
Feature.IsActive("TypeG") = False
Feature.IsActive("TypeH") = False
ElseIf CadlineLogo = "TypeC"
Feature.IsActive("TypeA") = False
Feature.IsActive("TypeB") = False
Feature.IsActive("TypeC") = True
Feature.IsActive("TypeD") = False
Feature.IsActive("TypeE") = False
Feature.IsActive("TypeF") = False
Feature.IsActive("TypeG") = False
Feature.IsActive("TypeH") = False
ElseIf CadlineLogo = "TypeD"
Feature.IsActive("TypeA") = False
Feature.IsActive("TypeB") = False
Feature.IsActive("TypeC") = False
Feature.IsActive("TypeD") = True
Feature.IsActive("TypeE") = False
Feature.IsActive("TypeF") = False
Feature.IsActive("TypeG") = False
Feature.IsActive("TypeH") = False
ElseIf CadlineLogo = "TypeE"
Feature.IsActive("TypeA") = False
Feature.IsActive("TypeB") = False
Feature.IsActive("TypeC") = False
Feature.IsActive("TypeD") = False
Feature.IsActive("TypeE") = True
Feature.IsActive("TypeF") = False
Feature.IsActive("TypeG") = False
Feature.IsActive("TypeH") = False
ElseIf CadlineLogo = "TypeF "
Feature.IsActive("TypeA") = False
Feature.IsActive("TypeB") = False
Feature.IsActive("TypeC") = False
Feature.IsActive("TypeD") = False
Feature.IsActive("TypeE") = False
Feature.IsActive("TypeF") = True
Feature.IsActive("TypeG") = False
Feature.IsActive("TypeH") = False
ElseIf CadlineLogo = "TypeG"
Feature.IsActive("TypeA") = False
Feature.IsActive("TypeB") = False
Feature.IsActive("TypeC") = False
Feature.IsActive("TypeD") = False
Feature.IsActive("TypeE") = False
Feature.IsActive("TypeF") = False
Feature.IsActive("TypeG") = True
Feature.IsActive("TypeH") = False
ElseIf CadlineLogo = "TypeH"
Feature.IsActive("TypeA") = False
Feature.IsActive("TypeB") = False
Feature.IsActive("TypeC") = False
Feature.IsActive("TypeD") = False
Feature.IsActive("TypeE") = False
Feature.IsActive("TypeF") = False
Feature.IsActive("TypeG") = False
Feature.IsActive("TypeH") = True
ElseIf CadlineLogo = "None"
Feature.IsActive("TypeA") = False
Feature.IsActive("TypeB") = False
Feature.IsActive("TypeC") = False
Feature.IsActive("TypeD") = False
Feature.IsActive("TypeE") = False
Feature.IsActive("TypeF") = False
Feature.IsActive("TypeG") = False
Feature.IsActive("TypeH") = False
End If
InventorVb.DocumentUpdate()
Ouch. That’s a lot of copying and pasting.
Perhaps you are using long-winded code like this? (don’t be ashamed – most iLogic users are!)
(Note: It is true that you can improve readability by using a ‘Select Case’ statement instead of an ‘If’ statement (see plenty of information online re. this). Unfortunately that doesn’t reduce all that nasty copying, pasting and editing, as the number of lines of code will remain the same)
Surely there’s something we can do to reduce the length and repetitiion of that code?
Well yes – on the important condition that we use the same names for the features as the values in our multi-value list (i.e. in our example the features should be called TypeA, TypeB etc…). If this is the case then the code can become much shorter and more elegant.
So our new code will do the following:
1) Run through all of the values in the multi-value list for the parameter “CadlineLogo”.
2) If it finds a feature with a name that matches a value in the multi-value list, but not the current value, it will suppress it (as another feature should be active).
3) If, however, it finds a feature with the same name as the current value of the parameter “CadlineLogo” it will set that feature to be active (un-suppress it).
The nice bonus with this method is that if there is no feature with the same name as the current value of the parameter (for instance if the current value of the parameter is “NONE”) then the rule will suppress ALL of the possible features.
If this is still confusing to you, don’t worry, you don’t need to understand the rule to use it. Simply:
1) Create a new part
2) Create a multivalue parameter called ‘CadlineLogo’, with values ‘FeatA’, ‘FeatB’, ‘FeatC’.
3) Create three features called ‘FeatA’, ‘FeatB’, ‘FeatC’.
4) Paste the iLogic code below into the part.
5) Change the value of ‘CadlineLogo’ parameter and see the model update.
Other benefits of this method:
1) This code should work fine with multivalue numeric parameters as well as multivalue text parameters.
2) If you need to add more potential features to switch between at a later point you won’t need to copy and paste any code in the iLogic rule. You simply need to add the new value(s) to the multivalue list for the parameter, and then make sure the new feature(s) have the same name, and are spelled correctly!
3) You can also duplicate this method any time you need to activate only one of a long list of possible features. Just replace the ‘CadlineLogo’ text in both locations in the code and you’re good to go.
Here’s the code:
' Start of iLogic code ******************************
' Create an arraylist containing the multi-value list
ParameterValues = MultiValue.List("CadlineLogo")
' Loop through all values in arraylist
For Each Value As String In ParameterValues
Try
If Value = CadlineLogo Then
Feature.IsActive(Value) = True
ElseIf Feature.IsActive(Value) = True
Feature.IsActive(Value) = False
End If
Catch
End Try
Next
InventorVb.DocumentUpdate()
' End of iLogic code ******************************
Let me know your thoughts. I’ll consider using this method on all my iLogic models in future I think!
Comments
0 comments
Please sign in to leave a comment.