Get all components in an assembly
Get all components in an assembly
(OP)
Does anyone know an easy way using NXOpen to get all the components in an assembly (including subassemblies) saved to an array? I like to sort the assembly tree in descending order and then export it to a spreadsheet. I would ideally like to get that same structure/order, but I need it in NXOPen variables so that I can manipulate the data.
Thanks
Thanks





RE: Get all components in an assembly
RE: Get all components in an assembly
http://www.eng-tips.com/viewthread.cfm?qid=264052
http://www.eng-tips.com/viewthread.cfm?qid=308802
John R. Baker, P.E.
Product 'Evangelist'
Product Engineering Software
Siemens PLM Software Inc.
Industry Sector
Cypress, CA
http://www.siemens.com/plm
UG/NX Museum: http://www.plmworld.org/p/cm/ld/fid=209
To an Engineer, the glass is twice as big as it needs to be.
RE: Get all components in an assembly
RE: Get all components in an assembly
CODE
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
'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)
'*** end of code to process root component
reportComponentChildren(c.RootComponent, 1)
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)
Dim numOccs(-1) As Tag
For Each child As Component In comp.GetChildren()
'*** insert code to process component or subassembly
lw.WriteLine(New String(" ", indent * 2) & child.DisplayName & vbTab & child.Name)
Try
ufs.Assem.AskOccsOfPart(comp.Prototype.OwningPart.Tag, child.Prototype.OwningPart.Tag, numOccs)
lw.WriteLine(New String(" ", indent * 2) & "Quantity: " & numOccs.GetLength(0))
Catch ex As System.NullReferenceException
lw.WriteLine(New String(" ", indent * 2) & "*** Component quantity information unavailable (component not loaded)")
Catch ex As ApplicationException
End Try
'*** 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)
'*** 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
lw.WriteLine("")
reportComponentChildren(child, indent + 1)
Next
End Sub
'**********************************************************
Public Function GetUnloadOption(ByVal dummy As String) As Integer
Return Session.LibraryUnloadOption.Immediately
End Function
'**********************************************************
End Module
www.nxjournaling.com