By Barbara Han
Though you have access to the BOM object through AssemblyComponentDefinition.BOM even in case of a document which is using a LOD (Level Of Detail) other than the Master, this only provides access to the "Model Data" view ("Unnamed"/kModelDataBOMViewType).
The "Structured" and "Parts Only" BOMView objects can only be accessed from a Master LOD document. So if the currently open assembly document is using a LOD other than the Master, then you can open its Master LOD version in the background and access those BOM views from there.
The below code sample shows how to do it:
VB.NET
Public Sub BOMQuery()
Dim m_inventorApp As Inventor.Application = Nothing
' Try to get an active instance of Inventor
Try
m_inventorApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application")
Catch ex As Exception
End Try
' If not active, create a new Inventor session
If m_inventorApp Is Nothing Then
Dim inventorAppType As Type = System.Type.GetTypeFromProgID("Inventor.Application")
m_inventorApp = System.Activator.CreateInstance(inventorAppType)
End If
Dim oDoc As AssemblyDocument
Dim oBOM As BOM
' Set a reference to the assembly document.
' This assumes an assembly document is active.
oDoc = m_inventorApp.ActiveDocument
'Gets back Master LOD
Dim oAsmDocMasterLOD As AssemblyDocument
oAsmDocMasterLOD = m_inventorApp.Documents.Open(oDoc.File.FullFileName, False)
'Obtains BOM from Master LOD only
oBOM = oAsmDocMasterLOD.ComponentDefinition.BOM
'From here you can operate on the BOM object...
'Following lines of code are examples only
' Set whether first level only or all levels
oBOM.StructuredViewFirstLevelOnly = True
' Make sure that the structured view is enabled
oBOM.StructuredViewEnabled = True
MsgBox("BOM Views: " & oBOM.BOMViews.count)
End Sub
VBA
Public Sub BOMQuery()
Dim oDoc As AssemblyDocument
Dim oBOM As BOM
' Set a reference to the assembly document.
' This assumes an assembly document is active.
Set oDoc = ThisApplication.ActiveDocument
'Gets back Master LOD
Dim oAsmDocMasterLOD As AssemblyDocument
Set oAsmDocMasterLOD = ThisApplication.Documents.Open(oDoc.file.FullFileName, False)
'Obtains BOM from Master LOD only
Set oBOM = oAsmDocMasterLOD.ComponentDefinition.BOM
'From here you can operate on the BOM object...
'Following lines of code are examples only
' Set whether first level only or all levels
oBOM.StructuredViewFirstLevelOnly = True
' Make sure that the structured view is enabled
oBOM.StructuredViewEnabled = True
MsgBox "BOM Views: " & oBOM.BOMViews.count
End Sub
// Edited by Adam Nagy