Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.Assemblies
Module Module1
Public s As Session = Session.GetSession()
Sub Main()
Dim theSession As Session = Session.GetSession()
Dim workPart As Part = s.Parts.Work
Dim wpName As String = workPart.FullPath.ToString()
Dim textFileName As String = Nothing
Dim myComponents() As TaggedObject
If SelectComponents("Select components", myComponents) = Selection.Response.Cancel Then
Return
End If
For Each tempComp As Component In myComponents
textFileName = "C:\temp\" & tempComp.Name & ".NC"
Dim outFile As IO.StreamWriter
outFile = My.Computer.FileSystem.OpenTextFileWriter(textFileName, _
False)
outFile.AutoFlush = True
outFile.WriteLine("/" & wpname)
Dim rotX As Double 'rotation about X axis
Dim rotY As Double 'rotation about Y axis
Dim rotZ As Double 'rotation about Z axis
Dim compPt As Point3d = CompPos(tempComp)
CompRot(tempComp, rotX, rotY, rotZ)
rotZ = rotZ + 180
outFile.WriteLine()
outFile.WriteLine("/" & tempComp.Name)
outFile.WriteLine("X" & compPt.X.ToString("F4") & " Y" & compPt.Y.ToString("F4") & " Z" & compPt.Z.ToString("F4") & " C" & rotZ.ToString("F4"))
outFile.Close()
'MsgBox("Text File Name: " & textFileName, MsgBoxStyle.Information)
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.SelectObjects(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