Writing a subroutine or loop in iLogic rule

Writing a subroutine or loop in iLogic rule

jfenter
Enthusiast Enthusiast
4,628 Views
4 Replies
Message 1 of 5

Writing a subroutine or loop in iLogic rule

jfenter
Enthusiast
Enthusiast

I am trying to write an iLogic rule using some calculations that will derive values to be used in multiple Rectangular Patterns in a part.  Rather than re-write the math calculations for each pattern, I would like to write a sub routine or loop that would derive the values that could then be applied to the various parameters throughout the part.   The math that would be repeated in a subroutine is as follows:

 

patternWidth = width - leftOffset - rightOffset
		
	'Calculates number of horizontal holes
	iteration = patternWidth / 90
		inc = 1 'rounding increment
		roundedIteration = Floor(Round(iteration, 4) / inc) * inc 
			'Check rounded number
			'MsgBox(roundedIteration)
			
			iterationNumber = (roundedIteration - 1) 'Pattern Iteration Number of Holes
						
	'Calculates distance between pattern holes
	patternSpace = patternWidth / roundedIteration
		inc = 1 'rounding increment
		roundedSpace = Ceil(Round(patternSpace, 4) / inc) * inc 
			'Check rounded number
			'MsgBox(roundedSpace)
							
	patternWidthCenter = patternWidth / 2 + leftOffset 
	top_patternWidthCenter_odd = patternWidthCenter + (roundedSpace / 2) 
	bottom_patternWidthCenter_odd = patternWidthCenter - (roundedSpace / 2) 

 

The values derived from above would then be used in a Select Case that sets the various configurations of the part (in this case, the number of folds in a back).

 

Select Case foldNumber
Case 0
d9=leftOffset
d10=rightOffset
d32=iterationNumber
d50=roundedSpace

Case 1
d9=leftOffset
d83=rightOffset
d78=iterationNumber
d80=roundedSpace

End Select

 

The case statement continues for further variables.  My question is how to set up the top code to run as a subroutine for each variable in the case statement.  I've seen some "Sub Main" type codes in google and forum searches, but the examples I've found quickly go over my head in complexity.


I am very much a beginner in iLogic coding and have no experience using visual basic.  I hoping to find an explanation of HOW to write the program I need rather than just have someone write it for me (although that would be a big help too).


Thank you!

0 Likes
Accepted solutions (1)
4,629 Views
4 Replies
Replies (4)
Message 2 of 5

R.Mabery
Advocate
Advocate

Hi jfenter,

 

Subroutines and Functions work the same with one difference, a subroutine simply does something, a function returns a value.

 

Are your width, leftOffset, and rightOffset parameters that are changing multiple times throughout the rule?

 

Making some assumptions on what you're doing, I'd set the equation portion as a Function so that you can return back the patternWidthCentertop_patternWidthCenter_odd, & the bottom_patternWidthCenter_odd parameters for use in your Select Case blocks.

 

That's rather generic information but I hope it helps you to get started.  You're on the right track...best to use subroutines and functions when you find yourself doing the same thing over and over in your code.

 

 


Thanks,
Randy Mabery
Applications Expert
IMAGINiT Technologies
0 Likes
Message 3 of 5

jfenter
Enthusiast
Enthusiast
Yes, various parameters would be changing throughout the rule. Would I write a function for each value? How do I set up a function to return the values used later in the rule? Could you give an example?

Thanks so much!


0 Likes
Message 4 of 5

R.Mabery
Advocate
Advocate
Accepted solution

Hi jfenter,

 

Below is an example where I wrote a function called CalcHorizHole and moved the just a few calculations to it.  Everything after the msgbox command is as you had it.

This shows an example of passing arguments (patternWidth & inc) to a function and getting a value back (roundedIteration) as a double.

You may end up completely changing what / how the functions are used in your particular case but this should help you understand via example.  There's a lot of other things that can be done or done differently.  I'd suggest reading up on Functions and Subroutines.

The only way I know of returning multiple values from a single function is with Dictionary keys and values.

Hope this helps!

 

Sub Main()
	' Constants
	inc = 1 'rounding increment
	width = 360 'only here for my example
	leftOffset = 1 'only here for my example
	rightOffset = 1 'only here for my example

	' Calcs
	patternWidth = width - leftOffset - rightOffset

	' Calculates number of horizontal holes
	roundedIteration = CalcHorizHole(patternWidth, inc) ' Here I'm calling the function and passing the patternWidth & inc arguments
	msgbox(roundedIteration)
	
	iterationNumber = (roundedIteration - 1) 'Pattern Iteration Number of Holes

	' Calculates distance between pattern holes
	patternSpace = patternWidth / roundedIteration
	inc = 1 'rounding increment
	roundedSpace = Ceil(Round(patternSpace, 4) / inc) * inc 
	
	patternWidthCenter = patternWidth / 2 + leftOffset 
	top_patternWidthCenter_odd = patternWidthCenter + (roundedSpace / 2) 
	bottom_patternWidthCenter_odd = patternWidthCenter - (roundedSpace / 2) 
End Sub

Public Function CalcHorizHole(patternWidth As Double, inc as Double) As Double
	' Calculates number of horizontal holes
	iteration = patternWidth / 90
	roundedIteration = Floor(Round(iteration, 4) / inc) * inc
	Return roundedIteration
End Function

Thanks,
Randy Mabery
Applications Expert
IMAGINiT Technologies
Message 5 of 5

R.Mabery
Advocate
Advocate

Hi jfenter,

 

Did that help you?  If so, please mark as solution.  Thanks!


Thanks,
Randy Mabery
Applications Expert
IMAGINiT Technologies
0 Likes