Error getting ComponentDefinition of BOMRow object within iAssembly (VBA)

Error getting ComponentDefinition of BOMRow object within iAssembly (VBA)

Anonymous
Not applicable
1,133 Views
5 Replies
Message 1 of 6

Error getting ComponentDefinition of BOMRow object within iAssembly (VBA)

Anonymous
Not applicable

Hello all,

 

I found a unique scenario where when traversing the BOMRow of the structured BomView, the ComponentDefinition is "Nothing" (During this line in the code below: Set oCompDef = oRow.ComponentDefinitions.Item(1)).

 

image.png

 

 

It only happens to items that are in iAssemblies that are two subassemblies deep. I've attached my file but here is the structured of my Assembly:

 

  1. Assembly1.iam
    1. SubAssembly1.iam
      1. SubSubAssembly1.iam
      2. SubSubAssembly2.iam

Assembly1.iam and SubAssembly1.iam are iAssemblies each with a different configurations of it's child assemblies.

 

The end goal is to export all the items from the highest level assembly, depending on which member is active (Assembly1.iam). Why am I unable to get access to the ComponentDefinition? It works in all other scenarios which makes me believe it has something to do with how Inventor combines the parts in the BOMView, specifially in iAssemblies.

 

Here is the code I am using, it's the same as ones I've seen floating around these forums. I am running Inventor 2018.1

 

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")
        
    Debug.Print "Item"; Tab(15); "Quantity"; Tab(30); "Part Number"; Tab(70); "Description"
    Debug.Print "----------------------------------------------------------------------------------"

    'Initialize the tab for ItemNumber
    Dim ItemTab As Long
    ItemTab = -3
    Call QueryBOMRowProperties(oBOMView.BOMRows, ItemTab)
End Sub

Private Sub QueryBOMRowProperties(oBOMRows As BOMRowsEnumerator, ItemTab As Long)
    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")

            Debug.Print Tab(ItemTab); oRow.ItemNumber; Tab(17); oRow.ItemQuantity; Tab(30); _
                oPartNumProperty.Value; Tab(70); 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")

            Debug.Print Tab(ItemTab); oRow.ItemNumber; Tab(17); oRow.ItemQuantity; Tab(30); _
                oPartNumProperty.Value; Tab(70); 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
0 Likes
Accepted solutions (1)
1,134 Views
5 Replies
Replies (5)
Message 2 of 6

jjstr8
Collaborator
Collaborator

I'm on 2017, so I couldn't use your attached files.  I attempted to recreate it with the following:

  • Created two empty assemblies (SubSubAssembly1 & SubSubAssembly2)
  • Created an assembly and added the two subsub assemblies and then made it an iAssembly (one member), saved as SubAssembly1
  • Created an assembly and placed SubAssembly1 (the only member), made it an iAssembly (one member), saved as Assembly1
  • Ran your code, "no" to first-level only

Output was

Item          Quantity       Part Number                             Description
----------------------------------------------------------------------------------
1                1           SubAssembly1-01                         
  1.1            1           SubSubAssembly1                         
  1.2            1           SubSubAssembly2   
0 Likes
Message 3 of 6

Anonymous
Not applicable

Unfortunately that is not how I have it setup. I think the critical thing here is there are multiple members which causes some sort of roll-up error. Here is what I have:

  • Created two assemblies each with a part in them (SubSubAssembly1 & SubSubAssembly2)
  • Created an assembly and added the two subsub assemblies and then made it an iAssembly (Two members, where one member has SubSubAssembly1 active and SubSubAssembly2 excluded, the other member has the opposite), saved as SubAssembly1
  • Created an assembly and made it an iAssembly (Two members), saved as Assembly1. Added SubAssembly1 where the first member of Assembly1 points to the first member of SubAssembly1 and the second member of Assembly1 points to the second member of SubAssembly1.
  • Ran the code, "yes" to first-level only (Because Inventor won't show all-levels if you go to the Bill of Materials screen of Assembly1)

Here are images showing my iAssembly tables:

 
 
 

SubAssembly1 tableSubAssembly1 tableAssembly1 tableAssembly1 table

0 Likes
Message 4 of 6

jjstr8
Collaborator
Collaborator
Accepted solution

Thanks for the additional details.  According to the documentation, the structured BOM in iAssemblies only supports first-level.  If I'm understanding what you're trying to do, you might want to use the model BOM.  Unfortunately, there's no item numbers.

 

Set oBOMView = oBOM.BOMViews.Item("Unnamed")

 

0 Likes
Message 5 of 6

Anonymous
Not applicable

Yes I was able to get that to export however I was hoping to do the structured view since it rolls up the quantities if the same part number is found in different subassemblies. How come the native export BOM button on the BOM screen allows an export of the structured view? It will grab everything I want it to, even two sub iAssemblies deep.

0 Likes
Message 6 of 6

jjstr8
Collaborator
Collaborator

The API has a number of limitations that native Inventor commands don't have.  Another odd one with BOM is there's no structured or parts-only views available through the API when the LOD is not set to master, however, they still show up with the native BOM command.  The best we can do is post suggestions and hope Autodesk listens.  In your case, a manual roll-up sounds like the simple work-around.

0 Likes