- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi @checkcheck_master. I'm sure there are other similar examples out there, but this is an example of one that I have used before. The Sub being used is fairly similar to your first Function code. I simplified the Sub Main code just for this example, so I'm sure you will want to use a different means to specify which document you want to find the total quantity of. And of course there are other ways of doing something like this, without using the BOM, depending on your specific requirements and the details of your scenario.
Sub Main
If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "")
Exit Sub
End If
Dim oADoc As AssemblyDocument = ThisApplication.ActiveDocument
oBOM = oADoc.ComponentDefinition.BOM
oBOM.StructuredViewEnabled = True
oBOM.StructuredViewFirstLevelOnly = False
oBOMView = oBOM.BOMViews.Item("Structured")
Dim oDocToCountQtyOf As Document = oADoc.ReferencedDocuments.Item(3)
Dim oTotalQty As Integer = 0
ProcessBOMRowsRecursively(oBOMView.BOMRows, oDocToCountQtyOf, oTotalQty)
MsgBox("Total Quantity found = " & oTotalQty, , "")
End Sub
Sub ProcessBOMRowsRecursively(oBOMRows As BOMRowsEnumerator, oDocToFind As Document, ByRef oTotalQty As Integer)
For Each oRow As BOMRow In oBOMRows
If oRow.BOMStructure = BOMStructureEnum.kPhantomBOMStructure Or _
oRow.BOMStructure = BOMStructureEnum.kReferenceBOMStructure Then
Continue For
End If
Dim oCD As ComponentDefinition = Nothing
oCD = oRow.ComponentDefinitions.Item(1)
If TypeOf oCD Is VirtualComponentDefinition Then Continue For
Dim oRowDoc As Document = Nothing
oRowDoc = oCD.Document
If oRowDoc Is oDocToFind Then
oTotalQty = oTotalQty + oRow.ItemQuantity
'oTotalQty = oTotalQty + oRow.TotalQuantity
End If
If Not oRow.ChildRows Is Nothing Then
ProcessBOMRowsRecursively(oRow.ChildRows, oDocToFind, oTotalQty)
End If
Next
End Sub
Sometimes I choose to use an additional Sub or Function to do the core functionality, then simply call that one within my recursive routine, in order to keep the recursive routine simpler and cleaner looking. But that's just situational preference.
Edit: I mistakenly posted an older version of the code as a Function earlier, then updated the code to the new Sub version later. I often have several versions of some of my codes stored in various places. Sometimes I did not change their names and get older versions confused with newer versions. I've got to do some house cleaning on my file system. ![]()
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 :bulb: or you can Explore My CONTRIBUTIONS
Wesley Crihfield
(Not an Autodesk Employee)