Friday, July 10, 2015

HoleTable Modifications

Hey all, long time since my last post.  I apologize for that.  Busy is a good thing, right?

I recently had a chance to look at something in the Inventor API that I had yet to have a reason to: Hole Tables.  A client was spending time on many drawings modifying a hole table column based upon the hole type.  The holes are iFeatures and he could not find a way of adding this data to the iFeature so that it would populate in the hole table automatically.

This presented me with a chance to see what access we had to Hole Tables in the Inventor API.

First, I checked for a hole table and for a drawing view.  If either doesn't exist then I'd warn the user.

    '    Declare variables
        Dim oDrawDoc As DrawingDocument
        oDrawDoc = ThisApplication.ActiveDocument
        Dim oSheet1 As Sheet
        oSheet1 = oDrawDoc.Sheets.Item(1)
    
    '    Check for a hole table
        If oSheet1.HoleTables.Count = 0 Then 
            MessageBox.Show("Please ensure a hole table is present on the current drawing.", "No Hole Table",MessageBoxButtons.OK,MessageBoxIcon.Warning)
            Return
        End If

    '    Check for drawing views
        If oSheet1.DrawingViews.Count = 0 Then 
            MessageBox.Show("Please ensure a drawing view is present on the current drawing.", "No Hole Table",MessageBoxButtons.OK,MessageBoxIcon.Warning)
            Return
        End If

Next, I cycled through the Hole Table columns to determine which column numbers were the columns that I needed to either get data from or write data to.  If any of those columns don't exist then I'd warn the user.

    '    Declare variables        
        Dim oView As DrawingView
        oView = oSheet1.DrawingViews.Item(1)
        Dim oHoleTable As HoleTable
        oHoleTable = oSheet1.HoleTables.Item(1)
        Dim oRow As HoleTableRow
        Dim oColumn As HoleTableColumn
        
    '    Cycle thru each column to determine the XDIM, YDIM, DESCRIPTION and CALLOUT columns.
        iXDIMColumn = 1
        For Each oColumn In oHoleTable.HoleTableColumns
            oXDIMColumnName = oColumn.Title
            If UCase(oXDIMColumnName) = "XDIM" Then Exit For
            iXDIMColumn = iXDIMColumn + 1
        Next
        If iXDIMColumn > oHoleTable.HoleTableColumns.Count Then 
            MessageBox.Show("Please ensure hole table has a column named XDIM.", "No XDIM column",MessageBoxButtons.OK,MessageBoxIcon.Warning)
            Return
        End If
        
        iYDIMColumn = 1
        For Each oColumn In oHoleTable.HoleTableColumns
            oYDIMColumnName = oColumn.Title
            If UCase(oYDIMColumnName) = "YDIM" Then Exit For
            iYDIMColumn = iYDIMColumn + 1
        Next
        If iYDIMColumn > oHoleTable.HoleTableColumns.Count Then 
            MessageBox.Show("Please ensure hole table has a column named YDIM.", "No YDIM column",MessageBoxButtons.OK,MessageBoxIcon.Warning)
            Return
        End If

        iDescColumn = 1
        For Each oColumn In oHoleTable.HoleTableColumns
            oDescColumnName = oColumn.Title
            If UCase(oDescColumnName) = "DESCRIPTION" Then Exit For
            iDescColumn = iDescColumn + 1
        Next
        If iDescColumn > oHoleTable.HoleTableColumns.Count Then 
            MessageBox.Show("Please ensure hole table has a column named DESCRIPTION.", "No DESCRIPTION column",MessageBoxButtons.OK,MessageBoxIcon.Warning)
            Return
        End If
        
        iCalloutColumn = 1
        For Each oColumn In oHoleTable.HoleTableColumns
            oCalloutColumnName = oColumn.Title
            If UCase(oCalloutColumnName) = "CALLOUT" Then Exit For
            iCalloutColumn = iCalloutColumn + 1
        Next
        If iCalloutColumn > oHoleTable.HoleTableColumns.Count Then 
            MessageBox.Show("Please ensure hole table has a column named CALLOUT.", "No CALLOUT column",MessageBoxButtons.OK,MessageBoxIcon.Warning)
            Return
        End If

Lastly, I checked the DESCRIPTION column and read it's text.  The client had specific data that he would add to the CALLOUT column based upon the DESCRIPTION column.  I did this with a Select Case but there are many other ways of accomplishing this.  The client also wished to modify the CALLOUT column for any additional holes that are part of the same iFeature.  I accomplished this by concatenating the XDIM & YDIM for all holes and if they matched the threaded hole then I modified their CALLOUT column as well.

    '    Cycle thru each row in hole table.  Split Description column (5th) and set callout based on text for rows where XDIM and YDIM match.
        For Each oRow In oHoleTable.HoleTableRows
            oText = oRow.Item(iDescColumn).Text
            Dim oTextLeft As String() = oText.Split(New Char() {"U"c})
            sThreadData = oTextLeft(0)
            
            Select Case sThreadData 
                Case "5/16-24 "
                    sCallout = "SAE2"
                Case "7/16-20 "
                    sCallout = "SAE4"
                Case "9/16-18 "
                    sCallout = "SAE6"
                Case "3/4-16 "
                    sCallout = "SAE8"
                Case "7/8-14 "
                    sCallout = "SAE10"
                Case "1 1/16-12 "
                    sCallout = "SAE12"
                Case "1 5/16-12 "
                    sCallout = "SAE16"
                Case "1 5/8-12 "
                    sCallout = "SAE20"
                Case "1 7/8-12 "
                    sCallout = "SAE24"
                Case "2 1/2-12 "
                    sCallout = "SAE32"
                Case Else
            End Select
            
        '    Concatenate XDIM & YDIM
            If Not sCallout = "" Then
                sLocation = oRow.Item(2).Text & oRow.Item(3).Text
                Call ChangeCallout(oRow,oHoleTable,sLocation,sCallout,iXDIMColumn,iYDIMColumn,iCalloutColumn)
            End If
            
            sCallout = ""
    Next

End Sub

Private Sub ChangeCallout(ByVal oRow As HoleTableRow, oHoleTable As HoleTable, sLocation As String, sCallout As String, iXDIMColumn As Integer,iYDIMColumn As Integer, iCalloutColumn As Integer)
    For Each oRow In oHoleTable.HoleTableRows
        If oRow.Item(iXDIMColumn).Text & oRow.Item(iYDIMColumn).Text = sLocation Then
            oRow.Item(iCalloutColumn).Text = sCallout
        End If
    Next
End Sub

The entire code is here:

Sub Main
    '    Declare variables
        Dim oDrawDoc As DrawingDocument
        oDrawDoc = ThisApplication.ActiveDocument
        Dim oSheet1 As Sheet
        oSheet1 = oDrawDoc.Sheets.Item(1)
    
    '    Check for a hole table
        If oSheet1.HoleTables.Count = 0 Then 
            MessageBox.Show("Please ensure a hole table is present on the current drawing.", "No Hole Table",MessageBoxButtons.OK,MessageBoxIcon.Warning)
            Return
        End If

    '    Check for drawing views
        If oSheet1.DrawingViews.Count = 0 Then 
            MessageBox.Show("Please ensure a drawing view is present on the current drawing.", "No Hole Table",MessageBoxButtons.OK,MessageBoxIcon.Warning)
            Return
        End If

    '    Declare variables        
        Dim oView As DrawingView
        oView = oSheet1.DrawingViews.Item(1)
        Dim oHoleTable As HoleTable
        oHoleTable = oSheet1.HoleTables.Item(1)
        Dim oRow As HoleTableRow
        Dim oColumn As HoleTableColumn
        
    '    Cycle thru each column to determine the XDIM, YDIM, DESCRIPTION and CALLOUT columns.
        iXDIMColumn = 1
        For Each oColumn In oHoleTable.HoleTableColumns
            oXDIMColumnName = oColumn.Title
            If UCase(oXDIMColumnName) = "XDIM" Then Exit For
            iXDIMColumn = iXDIMColumn + 1
        Next
        If iXDIMColumn > oHoleTable.HoleTableColumns.Count Then 
            MessageBox.Show("Please ensure hole table has a column named XDIM.", "No XDIM column",MessageBoxButtons.OK,MessageBoxIcon.Warning)
            Return
        End If
        
        iYDIMColumn = 1
        For Each oColumn In oHoleTable.HoleTableColumns
            oYDIMColumnName = oColumn.Title
            If UCase(oYDIMColumnName) = "YDIM" Then Exit For
            iYDIMColumn = iYDIMColumn + 1
        Next
        If iYDIMColumn > oHoleTable.HoleTableColumns.Count Then 
            MessageBox.Show("Please ensure hole table has a column named YDIM.", "No YDIM column",MessageBoxButtons.OK,MessageBoxIcon.Warning)
            Return
        End If

        iDescColumn = 1
        For Each oColumn In oHoleTable.HoleTableColumns
            oDescColumnName = oColumn.Title
            If UCase(oDescColumnName) = "DESCRIPTION" Then Exit For
            iDescColumn = iDescColumn + 1
        Next
        If iDescColumn > oHoleTable.HoleTableColumns.Count Then 
            MessageBox.Show("Please ensure hole table has a column named DESCRIPTION.", "No DESCRIPTION column",MessageBoxButtons.OK,MessageBoxIcon.Warning)
            Return
        End If
        
        iCalloutColumn = 1
        For Each oColumn In oHoleTable.HoleTableColumns
            oCalloutColumnName = oColumn.Title
            If UCase(oCalloutColumnName) = "CALLOUT" Then Exit For
            iCalloutColumn = iCalloutColumn + 1
        Next
        If iCalloutColumn > oHoleTable.HoleTableColumns.Count Then 
            MessageBox.Show("Please ensure hole table has a column named CALLOUT.", "No CALLOUT column",MessageBoxButtons.OK,MessageBoxIcon.Warning)
            Return
        End If
            
    '    Cycle thru each row in hole table.  Split Description column (5th) and set callout based on text for rows where XDIM and YDIM match.
        For Each oRow In oHoleTable.HoleTableRows
            oText = oRow.Item(iDescColumn).Text
            Dim oTextLeft As String() = oText.Split(New Char() {"U"c})
            sThreadData = oTextLeft(0)
            
            Select Case sThreadData 
                Case "5/16-24 "
                    sCallout = "SAE2"
                Case "7/16-20 "
                    sCallout = "SAE4"
                Case "9/16-18 "
                    sCallout = "SAE6"
                Case "3/4-16 "
                    sCallout = "SAE8"
                Case "7/8-14 "
                    sCallout = "SAE10"
                Case "1 1/16-12 "
                    sCallout = "SAE12"
                Case "1 5/16-12 "
                    sCallout = "SAE16"
                Case "1 5/8-12 "
                    sCallout = "SAE20"
                Case "1 7/8-12 "
                    sCallout = "SAE24"
                Case "2 1/2-12 "
                    sCallout = "SAE32"
                Case Else
            End Select
            
        '    Concatenate XDIM & YDIM
            If Not sCallout = "" Then
                sLocation = oRow.Item(2).Text & oRow.Item(3).Text
                Call ChangeCallout(oRow,oHoleTable,sLocation,sCallout,iXDIMColumn,iYDIMColumn,iCalloutColumn)
            End If
            
            sCallout = ""
    Next

End Sub

Private Sub ChangeCallout(ByVal oRow As HoleTableRow, oHoleTable As HoleTable, sLocation As String, sCallout As String, iXDIMColumn As Integer,iYDIMColumn As Integer, iCalloutColumn As Integer)
    For Each oRow In oHoleTable.HoleTableRows
        If oRow.Item(iXDIMColumn).Text & oRow.Item(iYDIMColumn).Text = sLocation Then
            oRow.Item(iCalloutColumn).Text = sCallout
        End If
    Next
End Sub


"A very small man can cast a very large shadow" ~ Varys

Happy Coding!

Randy

No comments:

Post a Comment