Quantcast
Channel: NX Journaling - Journaling / NXOpen API
Viewing all articles
Browse latest Browse all 787

Counting Total Number of Unique Components

$
0
0

Hello. First off, thank you so much for this great resource of a website. I have learned a lot as I am recently new to VB.net and NXOpen.

The goal of this journal is to import component name to an excel document, but right now I am stuck on this current issue. Right now this reports to the info window.

In an assembly with multiple sub assemblies with components. The count gets reset to the last component of the sub assembly.

Sample Assembly Tree

Main
SubAsm1 (Reports Count 1)
Comp1 (Reports Count 2)
Comp2 (Reports Count 3)
Comp3 (Reports Count 4)
SubAsm2 (Reports Count 2)
Comp4 (Reports Count 3)
Comp5 (Reports Count 4)
SubAsm3 (Reports Count 3)

I want to have it Count 1, 2, 3, 4, 5... down the list

Thanks

'Journal to recursively walk through the assembly structure
' will run on assemblies or piece parts
' will step through all components of the displayed part
'NX 7.5, native
'NXJournaling.com February 24, 2012
 
Option Strict Off
 
Imports System
Imports System.Collections.Generic
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.Utilities
Imports NXOpen.Assemblies
Imports System.Drawing
Imports System.Windows.Forms
Imports Microsoft.Office.Interop
 
Module NXJournal
 
    Public theSession As Session = Session.GetSession()
    Public ufs As UFSession = UFSession.GetUFSession()
    Public lw As ListingWindow = theSession.ListingWindow
 
    Sub Main()
		Dim theSession As Session = Session.GetSession()
		Dim theUfSession As UFSession = UFSession.GetUFSession()
		Dim theUISession As UI = UI.GetUI
 
		Dim workPart As Part = theSession.Parts.Work
		Dim dispPart As Part = theSession.Parts.Display
 
 
		Try
            Dim c As ComponentAssembly = dispPart.ComponentAssembly
 
			lw.Open
 
			Dim startRow As Integer = 1
 
			If Not IsNothing(c.RootComponent) Then
				'*** insert code to process 'root component' (assembly file)
				lw.WriteLine("Assembly: " & c.RootComponent.DisplayName)
 
				'*** end of code to process root component
				ReportComponentChildren(c.RootComponent, startRow)
			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
 
 
 
		ResetToMainAssembly()
 
	End Sub
 
	'**********************************************************
	Sub ReportComponentChildren(ByVal comp As Component, ByVal lastRow As Integer)
		' List to keep track of components along with their suppressed status
 
 
		Dim componentsWithStatus As New List(Of Tuple(Of Component, Boolean))
 
		' List to keep track of processed component display names for duplicate check
		Dim NameList As New List(Of String)
 
		' Collect components and their suppressed status
		For Each child As Component In comp.GetChildren()
			' Add component and its suppressed status to the list
			componentsWithStatus.Add(New Tuple(Of Component, Boolean)(child, child.IsSuppressed))
		Next
 
		' Sort the list so that suppressed components come first
		componentsWithStatus.Sort(Function(x, y) y.Item2.CompareTo(x.Item2))
 
		' Process sorted components
		For Each tuple As Tuple(Of Component, Boolean) In componentsWithStatus
			Dim child As Component = tuple.Item1
			Dim isSuppressed As Boolean = tuple.Item2
 
 
			' Dim wb = Excel.Application.Workbook
 
			' Check for duplicate part
			If NameList.Contains(child.DisplayName()) Then
				' Logic for handling duplicate parts
				' lw.WriteLine("Duplicate part skipped: " & child.DisplayName())
				Continue For
			Else
				NameList.Add(child.DisplayName()) ' Add new component display name to the list
			End If
 
			If isSuppressed Then
				'lw.WriteLine("Suppressed component skipped: " & child.DisplayName())
			Else
				lastRow = lastRow + 1
				lw.WriteLine("-" & lastRow & "-")
			End If
 
			' Retrieve and process the part associated with the child component
			Dim childPart As Part = LoadComponentAndGetPart(child)
 
			lw.WriteLine(child.Name)
 
			If child.GetChildren.Length <> 0 Then
				' Continue processing subassemblies
				ReportComponentChildren(child, lastRow)
			End If
		Next
	End Sub
 
	Function LoadComponentAndGetPart(ByVal component As Component) As Part
		Dim partLoadStatus As PartLoadStatus = Nothing
		Try
			' Set the work component to load the component
			theSession.Parts.SetWorkComponent(component, PartCollection.RefsetOption.Current, PartCollection.WorkComponentOption.Visible, partLoadStatus)
 
			' Get the part associated with the component
			If TypeOf component.Prototype Is Part Then
				Return CType(component.Prototype, Part)
			End If
		Catch ex As Exception
			lw.WriteLine("Exception during loading component: " & ex.Message)
			Return Nothing
		Finally
			' Dispose of the part load status
			If partLoadStatus IsNot Nothing Then
				partLoadStatus.Dispose()
			End If
		End Try
		Return Nothing
	End Function
 
	Sub ResetToMainAssembly()
		Dim partLoadStatus2 As PartLoadStatus = Nothing
 
		Try
			' Reset to main assembly
			theSession.Parts.SetWorkComponent(Nothing, PartCollection.RefsetOption.Current, PartCollection.WorkComponentOption.Visible, partLoadStatus2)
			'lw.WriteLine(" ")
			'lw.WriteLine("Reset to main assembly")
			'lw.WriteLine(" ")
		Catch ex As Exception
			lw.WriteLine("Failed to reset to main assembly: " & ex.Message)
		Finally
			' Dispose the PartLoadStatus object if it's not null
			If partLoadStatus2 IsNot Nothing Then
				partLoadStatus2.Dispose()
			End If
		End Try
	End Sub
	'**********************************************************
	Public Function GetUnloadOption(ByVal dummy As String) As Integer
        Return Session.LibraryUnloadOption.Immediately
    End Function
    '**********************************************************
 
End Module
<blockcode>

Viewing all articles
Browse latest Browse all 787

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>