AnsweredAssumed Answered

How to get rotation angle from Transform matrix when exceeding 180 degree?

Question asked by Chiao Yang Chang on May 29, 2018
Latest reply on Feb 24, 2019 by Jacob Corder

I use a part to rotate 200 degree in clockwise direction about Z axis.(see Fig2)

Use the following code to get orientation.

However,when the angle exceeds -180 degree, the angle I get is 160 degree rather than -200 degree.

It seems that solidworks changes direction automatically to calculate orientation when exceeding -180 degree or 180 degree.

How could I do if I want to get -200 degree information from the transform matrix?

 

==========================================================

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swMathUtil As SldWorks.MathUtility
Dim swTransform As SldWorks.MathTransform
Dim orientxform As SldWorks.MathTransform
Dim newtransform As SldWorks.MathTransform
Dim vxform As Variant
Dim swSelMgr As SldWorks.SelectionMgr
Dim swComp As SldWorks.Component2

 

Sub main()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swDraw = swModel
    Set swSelMgr = swModel.SelectionManager
    Set swMathUtil = swApp.GetMathUtility
    Set swComp = swSelMgr.GetSelectedObjectsComponent(1)

  

    Set swTransform = swComp.Transform2   

    Dim arr(9) As Double

    For a = 0 To 8
    arr(a) = swTransform.ArrayData(a)
    Next a

    Debug.Print "【Rotation Matrix】:"
    Debug.Print Math.Round(arr(0), 3) & "," & Math.Round(arr(1), 3) & "," & Math.Round(arr(2), 3)
    Debug.Print Math.Round(arr(3), 3) & "," & Math.Round(arr(4), 3) & "," & Math.Round(arr(5), 3)
    Debug.Print Math.Round(arr(6), 3) & "," & Math.Round(arr(7), 3) & "," & Math.Round(arr(8), 3)

  
    Dim R2D As Double
    R2D = 180 / 3.1415926

    
   Yrad = (-1) * arctan2(-arr(6), Math.Sqr(arr(0) * arr(0) + arr(3) * arr(3)))
   Ydeg = Yrad * R2D
  
   If Ydeg > 89.000001 And Ydeg < 90.0001 Then
   Zdeg = 0
   Xrad = (-1) * arctan2(arr(1), arr(4))
   Xdeg = Xrad * R2D
  
   ElseIf Ydeg < -89.000001 And Ydeg > -90.0001 Then
   Zdeg = 0
   Xrad = (-1) * -arctan2(arr(1), arr(4))
   Xdeg = Xrad * R2D
  
   Else
   Zrad = (-1) * arctan2(arr(3) / Math.Cos(Yrad), arr(0) / Cos(Yrad))
   Xrad = (-1) * arctan2(arr(7) / Cos(Yrad), arr(8) / Cos(Yrad))  
   Zdeg = Zrad * R2D
   Xdeg = Xrad * R2D   
   End If

      Debug.Print "【X-Y-Z fixed Angle】:"
      Debug.Print "A=" & Math.Round(Xdeg, 3) & " deg"
      Debug.Print "B=" & Math.Round(Ydeg, 3) & " deg"
      Debug.Print "C=" & Math.Round(Zdeg, 3) & " deg"    

End Sub

 

Public Function arctan2(ByVal y As Double, ByVal x As Double) As Double

Dim PI As Double
PI = 4 * Atn(1)
'Debug.Print pi

If x > 0 Then
    arctan2 = Atn(y / x)

ElseIf x = 0 Then
    If y > 0 Then
    arctan2 = PI / 2

    ElseIf y < 0 Then
    arctan2 = -PI / 2

    ElseIf y = 0 Then
    'undefined

    End If

ElseIf x < 0 Then
    If y >= 0 Then
    arctan2 = Atn(y / x) + PI

    ElseIf y < 0 Then
    arctan2 = Atn(y / x) - PI

    End If

End If

End Function

 

Public Function Arccos(x) As Double

If Round(x, 8) = 1# Then Arccos = 0#: Exit Function
If Round(x, 8) = -1# Then Arccos = PI: Exit Function
Arccos = Atn(-x / Sqr(-x * x + 1)) + 2 * Atn(1)

End Function

 

Public Function Arcsin(x As Double) As Double

If (Sqr(1 - x * x) <= 0.000000000001) And (Sqr(1 - x * x) >= -0.000000000001) Then
Arcsin = PI / 2

Else
Arcsin = Atn(x / Sqr(-x * x + 1))
End If

End Function

 

==================================================================

 

 

[Fig1]

Q1.png

 

[Fig2]

Q2.pngQ4.png

Outcomes