'NXJournaling
'May 14, 2013
'Select a component, report the rotations about the X, Y, and Z axes
'that orient the component from its initial position to its current position.
'ref: eng-tips thread561-344785
Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.Assemblies
Module Module1
Sub Main()
Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()
Dim myComponents() As TaggedObject
If SelectComponents("Select components", myComponents) = Selection.Response.Cancel Then
Return
End If
For Each tempComp As Component In myComponents
lw.WriteLine(tempComp.Name)
Dim rotX As Double 'rotation about X axis
Dim rotY As Double 'rotation about Y axis
Dim rotZ As Double 'rotation about Z axis
CompRot(tempComp, rotX, rotY, rotZ)
lw.WriteLine(" rotation about X axis: " & rotX)
lw.WriteLine(" rotation about Y axis: " & rotY)
lw.WriteLine(" rotation about Z axis: " & rotZ)
lw.WriteLine(" " & CompPos(tempComp).ToString)
lw.WriteLine("")
Next
End Sub
Function SelectComponents(ByVal prompt As String, ByRef selObj() As TaggedObject) As Selection.Response
Dim theUI As UI = UI.GetUI
Dim title As String = "Select components"
Dim includeFeatures As Boolean = False
Dim keepHighlighted As Boolean = False
Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
Dim selectionMask_array(0) As Selection.MaskTriple
With selectionMask_array(0)
.Type = UFConstants.UF_component_type
.Subtype = UFConstants.UF_component_subtype
End With
Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObjects(prompt, _
title, scope, selAction, _
includeFeatures, keepHighlighted, selectionMask_array, _
selobj)
If resp = Selection.Response.Ok Then
Return Selection.Response.Ok
Else
Return Selection.Response.Cancel
End If
End Function
Sub CompRot(ByVal someComponent As Component, ByRef RX1 As Double, ByRef RY1 As Double, ByRef RZ1 As Double)
'extract euler angles from rotation matrix:
'[URL unfurl="true"]https://d3cw3dd2w32x2b.cloudfront.net/wp-content/uploads/2012/07/euler-angles.pdf[/URL]
Dim pt As Point3d
Dim RotMat As Matrix3x3
someComponent.GetPosition(pt, RotMat)
Dim c1, c2, s1 As Double
RX1 = Math.Atan2(RotMat.Yz, RotMat.Zz)
c2 = Math.Sqrt(RotMat.Xx ^ 2 + RotMat.Xy ^ 2)
RY1 = Math.Atan2(-RotMat.Xz, c2)
s1 = Math.Sin(RX1)
c1 = Math.Cos(RX1)
RZ1 = Math.Atan2(s1 * RotMat.Zx - c1 * RotMat.Yx, c1 * RotMat.Yy - s1 * RotMat.Zy)
'convert angles from radians to degrees
RX1 *= (180 / Math.PI)
RY1 *= (180 / Math.PI)
RZ1 *= (180 / Math.PI)
End Sub
Function CompPos(ByVal someComponent As Component) As Point3d
Dim pt As Point3d
Dim RotMat As Matrix3x3
someComponent.GetPosition(pt, RotMat)
Return pt
End Function
Public Function GetUnloadOption(ByVal dummy As String) As Integer
'Unloads the image when the NX session terminates
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination
End Function
End Module