@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