Manipulate rows and columns of iPart – 1

By Xiaodong Liang

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.

image

e.g. Assume the iPart table has some rows and columns as below. One column (Author) is marked as custom column.

image

Close the table. Now edit the table via spreadsheet. You can see how the columns are defined in the spread sheet.

image

image

image

<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 &lt;free&gt;&lt;/free&gt;     <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>&#160;&#160;&#160; Dim oPartDoc As PartDocument      <br />&#160;&#160;&#160; Set oPartDoc = ThisApplication.ActiveDocument       <br />&#160;&#160;&#160; On Error Resume Next       <br />&#160;&#160;&#160; Dim oFactory As iPartFactory       <br />&#160;&#160;&#160; Set oFactory = oPartDoc.ComponentDefinition.iPartFactory       <br />&#160;&#160;&#160; 'Get the iPartTable       <br />&#160;&#160;&#160; If Err &gt; 0 Or oFactory Is Nothing Then       <br />&#160;&#160;&#160; Set oFactory = oPartDoc.ComponentDefinition.CreateFactory       <br />&#160;&#160;&#160; Else       <br />&#160;&#160;&#160; Exit Sub       <br />&#160;&#160;&#160; End If       <br />&#160;&#160;&#160; On Error GoTo 0       <br />&#160;&#160;&#160; Dim oWorkSheet As WorkSheet       <br />&#160;&#160;&#160; Set oWorkSheet = oFactory.ExcelWorkSheet       <br />&#160;&#160;&#160; Dim oCells As Range       <br />&#160;&#160;&#160; Set oCells = oWorkSheet.Cells       <br />&#160;&#160;&#160; ' 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. _
         &#16
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.

image

 

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:

image


Comments

One response to “Manipulate rows and columns of iPart – 1”

  1. Robbie Johnson Avatar
    Robbie Johnson

    Where do you put this VB code? Directly in Inventor or inside the iPart’s spreadsheet as a VBA module?

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading