ACCESSING INVENTOR 2023 BOM THROUGH API

ACCESSING INVENTOR 2023 BOM THROUGH API

K.TRYFONIDIS
Advocate Advocate
1,221 Views
5 Replies
Message 1 of 6

ACCESSING INVENTOR 2023 BOM THROUGH API

K.TRYFONIDIS
Advocate
Advocate

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.)



0 Likes
1,222 Views
5 Replies
Replies (5)
Message 2 of 6

CattabianiI
Collaborator
Collaborator

The error is quite self-explanatory: there is no BomCellValues field in BOMRow object.
ChatGPT and Inventor API are not a great duo.
Just check the manual to find out something similar to the cell concept: https://help.autodesk.com/view/INVNTOR/2023/ENU/?guid=GUID-D475FABC-33CE-4F0D-B15C-C92805BB23A7

Or just give a look at this example to export the BOM view:
https://help.autodesk.com/view/INVNTOR/2023/ENU/?guid=GUID-AA34F1D0-C486-4C18-903D-D34FA47D1131

Message 3 of 6

K.TRYFONIDIS
Advocate
Advocate

Thank you for taking the time to respond.

 

I have seen also those links but had no luck finding a read cell value Syntax. I assume would be much moe harder to transfer the thumbnails too..

 

The guides I found only apply for rows but i tried that and didn't work. I had compile errors.

 

You can now paste links to chatgpt to read and improve code but it seems there isn't a proper function for the job I want to do.

 

I don't know much about coding but I am trying to self learn.

 

The thing that bothers me the most is that despite the job seems simply and basically, all I want to do is copy the BOM to an external data grid, there aren't any proper guides to accomplish this.

 

While it is literally 1 click job to export it in Microsoft Excel manually, including thumbnails.

 

0 Likes
Message 4 of 6

CattabianiI
Collaborator
Collaborator

> While it is literally 1 click job to export it in Microsoft Excel manually, including thumbnails.

Have you seen the second link I posted? It's that click done via api, if i'm not wrong the thumbnail will be exported.

And if you want to create your datagrid this is a good example to start with: https://help.autodesk.com/view/INVNTOR/2023/ENU/?guid=GUID-33A8C8DA-FBD9-41DF-A7BD-793C693A485A

0 Likes
Message 5 of 6

K.TRYFONIDIS
Advocate
Advocate

Hello,
Sorry i didn't responded earlier, didn't had the time.

I tried pasting the code at the link inside visual studio 2019 but i get several errors and warnings.

Seems like the code syntax is outdated.

KTRYFONIDIS_0-1688550026984.png

 

0 Likes
Message 6 of 6

CattabianiI
Collaborator
Collaborator

The link I shared earlier contains VBA code, so you'll need to convert it to vb.net. In your case, this means removing the 'Let' and 'Set' statements.

The other errors are related to the missing Inventor interop reference. VB.NET is not a scripting language. You can find numerous posts in this forum or on Autodesk blogs to help you get started with add-ins or other approaches for working with Inventor.

Alternatively, you can explore iLogic as an option!