iLogic - Place a part and define rotation

iLogic - Place a part and define rotation

SharkDesign
Mentor Mentor
3,017 Views
10 Replies
Message 1 of 11

iLogic - Place a part and define rotation

SharkDesign
Mentor
Mentor

I've used the code from this post to insert parts into my assembly, but it doesn't allow rotation to be added.

https://forums.autodesk.com/t5/inventor-forum/place-components-using-coordinates/m-p/5962725/highlig...

 

I read a lengthy article on matrices and worked out what my matrix should look like but have no idea how to put this into my code and using the way in the document gives me an error that let and set are not supported anymore. 

 

So the question is, how do I change the rotation of the part I am inserting?

 

Thanks

(Inventor 2018)

  Inventor Certified Professional
0 Likes
Accepted solutions (2)
3,018 Views
10 Replies
Replies (10)
Message 2 of 11

SharkDesign
Mentor
Mentor

I have this code first which places but the parts face the wrong direction and would like to specify rotation. 

 

' File path to use
oPath = "location is in here /Control Panel.iam"

'[ Place component
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition
Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
Dim oMatrix As Matrix = oTG.CreateMatrix
Dim oOccurrence As ComponentOccurrence
'get user input - units are cm
oX = InputBox("Enter the X co-ordinate, in mm.", "iLogic", "0")
oY = InputBox("Enter the Y co-ordinate, in mm.", "iLogic", "0")
oZ = InputBox("Enter the Z co-ordinate, in mm.", "iLogic", "0")
'placement co-ordinates in mm's
oMatrix.SetTranslation(oTG.CreateVector(oX/10, oY/10, oZ/10)) 
oOccurrence = oAsmCompDef.Occurrences.Add(oPath, oMatrix)
oOccurrence.Grounded = True 
'zoom all
ThisApplication.ActiveView.Fit
']

I read all the matrix stuff on an Autodesk document and I think my matrix should read 

x 0 1 0

y -1 0 0

 

To get a 90 degree rotation but I have no idea how to put this into my code to get it to work. 

Probably worth noting that different parts will need different rotations. 

 

  Inventor Certified Professional
0 Likes
Message 3 of 11

JhoelForshav
Mentor
Mentor

Hi @SharkDesign 

 

I've written this function to create a matrix with rotation and translation:

 

the parameter argument oRotateOrder takes a string on the form "X,Y,Z" or maybe "Z,X,Y" depending on which axis you want to rotate around first.

Let me know if anything is not perfectly clear 🙂

 

Hope this helps!

 

Private 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)
End Sub

 

Message 4 of 11

SharkDesign
Mentor
Mentor

Thanks I will try that, but how do I use it with the first piece of code I posted. 

The first code grounds the object so the rotation code needs to come before that part but I'm not sure where to slip it in?

  Inventor Certified Professional
0 Likes
Message 5 of 11

JhoelForshav
Mentor
Mentor
Accepted solution

Something like this maybe? 🙂

Class ThisRule
Sub Main
' File path to use
oPath = "PATH HERE"

'[ Place component
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition
Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
Dim oOccurrence As ComponentOccurrence
'get user input - units are cm
oX = InputBox("Enter the X co-ordinate, in mm.", "iLogic", "0")
oY = InputBox("Enter the Y co-ordinate, in mm.", "iLogic", "0")
oZ = InputBox("Enter the Z co-ordinate, in mm.", "iLogic", "0")

oXr = InputBox("Enter the X rotation, in deg.", "iLogic", "0")
oYr = InputBox("Enter the Y rotation, in deg.", "iLogic", "0")
oZr = InputBox("Enter the Z rotation, in deg.", "iLogic", "0")

oRotateOrder = InputBox("Enter Rotationorder, in form A,B,C.", "iLogic", "X,Y,Z")

'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)
End Sub

End Class
Message 6 of 11

SharkDesign
Mentor
Mentor

Thank you, that is so complicated but I will work through and let you know how I go on. 

  Inventor Certified Professional
Message 7 of 11

JhoelForshav
Mentor
Mentor

@SharkDesign 

It's not really that complicated once you read the code. It creates an identity matrix and transforms it by standard rotation matrices with a given angle in a given order.

 

The code is quite long only because it takes some rows to define these rotation matrices. But their definitions can be found on wikipedia for example.

 

Skärmklipp.PNG

Other than that its just some conversion from degrees to radians, cm to mm etc. 🙂

Message 8 of 11

SharkDesign
Mentor
Mentor

Works perfectly! Thank you

  Inventor Certified Professional
Message 9 of 11

SharkDesign
Mentor
Mentor

Sorry another question on this. 

If the code is in one of the sub assemblies, it still inserts the part at the top level assembly. 

How do I specify that the part is inserted in the assembly that holds the code?

 

Thank you

  Inventor Certified Professional
0 Likes
Message 10 of 11

JhoelForshav
Mentor
Mentor
Accepted solution

This line:

oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

States that the componentdefinition to which you're adding the components is the active documents definition.

Change it to:

oAsmCompDef = ThisDoc.Document.ComponentDefinition

To add to the component definition of the document that the rule is in 🙂

Message 11 of 11

SharkDesign
Mentor
Mentor

Thanks! I was putting 'Thisdoc' into everything, didn't realise it was thisdoc.document.

  Inventor Certified Professional