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.
<p>That is to say: The columns of iProperties are defined as <br />Property Name [Property Set Name] <br />The custom column is defined with the XML tag <free></free> <br />While the common parameters (d0. d1) are defined with their name directly.</p> <p>Now, add one more column, </p> <p><a href="http://adndevblog.typepad.com/.a/6a0167607c2431970b017d4154d6c7970c-pi"><img style="border-right-width: 0px;display: inline;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.autodesk.io/wp-content/uploads/2013/02/mt_imported_image_1759224977-1.jpg" width="198" height="164" /></a> </p> <p><a href="http://adndevblog.typepad.com/.a/6a0167607c2431970b017c3725b523970b-pi"><img style="border-right-width: 0px;display: inline;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.autodesk.io/wp-content/uploads/2013/02/mt_imported_image_1759224977-2.jpg" width="403" height="89" /></a> </p> <p>Save and close the spreadsheet, open the table again by [Edit Table] in UI, you can see the result:</p> <p><a href="http://adndevblog.typepad.com/.a/6a0167607c2431970b017d4154d702970c-pi"><img style="border-right-width: 0px;display: inline;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.autodesk.io/wp-content/uploads/2013/02/mt_imported_image_1759224978.jpg" width="401" height="264" /></a> </p> <p>So, we can edit the rows/columns by editing the spread sheet. </p> <p>API allows you to access the corresponding spreadsheet: <strong>iPartFactory.ExcelWorkSheet </strong></p> <p>In this post, we firstly see how to configure the rows/columns if we create a new iPart table by code.</p> <p><strong>VBA</strong> <br />Please note to add reference to Mircrosoft Excel xx(version number,e.g.12) Object Library in this VB project</p> <p><a href="http://adndevblog.typepad.com/.a/6a0167607c2431970b017ee8c8a32e970d-pi"><img style="border-right-width: 0px;display: inline;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.autodesk.io/wp-content/uploads/2013/02/mt_imported_image_1759224978-1.jpg" width="375" height="233" /></a> </p> <p>‘ this sample assumes the part has at least 4 model parameters.</p> <p><em>Private Sub CreateNewiPartTable() </em></p> <p><em>    Dim oPartDoc As PartDocument <br />    Set oPartDoc = ThisApplication.ActiveDocument <br />    On Error Resume Next <br />    Dim oFactory As iPartFactory <br />    Set oFactory = oPartDoc.ComponentDefinition.iPartFactory <br />    'Get the iPartTable <br />    If Err > 0 Or oFactory Is Nothing Then <br />    Set oFactory = oPartDoc.ComponentDefinition.CreateFactory <br />    Else <br />    Exit Sub <br />    End If <br />    On Error GoTo 0 <br />    Dim oWorkSheet As WorkSheet <br />    Set oWorkSheet = oFactory.ExcelWorkSheet <br />    Dim oCells As Range <br />    Set oCells = oWorkSheet.Cells <br />    ' 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. _

0; 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 &#
160; .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 sPart
Number = 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:








Leave a Reply