'Journal to recursively walk through the assembly structure
' will run on assemblies or piece parts
' will step through all components of the displayed part
'NX 7.5, native
'NXJournaling.com February 24, 2012
Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.Assemblies
Module NXJournal
Public theSession As Session = Session.GetSession()
Public ufs As UFSession = UFSession.GetUFSession()
Public lw As ListingWindow = theSession.ListingWindow
Sub Main()
Dim workPart As Part = theSession.Parts.Work
Dim dispPart As Part = theSession.Parts.Display
lw.Open
Try
Dim c As ComponentAssembly = dispPart.ComponentAssembly
if not IsNothing(c.RootComponent) then
ReportComponentChildren(c.RootComponent, 0)
ReportComponentChildren2(c.RootComponent, 0)
else
lw.WriteLine("Part has no components")
end if
Catch e As Exception
theSession.ListingWindow.WriteLine("Failed: " & e.ToString)
End Try
lw.Close
End Sub
'*****************************************************************************************
' Empty run
'*****************************************************************************************
Sub reportComponentChildren( ByVal comp As Component, ByVal indent As Integer)
Dim workPart As Part = theSession.Parts.Work
Dim dispPart As Part = theSession.Parts.Display
For Each child As Component In comp.GetChildren()
Dim MyPart As Part = child.Prototype.OwningPart
Try
if child.IsSuppressed = true then
lw.writeline(" Error: " & child.DisplayName & ".prt" & " -> " & "File closed")
Continue for
end if
Catch e1 As Exception
theSession.ListingWindow.WriteLine("Failed: " & e1.ToString)
end try
If LoadComponent(child) Then
Else
'component could not be loaded
End If
reportComponentChildren(child, indent+1)
Next
End Sub
'*****************************************************************************************
' Changing file name
'*****************************************************************************************
Sub reportComponentChildren2( ByVal comp As Component, ByVal indent As Integer)
Dim workPart As Part = theSession.Parts.Work
Dim dispPart As Part = theSession.Parts.Display
For Each child As Component In comp.GetChildren()
Dim MyPart As Part = child.Prototype.OwningPart
Try
if child.IsSuppressed = true then
lw.writeline("Error: " & child.DisplayName & ".prt" & " " & "File closed")
exit sub
end if
Catch e1 As Exception
theSession.ListingWindow.WriteLine("Failed: " & e1.ToString)
end try
If LoadComponent(child) Then
Dim File_name as string
Dim TextLength as Integer
File_name = child.DisplayName()
TextLength = File_name.Length
'lw.writeline(File_name)
lw.writeline("component name: " & child.Name & "-> " & File_name )
child.SetName(File_name)
lw.writeline("new component name: " & child.Name)
'lw.writeline("ref set name: " & child.referenceset)
'lw.writeline("component part name: " & child.Prototype.OwningPart.fullpath)
lw.writeline("")
Else
'component could not be loaded
End If
reportComponentChildren2(child, indent + 1)
Next
End Sub
'**********************************************************
' Function loading components
'**********************************************************
Private Function LoadComponent(ByVal theComponent As Component) As Boolean
Dim thePart As Part = theComponent.Prototype.OwningPart
Dim partName As String = ""
Dim refsetName As String = ""
Dim instanceName As String = ""
Dim origin(2) As Double
Dim csysMatrix(8) As Double
Dim transform(3, 3) As Double
Try
If thePart.IsFullyLoaded Then
'component is fully loaded
Else
'component is partially loaded
End If
Return True
Catch ex As NullReferenceException
'component is not loaded
Try
ufs.Assem.AskComponentData(theComponent.Tag, partName, refsetName, instanceName, origin, csysMatrix, transform)
Dim theLoadStatus As PartLoadStatus
theSession.Parts.Open(partName, theLoadStatus)
If theLoadStatus.NumberUnloadedParts > 0 Then
Dim allReadOnly As Boolean = True
For i As Integer = 0 To theLoadStatus.NumberUnloadedParts - 1
If theLoadStatus.GetStatus(i) = 641058 Then
'read-only warning, file loaded ok
Else
'641044: file not found
lw.WriteLine("File not found")
allReadOnly = False
End If
Next
If allReadOnly Then
Return True
Else
'warnings other than read-only...
Return False
End If
Else
Return True
End If
Catch ex2 As NXException
if ex2.message = "File not found" then
lw.WriteLine("Error: " & partname & " " & "File not found")
else
lw.WriteLine("Error: " & partname & " " & ex2.Message)
end if
Return False
End Try
Catch ex As NXException
'unexpected error
lw.WriteLine("error: " & ex.Message)
Return False
End Try
End Function
'**********************************************************
Public Function GetUnloadOption(ByVal dummy As String) As Integer
Return Session.LibraryUnloadOption.Immediately
End Function
'**********************************************************
End Module