iLogic to Create Custom Table

iLogic to Create Custom Table

felix.cortes5K3Y2
Advocate Advocate
5,974 Views
8 Replies
Message 1 of 9

iLogic to Create Custom Table

felix.cortes5K3Y2
Advocate
Advocate

Hi Forum,

 

I am trying to create a custom table using iLogic and not VBA. I found a working code but it's only for VBA and I am trying to rewrite it using iLogic. The VBA code is as follows:

 

Public Sub CreateCustomTable()
' Set a reference to the drawing document.
' This assumes a drawing document is active.
Dim oDrawDoc As DrawingDocument
Set oDrawDoc = ThisApplication.ActiveDocument

' Set a reference to the active sheet.
Dim oSheet As Sheet
Set oSheet = oDrawDoc.ActiveSheet

' Set the column titles
Dim oTitles(1 To 3) As String
oTitles(1) = "Part Number"
oTitles(2) = "Quantity"
oTitles(3) = "Material"

' Set the contents of the custom table (contents are set row-wise)
Dim oContents(1 To 9) As String
oContents(1) = "1"
oContents(2) = "1"
oContents(3) = "Brass"
oContents(4) = "2"
oContents(5) = "2"
oContents(6) = "Aluminium"
oContents(7) = "3"
oContents(8) = "1"
oContents(9) = "Steel"

' Set the column widths (defaults to the column title width if not specified)
Dim oColumnWidths(1 To 3) As Double
oColumnWidths(1) = 2.5
oColumnWidths(2) = 2.5
oColumnWidths(3) = 4

' Create the custom table
Dim oCustomTable As CustomTable
Set oCustomTable = oSheet.CustomTables.Add("My Table", ThisApplication.TransientGeometry.CreatePoint2d(15, 15), _
3, 3, oTitles, oContents, oColumnWidths)

' Change the 3rd column to be left justified.
oCustomTable.Columns.Item(3).ValueHorizontalJustification = kAlignTextLeft

' Create a table format object
Dim oFormat As TableFormat
Set oFormat = oSheet.CustomTables.CreateTableFormat

' Set inside line color to red.
oFormat.InsideLineColor = ThisApplication.TransientObjects.CreateColor(255, 0, 0)

' Set outside line weight.
oFormat.OutsideLineWeight = 0.1

' Modify the table formats
oCustomTable.OverrideFormat = oFormat
End Sub

 

 

Here's what I've written but it doesn't seem to work:

 

Dim oDrawDoc As DrawingDocument = ThisApplication.ActiveDocument 
Dim oSheet As Sheet = oDrawDoc.ActivateSheet
Dim oTitles As New ArrayList
'oTitles.Add("Part Number")
'oTitles.Add("Qty")
'oTitles.Add("Material")
oTitles(1) = "Part Number"
oTitles(2) = "qty"
oTitles(3) = "Material"
Dim oContents As New ArrayList
oContents.Add("1")
oContents.Add("1")
oContents.Add("Brass")
oContents.Add("2")
oContents.Add("2")
oContents.Add("Aluminum")
oContents.Add("3")
oContents.Add("1")
oContents.Add("Steel")
Dim oColumnWidths As New ArrayList
oColumnWidths.Add(2.5)
oColumnWidths.Add(2.5)
oColumnWidths.Add(4)
Dim oCustomTables As CustomTable = oSheet.CustomTables.Add("My Table", ThisApplication.TransientGeometry.CreatePoint2d(15, 15), _
	3, 3, oTitles, oContents, oColumnWidths)

 

Best regards,

Felix Cortes

0 Likes
Accepted solutions (1)
5,975 Views
8 Replies
Replies (8)
Message 2 of 9

Sergio.D.Suárez
Mentor
Mentor
Accepted solution

Hello, try with this ilogic code.
I modified the entries a bit because ilogidc counts from "0" so that's why they tend to throw errors. I've tried the code and it worked for me. I keep it for my later use. Thank you so much!!

 

Dim oDrawDoc As DrawingDocument = ThisApplication.ActiveDocument
Dim oSheet As Sheet = oDrawDoc.ActiveSheet

' Set the column titles
Dim oTitles(2) As String
oTitles(0) = "Part Number"
oTitles(1) = "Quantity"
oTitles(2) = "Material"

' Set the contents of the custom table (contents are set row-wise)
Dim oContents(8) As String
oContents(0) = "1"
oContents(1) = "1"
oContents(2) = "Brass"
oContents(3) = "2"
oContents(4) = "2"
oContents(5) = "Aluminium"
oContents(6) = "3"
oContents(7) = "1"
oContents(8) = "Steel"

' Set the column widths (defaults to the column title width if not specified)
Dim oColumnWidths(2) As Double
oColumnWidths(0) = 2.5
oColumnWidths(1) = 2.5
oColumnWidths(2) = 4

oPlacementPoint = ThisApplication.TransientGeometry.CreatePoint2d(15, 15)
' Create the custom table
Dim oCustomTable As CustomTable
oCustomTable = oSheet.CustomTables.Add("My Table", oPlacementPoint, _
3, 3, oTitles, oContents, oColumnWidths)

' Change the 3rd column to be left justified.
oCustomTable.Columns.Item(3).ValueHorizontalJustification = kAlignTextLeft

' Create a table format object
Dim oFormat As TableFormat
oFormat = oSheet.CustomTables.CreateTableFormat

' Set inside line color to red.
oFormat.InsideLineColor = ThisApplication.TransientObjects.CreateColor(255, 0, 0)

' Set outside line weight.
oFormat.OutsideLineWeight = 0.1

' Modify the table formats
oCustomTable.OverrideFormat = oFormat

 


Please accept as solution and give likes if applicable.

I am attaching my Upwork profile for specific queries.

Sergio Daniel Suarez
Mechanical Designer

| Upwork Profile | LinkedIn

Message 3 of 9

felix.cortes5K3Y2
Advocate
Advocate

Thanks Sergio!

0 Likes
Message 4 of 9

Anonymous
Not applicable

Hi Sergio,

Can be possible to generate a form like an excel, so I can input the values for the parameters?

Thanks

0 Likes
Message 5 of 9

Terry.VandenDungen
Contributor
Contributor

Thank you for this as it is helping me with an issue as well.

 

The line 

oCustomTable = oSheet.CustomTables.Add("My Table", oPlacementPoint, _
3, 3, oTitles, oContents, oColumnWidths)

 is causing me an error though in my code. My array is smaller than the example, and I feel that is why I'm getting an error. My array is two columns by three rows of data without headers. Does the 3,3 need to be something reflecting my array (1,3). 

0 Likes
Message 6 of 9

WCrihfield
Mentor
Mentor

Hi @Terry.VandenDungen.  Here is the online help page for the CustomTables.Add method.  According to that, the third input variable should be the number of columns, followed by the number of rows.  If your table has two columns and three rows, that part is fairly self explanatory, but the Array used to fill in the data would have to be like oTitles(1) (which can hold 2 title entries), then oContents(5) (which would hold 6 data entries), then oColumnWidths(1) (which can hold 2 column widths).  Since Array's are zero based, their first item is Item(0), then the second item is Item(1), and so on.

Edit:  Below is a quickie basic example of creating a 2 column x 3 row CustomTable in a drawing.  Edited to not show any column headers or title.

 

Dim oDDoc As DrawingDocument = ThisDrawing.Document
Dim oSheet As Sheet = oDDoc.ActiveSheet
'create CustomTable that is 2 columns x 3 rows
' No Column Titles, but create something anyways
Dim oTitles(1) As String
oTitles(0) = ""
oTitles(1) = ""

Dim oContents(5) As String
oContents(0) = "1" 'first row & first column value
oContents(1) = "2" 'first row & second column value
oContents(2) = "3"
oContents(3) = "4"
oContents(4) = "5"
oContents(5) = "6"

' Set the column widths (defaults to the column title width if not specified)
Dim oColumnWidths(1) As Double
oColumnWidths(0) = 3
oColumnWidths(1) = 3

oPoint = ThisApplication.TransientGeometry.CreatePoint2d(0, 0)

Dim oCustomTable As CustomTable
oCustomTable = oSheet.CustomTables.Add("My Table", oPoint, _
2, 3, oTitles, oContents, oColumnWidths)
oCustomTable.HeadingPlacement = HeadingPlacementEnum.kNoHeading
oCustomTable.ShowTitle = False

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 7 of 9

Terry.VandenDungen
Contributor
Contributor

Thank you for the quick response

0 Likes
Message 8 of 9

varad.keshatwar
Contributor
Contributor

In Inventor 22, we cannot create instance of CustomTable as shown in above code, as it is an Interface.
Also CustomTables.Add Method for ActiveSheet, has contents parameter as object, but it just accepts the singular array as shown in above example from API help. It does not accept List, MultiDimensional Array or any Generic Collections.

Please let me know if I'm going wrong somewhere. 

0 Likes
Message 9 of 9

WCrihfield
Mentor
Mentor

Hi @varad.keshatwar.  It is true that both the CustomTables object and the CustomTable object represent an Interface, but iLogic handles them very well, as if they represented a regular Class, so there is usually no problem.  As far as I know, only 1-dimensional Array of String is accepted as input for the contents.  I do realize that it is a bit odd to work with, because a 2-dimensional array would seem more fitting, but you just have to account for the number of columns & rows when populating the contents of the array, to ensure the pieces of data end up in the right places in the table.  If you are not using iLogic, then what coding language and/or environment are you trying to write the code from (VBA editor, Visual Studio, vb.net, C#, other)?  When you say Inventor 22, do you mean the 2018 version, where 22 is its internal number, or the 2022 version, where 22 represents the release year (Link)?

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes