Show Case


AutoDesk Inventor Part Labeller

Overview

This program is not written in pure VBA, rather it is a simplified version called iLogic. VBA was used to figure out the object models within Inventor.

The purpose for the program is to add the Assembly sheet Index and the Bill Of Materials (BOM) item number to the view label of the part. This is controlled by a form form which the action to be performed is chosen. The choices are to Add only the First BOM Index, Add All BOM Sheet Indeces, Remove All Indexes from Labels.

Form
Inventor Form


From the Remove Index choice the part view label will be modified to have no index:

No index
No Index


Or it will have the choice number of indeces:

All BOM Indeces
All Indexed
Labelled View
First Indexed

The addition here is the (0-0) before everything in the label.

Another part of this program is to add the sheet index of where the part was found to the BOM SHT# column:

Added Sheet Number of Part View
Index of Part View In SHT#
Index of Part View In SHT#
BOM#2 Index of Part View In SHT#


These index values are gathered from the sheet names in the navigation panel.

No index
Navigation Panel

Execution

When the function is called a couple things happen. First the type of documents that is active is checked, this program will only operate on drawing documents. Then the Form is brought into view.

                            
Public Sub Main()
    Dim objDrawDoc As DrawingDocument
    
    objDrawDoc = CheckForDrawingDoc
    If objDrawDoc Is Nothing Then
        Exit Sub
    End If
    
    iLogicForm.Show("Part Label Actions")
End Sub  
                            
                        
Display Form

CheckForDrawingDoc is a function that does what it says, it then returns either the document if it is a drawing doc else, an error is displayed and Nothing is returned.

Selecting the action
The first action on the form, Apply First Index utilizes Inventor's User Parameters. These are basic central stored variables that can be utilized anywhere in the project to set a value.

                            
Sub main()
    CreateParam()
    Parameter("Index_Action") = "First"

    iLogicVb.RunRule("Part_Index_Populate")
End Sub

Function CreateParam()
    Dim userParams As UserParameters
    Dim param As Parameter 
    
    userParams = ThisApplication.ActiveDocument.Parameters.UserParameters
    param = userParams.AddByValue("Index_Action", "", UnitsTypeEnum.kTextUnits)
     
    For Each oPara As Parameter In userParams
        Dim oName As String = oPara.Name
        If oName = "Index_Action_1" Then 
            oPara.Delete
        End If
    Next
End Function   
                            
                        
Set Parameter to "First"

The first operation is to check that the parameter exists, if it doesn't then it is created. The parameter is then set to "First". This tells the Part_Index_Populate function what to add to the label.

The second option in the form Apply All Indeces is very similar. The only difference is it sets the Index_Action parameter to All.

Populating the index

Once all checks are passed the real action begins.

                            
Sub main()
    Dim objDrawDoc As DrawingDocument

    objDrawDoc = ThisApplication.ActiveDocument

    iLogicVB.RunRule("Part_Index_Remove")

    For Each Sheet In objDrawDoc.Sheets
        For Each BOM In Sheet.PartsLists
        
            BOMName = BOM.ReferencedDocumentDescriptor.DisplayName
            NewBOMSheetIndex = getSheetIndex(BOM.Parent.Name)
            
            If Not NewBOMSheetIndex <> -1 Then
                MyMsg = MsgBox("The " & BOMName & " does not have an index",vbOKOnly)
                Exit For
            End If
            
            For Each PartSheet In objDrawDoc.Sheets
                UpdatePartLabel(PartSheet, BOM, NewBOMSheetIndex)
            Next PartSheet
        Next BOM
    Next Sheet
End Sub
                            
                        
Populating Index Main()

The setup is, the drawing doc is put into a variable and all existing index labels are removed with Part_Index_Romove, this is the rule name that contains the function.

Following this is the main operation loop for the function. The first concern is to check each Sheet for a BOM. If that sheet has a BOM then the index is gathered.

                            
Private Function getSheetIndex(SheetName) As Integer
    Dim ColonIndex As Integer

    ColonIndex = InStr(1, SheetName, ":")
    If ColonIndex <> 0 Then
        getSheetIndex = Right(SheetName, Len(SheetName) - ColonIndex)
    Else
        getSheetIndex = -1
    End If
End Function
                            
                        
Get the Sheet Index

The sheet index is contained in the name of the sheet. So a simple find and grab operation is required to retrieve it. If there is not an index an error is thrown and the sheet is skipped.

When a BOM and index are located each sheet is then passed into UpdatePartLabel

UpdatePartLabel

                            
Private Function UpdatePartLabel(Sheet, BOM, NewBOMSheetIndex)

    For Each View In Sheet.DrawingViews
        For Each Part In BOM.PartsListRows
            PartName = Part.Item(4).Value
            VerifyWholeName = VerifyWholeString(View.Label.Text, PartName)
                
            If VerifyWholeName <> False Then
                ViewLabelName = View.Label.FormattedText

                ItemNumber = CStr(Part.Item(2).Value)            
                AddToLabel = "(" + CStr(NewBOMSheetIndex) + "-" + ItemNumber + ") "
                PreffixLabel = "<StyleOverride FontSize='0.635'>"
                SuffixLabel = "</StyleOverride>"
                ViewIndexString = PreffixLabel + AddToLabel + SuffixLabel
                
                IndexType = Parameter("Index_Action")
                IsLabelled = CheckForIndexLabel(ViewLabelName, PreffixLabel, SuffixLabel)
                If IsLabelled <> True Then
                    ModifyIndexLabelInView(View, ViewIndexString + ViewLabelName)
                Else If Not IndexType <> "All" Then
                    ModifyIndexLabelInView(View, ViewIndexString + ViewLabelName)
                End If
                PartSheetIndex = getSheetIndex(Sheet.Name)
                AddSheetIndexToBOM(Part, PartSheetIndex)
            End If
        Next Part
    Next View

End Function
                            
                        
Set the New Index Label

Each view on the sheet is then inspected for each PartName listed in BOM. On finding the matching PartName a few string variables are gathered. The ViewLabelName is the current label that the view has. All the other variables are used to create the new view label.

IndexType gets the Parameter value that was assigned earlier, this is either All or First.
IsLabelled get true or false from CheckForIndexLabel.

CheckForIndexLabel

                            
Private Function CheckForIndexLabel(ViewLabel, PreffixLabel, SuffixLabel) As Boolean
    Dim PreffixIndex As Integer
    Dim SuffixIndex As Integer
    Dim CheckString As String
    Dim Tags As Integer
    
    PreffixIndex = InStr(ViewLabel, PreffixLabel)
    If PreffixIndex > 0 Then
        SuffixIndex = InStr(PreffixIndex, ViewLabel, SuffixLabel)
        CheckString = Mid(ViewLabel, PreffixIndex, SuffixIndex + Len(SuffixLabel))
    
        Tags = InStr(CheckString, "Property")
        If Tags <> 0 Then
            CheckForIndexLabel = False
        Else
            CheckForIndexLabel = True
        End If
    Else
        CheckForIndexLabel = False
    End If
End Function
                            
                        
Check if the View Has any indeces

In the case that the parameter Index_Action equals First then the view already having an index in the label will skip the ModifyIndexLabelInView function call.

ModifyIndexLabelInView

                            
Private Function ModifyIndexLabelInView(View, ViewLabelString)
    View.Label.FormattedText = ViewLabelString
End Function
                            
                        
Adding the Index to the Label

Finally the PartSheetIndex is added to the BOM in the SHT# column

                            
Private Function AddSheetIndexToBOM(Part, PartSheetIndex)
    If PartSheetIndex <> -1 Then
        Part.Item(1).Value = PartSheetIndex
    End If
End Function
                            
                        
Adding the Index to the Label

Here the Part is an item row in the BOM, so Item(1) is the first column of the row.

That finishes the populating of the part label. The other option to Remove Index is taken care of by the Part_Index_Remove Rule.

Part_Index_Remove

                            
Sub main()
    objDrawDoc = ThisApplication.ActiveDocument

    For Each Sheet In objDrawDoc.Sheets
        For Each ItemView In Sheet.DrawingViews
            RemoveIndexLabel(ItemView)
        Next ItemView
    Next Sheet
End Sub
                            
                        
Remove Index Main()

The remove function takes the drawing document and loops through all views in each sheet to remove the indeces.

RemoveIndexLabel()

                            
Private Function RemoveIndexLabel(ItemView)
    
    ViewLabel = ItemView.Label.FormattedText
    PreffixLabel = "<StyleOverride FontSize='0.635'>"
    SuffixLabel = "</StyleOverride>"
    PreffixIndex = InStr(ViewLabel, PreffixLabel)

    If PreffixIndex > 0 Then
        SuffixIndex = InStr(PreffixIndex, ViewLabel, SuffixLabel)
        IndexRemoved = Left(ViewLabel, PreffixIndex - 1) & Mid(ViewLabel, SuffixIndex + Len(SuffixLabel), Len(ViewLabel))
        HasIndexLabel = CheckForIndexLabel(ViewLabel, PreffixLabel, SuffixLabel)

        If HasIndexLabel <> False Then
            ModifyIndexLabelInView(ItemView, IndexRemoved)
        End If

    End If

End Function
                            
                        
Function to Remove the Index from Labels

These strings are the same as I added earlier. The Preffix location in the label is found and a label that does not contain the index is stored in IndexRemoved. CheckForIndexLabel is the same as in the populating portion of the program. If CheckForIndexLabel returns true the label is then replace with the IndexRemoved label by the ModifyIndexLabelInView function.

Summary

This program required some knowledge of the object model of the BOM and sheet views. There is not much to say about it as is a simple object modification program.

I can see places it could be optimized, one idea I had tried previously was to create an array of all the views as a class. These included the sheetName, SheetIndex, and ViewLabel. This idea did not pan out as storing and passing the array was a problem. After completing it in this simple way and having more knowledge of the iLogic capabilities I will revisit this method.