Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Ilogic to count parts in subassembly

6 REPLIES 6
SOLVED
Reply
Message 1 of 7
goran.nilssonRSJCU
1717 Views, 6 Replies

Ilogic to count parts in subassembly

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?

Labels (1)
6 REPLIES 6
Message 2 of 7

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

 

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

@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

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

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

Message 7 of 7
Vickimcl
in reply to: JhoelForshav

@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 (?)

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report