'September 9, 2013
' Journal to generate an isometric TFR(Top, Front, Right) view from the current work view.
' Orient the work view to the desired "front" view and run the journal.
' The journal will orient the view to an isometric TFR view and save the view.
Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen
Imports NXOpenUI
Imports NXOpen.UF
Module Module1
Dim theSession As Session = Session.GetSession()
Dim theUfSession As UFSession = UFSession.GetUFSession()
Sub Main()
Dim theUISession As UI = UI.GetUI
Const viewPrefix As String = "ISO_"
Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()
Dim workPart As Part = theSession.Parts.Work
Dim currentView As View = workPart.Views.WorkView
Dim newViewMatrix As Matrix3x3 = IsoTfrView(currentView)
workPart.ModelingViews.WorkView.Orient(newViewMatrix)
Dim viewList As New List(Of String)
For Each tempView As View In workPart.ModelingViews
viewList.Add(tempView.Name)
Next
Dim i As Integer = 1
Dim newViewName As String = viewPrefix & i.ToString
Do Until Not viewList.Contains(newViewName) Or i = Integer.MaxValue
i += 1
newViewName = viewPrefix & i.ToString
Loop
If i = Integer.MaxValue Then
'could not save new view
Dim response As Integer
response = theUISession.NXMessageBox.Show("Error", NXMessageBox.DialogType.Error, "The custom view could not be saved")
Else
workPart.Views.SaveAs(workPart.ModelingViews.WorkView, newViewName, False, False)
End If
lw.Close()
End Sub
Function IsoTfrView(ByVal frontView As ModelingView) As Matrix3x3
'rotate view 45 degrees about view -Y axis
Dim rot1angle As Double = -Math.PI / 4
Dim rot1matrix As Matrix3x3
rot1matrix.Xx = Math.Cos(rot1angle)
rot1matrix.Xy = 0
rot1matrix.Xz = Math.Sin(rot1angle)
rot1matrix.Yx = 0
rot1matrix.Yy = 1
rot1matrix.Yz = 0
rot1matrix.Zx = -Math.Sin(rot1angle)
rot1matrix.Zy = 0
rot1matrix.Zz = Math.Cos(rot1angle)
'rotate view 35.264 degrees (arctan(sin(45)) about view X axis
Dim rot2angle As Double = Math.Atan(Math.Sin(Math.PI / 4))
Dim rot2matrix As Matrix3x3
rot2matrix.Xx = 1
rot2matrix.Xy = 0
rot2matrix.Xz = 0
rot2matrix.Yx = 0
rot2matrix.Yy = Math.Cos(rot2angle)
rot2matrix.Yz = -Math.Sin(rot2angle)
rot2matrix.Zx = 0
rot2matrix.Zy = Math.Sin(rot2angle)
rot2matrix.Zz = Math.Cos(rot2angle)
Dim newViewMatrix As Matrix3x3
newViewMatrix = MatrixMultiplication(frontView.Matrix, rot1matrix)
newViewMatrix = MatrixMultiplication(newViewMatrix, rot2matrix)
Return newViewMatrix
End Function
Function MatrixMultiplication(ByVal matrix1 As Matrix3x3, ByVal matrix2 As Matrix3x3) As Matrix3x3
Dim m1(8) As Double
Dim m2(8) As Double
Dim m3(8) As Double
Dim mAnswer As Matrix3x3
'convert Matrix3x3 structure to array of doubles
m1(0) = matrix1.Xx
m1(1) = matrix1.Xy
m1(2) = matrix1.Xz
m1(3) = matrix1.Yx
m1(4) = matrix1.Yy
m1(5) = matrix1.Yz
m1(6) = matrix1.Zx
m1(7) = matrix1.Zy
m1(8) = matrix1.Zz
m2(0) = matrix2.Xx
m2(1) = matrix2.Xy
m2(2) = matrix2.Xz
m2(3) = matrix2.Yx
m2(4) = matrix2.Yy
m2(5) = matrix2.Yz
m2(6) = matrix2.Zx
m2(7) = matrix2.Zy
m2(8) = matrix2.Zz
'call matrix multiplication function
theUfSession.Mtx3.Multiply(m1, m2, m3)
'convert array of doubles to Matrix3x3 structure
mAnswer.Xx = m3(0)
mAnswer.Xy = m3(1)
mAnswer.Xz = m3(2)
mAnswer.Yx = m3(3)
mAnswer.Yy = m3(4)
mAnswer.Yz = m3(5)
mAnswer.Zx = m3(6)
mAnswer.Zy = m3(7)
mAnswer.Zz = m3(8)
Return mAnswer
End Function
Public Function GetUnloadOption(ByVal dummy As String) As Integer
'Unloads the image when the NX session terminates
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination
'----Other unload options-------
'Unloads the image immediately after execution within NX
'GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately
'Unloads the image explicitly, via an unload dialog
'GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Explicitly
'-------------------------------
End Function
End Module