' This program will walk an assembly structure. For each component it will:
' Assign physical materials to the bodies in the components.
' This program changes the displayed part instead of simply changing the work part.
' Changing displayed part is slower than just changing work part, but it is safer. If you
' have mixed unit parts (english / metric) then the program will error when trying to change
' work parts.
Option Strict Off
Imports System
Imports System.IO
Imports System.Collections
Imports NXOpen
Imports NXOpen.Assemblies
Imports NXOpen.UF
Imports NXOpenUI
Imports NXOpen.UIStyler
Module NXJournal
' Declare module variables. These are available to all subroutines.
Dim theSession As Session = Session.GetSession()
Dim alreadyProcessed As Hashtable
Dim knt As Integer = 0
Dim partLoadStat As PartLoadStatus
Dim LW As ListingWindow = theSession.ListingWindow
Dim displayPart As Part = theSession.Parts.Display
Dim workPart As Part
Sub Main() ' Program entry point
LW.Open()
LW.WriteLine("Listing child components")
Try
Dim part1 As Part = theSession.Parts.Work
LW.WriteLine("Work part: " & part1.FullPath)
LW.WriteLine(part1.Leaf)
LW.WriteLine(CType(TimeOfDay(), String))
' Initialize a hash table to store components that have been processed.
' This will prevent components from being processed twice
alreadyProcessed = New Hashtable
Dim c As ComponentAssembly = part1.ComponentAssembly
LW.WriteLine("first walk: ")
Walk(c.RootComponent, 0)
' When the program is done "walking" the assembly it will return here.
' Set the work part back to what it was
LW.WriteLine("Done: ")
theSession.Parts.SetDisplay(part1, False, False, partLoadStat)
LW.WriteLine(CType(TimeOfDay(), String))
Catch e As Exception
LW.WriteLine("Error running application: " & e.Message)
End Try
End Sub
Sub Walk(ByVal c As Component, ByVal level As Integer)
LW.WriteLine("Walking: ")
Dim ufs As UFSession = UFSession.GetUFSession()
'Dim wrap As String = Chr(10)
'Dim dwrap As String = wrap & wrap
'Dim title As String
'Dim value As String
'Dim inx As Integer = 0
Dim prototype As Part
Dim children As Component() = c.GetChildren()
Dim child As Component
prototype = CType(c.Prototype, Part)
'LW.WriteLine("Component: " & child.ToString)
LW.WriteLine("Component: " & c.ToString)
If Not alreadyProcessed.Contains(prototype.Leaf) Then
' Add this prototype to the hash table so that we don't process it again
alreadyProcessed.Add(prototype.Leaf, prototype.Leaf)
knt = knt + 1
Dim msgText As String
msgText = knt.ToString & " " & New String(" "c, (level * 4)) & prototype.Leaf
'LW.WriteLine(msgText.ToString)
theSession.Parts.SetDisplay(prototype, False, False, partLoadStat)
' displayPart = theSession.Parts.Display
'==============
Dim workPart As Part = theSession.Parts.Work
Dim physicalMaterial1 As PhysicalMaterial
Try
'load from library in case it is not used in the part yet
physicalMaterial1 = workPart.MaterialManager.PhysicalMaterials.LoadFromNxmatmllibrary("Aluminum_6061")
Catch ex as Exception
' the material is already known in the part so use it
physicalMaterial1 = CType(workPart.MaterialManager.PhysicalMaterials.FindObject("PhysicalMaterial[Aluminum_6061]"), PhysicalMaterial)
End Try
Dim bodies As BodyCollection = workPart.Bodies
Dim solidBodies(0) As NXObject
Dim counter As Integer = 0
Dim bodyCount As Integer = bodies.ToArray.Length
LW.WriteLine("Total Bodies in Work Part: " & bodyCount.ToString())
If bodyCount > 0 Then
For Each thisBody As Body In bodies
If thisBody.IsSheetBody.Equals(False) Then
ReDim Preserve solidBodies(counter)
solidBodies(counter) = thisBody
LW.WriteLine("Solid Body: " & thisBody.ToString())
counter += 1
End If
Next
Dim solidBodyCount As Integer = solidBodies.Length()
LW.WriteLine("Solid Bodies in Work Part: " & _
solidBodyCount.ToString())
' At this point, all solid bodies
' should be in an array called solidBodies
If (solidBodies.Length > 0) Then
physicalMaterial1.AssignObjects(solidBodies)
End If
End If
Else
LW.WriteLine("Already processsed")
'theSession.DisplayManager.MakeUpToDate()
End if ' not already processed
'==================
For Each child In children
LW.Writeline("next child = " & child.ToString)
Walk(child, level + 1)
Next
End Sub
End Module