' =============================================================================
' NX Journal: test StepCreator flags that likely cause "0 solids" in batch
'
' The master is healthy (2 bodies, faces=3) but DisplayPart export gives 0
' solids. Prime suspects are StepCreator properties whose batch-mode defaults
' differ from interactive mode:
'
'   1. ExportSolidsAndSurfacesAs  (Precise vs Tessellated)  <-- TOP suspect
'      Tessellated needs display facets; batch has no rendering -> 0 solids.
'   2. ProcessHoldFlag  (if True, translator may hold and not produce)
'   3. FileSaveFlag
'
' This script dumps the defaults of these properties, then tries exporting with
' ExportSolidsAndSurfacesAs = Precise (and, for contrast, Tessellated).
'
' Run: WorkingDirectory = catalog-masters\KC; run_journal.exe <this file>
' Result: <cwd>\test-fix-flags.txt   Products: <cwd>\dbg_out\
'
' 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 TestFixFlags

    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-fix-flags.txt"), sb.ToString(), New UTF8Encoding(True))
    End Sub

    Function CountBodies(w As Part) As Integer
        Dim n As Integer = 0
        Try
            For Each b As NXOpen.Body In w.Bodies
                n += 1
            Next
        Catch
        End Try
        Return n
    End Function

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

    Sub Run()
        Dim cur As String = Directory.GetCurrentDirectory()
        Dim masterPath As String = Path.Combine(cur, "KC_master.prt")
        Log("masterExists=" & File.Exists(masterPath))
        If Not File.Exists(masterPath) Then Return

        Dim pls As NXOpen.PartLoadStatus = Nothing
        theSession.Parts.OpenBaseDisplay(masterPath, pls)
        If pls IsNot Nothing Then pls.Dispose()
        Dim w As Part = theSession.Parts.Work
        If w Is Nothing Then
            Log("[FAIL] Work=NULL")
            Return
        End If
        Log("bodies=" & CountBodies(w))

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

        ' ---- dump defaults of suspect properties ----
        Dim sc0 As NXOpen.StepCreator = theSession.DexManager.CreateStepCreator()
        Log("--- StepCreator defaults ---")
        Try : Log("ProcessHoldFlag=" & sc0.ProcessHoldFlag) : Catch ex As Exception : Log("ProcessHoldFlag throws: " & ex.Message) : End Try
        Try : Log("FileSaveFlag=" & sc0.FileSaveFlag) : Catch ex As Exception : Log("FileSaveFlag throws: " & ex.Message) : End Try
        Try : Log("ColorAndLayers=" & sc0.ColorAndLayers) : Catch ex As Exception : Log("ColorAndLayers throws: " & ex.Message) : End Try
        Try : Log("ExportFrom=" & sc0.ExportFrom.ToString()) : Catch ex As Exception : Log("ExportFrom throws: " & ex.Message) : End Try
        Try : Log("ExportAs=" & sc0.ExportAs.ToString()) : Catch ex As Exception : Log("ExportAs throws: " & ex.Message) : End Try
        Try : Log("ExportSolidsAndSurfacesAs=" & sc0.ExportSolidsAndSurfacesAs.ToString()) : Catch ex As Exception : Log("ESSAs throws: " & ex.Message) : End Try
        Try : Log("ReferenceType=" & sc0.ReferenceType.ToString()) : Catch ex As Exception : Log("ReferenceType throws: " & ex.Message) : End Try
        sc0.Destroy()

        ' ---- Attempt 1: DisplayPart + ESSAs=Precise (TOP hypothesis) ----
        Dim f1 As String = Path.Combine(outDir, "F1_display_precise.stp")
        If File.Exists(f1) Then File.Delete(f1)
        Try
            Dim sc As NXOpen.StepCreator = theSession.DexManager.CreateStepCreator()
            sc.ExportFrom = NXOpen.StepCreator.ExportFromOption.DisplayPart
            sc.ExportAs = NXOpen.StepCreator.ExportAsOption.Ap214
            sc.OutputFile = f1
            sc.ColorAndLayers = True
            Try : sc.ExportSolidsAndSurfacesAs = NXOpen.StepCreator.ExportSolidsAndSurfacesAsOption.Precise : Catch ex As Exception : Log("set ESSAs=Precise: " & ex.Message) : End Try
            sc.Commit()
            sc.Destroy()
            Log("F1_display_precise.stp size=" & FileSize(f1))
        Catch ex As Exception
            Log("F1 FAIL: " & ex.Message)
        End Try

        ' ---- Attempt 2: DisplayPart + ESSAs=Tessellated (contrast) ----
        Dim f2 As String = Path.Combine(outDir, "F2_display_tess.stp")
        If File.Exists(f2) Then File.Delete(f2)
        Try
            Dim sc As NXOpen.StepCreator = theSession.DexManager.CreateStepCreator()
            sc.ExportFrom = NXOpen.StepCreator.ExportFromOption.DisplayPart
            sc.ExportAs = NXOpen.StepCreator.ExportAsOption.Ap214
            sc.OutputFile = f2
            sc.ColorAndLayers = True
            Try : sc.ExportSolidsAndSurfacesAs = NXOpen.StepCreator.ExportSolidsAndSurfacesAsOption.Tessellated : Catch ex As Exception : Log("set ESSAs=Tessellated: " & ex.Message) : End Try
            sc.Commit()
            sc.Destroy()
            Log("F2_display_tess.stp size=" & FileSize(f2))
        Catch ex As Exception
            Log("F2 FAIL: " & ex.Message)
        End Try

        Log("DONE")
    End Sub

End Module
