Ilogic to count parts in subassembly

Ilogic to count parts in subassembly

goran.nilssonRSJCU
Participant Participant
2,195 Views
6 Replies
Message 1 of 7

Ilogic to count parts in subassembly

goran.nilssonRSJCU
Participant
Participant

Hello

I have a top-assembly that consists of a large number of sub-assemblies. I want to go through all the sub-assemblies and look for a specific part number ( like iProperties PartNumber ="73265" ) and then return in which sub-assemblies that the specified part number is included in. How can I get in which sub-assembly that the specified part number is included?

Accepted solutions (1)
2,196 Views
6 Replies
Replies (6)
Message 2 of 7

JhoelForshav
Mentor
Mentor

Hi @goran.nilssonRSJCU 

If the partnumber you're looking for is always a part you could try this rule 🙂

Dim oAsm As AssemblyDocument = ThisDoc.Document
Dim oPnr As String = InputBox("Part number: ", "Enter part number", "PARTNUMBER")
Dim Msg As String = "Part exists in: " & vbCrLf
Dim oList As New List(Of String)
Dim partExists As Boolean = False
For Each oOcc As ComponentOccurrence In oAsm.ComponentDefinition.Occurrences.AllLeafOccurrences
	If oOcc.Definition.Document.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value = oPnr
		partExists = True
		Try
			If oList.Contains(oOcc.ParentOccurrence.Name) = False Then oList.Add(oOcc.ParentOccurrence.Name)

		Catch
			'Part is in top level assembly
			If oList.Contains(oAsm.DisplayName) = False Then oList.Add(oAsm.DisplayName)
		End Try
	End If
Next
If partExists = True
For Each oItem As String In oList
	Msg = Msg & oItem & vbCrLf
Next
MessageBox.Show(Msg, "Sub-assemblys containing part", MessageBoxButtons.OK)
Else
MessageBox.Show("Cant find part with partnumber: " & oPnr, "Sub-assemblys containing part", MessageBoxButtons.OK)	
End If
Message 3 of 7

JhoelForshav
Mentor
Mentor
Accepted solution

@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 🙂

 

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
			
Message 4 of 7

goran.nilssonRSJCU
Participant
Participant

@JhoelForshav Thank you for your reply. It worked well but I wonder if it possible to even find virtual components in subassemblies? In some subbassemblies have we added a couple of virtual components,

Message 5 of 7

JhoelForshav
Mentor
Mentor

Hi @goran.nilssonRSJCU 

This should cover virtual components aswell 🙂

 

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 oOcc As ComponentOccurrence In oAsm.ComponentDefinition.Occurrences
	If TypeOf oOcc.Definition Is VirtualComponentDefinition
		If oOcc.Definition.PropertySets("Design Tracking Properties") _
			("Part Number").Value = Pnr
			If oList.ContainsKey("Top level assembly")
				oList.Item("Top level assembly") += 1
			Else
				oList.Add("Top level assembly", 1)
			End If
			pNrExists = True
		End If
	End If
Next
For Each oDoc As Document In oAsm.AllReferencedDocuments
	If oDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Or _
		oDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject
		If oDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject
			For Each oOcc As ComponentOccurrence In oDoc.ComponentDefinition.Occurrences
				If TypeOf oOcc.Definition Is VirtualComponentDefinition
					If oOcc.Definition.PropertySets("Design Tracking Properties") _
						("Part Number").Value = Pnr
						For Each parentOcc As ComponentOccurrence In oAsm.ComponentDefinition. _
							Occurrences.AllReferencedOccurrences(oDoc)
							If oList.ContainsKey(parentOcc.Name)
								oList.Item(parentOcc.Name) += 1
							Else
								oList.Add(parentOcc.Name, 1)
							End If
							pNrExists = True
						Next
					End If
				End If
			Next
		End If
		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
Message 6 of 7

Moverano
Advocate
Advocate

What is the diferrence with the bill material?  In it you already see all the data such as the piece (part number) quantity

0 Likes
Message 7 of 7

Vickimcl
Enthusiast
Enthusiast

@JhoelForshav 

 

Love your code. Fast and easy.

 

I noticed sometimes the qty per assembly is not always correct, it seems to occur with phantom parts (?)

0 Likes