How to Rotate an Assembly around its own Axis

How to Rotate an Assembly around its own Axis

muddassir01
Participant Participant
515 Views
4 Replies
Message 1 of 5

How to Rotate an Assembly around its own Axis

muddassir01
Participant
Participant

Hello everyone,

I have an assembly that I want to rotate around its Z-axis by 90 degrees. I’ve written an iLogic code that rotates each component, but I’m facing an issue: some components are rotating while others are not.

What could be the possible reason for this behavior? Are there any changes or improvements I should make to the code?

Any suggestions would be highly appreciated.

Best regards,
Muddassir

 

 

Dim dPi As Double
dPi = Math.PI ' Define π (Pi)
Dim oAsmDoc As AssemblyDocument = ThisDoc.Document
Dim oTG As TransientGeometry
oTG = ThisApplication.TransientGeometry
Dim oTransMatrix As Matrix
oTransMatrix = oTG.CreateMatrix
oTransMatrix.SetToRotation(90 * (dPi / 180), oTG.CreateVector(0, 0, 1), oTG.CreatePoint(0, 0, 0))
Dim matrixString As String = "Matrix Values:" & vbCrLf
For row As Integer = 1 To 3
	For col As Integer = 1 To 3
		matrixString &= Math.Round(oTransMatrix.Cell(row, col), 3).ToString() & vbTab
	Next
	matrixString &= vbCrLf
Next
MessageBox.Show(matrixString)
Dim i As Integer = 1
Dim oOcc As ComponentOccurrence
For Each oOcc In oAsmDoc.ComponentDefinition.Occurrences
	If oOcc.Suppressed = False Then
		Dim oMatrix As Matrix
		oMatrix = oOcc.Transformation
		If oOcc.Grounded Then
			oOcc.Grounded = False
		End If
		oMatrix.PreMultiplyBy(oTransMatrix)
		i += 1
		oOcc.Transformation = oMatrix
		Logger.Error("ROTATED :: " & oOcc.Name)
		Dim matrixString1 As String = "Matrix Values:" & vbCrLf
		For row As Integer = 1 To 3
			For col As Integer = 1 To 3
				matrixString1 &= Math.Round(oMatrix.Cell(row, col), 3).ToString() & vbTab
			Next
			matrixString1 &= vbCrLf
		Next
		Logger.Info(matrixString1)
	End If
Next
Logger.Info(i)
0 Likes
516 Views
4 Replies
Replies (4)
Message 2 of 5

Michael.Navara
Advisor
Advisor

It is hard to imagine what do you try to do.

If you use a mates in the assembly, it is possible the mates update position of the occurrence.

Manually you can create temp assembly, move all components to it, rotate temp assembly, move all components back to top and delete temp assembly. (I'm not sure there is some API for it)

0 Likes
Message 3 of 5

WCrihfield
Mentor
Mentor

Just a thought, but what about using the AssemblyComponentDefinition.TransformOccurrences method.  I can not remember using it before myself, but it sounds pretty fitting for what you are trying to do here.  It says you can either specify one matrix for each occurrence in the collection, or just one matrix, to transform them all the same way.  It also has a setting to 'IgnoreConstraints',  but it also says that updating the assembly will put the constraints back into effect, so...

 

Edit:  Also, I think that maybe the 'axis' and 'point' you are specifying within our SetToRotation might not be appropriate.  I just created a small test iLogic rule that lets me 'Pick' a component, then I get the actual Z-Axis and actual 'Origin' point from the definition of the assembly, and their underlying transient objects to use in that method.  When I did that, it seemed to work just find.  It rotated that component around the assembly's Z-axis 90 degrees.  Below is the quickie code I just typed up to test it.

Dim oPickedOcc As ComponentOccurrence = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAssemblyOccurrenceFilter, "Select a Component.")
If oPickedOcc Is Nothing Then Return
Dim oRotationMatrix As Matrix = ThisApplication.TransientGeometry.CreateMatrix()
Dim dAngle As Double = (90 * (Math.PI / 180))
Dim oAxisVector As Inventor.Vector = oPickedOcc.Parent.WorkAxes.Item(3).Line.Direction.AsVector
Dim oCenterPoint As Inventor.Point = oPickedOcc.Parent.WorkPoints.Item(1).Point
oRotationMatrix.SetToRotation(dAngle, oAxisVector, oCenterPoint)
Dim oTM As Matrix = oPickedOcc.Transformation.Copy
oTM.PreMultiplyBy(oRotationMatrix)
oPickedOcc.Transformation = oTM
oPickedOcc.Parent.Document.Update

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 5

muddassir01
Participant
Participant

muddassir01_0-1729486517434.png

Basically, I want to rotate my assembly like this. It is somewhat similar to this image. I want the blade 1 and 3 (Essentially horizontal pair) to move to the position of 2 and 4. This should be done through complete assembly rotation as there are many other configurations of assembly and I want to apply changes only for this. 

0 Likes
Message 5 of 5

muddassir01
Participant
Participant

Hi @WCrihfield,

Thank you for your suggestion. 

I figured out that the issue was with the mates. Mates were not allowing the components to rotate/move to new position.  Added a few lines and now assembly is behaving as expected. 

Dim constraint As AssemblyConstraint
For Each constraint In oAsmDoc.ComponentDefinition.Constraints
	If constraint.Suppressed = False
		constraint.Suppressed = True
	End If
Next

 

Now the issue is components are free in assembly, and I want to reapply constraints. I there anyway that I can apply mates at new position.

 

Best Regards