Cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted

Get count of unique comp and sub-assembly for files

Siemens Phenom Siemens Phenom
Siemens Phenom

Hi All,

 

I'm iterating thru a folder having parts and trying to collect the unique count of components and sub-assemblies of that part file. Below code seems to do the trick but it gives the same information as that of its assembly file.

 

Code:

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 PartsPath as string = "D:\workdir\1"

    Dim files() As String = IO.Directory.GetFiles(PartsPath)
    Dim uniqueParts As New List(Of Part)
    Dim uniqueSubAssemblies As New List(Of Part)
    Dim basePart1 As NXOpen.BasePart = Nothing
    Dim partLoadStatus1 As NXOpen.PartLoadStatus = Nothing
    Sub Main()
        lw.Open()
    For Each file As String In files
  
		basePart1 = theSession.Parts.OpenActiveDisplay(file, NXOpen.DisplayPartOption.AllowAdditional, partLoadStatus1) 
		partLoadStatus1.Dispose()           
        Dim displayPart As NXOpen.Part = theSession.Parts.Display
        
        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

            Dim c As ComponentAssembly = dispPart.ComponentAssembly
            If Not IsNothing(c.RootComponent) Then
                reportComponentChildren(c.RootComponent)
            Else
                lw.WriteLine("Part has no components")
            End If
        lw.WriteLine("")    
        lw.WriteLine("Part File name: " & file)
        lw.WriteLine("count of unique sub-assemblies: " & uniqueSubAssemblies.Count.ToString)
        For Each temp As Part In uniqueSubAssemblies
               ' lw.WriteLine("  " & temp.FullPath)
        Next

        lw.WriteLine("count of unique parts: " & uniqueParts.Count.ToString)
        For Each temp As Part In uniqueParts
                'lw.WriteLine("  " & temp.FullPath)
        Next
        Dim partCloseResponses1 As NXOpen.PartCloseResponses = Nothing
		partCloseResponses1 = theSession.Parts.NewPartCloseResponses()
		workPart.Close(NXOpen.BasePart.CloseWholeTree.True, NXOpen.BasePart.CloseModified.UseResponses, partCloseResponses1)
		workPart = Nothing
		displayPart = Nothing
		partCloseResponses1.Dispose()
		theSession.ApplicationSwitchImmediate("UG_APP_NOPART") 
    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

 

Current Output: 

Part File name: D:\workdir\1\assembly6.prt
count of unique sub-assemblies: 1
count of unique parts: 5
Part has no components

<One of the component of above assembly>
Part File name: D:\workdir\NX1202IP9\1\model19.prt
count of unique sub-assemblies: 1
count of unique parts: 5
Part has no components

Regards,
Samadhan

GTAC | NX Help: NX1102 | NX1202 | NX1847
Please mark post as an "Accepted Solution", if it answers your question/is more helpful!
3 REPLIES 3

Re: Get count of unique comp and sub-assembly for files

Legend
Legend

Hi,

 

I think you should use

 

  Sub reportComponentChildren(ByVal comp As Component)
        For Each child As Component In comp.GetChildren()
            Dim thePart As Part = child.Prototype
.....

Frederik Verscheure
Evolve Consulting bvba
Freelance NX & Teamcenter consultant
frederik@evolve-consulting.be

Re: Get count of unique comp and sub-assembly for files

Gears Esteemed Contributor Gears Esteemed Contributor
Gears Esteemed Contributor

The code processes all the files in a given directory, but the list variables uniqueParts and uniqueSubAssemblies are never cleared. This means that after processing assembly6.prt, which has 1 unique subassembly and 5 unique parts, the count of unique parts will never be less than 5 when processing subsequent parts. If you want to see the number of unique parts/subassemblies for each part in the directory, clear the lists after reporting the numbers and before starting the next iteration.

Re: Get count of unique comp and sub-assembly for files

Siemens Phenom Siemens Phenom
Siemens Phenom

Cleared lists with below lines and now it's working fine. Thank you @cowski1 !

 

	theSession.ApplicationSwitchImmediate("UG_APP_NOPART") 
        ''' Cleared lists 
        uniqueParts.clear()
        uniqueSubAssemblies.clear()
    Next

Regards,
Samadhan

GTAC | NX Help: NX1102 | NX1202 | NX1847
Please mark post as an "Accepted Solution", if it answers your question/is more helpful!