' NX 1988
' Journal created by jopp on Wed Dec 15 19:13:53 2021 Mitteleuropäische Zeit

'
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports System.Collections.Generic
Imports NXOpen.Assemblies
Imports System.IO
Imports System.Xml
Imports System.Data

''' <summary>
''' Module NXJournal_NXDP_01
''' </summary>
''' <remarks> 
''' #LUp: 3.1601;28.01.2022;S.Gueth
''' </remarks>
Module NXJournal_JT_to_NX

    Public Property NxSyslog As LogFile = Nothing
    Public Property lw As ListingWindow = Nothing

    Sub Main(ByVal args() As String)

        Dim NXSession As NXOpen.Session = NXOpen.Session.GetSession()
        Dim UFSession As UFSession = UFSession.GetUFSession()

        NxSyslog = NXSession.LogFile

        ' comment in if you want to see the lsiting window in the interactiv session
        'lw = NXSession.ListingWindow
        'lw.Open()

        Try

            Dim basePart1 As NXOpen.BasePart = Nothing
            Dim partLoadStatus1 As NXOpen.PartLoadStatus = Nothing

            WirteToLogfile("args: " + String.Join(" ", args))

            ' !!!ATTENTION!!! do not change order of the arguments
            Dim JobMgr_NXTemplate_DPNE As String = args(0)
            Dim JTFile_DPNE As String = args(1)

            WirteToLogfile("JobMgr_NXTemplate_DPNE:" + JobMgr_NXTemplate_DPNE)
            WirteToLogfile("JTFile_DPNE:" + JTFile_DPNE)

            basePart1 = NXSession.Parts.OpenDisplay(JobMgr_NXTemplate_DPNE, partLoadStatus1)
            Dim workPart As NXOpen.Part = NXSession.Parts.Work
            Dim displayPart As NXOpen.Part = NXSession.Parts.Display
            partLoadStatus1.Dispose()

            Dim partImporter1 As NXOpen.PartImporter = Nothing
            partImporter1 = workPart.ImportManager.CreatePartImporter()
            partImporter1.FileName = JTFile_DPNE
            partImporter1.Scale = 1.0
            partImporter1.CreateNamedGroup = True
            partImporter1.ImportViews = False
            partImporter1.ImportCamObjects = False
            partImporter1.LayerOption = NXOpen.PartImporter.LayerOptionType.Work
            partImporter1.DestinationCoordinateSystemSpecification = NXOpen.PartImporter.DestinationCoordinateSystemSpecificationType.Work

            Dim element1 As NXOpen.Matrix3x3 = Nothing
            element1.Xx = 1.0
            element1.Xy = 0.0
            element1.Xz = 0.0
            element1.Yx = 0.0
            element1.Yy = 1.0
            element1.Yz = 0.0
            element1.Zx = 0.0
            element1.Zy = 0.0
            element1.Zz = 1.0
            Dim nXMatrix1 As NXOpen.NXMatrix = Nothing
            nXMatrix1 = workPart.NXMatrices.Create(element1)

            partImporter1.DestinationCoordinateSystem = nXMatrix1

            Dim destinationPoint1 As NXOpen.Point3d = New NXOpen.Point3d(0.0, 0.0, 0.0)
            partImporter1.DestinationPoint = destinationPoint1

            Dim nXObject1 As NXOpen.NXObject = Nothing
            nXObject1 = partImporter1.Commit()

            partImporter1.Destroy()

            Dim partSaveStatus1 As NXOpen.PartSaveStatus = Nothing
            partSaveStatus1 = workPart.Save(NXOpen.BasePart.SaveComponents.False, NXOpen.BasePart.CloseAfterSave.True)
            If partSaveStatus1.NumberUnsavedParts = 0 Then
                WirteToLogfile("Save Part: OK")
            Else
                WirteToLogfile("Save Part: ERR")
                Environment.ExitCode = 210
            End If
            partSaveStatus1.Dispose()

        Catch ex As Exception
            WirteToLogfile("EX-Msg: " + ex.Message)
            WirteToLogfile("EX: " + ex.ToString)
            Environment.ExitCode = -1
            Throw ex
        End Try

    End Sub

    ''' <summary>
    ''' function to write text to the NX Syslog FIle and the Listing Window, when it is open
    ''' </summary>
    ''' <remarks> 
    ''' #New: 3.1601;28.01.2022;S.Gueth
    ''' </remarks>
    Private Sub WirteToLogfile(text_p As String)
        NxSyslog.WriteLine("##JT_to_NX: " + text_p)
        If lw IsNot Nothing Then lw.WriteLine(text_p)
    End Sub

End Module
