Hello.
Does anyone know how get the orientation angle of an occurrence using VBA?
Please see attached file for reference.
Thanks in advance.
Solved! Go to Solution.
ok, first I hope you know that retriving the euler angles not always give the result you expect, but this is no error, its just the way the angles work..
here are some code to retrieve the angles:
'some globals:
Global PI As Double
Global RadToDeg As Double
Global trans() As Double
' a few helper functions...
Private Function Atan2(y, x As Double)
' returned value is in radians
If x > 0 Then
Atan2 = VBA.Atn(y / x)
ElseIf (x < 0 And y >= 0) Then
Atan2 = VBA.Atn(y / x) + PI
ElseIf (x < 0 And y < 0) Then
Atan2 = VBA.Atn(y / x) - PI
ElseIf (x = 0 And y > 0) Then
Atan2 = PI / 2
ElseIf (x = 0 And y < 0) Then
Atan2 = -PI / 2
ElseIf (x = 0 And y = 0) Then
Atan2 = 0
End If
End Function
Function ArcSin(x As Double) As Double
' returned value is in radians
ArcSin = VBA.Atn(x / VBA.Sqr(-x * x + 1))
End Function
Function ArcCos(x As Double) As Double
' returned value is in radians
ArcCos = VBA.Atn(-x / VBA.Sqr(-x * x + 1)) + 2 * VBA.Atn(1)
End Function
Private Function EulerThree()
' returned value is in radians
If (trans(8) <> 1 And trans(8) <> -1) Then
EulerThree = Atan2(trans(4) / VBA.Cos(ArcSin(trans(8))), trans(0) / VBA.Cos(ArcSin(trans(8))))
Else
EulerThree = 0
End If
End Function
Private Function EulerTwo()
' returned value is in radians
If (trans(8) <> 1 And trans(8) <> -1) Then
EulerTwo = -ArcSin(trans(8))
ElseIf (trans(8) = -1) Then
EulerTwo = PI / 2
Else
EulerTwo = -PI / 2
End If
End Function
Private Function EulerOne()
' returned value is in radians
If (trans(8) <> 1 And trans(8) <> -1) Then
EulerOne = Atan2(trans(9) / VBA.Cos(ArcSin(trans(8))), trans(10) / VBA.Cos(ArcSin(trans(8))))
ElseIf (trans(8) = -1) Then
EulerOne = Atan2(trans(1), trans(2))
Else
EulerOne = Atan2(-trans(1), -trans(2))
End If
End Function
Private Function EulerAngles()
'convert from radian to degree
Dim eulers(2) As Double
eulers(0) = EulerOne() * RadToDeg
eulers(1) = EulerTwo() * RadToDeg
eulers(2) = EulerThree() * RadToDeg
EulerAngles = eulers
End Function
'finally the function that retrieves the rotations:
Sub GetAngleOfOccurence()
Dim oDoc As Inventor.Document
Set oDoc = ThisApplication.ActiveDocument
Select Case oDoc.DocumentType
Case kAssemblyDocumentObject
' Set a reference to the first occurence in the assembly
Dim oOcc1 As ComponentOccurrence
Set oOcc1 = oDoc.ComponentDefinition.Occurrences.Item(1)
Dim oMatrix As Matrix
' Get the transform from the occurrence
Set oMatrix = oOcc1.Transformation
' insert point is in the matrix:
Call oMatrix.GetMatrixData(trans) ' get the matrix as an Array
'test if we get an array..
If (Not IsNull(trans) And IsArray(trans)) Then
Debug.Print
Debug.Print "Insert point in centimeter X, Y, Z=: "; trans(3); trans(7); trans(11)
End If
' invert matrix to get the parts transform in the assembly's coordinates
Call oMatrix.Invert
' be aware that retrieving the eulers from the matrix may not produce the result you would expect.
' you should keep to the matrix if you can.
Call oMatrix.GetMatrixData(trans) ' get the matrix as an Array
'test if we get an array..
If (Not IsNull(trans) And IsArray(trans)) Then
PI = 3.14159265358979
RadToDeg = 180 / PI
Dim result() As Double
result = EulerAngles
Debug.Print "Euler 1=: "; result(0)
Debug.Print "Euler 2=: "; result(1)
Debug.Print "Euler 3=: "; result(2)
Debug.Print
End If
End Select
End Sub
Can't find what you're looking for? Ask the community or share your knowledge.