OK, so you have a drawing of a part, and you want to show on that drawing how many of this part are within these three other assembly's, right? There are likely many ways to do this, but likely none of them are going to be as convenient as you would like, because its complicated to do manually, so that complication level is going to multiply when code gets is involved. And code 'might' be the best automation tool here.
First we have to get those quantities. There are so many variables involved in this task, especially when the task is remote, it's hard to ask about them all. In this code, I would need to uniquely identify these 3 assemblies we are going to search through, one way or another. I'm just thinking in the most basic terms here and going to suggest manually entering (or cut/paste) those full file names into the start of the code. Next we need to know what we are searching for within each of them. There are many ways of identifying a component or part document within an assembly. I'll just choose to use the full file name of the Part in my example below.
This doesn't even try to use the BOM, or avoid content center stuff, it simply opens each listed assembly file, then searches through it for components that represent the Part file name specified, then adds 1 to the quantity for each one it finds.
Sub Main
Dim oAssemblyFiles As New List(Of String)
oAssemblyFiles.Add("C:\Temp\Machine 1.iam")
oAssemblyFiles.Add("C:\Temp\Machine 2.iam")
oAssemblyFiles.Add("C:\Temp\Machine 3.iam")
oPartFile = "C:\Temp\Part 5.ipt"
'create a variable to hold the quantity
Dim oQuantity As New List(Of Integer)
For Each oName In oAssemblys
Dim oAsmQuantity As Integer = 0
Dim oADoc As AssemblyDocument
If System.IO.File.Exists(oName) Then
oADoc = ThisApplication.Documents.Open(oName, False)
Else
MsgBox("Could not find that file. Skipping to next file name.", vbExclamation, "iLogic")
Continue For
End If
If IsNothing(oADoc) Then Exit Sub
oAllOccs = oADoc.ComponentDefinition.Occurrences
'run our custom Sub routine below
StepDown(oAllOccs, oPartFile, oAsmQuantity)
oQuantity.Add(oAsmQuantity)
' MsgBox("In this assembly:" & vbCrLf & oName & vbCrLf & _
' "we found " & oAsmQuantity & " of the specified Part.", vbInformation, "iLogic")
Next
'now our oQuantity list should contain the quantities of the specified Part in each listed assembly
For n As Integer = 1 To oAssemblyFiles.Count
For q As Integer = 1 To oQuantity.Count
MsgBox("In the assembly:" & vbCrLf & oAssemblyFiles.Item(n) & vbCrLf & _
"we found " & oQuantity.Item(q) & " of the specified Part.", vbInformation, "iLogic")
Next
Next
End Sub
Sub StepDown(oComps As ComponentOccurrences, oPartName As String, ByRef oAsmQuantity As Integer)
For Each oComp As ComponentOccurrence In oComps
Try
If oComp.Definition.Document.FullFileName = oPartName Then
oAsmQuantity = oAsmQuantity + 1
End If
Catch
End Try
If TypeOf oComp.Definition Is AssemblyComponentDefinition Then
StepDown(oComp.Definition.Occurrences, oPartName, oAsmQuantity)
End If
Next
End Sub
A more efficient process might have been to use something like the following:
oComp = ComponentOccurrences.ItemByName("occurrenceName1:1")
oCount = ComponentOccurrences.AllLeafOccurrences(oComp).Count
, but that needs you to specify a component name, which is often undesirable since they are usually auto-renamed.
You could have found the first component in the assembly of that Part, then used that component in that line, but that also involves a process which usually includes loops.
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 want and have time, I would appreciate your Vote(s) for My IDEAS 💡 or you can Explore My CONTRIBUTIONS
Wesley Crihfield

(Not an Autodesk Employee)