API provides you to access the iPart table and its existing rows & columns & cells. The object collection of row is iPartTableRows, the object for columns is iPartTableColumns. API allows you delete the row or column by
iPartTableRow.Delete
iPartTableColumn.Delete
Note: The "Member" and "Part Number" columns cannot be deleted.
Following is small demo. It deletes the first row and the column named “param1”.
Sub DeleteIPartRowColumn()
Dim oPartDoc As PartDocument
Set oPartDoc = ThisApplication.ActiveDocument
On Error Resume Next
Dim oFactory As iPartFactory
Set oFactory = oPartDoc.ComponentDefinition.iPartFactory
'Get the iPartTable
If Err > 0 Or oFactory Is Nothing Then
MsgBox ("no iPart table!")
Exit Sub
End If
If oFactory.TableRows.Count > 0 Then
'delete the first row
oFactory.TableRows(1).Delete
End If
If oFactory.TableColumns.Count > 0 Then
' Note :The "Member" and "Part Number" columns cannot be deleted.
'delete the column "param1"
Dim oEachCol As iPartTableColumn
For Each oEachCol In oFactory.TableColumns
If oEachCol.DisplayHeading = "param1" Then
oEachCol.Delete
End If
Next
End If
End Sub
But how to add rows or columns, and how to configure the rows/columns if we create a new iPart table by code?
Actually, every iPart table links to an Excel file. In UI, you can either add rows/columns by [Edit Table], or edit them by the Excel file.
e.g. Assume the iPart table has some rows and columns as below. One column (Author) is marked as custom column.
Close the table. Now edit the table via spreadsheet. You can see how the columns are defined in the spread sheet.
That is to say: The columns of iProperties are defined as
Property Name [Property Set Name]
The custom column is defined with the XML tag <free></free>
While the common parameters (d0. d1) are defined with their name directly.
Now, add one more column,
Save and close the spreadsheet, open the table again by [Edit Table] in UI, you can see the result:
So, we can edit the rows/columns by editing the spread sheet.
API allows you to access the corresponding spreadsheet: iPartFactory.ExcelWorkSheet
In this post, we firstly see how to configure the rows/columns if we create a new iPart table by code.
VBA
Please note to add reference to Mircrosoft Excel xx(version number,e.g.12) Object Library in this VB project
‘ this sample assumes the part has at least 4 model parameters.
Private Sub CreateNewiPartTable()
Dim oPartDoc As PartDocument
Set oPartDoc = ThisApplication.ActiveDocument
On Error Resume Next
Dim oFactory As iPartFactory
Set oFactory = oPartDoc.ComponentDefinition.iPartFactory
'Get the iPartTable
If Err > 0 Or oFactory Is Nothing Then
Set oFactory = oPartDoc.ComponentDefinition.CreateFactory
Else
Exit Sub
End If
On Error GoTo 0
Dim oWorkSheet As WorkSheet
Set oWorkSheet = oFactory.ExcelWorkSheet
Dim oCells As Range
Set oCells = oWorkSheet.Cells
' Column names...
'Set the key for the iPartTable, here we use the key 'Part Number"
With oCells
.Item(1, 2) = oCells.Item(1, 2) + "0"
.Item(1, 3) = oPartDoc.ComponentDefinition.Parameters.ModelParameters.Item(1).Name
.Item(1, 4) = oPartDoc.ComponentDefinition.Parameters.ModelParameters.Item(2).Name
.Item(1, 5) = oPartDoc.ComponentDefinition.Parameters.ModelParameters.Item(3).Name
.Item(1, 6) = oPartDoc.ComponentDefinition.Parameters.ModelParameters.Item(4).Name
End With
Dim oUM As UnitsOfMeasure
Set oUM = oPartDoc.UnitsOfMeasure
Dim oParameter As Parameter
Set oParameter = oPartDoc.ComponentDefinition.Parameters.ModelParameters.Item(1)
'The first row is column name in excel table, the first ipart row is the second row in fact
Dim iInitRow, i As Integer
iInitRow = 2
With oCells
.Item(iInitRow, 3) = _
oUM.GetStringFromValue( _
oParameter.Value, oParameter.Units)
Set oParameter = _
oPartDoc.ComponentDefinition. _
Parameters.ModelParameters.Item(2)
.Item(iInitRow, 4) = _
oUM.GetStringFromValue( _
oParameter.Value, oParameter.Units)
Set oParameter = _
oPartDoc.ComponentDefinition. _
Parameters.ModelParameters.Item(3)
.Item(iInitRow, 5) = _
oUM.GetStringFromValue( _
oParameter.Value, oParameter.Units)
.Item(iInitRow, 6) = _
oPartDoc.ComponentDefinition. _
Parameters.ModelParameters.Item(4).Expression
End With
'We need query the part number from iProperties of the part document.
Dim sPartNumber As String
sPartNumber = _
oPartDoc.PropertySets.Item( _
"{32853F0F-3444-11d1-9E93-0060B03C1CA6}"). _
ItemByPropId(kPartNumberDesignTrackingProperties).Value
Dim pos As Integer
pos = InStrRev(sPartNumber, "-")
Dim str As String
str = Left(sPartNumber, pos)
Dim iNumber As Integer
iNumber = Right(sPartNumber, Len(sPartNumber) - pos)
'Assume the offset of the parameter's value _
' (between two rows) is equal to 0.5cm
Dim offset As Double
offset = 0.5
'Add 20 rows into the iPartTable
For i = 1 To 20
' Row i values...
If (iNumber + i) < 10 Then
sPartNumber = _
str + "0" + CStr(iNumber + i)
Else
sPartNumber = _
str + CStr(iNumber + i)
End If
With oCells
.Item(iInitRow + i, 1) = _
sPartNumber
.Item(iInitRow + i, 2) = _
sPartNumber
Set oParameter = _
oPartDoc.ComponentDefinition. _
Parameters.ModelParameters.Item(1)
.Item(iInitRow + i, 3) = _
oUM.GetStringFromValue( _
oParameter.Value + offset * i, _
oParameter.Units)
Set oParameter = _
oPartDoc.ComponentDefinition.Parameters. _
ModelParameters.Item(2)
.Item(iInitRow + i, 4) = _
oUM.GetStringFromValue( _
oParameter.Value + offset * i, _
oParameter.Units)
Set oParameter = _
oPartDoc.ComponentDefinition. _
Parameters.ModelParameters.Item(3)
.Item(iInitRow + i, 5) = _
oUM.GetStringFromValue( _
oParameter.Value + offset * i, oParameter.Units)
.Item(iInitRow + i, 6) = _
oPartDoc.ComponentDefinition. _
Parameters.ModelParameters.Item(4).Expression
End With
Next
Set oUM = Nothing
Dim oWB As Workbook
Set oWB = oWorkSheet.Parent
oWB.Save
oWB.Close
End Sub
VB.NET
remember to add the reference of Excel.
Private Sub CreateNewiPartTable()
Dim oPartDoc As PartDocument
oPartDoc = ThisApplication.ActiveDocument
Dim oFactory As iPartFactory
'Get the iPartTable
Try
oFactory = oPartDoc.ComponentDefinition.iPartFactory
Catch ex As Exception
oFactory = oPartDoc.ComponentDefinition.CreateFactory
End Try
If oFactory Is Nothing Then
Exit Sub
End If
Try
Dim oWorkSheet As WorkSheet
oWorkSheet = oFactory.ExcelWorkSheet
Dim oCells As Range
oCells = oWorkSheet.Cells
' Column names...
'Set the key for the iPartTable, here we use the key 'Part Number"
With oCells
.Item(1, 2) = oCells.Item(1, 2) + "0"
.Item(1, 3) = oPartDoc.ComponentDefinition.Parameters.ModelParameters.Item(1).Name
.Item(1, 4) = oPartDoc.ComponentDefinition.Parameters.ModelParameters.Item(2).Name
.Item(1, 5) = oPartDoc.ComponentDefinition.Parameters.ModelParameters.Item(3).Name
.Item(1, 6) = oPartDoc.ComponentDefinition.Parameters.ModelParameters.Item(4).Name
End With
Dim oUM As UnitsOfMeasure
oUM = oPartDoc.UnitsOfMeasure
Dim oParameter As Parameter
oParameter = oPartDoc.ComponentDefinition.Parameters.ModelParameters.Item(1)
'The first row is column name in excel table, the first ipart row is the second row in fact
Dim iInitRow, i As Integer
iInitRow = 2
With oCells
.Item(iInitRow, 3) =
oUM.GetStringFromValue(oParameter.Value,
oParameter.Units)
oParameter =
oPartDoc.ComponentDefinition.
Parameters.ModelParameters.Item(2)
.Item(iInitRow, 4) =
oUM.GetStringFromValue(oParameter.Value,
oParameter.Units)
oParameter =
oPartDoc.ComponentDefinition.
Parameters.ModelParameters.Item(3)
.Item(iInitRow, 5) =
oUM.GetStringFromValue(
oParameter.Value, oParameter.Units)
.Item(iInitRow, 6) =
oPartDoc.ComponentDefinition.
Parameters.ModelParameters.Item(4).Expression
End With
'We need query the part number from iProperties of the part document.
Dim sPartNumber As String
sPartNumber =
oPartDoc.PropertySets.Item(
"{32853F0F-3444-11d1-9E93-0060B03C1CA6}").
ItemByPropId(
PropertiesForDesignTrackingPropertiesEnum.
kPartNumberDesignTrackingProperties).Value
Dim pos As Integer
pos = InStrRev(sPartNumber, "-")
Dim str As String
str = sPartNumber.Substring(0, pos)
Dim iNumber As Integer
iNumber = sPartNumber.Substring(
sPartNumber.Length - pos, sPartNumber.Length)
'Assume the offset of the parameter's value
'(between two rows) is equal to 0.5cm
Dim offset As Double
offset = 0.5
'Add 20 rows into the iPartTable
For i = 1 To 20
' Row i values...
If (iNumber + i) < 10 Then
sPartNumber = str + "0" + CStr(iNumber + i)
Else
sPartNumber = str + CStr(iNumber + i)
End If
With oCells
.Item(iInitRow + i, 1) = sPartNumber
.Item(iInitRow + i, 2) = sPartNumber
oParameter =
oPartDoc.ComponentDefinition.Parameters.
ModelParameters.Item(1)
.Item(iInitRow + i, 3) =
oUM.GetStringFromValue(
oParameter.Value + offset * i,
oParameter.Units)
oParameter =
oPartDoc.ComponentDefinition.
Parameters.ModelParameters.Item(2)
.Item(iInitRow + i, 4) =
oUM.GetStringFromValue(
oParameter.Value + offset * i,
oParameter.Units)
oParameter =
oPartDoc.ComponentDefinition.Parameters.
ModelParameters.Item(3)
.Item(iInitRow + i, 5) =
oUM.GetStringFromValue(
oParameter.Value + offset * i,
oParameter.Units)
.Item(iInitRow + i, 6) =
oPartDoc.ComponentDefinition.Parameters.
ModelParameters.Item(4).Expression
End With
Next
oUM = Nothing
Dim oWB As WorkBook
oWB = oWorkSheet.Parent
oWB.Save()
oWB.Close()
Catch ex As Exception
End Try
End Sub
After the code , the result is: