' ============================================================================= ' NX Journal: inspect Display vs Work state in batch mode (NX 2506) ' ' Last attempt failed to compile: Parts.Display is ReadOnly. This version only ' READS and prints state (no assignment), so it compiles and tells us whether ' the batch-mode Display part is NULL / same as Work / different. ' ' Prints: ' Work.FullPath, Work bodies ' Display (NULL or FullPath), Display bodies ' Display Is Work? ' then a DisplayPart STEP export to see solids count. ' ' Run: WorkingDirectory = catalog-masters\KC; run_journal.exe ' Result: \test-display-state.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 TestDisplayState 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-display-state.txt"), sb.ToString(), New UTF8Encoding(True)) End Sub Function CountBodies(p As BasePart) As Integer Dim n As Integer = 0 Try Dim pp As Part = CType(p, Part) If pp IsNot Nothing Then For Each b As NXOpen.Body In pp.Bodies n += 1 Next End If 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("Work=" & w.FullPath) Log("Work.bodies=" & CountBodies(w)) ' ---- READ Display state (no assignment) ---- Dim disp As BasePart = Nothing Try disp = theSession.Parts.Display Catch ex As Exception Log("Parts.Display get throws: " & ex.Message) End Try If disp Is Nothing Then Log("Display=NULL") Else Log("Display=" & disp.FullPath) Log("Display.bodies=" & CountBodies(disp)) Log("Display Is Work? " & (disp Is w)) End If ' ---- Update work + add bodies to MODEL refset (so the file is exportable) ---- Try Dim ufs As UFSession = UFSession.GetUFSession() ufs.Modl.Update() Dim bodyList As New System.Collections.Generic.List(Of NXOpen.NXObject)() For Each b As NXOpen.Body In w.Bodies bodyList.Add(b) Next For Each rs As NXOpen.ReferenceSet In w.GetAllReferenceSets() If rs.Name.ToUpperInvariant().Contains("MODEL") Then rs.AddObjectsToReferenceSet(bodyList.ToArray()) Exit For End If Next Log("refset added") Catch ex As Exception Log("refset FAIL: " & ex.Message) End Try ' ---- DisplayPart export ---- Dim f As String = Path.Combine(cur, "dbg_out", "S_display.stp") Directory.CreateDirectory(Path.Combine(cur, "dbg_out")) If File.Exists(f) Then File.Delete(f) Try Dim sc As NXOpen.StepCreator = theSession.DexManager.CreateStepCreator() sc.ExportFrom = NXOpen.StepCreator.ExportFromOption.DisplayPart sc.ExportAs = NXOpen.StepCreator.ExportAsOption.Ap214 sc.OutputFile = f sc.ColorAndLayers = True sc.Commit() sc.Destroy() Log("S_display.stp size=" & FileSize(f)) Catch ex As Exception Log("S_display FAIL: " & ex.Message) End Try Log("DONE") End Sub End Module