Writing a subroutine or loop in iLogic rule

Writing a subroutine or loop in iLogic rule

jfenter
Enthusiast Enthusiast
1,007 Views
9 Replies
Message 1 of 10

Writing a subroutine or loop in iLogic rule

jfenter
Enthusiast
Enthusiast

I have the start of a code that needs to repeat 4 times.  I can recognize the need for a loop, but I don't know how to write it. 

 

'Rule to position Block 1
preset1 = (railWidth + (blockWidth / 2) - oversize)
preset2 = length - railWidth - blockWidth / 2 + oversize
preset3 = innerRail1 - blockWidth / 2 - railWidth / 2
preset4 = innerRail1 + blockWidth / 2 + railWidth / 2
preset5 = innerRail2 - blockWidth / 2 - railWidth / 2
preset6 = innerRail2 + blockWidth / 2 + railWidth / 2


Select Case railCount
Case 0
	If block1position = 0 Then
		Feature.IsActive("Block1") = False
	ElseIf block1position = 1 Then
		Feature.IsActive("Block1") = True
		block1 = preset1
	ElseIf block1position = 2 Then
		Feature.IsActive("Block1") = True
		block1 = preset2	
	ElseIf block1position > 2 And block1position < preset1 Then
		MessageBox.Show("Block position not available in this configuration", "Block Position Error")
		Feature.IsActive("Block1") = False
	ElseIf block1position >= preset1 Then
		Feature.IsActive("Block1") = True
		block1 = block1position
	End If
		
Case 1
	If block1position = 0 Then
		Feature.IsActive("Block1") = False
	ElseIf block1position = 1 Then
		Feature.IsActive("Block1") = True
		block1 = preset1
	ElseIf block1position = 2 Then
		Feature.IsActive("Block1") = True
		block1 = preset2
	ElseIf block1position = 3 Then
		Feature.IsActive("Block1") = True
		block1 = preset3
	ElseIf block1position = 4 Then
		Feature.IsActive("Block1") = True
		block1 = preset4
	ElseIf block1position > 4 And block1position < preset1 Then
		MessageBox.Show("Block position not available in this configuration", "Block Position Error")
		Feature.IsActive("Block1") = False
	ElseIf block1position >= preset1 Then
		Feature.IsActive("Block1") = True
			block1 = block1position
	End If
		
Case 2
	If block1position = 0 Then
		Feature.IsActive("Block1") = False
	ElseIf block1position = 1 Then
		Feature.IsActive("Block1") = True
		block1 = preset1
	ElseIf block1position = 2 Then
		Feature.IsActive("Block1") = True
		block1 = preset2
	ElseIf block1position = 3 Then
		Feature.IsActive("Block1") = True
		block1 = preset3
	ElseIf block1position = 4 Then
		Feature.IsActive("Block1") = True
		block1 = preset4
	ElseIf block1position = 5 Then
		Feature.IsActive("Block1") = True
		block1 = preset5
	ElseIf block1position = 6 Then
		Feature.IsActive("Block1") = True
		block1 = preset6
	ElseIf block1position > 6 And block1position < preset1 Then
		MessageBox.Show("Block position not available in this configuration", "Block Position Error")
		Feature.IsActive("Block1") = False
	ElseIf block1position >= preset1 Then
		Feature.IsActive("Block1") = True
		block1 = block1position
	End If
End Select

There are 4 blocks that will be placed, so the above code needs to run for Block1, Block2, Block3 and Block4.  I assume I need to dimension the integer for the blocks and possibly also dimension the integer for the preset positions.  I think this should be an easy code to write, but I just don't know where to start.  Any help greatly appreciated.

0 Likes
1,008 Views
9 Replies
Replies (9)
Message 2 of 10

Anonymous
Not applicable
Do while ... <>= ...
coding
Loop
0 Likes
Message 3 of 10

Curtis_Waguespack
Consultant
Consultant

Hi @jfenter 

 

You could do something like this example.

 

In this case this simple example is just setting the OD parameter in each component (Block1, Block2, Block3 and Block4), but it shows how you hand the component name and parameter value from the main sub to the subroutine named "DoStuff" .

 

Sub Main

	For i = 1 To 4
	
		'assemble name string
		sName = "Block" & i	
		
		'get current OD and add 1.125
		dOD = Parameter(sName, "OD") + 1.125 
	
		'call sub routine, sending name and new OD value to the sub
		Call DoStuff (sName, dOD)		
		
	Next i
	
	InventorVb.DocumentUpdate()
End Sub


Sub DoStuff (sName As String, dOD As Double)

	'update the OD parameter in the component
	'that matches the name string
	Parameter(sName, "OD") = dOD

End Sub

EESignature

0 Likes
Message 4 of 10

jfenter
Enthusiast
Enthusiast

I'll give that a try.  I've been working toward something similar, but can't get it to work properly.  This is what I've got so far:

 

'Rule to position Block 1
preset1 = (railWidth + (blockWidth / 2) - oversize)
preset2 = length - railWidth - blockWidth / 2 + oversize
preset3 = innerRail1 - blockWidth / 2 - railWidth / 2
preset4 = innerRail1 + blockWidth / 2 + railWidth / 2
preset5 = innerRail2 - blockWidth / 2 - railWidth / 2
preset6 = innerRail2 + blockWidth / 2 + railWidth / 2

'*** Define Block Features
Dim blockfeature(4) As Object
blockfeature(1) = Feature.IsActive("Block1")
blockfeature(2) = Feature.IsActive("Block2")
blockfeature(3) = Feature.IsActive("Block3")
blockfeature(4) = Feature.IsActive("Block4")

'*** Define Block Dimension Parameter
Dim block(4) As Object
block(1) = block1
block(2) = block2
block(3) = block3
block(4) = block4

'*** Define Block Position Variable
Dim blockposition(4) As Double
blockposition(1) = block1position
blockposition(2) = block2position
blockposition(3) = block3position
blockposition(4) = block4position

Select Case railCount
Case 0
	For i = 1 To 4
		
	If blockposition(i) = 0 Then
		blockfeature(i) = False
	ElseIf blockposition(i)= 1 Then
		blockfeature(i) = True
		block(i) = preset1
	ElseIf blockposition(i) = 2 Then
		blockfeature(i) = True
		block(i) = preset2	
	ElseIf blockposition(i) > 2 And blockposition(i) < preset1 Then
		MessageBox.Show("Block position not available in this configuration", "Block Position Error")
		blockfeature(i) = False
	ElseIf blockposition(i) >= preset1 Then
		blockfeature(i) = True
		block(i) = blockposition(i)
	End If
	
Next i

End Select
0 Likes
Message 5 of 10

Anonymous
Not applicable

Can you share your assembly so I can take a look at your definitions and where you go wrong?

0 Likes
Message 6 of 10

jfenter
Enthusiast
Enthusiast

The code I'm working on is "Rule 3".  The rule named "Block 1 Position" accomplishes what I want to do, but it needs repeated for blocks 2, 3 and 4.  I'm just looking for a way to condense the code into something easier to manage for debugging and updating later.

0 Likes
Message 7 of 10

Anonymous
Not applicable

I have a little peace of code added to your "block 1 position"

 

Here is what I added:

looking through all features

getting features with "Block"

Setting Block as a string with i attached

running the rules with Block + i

running your rule through block1 then adding +1 to string block so it becomes block2 excetra.

A messagebox as confirmation that it runs trough every block

 

Please accept as solution if you like it.

'API functions for features
Dim oFeature As PartFeature
Dim oFeatures As PartFeatures
oFeatures = ThisDoc.Document.ComponentDefinition.Features

'Rule to position Block 1
preset1 = (railWidth + (blockWidth / 2) - oversize)
preset2 = length - railWidth - blockWidth / 2 + oversize
preset3 = innerRail1 - blockWidth / 2 - railWidth / 2
preset4 = innerRail1 + blockWidth / 2 + railWidth / 2
preset5 = innerRail2 - blockWidth / 2 - railWidth / 2
preset6 = innerRail2 + blockWidth / 2 + railWidth / 2

Dim i As Integer = 1

'looks for all features containing Block and runs for Block 1 to 4
For Each oFeature In oFeatures
	If oFeature.Name.Contains("Block") = True
		Parameter.Quiet = True
		Dim Block As String = ("Block" & i)
		

Select Case railCount
Case 0
	If block1position = 0 Then
		Feature.IsActive(Block) = False
	ElseIf block1position = 1 Then
		Feature.IsActive(Block) = True
		block1 = preset1
	ElseIf block1position = 2 Then
		Feature.IsActive(Block) = True
		block1 = preset2	
	ElseIf block1position > 2 And block1position < preset1 Then
		MessageBox.Show(Block & " position not available in this configuration", "Block Position Error")
		Feature.IsActive(Block) = False
	ElseIf block1position >= preset1 Then
		Feature.IsActive(Block) = True
		block1 = block1position
	End If
		
Case 1
	If block1position = 0 Then
		Feature.IsActive(Block) = False
	ElseIf block1position = 1 Then
		Feature.IsActive(Block) = True
		block1 = preset1
	ElseIf block1position = 2 Then
		Feature.IsActive(Block) = True
		block1 = preset2
	ElseIf block1position = 3 Then
		Feature.IsActive(Block) = True
		block1 = preset3
	ElseIf block1position = 4 Then
		Feature.IsActive(Block) = True
		block1 = preset4
	ElseIf block1position > 4 And block1position < preset1 Then
		MessageBox.Show(Block & "position not available in this configuration", "Block Position Error")
		Feature.IsActive(Block) = False
	ElseIf block1position >= preset1 Then
		Feature.IsActive(Block) = True
			block1 = block1position
	End If
		
Case 2
	If block1position = 0 Then
		Feature.IsActive(Block) = False
	ElseIf block1position = 1 Then
		Feature.IsActive(Block) = True
		block1 = preset1
	ElseIf block1position = 2 Then
		Feature.IsActive(Block) = True
		block1 = preset2
	ElseIf block1position = 3 Then
		Feature.IsActive(Block) = True
		block1 = preset3
	ElseIf block1position = 4 Then
		Feature.IsActive(Block) = True
		block1 = preset4
	ElseIf block1position = 5 Then
		Feature.IsActive(Block) = True
		block1 = preset5
	ElseIf block1position = 6 Then
		Feature.IsActive(Block) = True
		block1 = preset6
	ElseIf block1position > 6 And block1position < preset1 Then
		MessageBox.Show(Block & " position not available in this configuration", "Block Position Error")
		Feature.IsActive(Block) = False
	ElseIf block1position >= preset1 Then
		Feature.IsActive(Block) = True
		block1 = block1position
	End If
End Select
MsgBox  (Block)
i = i + 1
End If
Next
0 Likes
Message 8 of 10

Anonymous
Not applicable

 

Sorry forgot the blockposition code so here is your full coding (Please note that is not fully foolproof)

'API functions for features
Dim oFeature As PartFeature
Dim oFeatures As PartFeatures
oFeatures = ThisDoc.Document.ComponentDefinition.Features

'Rule to position Block 1
preset1 = (railWidth + (blockWidth / 2) - oversize)
preset2 = length - railWidth - blockWidth / 2 + oversize
preset3 = innerRail1 - blockWidth / 2 - railWidth / 2
preset4 = innerRail1 + blockWidth / 2 + railWidth / 2
preset5 = innerRail2 - blockWidth / 2 - railWidth / 2
preset6 = innerRail2 + blockWidth / 2 + railWidth / 2

Dim i As Integer = 1

'looks for all features containing Block and runs for Block 1 to 4
For Each oFeature In oFeatures
	If oFeature.Name.Contains("Block") = True
		Parameter.Quiet = True
		Dim Block As String = ("Block" & i)
		
		Dim blockpositiontussen As String = ("Block" & i & "position")
		Dim blockposition As Double = Parameter(blockpositiontussen)
		
		MsgBox(Block)
		MsgBox(blockposition & " = " & blockpositiontussen)
		
		
Select Case railCount
Case 0
	If blockposition = 0 Then
		Feature.IsActive(Block) = False
	ElseIf blockposition = 1 Then
		Feature.IsActive(Block) = True
		Block = preset1
	ElseIf blockposition = 2 Then
		Feature.IsActive(Block) = True
		Block = preset2	
	ElseIf blockposition > 2 And blockposition < preset1 Then
		MessageBox.Show(Block & " position not available in this configuration", "Block Position Error")
		Feature.IsActive(Block) = False
	ElseIf blockposition >= preset1 Then
		Feature.IsActive(Block) = True
		Block = blockposition
	End If
		
Case 1
	If blockposition = 0 Then
		Feature.IsActive(Block) = False
	ElseIf blockposition = 1 Then
		Feature.IsActive(Block) = True
		Block = preset1
	ElseIf blockposition = 2 Then
		Feature.IsActive(Block) = True
		Block = preset2
	ElseIf blockposition = 3 Then
		Feature.IsActive(Block) = True
		Block = preset3
	ElseIf blockposition = 4 Then
		Feature.IsActive(Block) = True
		Block = preset4
	ElseIf blockposition > 4 And blockposition < preset1 Then
		MessageBox.Show(Block & "position not available in this configuration", "Block Position Error")
		Feature.IsActive(Block) = False
	ElseIf blockposition >= preset1 Then
		Feature.IsActive(Block) = True
			Block = blockposition
	End If
		
Case 2
	If blockposition = 0 Then
		Feature.IsActive(Block) = False
	ElseIf blockposition = 1 Then
		Feature.IsActive(Block) = True
		Block = preset1
	ElseIf blockposition = 2 Then
		Feature.IsActive(Block) = True
		Block = preset2
	ElseIf blockposition = 3 Then
		Feature.IsActive(Block) = True
		Block = preset3
	ElseIf blockposition = 4 Then
		Feature.IsActive(Block) = True
		Block = preset4
	ElseIf blockposition = 5 Then
		Feature.IsActive(Block) = True
		Block = preset5
	ElseIf blockposition = 6 Then
		Feature.IsActive(Block) = True
		Block = preset6
	ElseIf blockposition > 6 And blockposition < preset1 Then
		MessageBox.Show(Block & " position not available in this configuration", "Block Position Error")
		Feature.IsActive(Block) = False
	ElseIf blockposition >= preset1 Then
		Feature.IsActive(Block) = True
		Block = blockposition
	End If
End Select
i = i + 1
End If
Next
0 Likes
Message 9 of 10

jfenter
Enthusiast
Enthusiast

That looks like it will work, but I'm out of the office all week.  I will test it at the end of the week and accept your solution if it works.  Thank you very much!

0 Likes
Message 10 of 10

Anonymous
Not applicable

Did it work for you?

 

Just want to make sure it works and doesn't couse any problems for you.

0 Likes