Count Components in an Assembly by name

Count Components in an Assembly by name

philip.tuttleQLWHY
Explorer Explorer
2,008 Views
7 Replies
Message 1 of 8

Count Components in an Assembly by name

philip.tuttleQLWHY
Explorer
Explorer

Trying to return a count on objects in my assembly based on the name I have assigned to the component.  I basically want to be able to do a count of how many of each panel has been used, then I can pull that count to another iLogic formula and create more of the same panels if needed.

 

Here is what I have:

 

' Get the active assembly.
Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisApplication.ActiveDocument

' Get the assembly component definition.
Dim oAsmDef As AssemblyComponentDefinition
oAsmDef = oAsmDoc.ComponentDefinition

' Get all of the leaf occurrences of the assembly.
Dim oLeafOccs As ComponentOccurrencesEnumerator
oLeafOccs = oAsmDef.Occurrences.AllLeafOccurrences

' Iterate through the occurrences and print the name.
Dim cnt As Integer
cnt = 0
Dim oOcc As ComponentOccurrence
Dim OCCNAME As String = oOcc.Name.Contains("PANEL xxx-xxxxxxxx")
For Each OCCNAME In oLeafOccs
cnt = cnt + 1
Next
MessageBox.Show(cnt, "PANEL COUNT")

 

Also, attached is a picture of my assembly. 

 

Thanks,

0 Likes
Accepted solutions (1)
2,009 Views
7 Replies
Replies (7)
Message 2 of 8

WCrihfield
Mentor
Mentor

Maybe this will work better for you.  I believe I fixed your loop check process.  You needed to loop through each component occurrence in the all leaf occurrences collection, not the String name.  Then check if the name of each component contains the target string.  If so, increase the count.

Here's the updated code:

 

'Get the active assembly.
Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisApplication.ActiveDocument

' Get the assembly component definition.
Dim oAsmDef As AssemblyComponentDefinition
oAsmDef = oAsmDoc.ComponentDefinition

' Get all of the leaf occurrences of the assembly.
Dim oLeafOccs As ComponentOccurrencesEnumerator
oLeafOccs = oAsmDef.Occurrences.AllLeafOccurrences

' Iterate through the occurrences and print the name.
Dim cnt As Integer
cnt = 0
Dim oOcc As ComponentOccurrence
For Each oOcc In oLeafOccs
	If oOcc.Name.Contains("PANEL xxx-xxxxxxxx") Then
		cnt = cnt + 1
	End If
Next
MessageBox.Show(cnt, "PANEL COUNT")

 

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.

If you have time, please... Vote For My IDEAS 💡or you can Explore My CONTRIBUTIONS

Inventor 2021 Help | Inventor Forum | Inventor Customization Forum | Inventor Ideas Forum

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 8

philip.tuttleQLWHY
Explorer
Explorer

I am getting a return but the count is always 0.  So, it seems to be going through the logic fine but the count is not actually counting. I get the message box popping up but no increase in the count. the disconnect seems to be recognizing the name of the panel. I have tried substituting  the name with a variable and still get returned with a 0 for a count.

0 Likes
Message 4 of 8

WCrihfield
Mentor
Mentor

I assumed you intended to change "PANEL xxx-xxxxxxxx" to the specific name of an existing component occurrence.  If you leave "PANEL xxx-xxxxxxxx" there, it won't find any of them.  If you don't want to specify the entire name of a specific panel, then maybe you should eliminate the "xxx-xxxxxxxx" part, and just check if its name contains "PANEL", instead.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 8

philip.tuttleQLWHY
Explorer
Explorer

Yes I did this. I used the name that I assign it upon adding it to the assembly. I have the name "PANEL 1xxxxx" whatever and it assign that name based on the panel size, it then makes multiple copies depending on how many are needed. So the part in my assembly tree is "name" & ":" & "number used". Now the name assignment is set at when the part gets added but the name only applies in the assembly the filename is saved as something else. 

 

I am trying to access and count these parts only as the assigned name in the assembly. I have done as you said and the count still returns a zero. just using "panel" yields no change in count. I assumed with me setting this to ThisAssembly that would allow for that to happen. I am not sure why this is cant pull it since everything seems synced up.

 

Thanks for your help so far. 

0 Likes
Message 6 of 8

philip.tuttleQLWHY
Explorer
Explorer

So my code works! The issue I am having is the parts that I am putting in are not. ipt, they are .iam. my code works perfectly on .ipt but since what im counting are .iam the count does not work.  If someone would be able to help me find the soultion to make this work counting the components that I put in as sub asemblies, that would be much appreciated.

0 Likes
Message 7 of 8

dutt.thakar
Collaborator
Collaborator
Accepted solution

@philip.tuttleQLWHY 

 

As you are iterating through leaf occurrences, it will not consider iam files. Try below code, it will count only the assemblies that contain a specific name inside your main assembly, please note that this will not go to all levels, it will only count on the first level.

 

'Get the active assembly.
Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisApplication.ActiveDocument

' Get the assembly component definition.
Dim oAsmDef As AssemblyComponentDefinition
oAsmDef = oAsmDoc.ComponentDefinition

' Iterate through the occurrences and print the name.
Dim cnt As Integer
cnt = 0
Dim oOcc As ComponentOccurrence
For Each oOcc In oAsmDef.Occurrences
	If oOcc.Name.Contains("PANEL xxx-xxxxxxxx") And oOcc.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
		cnt = cnt + 1
	End If
Next
MessageBox.Show(cnt, "PANEL COUNT")

If you want to iterate through all levels, you need to use a recursive loop. See below code. I assume that the name panel which you are looking for is only applicable for assembly files and not the part files, so it will only search the assemblies, which has the name "Panel" inside it. You can change the second line whenever you want to change the search criteria, so instead of the word panel, if you want to count assemblies that contain another name just change in the second line.

 

Sub Main
Dim oAsm As AssemblyDocument = ThisDoc.Document
Dim oName As String = "PANEL" ' You can add your search name here for which want to count
MessageBox.Show("Number of occurrences with name: " & oName & " = " & CountOccs(oAsm, oName), "Count occurrences")
End Sub
Function CountOccs(oAssy As AssemblyDocument, oName As String) As Integer
	Dim oCount As Integer = 0
	For Each oOcc As ComponentOccurrence In oAssy.ComponentDefinition.Occurrences
		If TypeOf (oOcc.Definition) Is AssemblyComponentDefinition
			oCount += CountOccs(oOcc.Definition.Document, oName)
		End If
			If oOcc.Name.Contains(oName) And oOcc.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then oCount += 1
	Next
	Return oCount
End Function

Hope this will help.

If this answer has solved your problem please ACCEPT SOLUTION and hit like if you found it helpful..!


Regards,
Dutt Thakar
LinkedIn
0 Likes
Message 8 of 8

philip.tuttleQLWHY
Explorer
Explorer

Works Perfect! Thanks for the help!

0 Likes