' 检查母模: 特征数 / 实体数 / 表达式值
Option Strict Off
Imports System
Imports System.IO
Imports System.Text
Imports NXOpen
Imports NXOpen.UF

Module CheckMaster
    Sub Main()
        Dim sb As New StringBuilder()
        Dim outPath As String = Path.Combine(Directory.GetCurrentDirectory(), "KC_master.prt")
        Dim partLoadStatus1 As NXOpen.PartLoadStatus = Nothing
        Session.GetSession().Parts.OpenBaseDisplay(outPath, partLoadStatus1)
        Dim w As Part = Session.GetSession().Parts.Work
        If w Is Nothing Then
            sb.AppendLine("Work=NULL")
        Else
            Dim featCount As Integer = 0
            Try
                For Each f As NXOpen.Features.Feature In w.Features.GetFeatures()
                    featCount += 1
                Next
            Catch
            End Try
            sb.AppendLine("特征数=" & featCount)
            Dim bodyCount As Integer = 0
            Try
                For Each b As NXOpen.Body In w.Bodies
                    bodyCount += 1
                Next
            Catch
            End Try
            sb.AppendLine("实体数=" & bodyCount)
            Dim bi As Integer = 0
            Try
                For Each b As NXOpen.Body In w.Bodies
                    Dim fc As Integer = 0
                    Try
                        For Each f As NXOpen.Face In b.GetFaces()
                            fc += 1
                        Next
                    Catch
                    End Try
                    bi += 1
                    Try
                        For Each f As NXOpen.Face In b.GetFaces()
                            fc += 1
                        Next
                    Catch
                    End Try
                    sb.AppendLine("实体" & bi & " 面数=" & fc)
                Next
            Catch ex As Exception
                sb.AppendLine("实体检查异常: " & ex.Message)
            End Try
            For Each nm As String In New String() {"bore", "stroke", "magOn", "mountIdx"}
                Try
                    Dim e As NXOpen.Expression = w.Expressions.FindObject(nm)
                    sb.AppendLine("表达式 " & nm & "=" & (If(e Is Nothing, "(未找到)", e.Value)))
                Catch ex As Exception
                    sb.AppendLine("表达式 " & nm & " 异常: " & ex.Message)
                End Try
            Next
            sb.AppendLine("IsModified=" & w.IsModified)
        End If
        File.WriteAllText(Path.Combine(Directory.GetCurrentDirectory(), "check-master.txt"), sb.ToString(), Encoding.UTF8)
    End Sub
End Module
