Automatic creation of columns in iPart.

Automatic creation of columns in iPart.

istehno
Participant Participant
1,065 Views
10 Replies
Message 1 of 11

Automatic creation of columns in iPart.

istehno
Participant
Participant

I would like to create/update an iPart using a function or iLogic, where a column is created from custom properties (e.g., item_name_cz, modificationtext_0, etc.). We work a lot with iParts, and this functionality would greatly simplify our workflow. Ideally, it would also compare whether a value already exists in the table, and if it does, skip it.

istehno_0-1725956304423.png

 

 

0 Likes
Accepted solutions (1)
1,066 Views
10 Replies
Replies (10)
Message 2 of 11

jiri_benes
Explorer
Explorer

Hi, @istehno I'm dealing with the same problem - I hope we can find a solution together! 

Best regards,
Jiri

0 Likes
Message 3 of 11

pawel.gromanski
Enthusiast
Enthusiast

Hi,

you can access iPart table coluns via oDoc.ComponentDefinition.iPartFactory.TableColumns and get existing column names. 
You can add columns by editing iPart spearsheet: oDoc.ComponentDefinition.iPartFactory.ExcelWorkSheet.

 

custom columns requier <free></free> tags
parameters just use name
properties are defined with "property name" ["property set name"]

 

Dim xlApp As excel.Application = New Microsoft.Office.Interop.Excel.Application
Dim xlWorkbooks As excel.Workbooks = xlApp.Workbooks
Dim xlWorkbook As excel.Workbook
Dim xlWorksheet As excel.Worksheet = oFactory.ExcelWorkSheet

 

xlWorksheet = oFactory.ExcelWorkSheet


xlWorksheet.Range("E1").Value = "NewParameter"
xlWorksheet.Range("E2").Value = "50 mm"
xlWorksheet.Range("E3").Value = "150 mmr"

 

 

 


xlWorkbook = xlWorksheet.Parent
xlWorkbook.Save()
xlWorkbook.Close()

 

0 Likes
Message 4 of 11

istehno
Participant
Participant

Hi,

 

Thank you for the suggestion, but I don't quite understand how to use Excel when creating a new column. Could I ask for an example of what the code would look like? Also, is it necessary to use Excel if I already know the name of the column and the value exists in custom properties?

 

 

0 Likes
Message 5 of 11

pawel.gromanski
Enthusiast
Enthusiast

 

 

        'requieres Microsoft Excel object library
        'Imports excel = Microsoft.Office.Interop.Excel

        Dim inventorApp As Inventor.Application = Nothing
        Try
            inventorApp = Marshal.GetActiveObject("Inventor.Application")
        Catch ex As Exception
            MessageBox.Show("Cannot connect to Inventor")
            Exit Sub
        End Try



        ' Get the active document (assuming it's an iPart factory document)
        Dim oDoc As PartDocument
        oDoc = inventorApp.ActiveDocument

        ' Ensure the document is an iPart factory
        If oDoc.ComponentDefinition.iPartFactory Is Nothing Then
            MsgBox("This document is not an iPart factory.")
            Exit Sub
        End If

        'Get iPartFactory
        Dim oFactory As iPartFactory
        oFactory = oDoc.ComponentDefinition.iPartFactory

        ' Start Excel application
        Dim xlApp As excel.Application = New Microsoft.Office.Interop.Excel.Application
        'Define workbook
        Dim xlWorkbook As excel.Workbook
        'Define workshett 
        Dim xlWorksheet As excel.Worksheet = oFactory.ExcelWorkSheet

        'parameter
        xlWorksheet.Range("E1").Value = "DIAMETER_BASE"
        xlWorksheet.Range("E2").Value = "20 mm"
        xlWorksheet.Range("E3").Value = "150 mm"

        'custom iProperty
        xlWorksheet.Range("F1").Value = "NAME [Inventor User Defined Properties]"
        xlWorksheet.Range("F2").Value = "name1"
        xlWorksheet.Range("F3").Value = "name2"


        'iProperty
        xlWorksheet.Range("G1").Value = "Subject [Inventor Summary Information]"
        xlWorksheet.Range("G2").Value = "xyz"
        xlWorksheet.Range("G3").Value = "asd"




        'get worksheet parent -> workbook
        xlWorkbook = xlWorksheet.Parent
        'save workbook
        xlWorkbook.Save()
        'close workbook
        xlWorkbook.Close()

 

 

 

I have attached test file and code, should be more clear now.

once you have your worksheet:
Dim xlWorksheet As excel.Worksheet = oFactory.ExcelWorkSheet

you can just add values to specific cells.



As far as I know there is no function to add column to the table.
You can add rows with:
oDoc.ComponentDefinition.iPartFactory.CreateMember()

0 Likes
Message 6 of 11

istehno
Participant
Participant
Thank you for the proposed solution, but if I get the value into Excel, the value loses its connection to the iProperties.
0 Likes
Message 7 of 11

pawel.gromanski
Enthusiast
Enthusiast

Could you please explain in more details what you mean?
Do you mean lining user defined iProperty to iProperty like below?

pawelgromanski_1-1726136698907.png

 

 



0 Likes
Message 8 of 11

istehno
Participant
Participant

I somehow put together a code in VBA that creates columns. But if I check it in the table, the columns are new without reference to the properties of the component.

Sub Test_iPart_Table_ver2()

' Connect to Inventor
Dim inventorApp As Object
On Error Resume Next
Set inventorApp = GetObject(, "Inventor.Application")
On Error GoTo 0

' If unable to connect to Inventor
If inventorApp Is Nothing Then
MsgBox "Cannot connect to Inventor"
Exit Sub
End If

' Get the active document (assuming it's an iPart factory)
Dim oDoc As Object
Set oDoc = inventorApp.ActiveDocument

' Verify if the document is an iPart factory
If oDoc.ComponentDefinition.iPartFactory Is Nothing Then
MsgBox "This document is not an iPart factory."
Exit Sub
End If

' Get the iPart factory
Dim oFactory As Object
Set oFactory = oDoc.ComponentDefinition.iPartFactory

' Start the Excel application in the background (invisible)
Dim xlApp As Excel.Application
Set xlApp = New Excel.Application
xlApp.Visible = False ' Excel runs in the background, not visible

' Define workbook and worksheet
Dim xlWorkbook As Excel.Workbook
Dim xlWorksheet As Excel.WorkSheet
Set xlWorksheet = oFactory.ExcelWorkSheet ' Get the Excel worksheet from iPart

' Get Custom iProperties
Dim oPropSets As PropertySets
Set oPropSets = oDoc.PropertySets

Dim oCustomProps As PropertySet
Set oCustomProps = oPropSets.Item("Inventor User Defined Properties")

' Get values from Custom iProperties
Dim item_name_cz As String
Dim modificationtext_0 As String
Dim add_titleblockinfo As String

On Error Resume Next ' Error protection
item_name_cz = oCustomProps.Item("item_name_cz").Value
modificationtext_0 = oCustomProps.Item("modificationtext_0").Value
add_titleblockinfo = oCustomProps.Item("add_titleblockinfo").Value
On Error GoTo 0 ' Restore normal error handling

' Check and add columns
Dim nextColumn As Integer

' Add column for item_name_cz
If Not ColumnExists("item_name_cz", xlWorksheet) Then
nextColumn = FindFirstFreeColumn(xlWorksheet)
xlWorksheet.Cells(1, nextColumn).Value = "item_name_cz"
xlWorksheet.Cells(2, nextColumn).Value = item_name_cz
Else
MsgBox "Column 'item_name_cz' already exists."
End If

' Add column for modificationtext_0
If Not ColumnExists("modificationtext_0", xlWorksheet) Then
nextColumn = FindFirstFreeColumn(xlWorksheet)
xlWorksheet.Cells(1, nextColumn).Value = "modificationtext_0"
xlWorksheet.Cells(2, nextColumn).Value = modificationtext_0
Else
MsgBox "Column 'modificationtext_0' already exists."
End If

' Add column for add_titleblockinfo
If Not ColumnExists("add_titleblockinfo", xlWorksheet) Then
nextColumn = FindFirstFreeColumn(xlWorksheet)
xlWorksheet.Cells(1, nextColumn).Value = "add_titleblockinfo"
xlWorksheet.Cells(2, nextColumn).Value = add_titleblockinfo
Else
MsgBox "Column 'add_titleblockinfo' already exists."
End If

' Get reference to workbook
Set xlWorkbook = xlWorksheet.Parent

' Save and close workbook
xlWorkbook.Save
xlWorkbook.Close

' Quit Excel application
xlApp.Quit
Set xlApp = Nothing

End Sub

' Function to check if a column exists
Function ColumnExists(columnName As String, xlWorksheet As Excel.WorkSheet) As Boolean
Dim cell As Excel.Range
For Each cell In xlWorksheet.Rows(1).Cells
If cell.Value = columnName Then
ColumnExists = True
Exit Function
End If
Next
ColumnExists = False
End Function

' Find the first free column
Function FindFirstFreeColumn(xlWorksheet As Excel.WorkSheet) As Integer
Dim cell As Excel.Range
For Each cell In xlWorksheet.Rows(1).Cells
If IsEmpty(cell.Value) Then
FindFirstFreeColumn = cell.Column
Exit Function
End If
Next
' If no free column exists, add a new one at the end
FindFirstFreeColumn = xlWorksheet.Cells(1, xlWorksheet.Columns.Count).End(xlToLeft).Column + 1
End Function

istehno_0-1726144700060.pngistehno_1-1726144801937.png

manually added values (there are duplicates)

0 Likes
Message 9 of 11

pawel.gromanski
Enthusiast
Enthusiast

your column name is incorrect, try this:
"item_name_cz [Inventor User Defined Properties]"

"modificationtext_0 [Inventor User Defined Properties]"
"add_titleblockinfo[Inventor User Defined Properties]"

 

I also don't understand why you're adding column name to first and second row?
xlWorksheet.Cells(1, nextColumn).Value = "add_titleblockinfo"
xlWorksheet.Cells(2, nextColumn).Value = add_titleblockinfo

??

this is how my table looks before and after running code:
before

pawelgromanski_0-1726153868380.png

after

pawelgromanski_1-1726153932108.pngpawelgromanski_2-1726153952418.png

 

 

0 Likes
Message 10 of 11

istehno
Participant
Participant

Thank you for helping with the code. I’ve put together this. It does exactly what I need, but it doesn't work on an assembly. Do you know how it could be adjusted? I'm not a good programmer, and AI helps me a lot with the code.

 

The code runs for a long time, but that's not a big problem.

 

Sub iPart_table_v1()
 
    ' Connect to Inventor
    Dim inventorApp As Object
    On Error Resume Next
    Set inventorApp = GetObject(, "Inventor.Application")
    On Error GoTo 0
    
    ' If unable to connect to Inventor
    If inventorApp Is Nothing Then
        MsgBox "Cannot connect to Inventor"
        Exit Sub
    End If
 
    ' Get the active document (assuming it is an iPart factory)
    Dim oDoc As Object
    Set oDoc = inventorApp.ActiveDocument
 
    ' Verify if the document is an iPart factory
    If oDoc.ComponentDefinition.iPartFactory Is Nothing Then
        MsgBox "This document is not an iPart factory."
        Exit Sub
    End If
 
    ' Get the iPart factory
    Dim oFactory As Object
    Set oFactory = oDoc.ComponentDefinition.iPartFactory
 
    ' Start Excel application in the background
    Dim xlApp As Excel.Application
    Set xlApp = New Excel.Application
 
    ' Optimize performance by turning off screen updates and events
    xlApp.ScreenUpdating = False
    xlApp.DisplayAlerts = False
    xlApp.EnableEvents = False
    
    xlApp.Visible = False ' Excel runs in the background
 
    ' Define the workbook and worksheet
    Dim xlWorkbook As Excel.Workbook
    Dim xlWorksheet As Excel.WorkSheet
    Set xlWorksheet = oFactory.ExcelWorkSheet ' Get the Excel sheet from iPart
 
    ' Get Custom iProperties
    Dim oPropSets As PropertySets
    Set oPropSets = oDoc.PropertySets
    
    Dim oCustomProps As PropertySet
    Set oCustomProps = oPropSets.Item("Inventor User Defined Properties")
    
    ' Get Summary iProperties
    Dim oSummaryProps As PropertySet
    Set oSummaryProps = oPropSets.Item("Inventor Summary Information")
    
    ' Get the value of Title from Summary iProperties
    Dim titleValue As String
    titleValue = oSummaryProps.Item("Title").Value
 
    ' Get values from Custom iProperties
 
    Dim modificationtext_0 As String
    Dim add_titleblockinfo As String
    Dim cad_system As String
Dim surface_finish As String
Dim item_id As String
    On Error Resume Next ' Error handling
 
    modificationtext_0 = oCustomProps.Item("modificationtext_0").Value
    add_titleblockinfo = oCustomProps.Item("add_titleblockinfo").Value
    cad_system = oCustomProps.Item("CAD System").Value
    surface_finish = oCustomProps.Item("Surface finish").Value
    item_id = oCustomProps.Item("Item ID").Value
    On Error GoTo 0 ' Resume normal error handling
 
    ' Check and add columns
    Dim nextColumn As Integer
    
    ' Add column for Title
    If Not ColumnExists("Title [Inventor Summary Information]", xlWorksheet) Then
        nextColumn = FindFirstFreeColumn(xlWorksheet)
        xlWorksheet.Cells(1, nextColumn).Value = "Title [Inventor Summary Information]"
        
    Else
        MsgBox "The 'Title' column already exists."
    End If
    
    ' Add column for Item ID
    If Not ColumnExists("Item ID [Inventor User Defined Properties]", xlWorksheet) Then
        nextColumn = FindFirstFreeColumn(xlWorksheet)
        xlWorksheet.Cells(1, nextColumn).Value = "Item ID [Inventor User Defined Properties]"
        
    Else
        MsgBox "The 'Item ID' column already exists."
    End If
 
    
    ' Add column for add_titleblockinfo
    If Not ColumnExists("add_titleblockinfo [Inventor User Defined Properties]", xlWorksheet) Then
        nextColumn = FindFirstFreeColumn(xlWorksheet)
        xlWorksheet.Cells(1, nextColumn).Value = "add_titleblockinfo [Inventor User Defined Properties]"
        
    Else
        MsgBox "The 'add_titleblockinfo' column already exists."
    End If
    
    ' Add column for Surface finish
    If Not ColumnExists("Surface finish [Inventor User Defined Properties]", xlWorksheet) Then
        nextColumn = FindFirstFreeColumn(xlWorksheet)
        xlWorksheet.Cells(1, nextColumn).Value = "Surface finish [Inventor User Defined Properties]"
        
    Else
        MsgBox "The 'Surface finish' column already exists."
    End If
 
    ' Add column for modificationtext_0
    If Not ColumnExists("modificationtext_0 [Inventor User Defined Properties]", xlWorksheet) Then
        nextColumn = FindFirstFreeColumn(xlWorksheet)
        xlWorksheet.Cells(1, nextColumn).Value = "modificationtext_0 [Inventor User Defined Properties]"
       
    ' Write the value "First revision" to all rows
    For rowIndex = 2 To xlWorksheet.UsedRange.Rows.Count
        xlWorksheet.Cells(rowIndex, nextColumn).Value = "First revision"
    Next rowIndex
          
    Else
        MsgBox "The 'modificationtext_0' column already exists."
    End If
    
   
    ' Add column for CAD System
    If Not ColumnExists("CAD System [Inventor User Defined Properties]", xlWorksheet) Then
        nextColumn = FindFirstFreeColumn(xlWorksheet)
        xlWorksheet.Cells(1, nextColumn).Value = "CAD System [Inventor User Defined Properties]"
          
    ' Write the value "Inventor" to all rows
    For rowIndex = 2 To xlWorksheet.UsedRange.Rows.Count
        xlWorksheet.Cells(rowIndex, nextColumn).Value = "Inventor"
    Next rowIndex
       
    Else
        MsgBox "The 'CAD System' column already exists."
    End If
 
    ' Get reference to the workbook
    Set xlWorkbook = xlWorksheet.Parent
 
    ' Save and close the workbook
    xlWorkbook.Save
    xlWorkbook.Close
 
    ' Restore Excel settings
    xlApp.ScreenUpdating = True
    xlApp.DisplayAlerts = True
    xlApp.EnableEvents = True
 
    ' Quit Excel application
    xlApp.Quit
    Set xlApp = Nothing
 
    ' Display completion message
    MsgBox "Finish!"
 
End Sub
 
' Function to check if a column exists
Function ColumnExists(columnName As String, xlWorksheet As Excel.WorkSheet) As Boolean
    Dim cell As Excel.Range
    For Each cell In xlWorksheet.Rows(1).Cells
        If cell.Value = columnName Then
            ColumnExists = True
            Exit Function
        End If
    Next
    ColumnExists = False
End Function
 
' Find first free column
Function FindFirstFreeColumn(xlWorksheet As Excel.WorkSheet) As Integer
    Dim cell As Excel.Range
    For Each cell In xlWorksheet.Rows(1).Cells
        If IsEmpty(cell.Value) Then
            FindFirstFreeColumn = cell.Column
            Exit Function
        End If
    Next
    ' If no free column exists, add a new one at the end
    FindFirstFreeColumn = xlWorksheet.Cells(1, xlWorksheet.Columns.Count).End(xlToLeft).Column + 1
End Function
0 Likes
Message 11 of 11

pawel.gromanski
Enthusiast
Enthusiast
Accepted solution

This should help with assembly:

        ' Get the active document 
        Dim oDoc As Document
        oDoc = inventorApp.ActiveDocument

        Dim isIPartFactory As Boolean
        Dim isIAssemblyFactory As Boolean


        If oDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
            If Not oDoc.ComponentDefinition.iPartFactory Is Nothing Then
                isIPartFactory = True
            End If
        ElseIf oDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
            If Not oDoc.ComponentDefinition.iAssemblyFactory Is Nothing Then
                isIAssemblyFactory = True
            End If
        End If


        If isIPartFactory Then
            ' If it's an iPart factory, handle the Excel worksheet for the iPart
            MsgBox("ipart")
            Set xlWorksheet = oDoc.ComponentDefinition.iPartFactory.ExcelWorkSheet
        ElseIf isIAssemblyFactory Then
            ' If it's an iAssembly factory, handle the Excel worksheet for the iAssembly
            MsgBox("iassy")
            Set xlWorksheet = oDoc.ComponentDefinition.iAssemblyFactory.ExcelWorkSheet
        End If


You get activeDocument, check if it's part or assembly, check if it's iPart or iAssembly and define excel sheet accordingly.  

 

 

0 Likes