ACCESSING INVENTOR 2023 BOM THROUGH API
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello everyone,
I am trying to make an exe file via Visual studio 2019 and be able to read the data from Bill of Materials in the currently open assembly document of my Autodesk Inventor 2023 then clone the parts only tab table to a datagrid and see what i can do with the data from there.
Since the Bom table provided by inventor is straight forward i want to take advantage of this function and not read the properties of the files again inside the assembly.
* i don't want the export to excel, i would like the program to be independent from excel or other programs.
I have tried to use chatgpt for coding but this is the far as i went to.
i get no compile errors but when i run the code i get:
System.MissingMemberException
HResult=0x80131512
Message=Public member 'BOMCellValues' on type 'BOMRow' not found.
Source=Microsoft.VisualBasic
StackTrace:
at Microsoft.VisualBasic.CompilerServices.LateBinding.LateGet(Object o, Type objType, String name, Object[] args, String[] paramnames, Boolean[] CopyBack)
at Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateGet(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack)
at Pergolas_Part_Lists.Form1.Form1_Load(Object sender, EventArgs e) in C:\Users\KTrifonidisNyfan\Visual Basic\source\repos\DESIGN\Pergolas Part Lists\Form1.vb:line 45
at System.EventHandler.Invoke(Object sender, EventArgs e)
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
My code:
Imports Inventor
Imports System.Data
Imports System.Drawing
Imports System.Windows.Forms
Public Class Form1
Inherits Form
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Initialize Autodesk Inventor application
Dim inventorApp As Inventor.Application = Nothing
Try
inventorApp = DirectCast(System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application"), Inventor.Application)
Catch ex As Exception
MessageBox.Show("Autodesk Inventor is not running or not accessible.", "Error")
Me.Close()
Return
End Try
' Retrieve the BOM data
Dim dataTable As New DataTable()
' Get the active document
Dim activeDoc As Document = inventorApp.ActiveDocument
' Check if the active document is an assembly document
If TypeOf activeDoc Is AssemblyDocument Then
' Cast the active document as an assembly document
Dim assemblyDoc As AssemblyDocument = DirectCast(activeDoc, AssemblyDocument)
' Get the Bill of Materials
Dim bom As BOM = assemblyDoc.ComponentDefinition.BOM
Dim bomView As BOMView = bom.BOMViews.Item("Parts Only")
If bomView Is Nothing Then
MessageBox.Show("The 'Parts Only' BOM view is not available.", "Error")
inventorApp.Quit() ' Close Inventor application
Me.Close() ' Close the form
Return
End If
' Add columns to the DataTable based on the BOM view
For Each row As BOMRow In bomView.BOMRows
For Each cellValue As String In row.BOMCellValues
dataTable.Columns.Add(cellValue, GetType(String))
Next
Exit For ' We only need to process the first row for column headers
Next
' Process the rows in the BOM view
For Each row As BOMRow In bomView.BOMRows
Dim rowData As New List(Of Object)()
' Retrieve the data for each cell in the row
For Each cellValue As String In row.BOMCellValues
rowData.Add(cellValue)
Next
' Add a row to the DataTable with the BOM data
dataTable.Rows.Add(rowData.ToArray())
Next
' Bind the DataTable to a DataGridView control on the form
Dim dataGridView As New DataGridView()
dataGridView.DataSource = dataTable
dataGridView.Dock = DockStyle.Fill
Me.Controls.Add(dataGridView)
Else
MessageBox.Show("The active document is not an assembly document.", "Error")
End If
' Release the resources
If inventorApp IsNot Nothing Then
System.Runtime.InteropServices.Marshal.ReleaseComObject(inventorApp)
End If
End Sub
End Class
Since i have only autodesk vault basic ( can't get professional yet ) my final goal is to make universal reports ( without the need of excel software or other program. Internet browser is okay ) of 5 top assemblies and be able to share with other departments ( accounting, management, production.)