Submitted by jstrydom on Mon, 05/29/2017 - 04:56
Forums:
What demand/function should one use to change the arrangements of children in an assembly. I am thinking of using "setUsedArrangement", any examples of code that will walk through all the children and change the active arrangement to a specific arrangement. Thanks
re: change arrangement
http://nxjournaling.com/content/creating-subroutine-process-all-componen...
The code in the link above shows how to process all the components of an assembly. The code reports the currently active arrangement for each subassembly. As you have noted, you can use the .SetUsedArrangement method to change the arrangement. If you get a reference to the part's .ComponentAssembly, you can query the available arrangements for use.
re: change arrangement
Here is some code that will report the available arrangements for each subassembly. Note that all components should be loaded before running this code; unloaded components will throw an error that is not handled in this example code.
Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.Assemblies
Module Module2
Public theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim displayPart As Part = theSession.Parts.Display
Dim startWorkPart As Part = theSession.Parts.Work
Dim startDispPart As Part = theSession.Parts.Display
Public ufs As UFSession = UFSession.GetUFSession()
Public lw As ListingWindow = theSession.ListingWindow
Sub Main()
Dim startWorkPart As Part = theSession.Parts.Work
Dim startDispPart As Part = theSession.Parts.Display
lw.Open()
Try
Dim c As ComponentAssembly = displayPart.ComponentAssembly
'to process the work part rather than the display part,
' comment the previous line and uncomment the following line
'Dim c As ComponentAssembly = workPart.ComponentAssembly
If Not IsNothing(c.RootComponent) Then
'*** insert code to process 'root component' (assembly file)
lw.WriteLine("Assembly: " & c.RootComponent.DisplayName)
lw.WriteLine(" + Active Arrangement: " & c.ActiveArrangement.Name)
lw.WriteLine(" Available arrangements:")
For Each tempArrangement As Assemblies.Arrangement In c.Arrangements
lw.WriteLine(" " & tempArrangement.Name)
Next
'*** end of code to process root component
reportComponentChildren(c.RootComponent, 0)
Else
'*** insert code to process piece part
lw.WriteLine("Part has no components")
End If
Catch e As Exception
theSession.ListingWindow.WriteLine("Failed: " & e.ToString)
End Try
lw.Close()
End Sub
'**********************************************************
Sub reportComponentChildren(ByVal comp As Component,
ByVal indent As Integer)
'change displayed part to comp
'ChangeDisplayPart(comp)
'MsgBox("Displayed part is now: " & comp.Prototype.OwningPart.FullPath)
'add code to process displayed part
'
'
'change display part back to the display part when journal started
'ChangeDisplayPart(startDispPart)
For Each child As Component In comp.GetChildren()
'*** insert code to process component or subassembly
lw.WriteLine(New String(" ", indent * 2) & child.DisplayName())
'lw.WriteLine(New String(" ", indent * 2) & "owning part: " & child.OwningPart.FullPath)
'*** end of code to process component or subassembly
If child.GetChildren.Length <> 0 Then
'*** this is a subassembly, add code specific to subassemblies
lw.WriteLine(New String(" ", indent * 2) &
"* subassembly with " &
child.GetChildren.Length & " components")
lw.WriteLine(New String(" ", indent * 2) &
" + Active Arrangement: " &
child.OwningPart.ComponentAssembly.ActiveArrangement.Name)
lw.WriteLine(New String(" ", indent * 2) & " Available arrangements:")
For Each tempArrangement As Assemblies.Arrangement In child.Prototype.OwningPart.ComponentAssembly.Arrangements
lw.WriteLine(New String(" ", indent * 2 + 2) & "- " & tempArrangement.Name)
Next
'*** end of code to process subassembly
Else
'this component has no children (it is a leaf node)
'add any code specific to bottom level components
End If
reportComponentChildren(child, indent + 1)
Next
End Sub
Public Function GetUnloadOption(ByVal dummy As String) As Integer
Return Session.LibraryUnloadOption.Immediately
End Function
End Module