Using goto in ilogic

Using goto in ilogic

SharkDesign
Mentor Mentor
3,899 Views
2 Replies
Message 1 of 3

Using goto in ilogic

SharkDesign
Mentor
Mentor

I've used the goto command successfully in the past, but I'm having trouble across multiple subs, I assume it doesn't like this?

 

[Sub main

start: (this is where I am defining my label)

code

code

code

]

 

[Sub

code

code

goto start (this is where I want to send the code back to the label)

]

 

Basically what I'm doing is running the code from this page https://forums.autodesk.com/t5/inventor-customization/ilogic-place-a-part-and-define-rotation/m-p/89...

 

But instead of user input the xyz is defined. 

Then if a certain parameter is true it redefines the xyz and 'goto' the begging of the code to run again.

It does this 8 times depending on whether these parameters say true or false.

 

The error I am getting is that the goto label is not defined and the only reason I can see for this is that the label and the goto are in different subs but I'm not clever enough to work out how to fix this. 

  Inventor Certified Professional
0 Likes
Accepted solutions (1)
3,900 Views
2 Replies
Replies (2)
Message 2 of 3

SharkDesign
Mentor
Mentor
Class ThisRule
	
Sub Main
	
' File path to use
oPath = "path and part"

'get user input - units are cm
oX = -RobotFrameWidth/2-UprightBoxWidth
oY = 2504
oZ = ( RobotFrameDepth / 2 ul ) - 240 
oXr = 0
oYr = 90
oZr = 0
oRotateOrder = "X,Y,Z"

If B1C = None Then 
	GoTo B2C1
Else oWhere = "B1C2"
End If

'[ Place component
Start:
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition
Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
Dim oOccurrence As ComponentOccurrence


'placement co-ordinates in mm's 
oOccurrence = oAsmCompDef.Occurrences.Add(oPath, CreateMatrixRotate(oX, oY, oZ, _
	oXr, oYr, oZr, oRotateOrder))
oOccurrence.Grounded = True 
'zoom all
ThisApplication.ActiveView.Fit
']
End Sub
Function CreateMatrixRotate(X As Double, Y As Double, Z As Double, _
	XAngleDeg As Double, YAngleDeg As Double, ZAngleDeg As Double, oRotateOrder As String) As Matrix
	
	Dim XAngle As Double = XAngleDeg * (Math.PI / 180)
	Dim YAngle As Double = YAngleDeg * (Math.PI / 180)
	Dim ZAngle As Double = ZAngleDeg * (Math.PI / 180)
	
	
	Dim oTg As Inventor.TransientGeometry = ThisApplication.TransientGeometry

	Dim oMatrix As Matrix = oTg.CreateMatrix

	
	
	'ROTATE MATRIX
	
	Dim oFirstAxis As String = oRotateOrder.Split(",")(0)
	Dim oSecondAxis As String = oRotateOrder.Split(",")(1)
	Dim oThirdAxis As String = oRotateOrder.Split(",")(2)
	
	
	If oFirstAxis = "X"
		RotateAroundX(oMatrix, XAngle)
	ElseIf oFirstAxis = "Y"
		RotateAroundY(oMatrix, YAngle)
	ElseIf oFirstAxis = "Z"
		RotateAroundZ(oMatrix, ZAngle)
	End If
	
	If oSecondAxis = "X"
		RotateAroundX(oMatrix, XAngle)
	ElseIf oSecondAxis = "Y"
		RotateAroundY(oMatrix, YAngle)
	ElseIf oSecondAxis = "Z"
		RotateAroundZ(oMatrix, ZAngle)
	End If
	
	If oThirdAxis = "X"
		RotateAroundX(oMatrix, XAngle)
	ElseIf oThirdAxis = "Y"
		RotateAroundY(oMatrix, YAngle)
	ElseIf oThirdAxis = "Z"
		RotateAroundZ(oMatrix, ZAngle)
	End If
	
	'--------------------------------------------
	
	'Translate Matrix
	Dim oTranslateVector As Vector = oTg.CreateVector(X/10,Y/10,Z/10)
	oMatrix.SetTranslation(oTranslateVector, False)
	
	Return oMatrix

End Function
Sub RotateAroundX(oMatrix As Matrix, rotationAngle As Double)
Dim oRotateMatrix As Matrix = ThisApplication.TransientGeometry.CreateMatrix
		oRotateMatrix.Cell(1, 1) = 1
		oRotateMatrix.Cell(1, 2) = 0
		oRotateMatrix.Cell(1, 3) = 0
		oRotateMatrix.Cell(2, 1) = 0
		oRotateMatrix.Cell(2, 2) = Math.Cos(rotationAngle)
		oRotateMatrix.Cell(2, 3) = - Math.Sin(rotationAngle)
		oRotateMatrix.Cell(3, 1) = 0
		oRotateMatrix.Cell(3, 2) = Math.Sin(rotationAngle)
		oRotateMatrix.Cell(3, 3) = Math.Cos(rotationAngle)
		
		oMatrix.TransformBy(oRotateMatrix)

End Sub
Sub RotateAroundY(oMatrix As Matrix, rotationAngle As Double)
Dim oRotateMatrix As Matrix = ThisApplication.TransientGeometry.CreateMatrix
		oRotateMatrix.Cell(1, 1) = Math.Cos(rotationAngle)
		oRotateMatrix.Cell(1, 2) = 0
		oRotateMatrix.Cell(1, 3) = Math.Sin(rotationAngle)
		oRotateMatrix.Cell(2, 1) = 0
		oRotateMatrix.Cell(2, 2) = 1
		oRotateMatrix.Cell(2, 3) = 0
		oRotateMatrix.Cell(3, 1) = - Math.Sin(rotationAngle)
		oRotateMatrix.Cell(3, 2) = 0
		oRotateMatrix.Cell(3, 3) = Math.Cos(rotationAngle)
		
		oMatrix.TransformBy(oRotateMatrix)

End Sub
Sub RotateAroundZ(oMatrix As Inventor.Matrix, rotationAngle As Double)

		Dim oRotateMatrix As Matrix = ThisApplication.TransientGeometry.CreateMatrix
		oRotateMatrix.Cell(1, 1) = Math.Cos(rotationAngle)
		oRotateMatrix.Cell(1, 2) = - Math.Sin(rotationAngle)
		oRotateMatrix.Cell(1, 3) = 0
		oRotateMatrix.Cell(2, 1) = Math.Sin(rotationAngle)
		oRotateMatrix.Cell(2, 2) = Math.Cos(rotationAngle)
		oRotateMatrix.Cell(2, 3) = 0
		oRotateMatrix.Cell(3, 1) = 0
		oRotateMatrix.Cell(3, 2) = 0
		oRotateMatrix.Cell(3, 3) = 1
		
		oMatrix.TransformBy(oRotateMatrix)
GoTo oWhere
End Sub

End Class


'Place second cabinet
B1C2:
oZ = -( RobotFrameDepth / 2 ul ) - 240
oYr = - 90
oWhere = "B2C1"
GoTo Start

B2C1: 
etc etc

  Inventor Certified Professional
0 Likes
Message 3 of 3

JhoelForshav
Mentor
Mentor
Accepted solution

Hi @SharkDesign 

Your label has to be in the same sub as the GoTo-statement.

In the code you posted now your label isn't even in the class nor a sub...

 

I dont think using GoTo is your best option here. GoTo is almost never the best option IMO 🙂

 

What i suggest you do is this:

Dim your variables with initial values when the class is instantiated.

Put your different If-statements that determains how the variables should be set and have them call subs setting the variables.

 

If the code should run from the top, simply call the main sub from within by Main().

This has to be in an If-statement because if it always runs Main from within Main you'll get stuck in an infinity loop.

 

Here's a very simple example on how to call Main from within itself:

 

Class ThisRule
	Dim i As Integer = 1
	Sub Main
		MsgBox(i)
		i += 1
		If i <= 8
			Main()
		End If
	End Sub
End Class