Issue when getting total quantity of Parts and Subassemblies

Issue when getting total quantity of Parts and Subassemblies

sergio_duran
Advocate Advocate
815 Views
5 Replies
Message 1 of 6

Issue when getting total quantity of Parts and Subassemblies

sergio_duran
Advocate
Advocate

Hi Everyone,

 

I'm using a code that gets the total quantity of Parts and Subassemblies. As you know, the BOM View Parts Only only shows total quantity of parts but not the Normal Subassemblies. BOM View Structured shows the total quantity per subassembly but not the total quantity of parts and subassemblies in the whole assembly regardless of the level. My code works well except when my subassemblies are set to a LOD. For instance, my Top Level Assembly is set Custom LOD 'iLogic' and this LOD calls LODs in subassemblies (e.g. Subassembly1(iLogic) - iLogic is the LOD name). Do you know why it doesn't work? if so how can I fix it? Thanks! Here is the code (BTW, I'm traversing the assembly with Referenced Documents)

SyntaxEditor Code Snippet

'***GETS TOTAL QTY OF PARTS AND SUBASSEMBLIES
Sub Main()
'Gets the document where this rule is
oDoc = ThisDoc.Document
ThisDoc.Save

'Makes sure the document is an assembly
If oDoc.DocumentType = kAssemblyDocumentObject
    
    'Get all Of the referenced documents. 
    Dim oRefDocs As DocumentsEnumerator 
    oRefDocs = oDoc.AllReferencedDocuments
    
    Dim oRefDoc As Document

'[ 'GETS THE TOTAL QUANTITY OF COMPONENTS
    For Each oRefDoc In oRefDocs
        'Make sure the file can be edited
        Try
            'Make sure the file can be edited
            If oRefDoc.IsModifiable = True Then
                Dim AssyDoc As AssemblyDocument
                AssyDoc = oDoc
                'Gets all assembly occurrences
                Dim oOccs As ComponentOccurrences
                oOccs = AssyDoc.ComponentDefinition.Occurrences
                'Gets all referenced document occurrences
                Dim oFileOccs As ComponentOccurrencesEnumerator
                oFileAllOccs = oOccs.AllReferencedOccurrences(oRefDoc)
                
                'Gets Referenced Doc Custom properties
                Dim oCustomProps As PropertySet
                oCustomProps = oRefDoc.PropertySets.Item("Inventor User Defined Properties")
                
                'Gets/adds the custom property 'TotalQty' and gets the Total Quantity
                Dim oTotalQty As Inventor.Property
                Try
                oTotalQty = oCustomProps.Item("TotalQty")
                Catch
                oTotalQty = oCustomProps.Add("", "TotalQty")
                Finally
                oTotalQty.Value = oFileAllOccs.Count
                End Try
            End If
        Catch
        'All instances of the file are suppressed
        End Try
    Next
    
End  If
End Sub

 

 

0 Likes
816 Views
5 Replies
Replies (5)
Message 2 of 6

bradeneuropeArthur
Mentor
Mentor
You need to use the BOM object fir this.
Loop through the BOM occurences and you have what you need.
I have an example in tuesday for you.

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 3 of 6

sergio_duran
Advocate
Advocate

Thanks for your reply last Friday! I'd really appreciate any help. I thought it should be possible with Referenced Documents. Please send me the example. Thanks!

0 Likes
Message 4 of 6

bradeneuropeArthur
Mentor
Mentor

Sorry forgotten due to the many topics I follow and respond to.

Here you have something for VBA:

 

'activate excel to open file
'Set oExcelApp = New Excel.Application
Dim oExcelApp As excel.Application
Public Sub BOMQuery()
    ' Set a reference to the assembly document.
    ' This assumes an assembly document is active.
    Dim oDoc As AssemblyDocument
    Set oDoc = ThisApplication.ActiveDocument

    Dim FirstLevelOnly As Boolean
    If MsgBox("First level only?", vbYesNo) = vbYes Then
        FirstLevelOnly = True
    Else
        FirstLevelOnly = False
    End If
    
    ' Set a reference to the BOM
    Dim oBOM As BOM
    Set oBOM = oDoc.ComponentDefinition.BOM
    
    ' Set whether first level only or all levels.
    If FirstLevelOnly Then
        oBOM.StructuredViewFirstLevelOnly = True
    Else
        oBOM.StructuredViewFirstLevelOnly = False
    End If
    
    ' Make sure that the structured view is enabled.
    oBOM.StructuredViewEnabled = True
    
    'Set a reference to the "Structured" BOMView
    Dim oBOMView As BOMView
    Set oBOMView = oBOM.BOMViews.Item("Structured")

Set oExcelApp = New excel.Application

oExcelApp.Visible = True
oExcelApp.Workbooks.Add

oExcelApp.ActiveSheet.Name = ThisApplication.ActiveDocument.DisplayName
oExcelApp.Rows("1:1").Font.Bold = True


oExcelApp.ActiveSheet.Cells(1, 1) = "Item"
oExcelApp.ActiveSheet.Cells(1, 2) = "Quantity"
oExcelApp.ActiveSheet.Cells(1, 3) = "Part Number"
oExcelApp.ActiveSheet.Cells(1, 4) = "Description"
       
    'Initialize the tab for ItemNumber
    Dim ItemTab As Long
    ItemTab = -3
    Call QueryBOMRowProperties(oBOMView.BOMRows, ItemTab)
End Sub

Public Sub QueryBOMRowProperties(oBOMRows As BOMRowsEnumerator, ItemTab As Long)

Set oExcelApp = excel.Application

    ItemTab = ItemTab + 3
    ' Iterate through the contents of the BOM Rows.
    Dim i As Long
    For i = 1 To oBOMRows.Count
        ' Get the current row.
        Dim oRow As BOMRow
        Set oRow = oBOMRows.Item(i)

        'Set a reference to the primary ComponentDefinition of the row
        Dim oCompDef As ComponentDefinition
        Set oCompDef = oRow.ComponentDefinitions.Item(1)

        Dim oPartNumProperty As Property
        Dim oDescripProperty As Property

        If TypeOf oCompDef Is VirtualComponentDefinition Then

            'Get the file property that contains the "Part Number"
            'The file property is obtained from the virtual component definition
            Set oPartNumProperty = oCompDef.PropertySets.Item("Design Tracking Properties").Item("Part Number")

            'Get the file property that contains the "Description"
            Set oDescripProperty = oCompDef.PropertySets.Item("Design Tracking Properties").Item("Description")
                
'oExcelApp.ActiveSheet.Cells(i + 1, 1) = oRow.ItemNumber
'oExcelApp.ActiveSheet.Cells(i + 1, 2) = oRow.ItemQuantity
'oExcelApp.ActiveSheet.Cells(i + 1, 3) = oPartNumProperty.Value
'oExcelApp.ActiveSheet.Cells(i + 1, 4) = oDescripProperty.Value

        Else

            'Get the file property that contains the "Part Number"
            'The file property is obtained from the parent
            'document of the associated ComponentDefinition.
            Set oPartNumProperty = oCompDef.Document.PropertySets.Item("Design Tracking Properties").Item("Part Number")

            'Get the file property that contains the "Description"
            Set oDescripProperty = oCompDef.Document.PropertySets.Item("Design Tracking Properties").Item("Description")
            
oExcelApp.ActiveSheet.Cells(i + 1, 1) = oRow.ItemNumber
oExcelApp.ActiveSheet.Cells(i + 1, 2) = oRow.ItemQuantity
oExcelApp.ActiveSheet.Cells(i + 1, 3) = oPartNumProperty.Value
oExcelApp.ActiveSheet.Cells(i + 1, 4) = oDescripProperty.Value

            'Recursively iterate child rows if present.
            If Not oRow.ChildRows Is Nothing Then
                Call QueryBOMRowProperties(oRow.ChildRows, ItemTab)
            End If
        End If
    Next
    ItemTab = ItemTab - 3

End Sub

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 5 of 6

sergio_duran
Advocate
Advocate

Thanks for your help! But I think this is not my question. You sent me a modified version of one of the samples of the API Help (Using the BOM APIs). I'd mentioned that the Structured tab doesn't help me since it gives me the total quantity of parts per Subassembly. I need the WHOLE total quantity of parts and subassemblies regardless of the level they are. As I'd mentioned my code works except when I have Subassemblies with Active LODs. Thanks for your help anyway.

0 Likes
Message 6 of 6

bradeneuropeArthur
Mentor
Mentor

Sorry misunderstood.
In Vb.net you could use dictionaries to count each partnumber and add it to the dictionary.
Afterwards you could export the dictionary with the total quantities.
Do you know what I mean.
Preparing this will take some effort, but if you understand how to do it, then it is not that complicated.

Regards,

Autodesk Software: Inventor Professional 2018 | Vault Professional 2018 | Autocad Mechanical 2018
Programming Skills: Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Dimension Component! | Partlist Export! | Derive I-properties! | Vault Prompts Via API! | Vault Handbook/Manual!
Drawing Toggle Sheets! | Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes