' =============================================================================
' NX Journal: 冒烟测试母模创建 (按需生成服务联调用, 2026-08-26; NX 2506 实测修正版)
'
' ▍干什么: 打开"当前工作目录\KC_master.prt"(启动前由脚本从 NX 模板复制) →
'   建 bore/stroke/magOn/mountIdx 四个表达式 (UF 底层 API, 跨版本稳定) → 建方块实体 → 保存
'   用途: 验证 worker_journal.vb 全链路 (打开→设表达式→更新→导出 4 格式) 在无真实母模时跑通。
'   真母模建好后同名替换 catalog-masters\KC\KC_master.prt, 本脚本可弃。
'
' ▍运行 (run_journal 只接受一个参数, 用 WorkingDirectory 定位):
'   WorkingDirectory = catalog-masters\KC
'   run_journal.exe <本文件完整路径>
' =============================================================================
Option Strict Off
Imports System
Imports System.IO
Imports NXOpen
Imports NXOpen.UF

Module CreateTestMaster

    Sub Main()
        Dim theSession As Session = Session.GetSession()
        Dim lw As ListingWindow = theSession.ListingWindow
        lw.Open()

        Dim outPath As String = Path.Combine(Directory.GetCurrentDirectory(), "KC_master.prt")
        If Not File.Exists(outPath) Then
            lw.WriteLine("部件文件缺失: " & outPath & " (请先从 NX 模板复制到当前目录)")
            Return
        End If

        ' 打开模板部件 (批处理必须用 OpenBaseDisplay 才能成为工作部件, OpenBase 打开后 Work=NULL)
        Dim partLoadStatus1 As NXOpen.PartLoadStatus = Nothing
        Dim basePart1 As NXOpen.BasePart = theSession.Parts.OpenBaseDisplay(outPath, partLoadStatus1)
        If partLoadStatus1 IsNot Nothing Then partLoadStatus1.Dispose()
        Dim part1 As Part = theSession.Parts.Work
        If part1 Is Nothing Then
            lw.WriteLine("打开失败: " & outPath)
            Return
        End If
        lw.WriteLine("部件已打开: " & part1.FullPath)

        ' 表达式 (NX 2506: 用 ExpressionCollection.CreateExpression(值, 名) 创建无单位表达式;
        ' 特征尺寸按部件单位解释; EditExp 只编辑已存在的表达式)
        Dim mm As NXOpen.Unit = part1.UnitCollection.FindObject("MilliMeter")
        If mm Is Nothing Then
            lw.WriteLine("警告: 未找到 MilliMeter 单位对象, 尝试 Millimetre")
            mm = part1.UnitCollection.FindObject("Millimetre")
        End If
        ' 缸径/行程带长度单位 (特征尺寸表达式需要); 开关无单位
        If mm IsNot Nothing Then
            part1.Expressions.CreateExpressionWithUnit("Number", "bore=32", mm)
            part1.Expressions.CreateExpressionWithUnit("Number", "stroke=10", mm)
        Else
            part1.Expressions.CreateExpression("Number", "bore=32")
            part1.Expressions.CreateExpression("Number", "stroke=10")
        End If
        part1.Expressions.CreateExpression("Number", "magOn=0")
        part1.Expressions.CreateExpression("Number", "mountIdx=0")
        lw.WriteLine("表达式已建: bore=32 stroke=10 magOn=0 mountIdx=0")

        ' 仿真气缸母模 (测试级真实感): 缸筒 + 活塞杆, 尺寸全部由表达式驱动
        ' 缸筒: 外径 bore+8 (壁厚4), 长度 stroke; 活塞杆: 直径 bore*0.33, 长度 stroke+30 (前伸 30)
        Dim ufs As UFSession = UFSession.GetUFSession()
        Dim origin(2) As Double
        Dim dir(2) As Double
        dir(2) = 1.0   ' +Z 轴向
        ' NX 2506 签名: CreateCyl1(sign, origin, height, diam, direction, ByRef tag)
        Dim tagTube As Tag
        ufs.Modl.CreateCyl1(NXOpen.UF.FeatureSigns.Nullsign, origin, "stroke", "bore+8", dir, tagTube)
        Dim tagRod As Tag
        ufs.Modl.CreateCyl1(NXOpen.UF.FeatureSigns.Nullsign, origin, "stroke+30", "bore*0.33", dir, tagRod)
        lw.WriteLine("仿真气缸已建: 缸筒(bore+8 x stroke) + 活塞杆(bore*0.33 x stroke+30), 表达式驱动")

        ' 更新模型 (特征刚建, 必须 Update 后才成为实体几何)
        ufs.Modl.Update()
        lw.WriteLine("模型已更新")

        ' 把实体加进 Model 引用集 (UF 建的实体默认不进引用集 → 导出器按引用集过滤会导出空几何)
        Dim bodyList As New System.Collections.Generic.List(Of NXOpen.NXObject)()
        For Each b As NXOpen.Body In part1.Bodies
            bodyList.Add(b)
        Next
        If bodyList.Count > 0 Then
            For Each rs As NXOpen.ReferenceSet In part1.GetAllReferenceSets()
                If rs.Name.ToUpperInvariant().Contains("MODEL") Then
                    rs.AddObjectsToReferenceSet(bodyList.ToArray())
                    lw.WriteLine("实体已加入引用集 " & rs.Name & " (" & bodyList.Count & " 个)")
                    Exit For
                End If
            Next
        End If

        ' 保存 (NX 2506: Save 两参数, 返回 PartSaveStatus)
        Dim pss As NXOpen.PartSaveStatus = part1.Save(NXOpen.BasePart.SaveComponents.True, NXOpen.BasePart.CloseAfterSave.False)
        If pss IsNot Nothing Then pss.Dispose()
        lw.WriteLine("已保存: " & outPath)
    End Sub

End Module
