' 调试: 四格式导出交叉测试 (STEP/Parasolid/IGES/STL) — 判断是几何问题还是翻译器配置问题
Option Strict Off
Imports System
Imports System.IO
Imports System.Text
Imports NXOpen
Imports NXOpen.UF

Module DebugExportAll
    Sub Main()
        Dim sb As New StringBuilder()
        Dim cur As String = Directory.GetCurrentDirectory()
        Dim masterPath As String = Path.Combine(cur, "KC_master.prt")
        Dim partLoadStatus1 As NXOpen.PartLoadStatus = Nothing
        Session.GetSession().Parts.OpenBaseDisplay(masterPath, partLoadStatus1)
        Dim w As Part = Session.GetSession().Parts.Work
        Dim outDir As String = Path.Combine(cur, "dbg_out")
        Directory.CreateDirectory(outDir)
        For Each f As String In Directory.GetFiles(outDir)
            File.Delete(f)
        Next

        ' 保存工作部件 (导出器可能读文件; 也验证 Save 后实体仍在)
        Try
            Dim pss As NXOpen.PartSaveStatus = w.Save(NXOpen.BasePart.SaveComponents.True, NXOpen.BasePart.CloseAfterSave.False)
            If pss IsNot Nothing Then pss.Dispose()
            sb.AppendLine("Save OK")
        Catch ex As Exception
            sb.AppendLine("Save 失败: " & ex.Message)
        End Try

        ' 1) STL (显示几何三角化; 若此也不出网格 → 几何本身有问题)
        Try
            Dim stc As NXOpen.STLCreator = Session.GetSession().DexManager.CreateStlCreator()
            stc.OutputType = NXOpen.STLCreator.OutputTypeEnum.Binary
            stc.ChordalTol = 0.5
            stc.AdjacencyTol = 0.1
            stc.AutoNormalGen = True
            stc.OutputFile = Path.Combine(outDir, "T.stl")
            stc.Commit()
            stc.Destroy()
            sb.AppendLine("STL: " & (New FileInfo(Path.Combine(outDir, "T.stl"))).Length & " 字节")
        Catch ex As Exception
            sb.AppendLine("STL 失败: " & ex.Message)
        End Try

        ' 2) Parasolid (文件方式)
        Try
            Dim pe As NXOpen.ParasolidExporter = Session.GetSession().DexManager.CreateParasolidExporter()
            pe.ExportFrom = NXOpen.ParasolidExporter.ExportFromOption.DisplayedPart
            pe.ParasolidVersion = NXOpen.ParasolidExporter.ParasolidVersionOption.Current
            pe.OutputFile = Path.Combine(outDir, "T.x_t")
            pe.Commit()
            pe.Destroy()
            sb.AppendLine("Parasolid: " & (New FileInfo(Path.Combine(outDir, "T.x_t"))).Length & " 字节")
        Catch ex As Exception
            sb.AppendLine("Parasolid 失败: " & ex.Message)
        End Try

        ' 3) IGES (文件方式 ExistingPart)
        Try
            Dim ic As NXOpen.IgesCreator = Session.GetSession().DexManager.CreateIgesCreator()
            ic.ExportFrom = NXOpen.IgesCreator.ExportFromOption.ExistingPart
            ic.InputFile = masterPath
            ic.ExportModelData = True
            ic.OutputFile = Path.Combine(outDir, "T.igs")
            ic.Commit()
            ic.Destroy()
            sb.AppendLine("IGES: " & (New FileInfo(Path.Combine(outDir, "T.igs"))).Length & " 字节")
        Catch ex As Exception
            sb.AppendLine("IGES 失败: " & ex.Message)
        End Try

        ' 4) STEP (文件方式 ExistingPart)
        Try
            Dim sc As NXOpen.StepCreator = Session.GetSession().DexManager.CreateStepCreator()
            sc.ExportFrom = NXOpen.StepCreator.ExportFromOption.ExistingPart
            sc.InputFile = masterPath
            sc.ExportAs = NXOpen.StepCreator.ExportAsOption.Ap214
            sc.OutputFile = Path.Combine(outDir, "T.stp")
            sc.ColorAndLayers = True
            sc.Commit()
            sc.Destroy()
            sb.AppendLine("STEP: " & (New FileInfo(Path.Combine(outDir, "T.stp"))).Length & " 字节")
        Catch ex As Exception
            sb.AppendLine("STEP 失败: " & ex.Message)
        End Try

        File.WriteAllText(Path.Combine(cur, "debug-export-all.txt"), sb.ToString(), Encoding.UTF8)
    End Sub
End Module
