Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.
JhoelForshav
in reply to: JhoelForshav

@goran.nilssonRSJCU 

This is probably better. This code searches for both parts and assemblies with the specified part number.

Then it returns a list of all the subassemblies (or top assembly) that contains the an occurrence with that part number.

It also counts how many of it exists in each subassembly :slightly_smiling_face:

 

Dim oAsm As AssemblyDocument = ThisDoc.Document
Dim Pnr As String = InputBox("Enter partnumber: ", "Search for partnumber", "000000")
Dim oList As New Dictionary(Of String, Integer)
Dim Msg As String = "Component count in Assembly/Subassemblies: " & vbCrLf
Dim pNrExists As Boolean = False
For Each oDoc As Document In oAsm.AllReferencedDocuments
	If oDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Or _
		oDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject
		If oDoc.PropertySets.Item("Design Tracking Properties") _
			.Item("Part Number").Value = Pnr
			For Each oOcc As ComponentOccurrence In oAsm.ComponentDefinition.Occurrences.AllReferencedOccurrences(oDoc)
				pNrExists = True
				Try
					If oList.ContainsKey(oOcc.ParentOccurrence.Name)
						oList.Item(oOcc.ParentOccurrence.Name) += 1
					Else
						oList.Add(oOcc.ParentOccurrence.Name, 1)
					End If
				Catch
					If oList.ContainsKey("Top level assembly")
						oList.Item("Top level assembly") += 1
					Else
						oList.Add("Top level assembly", 1)
					End If
				End Try
			Next
		End If
	End If
Next
For Each oKey As String In oList.Keys
	Msg = Msg & oKey & " - " & oList.Item(oKey) & " st" & vbCrLf
Next
If pNrExists
	MessageBox.Show(Msg, "Found components", MessageBoxButtons.OK)
Else
	MessageBox.Show("None!", "Found components", MessageBoxButtons.OK)
End If