' =============================================================================
' NX Journal: test ExistingPart (read-from-disk) STEP export, no open part.
'
' The last test proved the master is healthy (2 bodies, faces=3) but:
'   - DisplayPart export  -> "0 solids" (batch has no display part)
'   - ExistingPart export -> MISSING (because KC_master.prt was locked by the
'                             open Work part when we tried to read it)
'
' Hypothesis: In batch (run_journal), the ONLY reliable way to export is
'   ExistingPart + InputFile reading the SAVED .prt file, with NO part open.
'
' This script does NOT call OpenBaseDisplay. It only:
'   1. ExistingPart STEP export  -> E1_existing.stp
'   2. ExistingPart IGES export  -> E1_existing.igs
'
' Run (run_journal takes one arg; use WorkingDirectory):
'   WorkingDirectory = catalog-masters\KC
'   run_journal.exe <this file full path>
' Result log: <cwd>\test-existingpart.txt
'
' Note: English comments only (avoids GBK/BOM mojibake on no-BOM .vb files).
' =============================================================================
Option Strict Off
Imports System
Imports System.IO
Imports System.Text
Imports NXOpen
Imports NXOpen.UF

Module TestExistingPart

    Dim theSession As Session = Session.GetSession()
    Dim sb As New StringBuilder()

    Sub Log(s As String)
        sb.AppendLine(s)
    End Sub

    Sub Main()
        Try
            Run()
        Catch ex As Exception
            Log("[FATAL] " & ex.GetType().Name & " / " & ex.Message)
        End Try
        File.WriteAllText(Path.Combine(Directory.GetCurrentDirectory(), "test-existingpart.txt"), sb.ToString(), New UTF8Encoding(True))
    End Sub

    Sub Run()
        Dim cur As String = Directory.GetCurrentDirectory()
        Dim masterPath As String = Path.Combine(cur, "KC_master.prt")
        Log("CWD=" & cur)
        Log("masterExists=" & File.Exists(masterPath))
        If Not File.Exists(masterPath) Then
            Log("[FAIL] master missing: " & masterPath)
            Return
        End If

        Dim outDir As String = Path.Combine(cur, "dbg_out")
        Directory.CreateDirectory(outDir)

        ' 1) STEP via ExistingPart (reads saved file from disk, no open part)
        Dim stp As String = Path.Combine(outDir, "E1_existing.stp")
        If File.Exists(stp) Then File.Delete(stp)
        Try
            Dim sc As NXOpen.StepCreator = theSession.DexManager.CreateStepCreator()
            sc.ExportFrom = NXOpen.StepCreator.ExportFromOption.ExistingPart
            sc.InputFile = masterPath
            sc.ExportAs = NXOpen.StepCreator.ExportAsOption.Ap214
            sc.OutputFile = stp
            sc.ColorAndLayers = True
            sc.Commit()
            sc.Destroy()
            Log("E1_existing.stp size=" & FileSize(stp))
        Catch ex As Exception
            Log("STEP ExistingPart FAIL: " & ex.Message)
        End Try

        ' 2) IGES via ExistingPart
        Dim igs As String = Path.Combine(outDir, "E1_existing.igs")
        If File.Exists(igs) Then File.Delete(igs)
        Try
            Dim ic As NXOpen.IgesCreator = theSession.DexManager.CreateIgesCreator()
            ic.ExportFrom = NXOpen.IgesCreator.ExportFromOption.ExistingPart
            ic.InputFile = masterPath
            ic.ExportModelData = True
            ic.OutputFile = igs
            ic.Commit()
            ic.Destroy()
            Log("E1_existing.igs size=" & FileSize(igs))
        Catch ex As Exception
            Log("IGES ExistingPart FAIL: " & ex.Message)
        End Try

        Log("DONE")
    End Sub

    Function FileSize(p As String) As String
        If Not File.Exists(p) Then Return "MISSING"
        Return (New FileInfo(p)).Length & " bytes"
    End Function

End Module
