Simplifying an IF STATEMENT

hmk999
Lounger
Posts: 31
Joined: 10 Dec 2013, 22:25

Simplifying an IF STATEMENT

Post by hmk999 »

Can anyone help me how I can simplify this code?

I have a variable called PART_NO. and there are four different part numbers. In every different part number R1, R2, R3 and R4 are always True.
Is there a better way to write the code so I don't have to keep repeating R1, R2, R3 and R4 in every new part number I add to my code? I could end up with 20 part numbers in the end.

Out of interest, this is code I am writing within a 3d modelling package called inventor using iLogic which is VB.NET
Note: R1 to R7 are all variables.

Code: Select all

If PART_NO = 317676600 Then
Feature.IsActive("R5") = False
Feature.IsActive("R6") = False
Feature.IsActive("R7") = False
Feature.IsActive("R1") = True
Feature.IsActive("R2") = True
Feature.IsActive("R3") = True
Feature.IsActive("R4") = True

ElseIf PART_NO = 318360800 Then
Feature.IsActive("R5") = False
Feature.IsActive("R7") = False
Feature.IsActive("R1") = True
Feature.IsActive("R2") = True
Feature.IsActive("R3") = True
Feature.IsActive("R4") = True
Feature.IsActive("R6") = True

ElseIf PART_NO = 321521900 Then
Feature.IsActive("R6") = False
Feature.IsActive("R7") = False
Feature.IsActive("R1") = True
Feature.IsActive("R2") = True
Feature.IsActive("R3") = True
Feature.IsActive("R4") = True
Feature.IsActive("R5") = True

ElseIf PART_NO = 321521900 Then
Feature.IsActive("R5") = False
Feature.IsActive("R6") = False
Feature.IsActive("R1") = True
Feature.IsActive("R2") = True
Feature.IsActive("R3") = True
Feature.IsActive("R4") = True
Feature.IsActive("R7") = True
End If

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Simplifying an IF STATEMENT

Post by Rudi »

Have you tried a single line loop?

For i = 1 To 4: Feature.IsActive("R" & i) = True: Next
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
HansV
Administrator
Posts: 78393
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Simplifying an IF STATEMENT

Post by HansV »

Perhaps like this?

Code: Select all

    Feature.IsActive("R1") = True
    Feature.IsActive("R2") = True
    Feature.IsActive("R3") = True
    Feature.IsActive("R4") = True
    Feature.IsActive("R5") = False
    Feature.IsActive("R6") = False
    Feature.IsActive("R7") = False
    Select Case PART_NO
        Case 317676600
            ' No need to do anything
        Case 318360800
            Feature.IsActive("R6") = True
        Case 321521900
            Feature.IsActive("R5") = True
        Case 321521900
            Feature.IsActive("R7") = True
    End Select
Best wishes,
Hans

hmk999
Lounger
Posts: 31
Joined: 10 Dec 2013, 22:25

Re: Simplifying an IF STATEMENT

Post by hmk999 »

Thank you Hans I will try this case option and see how I get on.

Rudi, Can you show me how the code would look if I used the single line loop option? I'm unsure where this would fit in my code.

Thanks again
hmk999

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Simplifying an IF STATEMENT

Post by Rudi »

It would look like this, assuming that syntax in iLogic is similar to VB...

Code: Select all

Dim i as Integer

If PART_NO = 317676600 Then
Feature.IsActive("R5") = False
Feature.IsActive("R6") = False
Feature.IsActive("R7") = False
For i = 1 To 4: Feature.IsActive("R" & i) = True: Next

ElseIf PART_NO = 318360800 Then
Feature.IsActive("R5") = False
Feature.IsActive("R7") = False
For i = 1 To 4: Feature.IsActive("R" & i) = True: Next
Feature.IsActive("R6") = True

etc...
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

hmk999
Lounger
Posts: 31
Joined: 10 Dec 2013, 22:25

Re: Simplifying an IF STATEMENT

Post by hmk999 »

Thanks Rudi and Hans I can look into both options.

I will let you know how I get on.

Much appreciated.

hmk999

hmk999
Lounger
Posts: 31
Joined: 10 Dec 2013, 22:25

Re: Simplifying an IF STATEMENT

Post by hmk999 »

Hi, I've been trying the Select Case option with a similar exercise but I'm getting the following error in my program. Can you see whats wrong? Listed errors are below the code.

Code: Select all

Feature.IsActive("Rev1") = False
	Feature.IsActive("Rev10") = False
	Feature.IsActive("Rev13") = False
	Feature.IsActive("Rev4") = False
	Feature.IsActive("Rev7") = False
	Feature.IsActive("Rev2") = False
	Feature.IsActive("Rev11") = False
	Feature.IsActive("Rev14") = False
	Feature.IsActive("Rev5") = False
	Feature.IsActive("Rev8") = False
	Feature.IsActive("Rev3") = True
	Feature.IsActive("Rev12") = True
	Feature.IsActive("Rev15") = True
	Feature.IsActive("Rev6") = True
	Feature.IsActive("Rev9") = True

Select Case ROTOR_SHAFT 111
	Feature.IsActive("Rev1") = True
	Feature.IsActive("Rev10") = True
	Feature.IsActive("Rev13") = True
	Feature.IsActive("Rev4") = True
	Feature.IsActive("Rev7") = True
	Feature.IsActive("Rev3") = False
	Feature.IsActive("Rev12") = False
	Feature.IsActive("Rev15") = False
	Feature.IsActive("Rev6") = False
	Feature.IsActive("Rev9") = False

Case 222
	Feature.IsActive("Rev2") = True
	Feature.IsActive("Rev11") = True
	Feature.IsActive("Rev14") = True
	Feature.IsActive("Rev5") = True
	Feature.IsActive("Rev8") = True
	Feature.IsActive("Rev3") = False
	Feature.IsActive("Rev12") = False
	Feature.IsActive("Rev15") = False
	Feature.IsActive("Rev6") = False
	Feature.IsActive("Rev9") = False
End Select



Listed errors

Error on Line 17 : End of statement expected.
Error on Line 18 : Statements and labels are not valid between 'Select Case' and first 'Case'.
Error on Line 19 : Statements and labels are not valid between 'Select Case' and first 'Case'.
Error on Line 20 : Statements and labels are not valid between 'Select Case' and first 'Case'.
Error on Line 21 : Statements and labels are not valid between 'Select Case' and first 'Case'.
Error on Line 22 : Statements and labels are not valid between 'Select Case' and first 'Case'.
Error on Line 23 : Statements and labels are not valid between 'Select Case' and first 'Case'.
Error on Line 24 : Statements and labels are not valid between 'Select Case' and first 'Case'.
Error on Line 25 : Statements and labels are not valid between 'Select Case' and first 'Case'.
Error on Line 26 : Statements and labels are not valid between 'Select Case' and first 'Case'.
Error on Line 27 : Statements and labels are not valid between 'Select Case' and first 'Case'.

Thanks
hmk999

User avatar
HansV
Administrator
Posts: 78393
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Simplifying an IF STATEMENT

Post by HansV »

Change

Code: Select all

Select Case ROTOR_SHAFT 111
to

Code: Select all

Select Case ROTOR_SHAFT
Case 111
Best wishes,
Hans

hmk999
Lounger
Posts: 31
Joined: 10 Dec 2013, 22:25

Re: Simplifying an IF STATEMENT

Post by hmk999 »

Thank you Hans thats working everytime now.

Much appreciated

hmk999

User avatar
HansV
Administrator
Posts: 78393
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Simplifying an IF STATEMENT

Post by HansV »

The complete syntax of Select Case is

Code: Select all

   Select Case expression
        Case value1
            ' Instructions to execute if expression equals value1
        Case value2
            ' Instructions to execute if expression equals value2
        ...
        Case Else
            ' Instructions to execute if expression equals none of the above values
    End Select
The Case Else part is not required, you can omit it if you wish.
Instead of a single value, you can also specify a list of values:

Code: Select all

        Case value1, value2, value3
or a range of values:

Code: Select all

        Case value1 To value2
or expressions with =, <>, >, >=, < or <= prefixed by Is:

Code: Select all

        Case Is >= value1
These variations can be combined:

Code: Select all

        Case 3, 5, 8 To 10, Is >= 15
This example specifies the values 3, 5, 8, 9, 10, 15, 16, 17, ...
Best wishes,
Hans

hmk999
Lounger
Posts: 31
Joined: 10 Dec 2013, 22:25

Re: Simplifying an IF STATEMENT

Post by hmk999 »

Thats a really big help with your explanation Hans.

Thanks very much

hmk999

hmk999
Lounger
Posts: 31
Joined: 10 Dec 2013, 22:25

Re: Simplifying an IF STATEMENT

Post by hmk999 »

Hi Hans.......... i have just one more question on this topic.

Basically i have finished my program that works everytime thanks to your help.

Can you have a look at my code, my question is............. Can the code be improved even further as my aim is to reduce the amount of code.

Let me know your thoughts

Reards
hmk999

Code: Select all

'SUPPRESS AND UNSUPPRESS RADIUS AND REVOLUTION FEATURES.

'STANDARD FEATURES
'[
	Feature.IsActive("R11") = True	'STANDARD BARREL RADIUS
	Feature.IsActive("R12") = True	'STANDARD BARREL RADIUS
	Feature.IsActive("R13") = True	'STANDARD BARREL RADIUS
	Feature.IsActive("R14") = True	'STANDARD BARREL RADIUS
	Feature.IsActive("R15") = True	'STANDARD BARREL RADIUS
	Feature.IsActive("R16") = True	'STANDARD BARREL RADIUS
	Feature.IsActive("R17") = True	'STANDARD BARREL RADIUS
	Feature.IsActive("R18") = True	'STANDARD BARREL RADIUS
	Feature.IsActive("R19") = True	'STANDARD BARREL RADIUS
	Feature.IsActive("R20") = True	'STANDARD BARREL RADIUS
	Feature.IsActive("R21") = True	'STANDARD BARREL RADIUS
	Feature.IsActive("R22") = True	'STANDARD BARREL RADIUS
	
	Feature.IsActive("Revolution1") = False
	Feature.IsActive("Revolution2") = False
	Feature.IsActive("Revolution3") = False
	Feature.IsActive("Revolution4") = False
	Feature.IsActive("Revolution5") = False
	Feature.IsActive("Revolution6") = False
	Feature.IsActive("Revolution7") = False

	Feature.IsActive("Revolution8") = False		'PMX MAGNET COMPLETE SECTION 321628600
	Feature.IsActive("Extrusion10") = False			
	Feature.IsActive("Hole1") = False				
	Feature.IsActive("Circular Pattern1") = False	

	Feature.IsActive("Revolution9") = False
	Feature.IsActive("Revolution10") = False
	Feature.IsActive("Revolution21") = False

	Feature.IsActive("Revolution22") = False	'PMX MAGNET INDIVIDUAL SECTION 700578102
	Feature.IsActive("Extrusion24") = False
	Feature.IsActive("Hole19") = False
	Feature.IsActive("Circular Pattern10") = False

	Feature.IsActive("Revolution23") = False
	
	Feature.IsActive("R7") = False
	Feature.IsActive("R8") = False
	Feature.IsActive("R10") = False
	Feature.IsActive("R23") = False
	Feature.IsActive("R25") = False
	Feature.IsActive("R26") = False
	Feature.IsActive("R35") = False
	Feature.IsActive("R36") = False
	Feature.IsActive("R38") = False
	Feature.IsActive("R39") = False
	Feature.IsActive("R41") = False
	Feature.IsActive("R42") = False
	Feature.IsActive("R51") = False
	Feature.IsActive("R52") = False
	Feature.IsActive("R55") = False
	Feature.IsActive("R56") = False
']	


Select Case ROTOR_SHAFT_MACHINING
Case 317676600, 318360800
	Feature.IsActive("Revolution1") = True
	Feature.IsActive("Revolution4") = True
	Feature.IsActive("Revolution9") = True
	Feature.IsActive("R7") = True
	Feature.IsActive("R8") = True
	Feature.IsActive("R10") = True
	Feature.IsActive("R23") = True
	Feature.IsActive("R25") = True
	Feature.IsActive("R26") = True	

Case 321521900
	Feature.IsActive("Revolution2") = True
	Feature.IsActive("Revolution5") = True
	Feature.IsActive("Revolution10") = True
	Feature.IsActive("R35") = True
	Feature.IsActive("R36") = True
	Feature.IsActive("R38") = True
	Feature.IsActive("R39") = True
	Feature.IsActive("R41") = True
	Feature.IsActive("R42") = True
	
Case 320091200
	Feature.IsActive("Revolution3") = True
	Feature.IsActive("Revolution6") = True
	Feature.IsActive("Revolution10") = True
	Feature.IsActive("R51") = True
	Feature.IsActive("R52") = True
	Feature.IsActive("R55") = True
	Feature.IsActive("R56") = True
	
Case 318882100
	Feature.IsActive("Revolution3") = True
	Feature.IsActive("Revolution7") = True
	Feature.IsActive("Revolution10") = True
	Feature.IsActive("R51") = True
	Feature.IsActive("R52") = True
	Feature.IsActive("R55") = True
	Feature.IsActive("R56") = True
	
Case 321628600
	Feature.IsActive("Revolution3") = True
	Feature.IsActive("Revolution8") = True	'PMX MAGNET COMPLETE SECTION 321628600
	Feature.IsActive("Extrusion10") = True
	Feature.IsActive("Hole1") = True
	Feature.IsActive("Circular Pattern1") = True
	Feature.IsActive("Revolution9") = True
	Feature.IsActive("R51") = True
	Feature.IsActive("R52") = True
	Feature.IsActive("R55") = True
	Feature.IsActive("R56") = True
	
Case 701465502 
	Feature.IsActive("Revolution3") = True	
	Feature.IsActive("Revolution10") = True
	Feature.IsActive("Revolution21") = True
	Feature.IsActive("R51") = True
	Feature.IsActive("R52") = True
	Feature.IsActive("R55") = True
	Feature.IsActive("R56") = True
	
Case 700578102 
	Feature.IsActive("Revolution1") = True
	Feature.IsActive("Revolution9") = True	'PMX MAGNET INDIVIDUAL SECTION 700578102	
	Feature.IsActive("Revolution22") = True
	Feature.IsActive("Extrusion24") = True
	Feature.IsActive("Hole19") = True
	Feature.ThreadDesignation("Hole19") = "M10x1.5"
	Feature.IsActive("Circular Pattern10") = True
	Feature.IsActive("Revolution23") = True
	Feature.IsActive("R7") = True
	Feature.IsActive("R8") = True
	Feature.IsActive("R10") = True
	Feature.IsActive("R23") = True
	Feature.IsActive("R25") = True
	Feature.IsActive("R26") = True
	
Case 321681502 
	Feature.IsActive("Revolution1") = True
	Feature.IsActive("Revolution10") = True
	Feature.IsActive("Revolution21") = True
	Feature.IsActive("R7") = True
	Feature.IsActive("R8") = True
	Feature.IsActive("R10") = True
	Feature.IsActive("R23") = True
	Feature.IsActive("R25") = True
	Feature.IsActive("R26") = True
End Select

User avatar
HansV
Administrator
Posts: 78393
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Simplifying an IF STATEMENT

Post by HansV »

Add the following declarations at the beginning of the macro:

Code: Select all

   Dim i As Long
   Dim v As Variant
Here is a modified version of the first part of the code; I hope you'll be able to adapt the rest along the same lines:

Code: Select all

'STANDARD FEATURES
'[
   For i = 11 To 22
      Feature.IsActive("R" & i) = True   'STANDARD BARREL RADIUS
   Next i

   For i = 1 To 10
       Feature.IsActive("Revolution" & i) = False
   Next i
   For i = 21 To 23
       Feature.IsActive("Revolution" & i) = False
   Next i

   For Each v In Array("Extrusion10", "Hole1", "CircularPattern1", "Extrusion24", "Hole19", "Circular Pattern10")
      Feature.IsActive(v) = False
   Next v

   For Each v In Array(7, 8, 10, 23, 25, 26, 35, 36, 38, 39, 41, 42, 51, 52, 55, 56)
      Feature.IsActive("R" & v) = False
   Next v
']

Select Case ROTOR_SHAFT_MACHINING
Case 317676600, 318360800
   For Each v In Array("Revolution1", "Revolution4", "Revolution9", "R7", "R8", "R10", "R23", "R25", "R26")
      Feature.IsActive(v) = True
   Next v
Best wishes,
Hans

hmk999
Lounger
Posts: 31
Joined: 10 Dec 2013, 22:25

Re: Simplifying an IF STATEMENT

Post by hmk999 »

This is really helpful Hans........ Thank you :clapping:

Will let you know how i get on.

Regards
hmk999

hmk999
Lounger
Posts: 31
Joined: 10 Dec 2013, 22:25

Re: Simplifying an IF STATEMENT

Post by hmk999 »

Hi Hans, I've updated my code as per your example and I'm getting the following error messages when i run the solution. I dont understand what the problem is do you have Any ideas where I'm going wrong?

Rule Compile Errors in RADIUS_FEATURE_SHAFT_RULE, in Rotor Shaft - Machining.ipt

Error on Line 23 : 'Array' is a type and cannot be used as an expression.
Error on Line 27 : 'Array' is a type and cannot be used as an expression.
Error on Line 34 : 'Array' is a type and cannot be used as an expression.
Error on Line 39 : 'Array' is a type and cannot be used as an expression.
Error on Line 44 : 'Array' is a type and cannot be used as an expression.
Error on Line 49 : 'Array' is a type and cannot be used as an expression.
Error on Line 54 : 'Array' is a type and cannot be used as an expression.
Error on Line 59 : 'Array' is a type and cannot be used as an expression.
Error on Line 64 : 'Array' is a type and cannot be used as an expression.
Error on Line 69 : 'Array' is a type and cannot be used as an expression.
Error on Line 74 : 'Array' is a type and cannot be used as an expression.

Code: Select all

Dim i As Long

Dim v As Object

'STANDARD FEATURES
'[
For i = 11 To 22
Feature.IsActive("R" & i) = True 'STANDARD BARREL RADIUS
Next i

For i = 1 To 10
Feature.IsActive("Revolution" & i) = False
Next i

For i = 21 To 24
Feature.IsActive("Revolution" & i) = False
Next i 

For Each v In Array("Extrusion10", "Hole1", "Circularpattern1", "Extrusion24", "Hole19", "Circularpattern10")
Feature.IsActive(v) = False
Next v

For Each v In Array(7, 8, 10, 23, 25, 26, 35, 36, 38, 39, 41, 42, 51, 52, 55, 56)
Feature.IsActive("R" & v) = False
Next v
']

Select Case ROTOR_SHAFT_MACHINING
Case 317676600, 318360800
For Each v In Array("Revolution1", "Revolution4", "Revolution9", "R7", "R8", "R10", "R23", "R25", "26")
Feature.IsActive(v) = True
Next v

Case 321521900
For Each v In Array("Revolution2", "Revolution5", "Revolution10", "R35", "R36", "R38", "R39", "R41", "42")
Feature.IsActive(v) = True
Next v

Case 320091200
For Each v In Array("Revolution3", "Revolution6", "Revolution10", "R51", "R52", "R55", "R56")
Feature.IsActive(v) = True
Next v

Case 318882100
For Each v In Array("Revolution3", "Revolution7", "Revolution10", "R51", "R52", "R55", "R56")
Feature.IsActive(v) = True
Next v

Case 321628600	'PMX MAGNET COMPLETE SECTION
For Each v In Array("Revolution3", "Revolution8", "Extrusion10", "Hole1", "Circularpattern1", "Revolution9", "R51", "R52", "R55", "R56")
Feature.IsActive(v) = True
Next v

Case 701465502
For Each v In Array("Revolution3", "Revolution10", "Revolution21", "R51", "R52", "R55", "R56")
Feature.IsActive(v) = True
Next v

Case 700578102	'PMX MAGNET INDIVIDUAL SECTION
For Each v In Array("Revolution1", "Revolution9", "Revolution22", "Extrusion24", "Hole19", "Circularpattern10", "Revolution23", "R7", "R8", "R10", "R23", "R25", "R26")
Feature.IsActive(v) = True
Next v

Case 321681502
For Each v In Array("Revolution1", "Revolution10", "Revolution21", "R7", "R8", "R10", "R23", "R25", "R26")
Feature.IsActive(v) = True
Next v

Case 701584402
For Each v In Array("Revolution1", "Revolution4", "Revolution9", "Revolution24", "R7", "R8", "R10", "R23", "R25", "R26")
Feature.IsActive(v) = True
Next
End Select
Thanks
hmk999

User avatar
HansV
Administrator
Posts: 78393
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Simplifying an IF STATEMENT

Post by HansV »

You changed the declaration

Code: Select all

    Dim v As Variant
from my code to

Code: Select all

    Dim v As Object
That causes the problem.
Best wishes,
Hans

hmk999
Lounger
Posts: 31
Joined: 10 Dec 2013, 22:25

Re: Simplifying an IF STATEMENT

Post by hmk999 »

Hi Hans, I've changed it back to Variant but it tells me to use the object type instead.
Do you know why?

Rule Compile Errors in RADIUS_FEATURE_SHAFT_RULE, in Rotor Shaft - Machining.ipt

Error on Line 7 : 'Variant' is no longer a supported type; use the 'Object' type instead.
Error on Line 24 : 'Array' is a type and cannot be used as an expression.
Error on Line 28 : 'Array' is a type and cannot be used as an expression.
Error on Line 35 : 'Array' is a type and cannot be used as an expression.
Error on Line 40 : 'Array' is a type and cannot be used as an expression.
Error on Line 45 : 'Array' is a type and cannot be used as an expression.
Error on Line 50 : 'Array' is a type and cannot be used as an expression.
Error on Line 55 : 'Array' is a type and cannot be used as an expression.
Error on Line 60 : 'Array' is a type and cannot be used as an expression.
Error on Line 65 : 'Array' is a type and cannot be used as an expression.
Error on Line 70 : 'Array' is a type and cannot be used as an expression.
Error on Line 75 : 'Array' is a type and cannot be used as an expression.

Regards
hmk999

User avatar
HansV
Administrator
Posts: 78393
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Simplifying an IF STATEMENT

Post by HansV »

I assumed that you were using Excel VBA. Are you writing your code in VB.NET?
Best wishes,
Hans

hmk999
Lounger
Posts: 31
Joined: 10 Dec 2013, 22:25

Re: Simplifying an IF STATEMENT

Post by hmk999 »

Yes sorry the language in the software I'm using is VB.NET

Regards
hmk999

User avatar
HansV
Administrator
Posts: 78393
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Simplifying an IF STATEMENT

Post by HansV »

I don't "speak" VB.Net but does this work?

- Change the declaration of v to

Code: Select all

    Dim v As String
Change the loops that use an array of strings like this:

Code: Select all

   For Each v In {"Extrusion10", "Hole1", "CircularPattern1", "Extrusion24", "Hole19", "Circular Pattern10"}
      Feature.IsActive(v) = False
   Next v
and the loops that use numbers like this:

Code: Select all

   For Each i In {7, 8, 10, 23, 25, 26, 35, 36, 38, 39, 41, 42, 51, 52, 55, 56}
      Feature.IsActive("R" & i) = False
   Next i
Best wishes,
Hans