Struggle to rotate occurence in context of assembly

Struggle to rotate occurence in context of assembly

MechMachineMan
Advisor Advisor
1,502 Views
2 Replies
Message 1 of 3

Struggle to rotate occurence in context of assembly

MechMachineMan
Advisor
Advisor

In order to make laying out large projects, I'm trying to make a program that will allow us to rotate ground parts with respect to an origin axis, but keep their relative position. I have tried using a couple of the matrix operations, but they are not functioning as expected.

 

As in below, I have created a dummy from vector in the context of the assembly, and then use an angle to set a dummy to vector on the same flat plane. However, every time I run this rule, the transformation instead changes the matrix to align the horizontal planes of both occurrences, whereas I just want the rotation to be as per the given orientation. (Every time I run the rule, it performs 2 rotations, instead of the 1 rotation I am looking for.)

 

Sub Main()

	Dim oDoc As Document
	oDoc = ThisApplication.ActiveDocument
	
	Dim oOcc As ComponentOccurrence

	Dim oSelectSet As SelectSet 
	oSelectSet = oDoc.SelectSet 
	
	If oSelectSet.Count = 1
		oOcc = oSelectSet.Item(1)
	Else
		MsgBox("Invalid Selection Set!" & vbLf & vbLf & "Aborting rule!")
		Exit Sub
	End If	
	
	r = InputBox("Set values of relative rotation of occurence in context of assembly:", "Rotation", "0")

	Call RotateOccurrence(oDoc, oOcc, r, "Y")

End Sub

Sub RotateOccurrence(oAsmDoc As Document, oOcc As ComponentOccurrence, oRotationDeg As Double, oAxis As String)
	Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
	
	Select Case True
		Case oAxis = "X"
			oAsmAxis = oAsmDoc.ComponentDefinition.WorkAxes.Item(1)
		Case oAxis = "Y"
			oAsmAxis = oAsmDoc.ComponentDefinition.WorkAxes.Item(2)
		Case oAxis = "Z"
			oAsmAxis = oAsmDoc.ComponentDefinition.WorkAxes.Item(3)
		Case Else
			MsgBox("Error in fectching Asm Axis")
			Exit Sub
	End Select

	oOcc.CreateGeometryProxy(oOcc.Definition.WorkPoints.Item(1), oCenterPointProxy)

	Dim oOccTransform As Matrix = oOcc.Transformation
	Dim oTransVec As Vector = oOccTransform.Translation
	
	Dim oFromVector As Vector = oTG.CreateVector(1,0,0)
	Dim oToVector As Vector = oTG.CreateVector(Math.Cos(oRotationDeg*Math.PI/180), 0, Math.Sin(oRotationDeg*Math.PI/180))
	
	oOccTransform.SetToRotateTo(oFromVector,oToVector, oAsmAxis.Line.Direction.AsVector)
	oOccTransform.SetTranslation(oTransVec, False)
	oOcc.SetTransformWithoutConstraints(oOccTransform)
End Sub

 


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Accepted solutions (1)
1,503 Views
2 Replies
Replies (2)
Message 2 of 3

MechMachineMan
Advisor
Advisor
Accepted solution

After all of the annoyance with trying to use the built in functions, I just went back to the basic math and made something that works;

Sub Main()

	Dim oDoc As Document
	oDoc = ThisApplication.ActiveDocument
	
	Dim oOcc As ComponentOccurrence

	Dim oSelectSet As SelectSet 
	oSelectSet = oDoc.SelectSet 
	
	If oSelectSet.Count = 1
		oOcc = oSelectSet.Item(1)
	Else
		MsgBox("Invalid Selection Set!" & vbLf & vbLf & "Aborting rule!")
		Exit Sub
	End If	
	
	r = InputBox("Set values of relative rotation of occurence in context of assembly:" & vbLf & "X:90 OR other format for reset", "Rotation", "X:0")
	Try
		Dim substring() As String = r.Split(":")
		Call RotateOccurrence(oDoc, oOcc, substring(1), substring(0))
	Catch
		Call RotateOccurrence(oDoc, oOcc, 0, "")
	End Try
End Sub

Sub RotateOccurrence(oAsmDoc As Document, oOcc As ComponentOccurrence, oRotationDeg As Double, oAxis As String)
	Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
	
	Dim oRx As Matrix
	oRx = oTG.CreateMatrix()
	oRx.Cell(2,2) = Math.Cos(oRotationDeg*Math.PI/180)
	oRx.Cell(2,3) = -Math.Sin(oRotationDeg*Math.PI/180)
	oRx.Cell(3,2) = Math.Sin(oRotationDeg*Math.PI/180)
	oRx.Cell(3,3) = Math.Cos(oRotationDeg*Math.PI/180)
	
	Dim oRy As Matrix
	oRy = oTG.CreateMatrix()
	oRy.Cell(1,1) = Math.Cos(oRotationDeg*Math.PI/180)
	oRy.Cell(1,3) = Math.Sin(oRotationDeg*Math.PI/180)
	oRy.Cell(3,1) = -Math.Sin(oRotationDeg*Math.PI/180)
	oRy.Cell(3,3) = Math.Cos(oRotationDeg*Math.PI/180)
	
	Dim oRz As Matrix
	oRz = oTG.CreateMatrix()
	oRz.Cell(1,1) = Math.Cos(oRotationDeg*Math.PI/180)
	oRz.Cell(1,2) = -Math.Sin(oRotationDeg*Math.PI/180)
	oRz.Cell(2,1) = Math.Sin(oRotationDeg*Math.PI/180)
	oRz.Cell(2,2) = Math.Cos(oRotationDeg*Math.PI/180)
	
	Dim oOccTransform As Matrix = oOcc.Transformation
	Dim oTransVec As Vector = oOccTransform.Translation
	
	Select Case True
		Case oAxis = "X"
			oOccTransform.PreMultiplyBy(oRx)
		Case oAxis = "Y"
			oOccTransform.PreMultiplyBy(oRy)
		Case oAxis = "Z"
			oOccTransform.PreMultiplyBy(oRz)
		Case Else
			MsgBox("Error in rotating matrix; returning to identity")
			oOccTransform.SetToIdentity
	End Select

	oOccTransform.SetTranslation(oTransVec, False)
	oOcc.SetTransformWithoutConstraints(oOccTransform)
	
	PrintMatrix(oOccTransform)
End Sub

Sub PrintMatrix(oMatrix As Matrix)

	For i = 4 To 1 Step -1
		For j = 4 To 1 Step -1
			oStr = oMatrix.Cell(i,j) & "     " & oStr
		Next
		oStr =  vbLf & oStr
	Next
	
	MsgBox(oStr)

End Sub

--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
Message 3 of 3

Anonymous
Not applicable

Very usefull piece of coding;

 

I have addapted a little but so it will work with my vba.

I also needed to calculate pi myself:

 

'calculate pi
    pi = Atn(1) * 4

I hope this helps other people as well.

0 Likes