cancel
Showing results for 
Search instead for 
Did you mean: 

How to get all unique part and sub assembly from main assembly in NX7.5

Creator
Creator

Hi All,

 

I need count of all unique part and sub assembly of any Assembly file through VBA/VB.Net.

 

Ex. Let Suppose i have an assebly naming Assembly1. Assembly1 Contains both part file as well as sub Assembly file. Then,

 

Dim Count_Part As Integer  = count of unique part file present in Assembly1

Dim Count_SubAssm As Integer = Count of all unique sub assembly file present in Assembly1

 

Any help would be greatly appreciated.

2 REPLIES

Re: How to get all unique part and sub assembly from main assembly in NX7.5

Phenom
Phenom

Here's an example:

 

Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen
Imports NXOpen.Assemblies
Imports NXOpen.UF

Module Module1

    Dim theSession As Session = Session.GetSession()
    Dim theUfSession As UFSession = UFSession.GetUFSession()
    Dim lw As ListingWindow = theSession.ListingWindow

    Dim uniqueParts As New List(Of Part)
    Dim uniqueSubAssemblies As New List(Of Part)

    Sub Main()

        If IsNothing(theSession.Parts.BaseWork) Then
            'active part required
            Return
        End If

        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)
            Else
                lw.WriteLine("Part has no components")
            End If
        Catch e As Exception
            theSession.ListingWindow.WriteLine("Failed: " & e.ToString)
        End Try

        lw.WriteLine(dispPart.FullPath)
        lw.WriteLine("count of unique sub-assemblies: " & uniqueSubAssemblies.Count.ToString)
        For Each temp As Part In uniqueSubAssemblies
            Try
                'may throw an error if the file is not loaded
                lw.WriteLine("  " & temp.FullPath)
            Catch ex As Exception
            End Try
        Next
        lw.WriteLine("")

        lw.WriteLine("count of unique parts: " & uniqueParts.Count.ToString)
        For Each temp As Part In uniqueParts
            Try
                'may throw an error if the file is not loaded
                lw.WriteLine("  " & temp.FullPath)
            Catch ex As Exception
            End Try
        Next

        lw.Close()

    End Sub

    Sub reportComponentChildren(ByVal comp As Component)

        For Each child As Component In comp.GetChildren()
            Dim thePart As Part = child.Prototype.OwningPart
            If child.GetChildren.Length <> 0 Then
                '*** this is a subassembly, add code specific to subassemblies
                If Not uniqueSubAssemblies.Contains(thePart) Then
                    uniqueSubAssemblies.Add(thePart)
                End If
                '*** end of code to process subassembly
            Else
                'this component has no children (it is a leaf node)
                If Not uniqueParts.Contains(thePart) Then
                    uniqueParts.Add(thePart)
                End If
            End If
            reportComponentChildren(child)
        Next
    End Sub

    Public Function GetUnloadOption(ByVal dummy As String) As Integer

        'Unloads the image immediately after execution within NX
        GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately

    End Function

End Module

Re: How to get all unique part and sub assembly from main assembly in NX7.5

Creator
Creator

Thank you so much for the code.