Files
OnebotCatalog/sample-data/tools/test-display-part.vb

140 lines
4.8 KiB
VB.net

' =============================================================================
' NX Journal: test whether batch-mode DisplayPart is empty (NX 2506)
'
' Hypothesis: ExportFrom=DisplayPart exports Session.Parts.Display, but in
' batch (run_journal) OpenBaseDisplay sets Work part WITHOUT setting the
' Display part -> Display is null -> translator sees 0 solids.
'
' This script:
' 1. OpenBaseDisplay -> print Work AND Display state
' 2. Try setting Display = workPart
' 3. Update + add to MODEL refset
' 4. DisplayPart STEP export (should now produce a real solid)
'
' Run: WorkingDirectory = catalog-masters\KC; run_journal.exe <this file>
' Result: <cwd>\test-display-part.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 TestDisplayPart
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-part.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("Work=" & w.FullPath)
' ---- KEY: state of the DISPLAY part ----
Try
Dim disp As BasePart = theSession.Parts.Display
Log("Display=" & (If(disp Is Nothing, "NULL", disp.FullPath)))
Catch ex As Exception
Log("Parts.Display get throws: " & ex.Message)
End Try
' ---- try to SET Display = work part ----
Try
theSession.Parts.Display = w
Log("set Display=work OK")
Catch ex As Exception
Log("set Display=work FAIL: " & ex.Message)
End Try
Try
Dim disp2 As BasePart = theSession.Parts.Display
Log("Display(after set)=" & (If(disp2 Is Nothing, "NULL", disp2.FullPath)))
Catch ex As Exception
Log("Parts.Display get2 throws: " & ex.Message)
End Try
' ---- Update + refset (align with the flow that produced a file) ----
Dim ufs As UFSession = UFSession.GetUFSession()
ufs.Modl.Update()
Log("afterUpdate bodies=" & CountBodies(w))
Try
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 bodies added")
Catch ex As Exception
Log("refset add FAIL: " & ex.Message)
End Try
' ---- DisplayPart STEP export ----
Dim f As String = Path.Combine(cur, "dbg_out", "D_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("D_display.stp size=" & FileSize(f))
Catch ex As Exception
Log("D_display FAIL: " & ex.Message)
End Try
Log("DONE")
End Sub
End Module