My First Plug-in Training

Lesson 6: My Final Plug-in

In Lesson 5, you used attributes to tag Inventor components. You then used those attributes to select the components and hide or display them. In this lesson, instead of toggling the display of the components, you will add an additional button to provide the user with the direct capability to either show or hide the components. You will also add two more buttons, one to remove your attribute tag from the selected components and another to remove the attribute from all components. We will make your code easier to extend and maintain by creating procedures that are called when buttons are clicked. You will pass a Boolean argument to these procedures, allowing the same procedure to be used in slightly different contexts.

Provide Feedback: Please provide feedback about this Inventor Training or this lesson via email: myfirstplugin@autodesk.com
Lesson Downloads
lesson6_vb-net.zip (zip - 26Kb)
lesson6_c-sharp.zip (zip - 28Kb)
  1. Open the project from Lesson 5 and display the Designer for Form1. If the code window is active, right-click on Form1.vb in the Solution Explorer and select View Designer. Add three buttons so that the form looks like the screenshot below. Change the text for the button you added in Lesson 4 (Button2) to Hide group. You will use another button to show the components in the group.  In previous lessons, you learned how to add buttons to the form and change the text for the buttons. Show the toolbox; drag a button to place it on the form. Make the text on the three new buttons: 

    Show group
    Remove from group
    Remove all from group


  2. Type the following in the code window for Form1, before the End Class keyword and after the last End Sub.

    Public Sub HideOrShowGroup(ByVal hide As Boolean)
     
    End Sub

  3. Cut the code from the Button2_Click Sub and paste it into the Sub you created in step 2. Here is the code you need to move into the HideOrShowGroup Sub:

        If _invApp.Documents.Count = 0 Then
          MsgBox("Need to open an Assembly document")
          Return
        End If
     
        If _invApp.ActiveDocument.DocumentType <> _
             DocumentTypeEnum.kAssemblyDocumentObject Then
          MsgBox("Need to have an Assembly document active")
          Return
        End If
     
        Dim asmDoc As AssemblyDocument
        asmDoc = _invApp.ActiveDocument
     
        Try
          Dim attbMan As AttributeManager = asmDoc.AttributeManager
     
          Dim objsCol As ObjectCollection
          objsCol = _
            attbMan.FindObjects("myPartGroup", "PartGroup1", "Group1")
     
          Dim compOcc As ComponentOccurrence
          For Each obj As Object In objsCol
            compOcc = obj
            ' Toggle the visibility of the Component Occurrence
            compOcc.Visible = Not compOcc.Visible
          Next
     
        Catch ex As Exception
          MsgBox("Problem hiding component")
        End Try

  4. Change the contents of the For Each loop, so that it makes the Visible property of the component equal to the Boolean value that is passed in (hide).

    For Each obj As Object In objsCol
        compOcc = obj
        compOcc.Visible = hide
    Next

  5. Add the following to the now-empty Button2_Click Sub. When this button is clicked it will run the HideOrShowGroup Sub passing in False.

    HideOrShowGroup(False)

  6. Add this to the Button3_Click Sub, the Button with the text Show group. When this button is clicked, it will run the HideOrShowGroup Sub passing in True.

    HideOrShowGroup(True)

  7. Type the following into the code window for Form1, before the End Class keyword and after the last End Sub. This will create another Sub that you can call, passing in a Boolean value.

    Public Sub AddOrRemoveFromGroup(ByVal add As Boolean
     
    End Sub

  8. Cut and paste this code from the Button1_Click Sub into the AddOrRemoveFromGroup Sub that you added in step 7.

        If _invApp.Documents.Count = 0 Then
          MsgBox("Need to open an Assembly document")
          Return
        End If
     
        If _invApp.ActiveDocument.DocumentType <> _
             DocumentTypeEnum.kAssemblyDocumentObject Then
          MsgBox("Need to have an Assembly document active")
          Return
        End If
     
        Dim asmDoc As AssemblyDocument
        asmDoc = _invApp.ActiveDocument
     
        If asmDoc.SelectSet.Count = 0 Then
          MsgBox("Need to select a Part or Sub Assembly")
          Return
        End If
     
        Dim selSet As SelectSet
        selSet = asmDoc.SelectSet
     
        Try
          Dim compOcc As ComponentOccurrence
          Dim obj As Object
          For Each obj In selSet
            compOcc = obj
            Debug.Print(compOcc.Name)
            'compOcc.Visible = False
     
            Dim attbSets As AttributeSets = compOcc.AttributeSets
     
            ' Add the attributes to the ComponentOccurrence
            If Not attbSets.NameIsUsed("myPartGroup") Then
     
              Dim attbSet As AttributeSet = attbSets.Add("myPartGroup")
     
              Dim attb As Inventor.Attribute = _
                 attbSet.Add( _
                   "PartGroup1", ValueTypeEnum.kStringType, "Group1")
     
            End If
     
     
          Next
        Catch ex As Exception
          MsgBox("Is the selected item a Component?")
          MsgBox(ex.ToString())
          Return
        End Try

  9. Add an If Then Else statement and test the add Boolean variable that is passed into the Sub. Insert the If Then statement right after the code where the attbSets is created, just before the block of code to “Add the attributes to the ComponentOccurrence”.

    If add Then

    Insert the Else statement right after the block of code to “Add the attributes to the ComponentOccurrence” – after its End If – right before the Next keyword. Add an End If after the Else keyword.

              ' Add the attributes to the ComponentOccurrence
              If Not attbSets.NameIsUsed("myPartGroup") Then
     
                Dim attbSet As AttributeSet = attbSets.Add("myPartGroup")
     
                Dim attb As Inventor.Attribute = _
                  attbSet.Add( _
                    "PartGroup1", ValueTypeEnum.kStringType, "Group1")
              End If
     
            Else
     
            End If
        Next

  10. Add a nested If Then statement that uses the NameIsUsed() method to check if the name of the attribute “myPartGroup” is used, putting it between the Else and End If statements added in the previous step. If it has indeed been used, get the AttributeSet by passing its name, “myPartGroup”, to the Item method of the attbSets object, and then call Delete() on it to delete the AttributeSet from the component.

         ' Delete the attributes from the ComponentOccurrence
         If attbSets.NameIsUsed("myPartGroup") Then
             attbSets.Item("myPartGroup").Delete()
         End If

  11. Call the AddOrRemoveFromGroup Sub from the now-empty Button1_Click passing True as the argument.

    Private Sub Button1_Click(ByVal sender As Object, 
    ByVal e As System.EventArgs) Handles Button1.Click
            AddOrRemoveFromGroup(True)
    End Sub

  12. Call the AddOrRemoveFromGroup Sub from the click event handler for the button with the text “Remove selected components from group”, passing False as the argument. This is likely to be in the Button4_Click Sub, depending on the order in which you created the buttons at the beginning of this lesson. You can double-click the button in the Designer view to display the code window and add Sub Button4_Click.

    Private Sub Button4_Click(ByVal sender As System.Object, 
    ByVal e As System.EventArgs) Handles Button4.Click
         AddOrRemoveFromGroup(False)
    End Sub

  13. You have one more button that you need to complete. This is the button with the text “Remove all components from group”.  Double click on this button, most likely Button5, in the Designer to open the code window and create the Button5_Click Sub. You can copy and paste code from the other functions to complete the implementation. Here is the completed Button5_Click Sub: notice how the code you added for your other button handlers is being reused.

    Private Sub Button5_Click( _
        ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles Button5.Click
     
        If _invApp.Documents.Count = 0 Then
          MsgBox("Need to open an Assembly document")
          Return
        End If
     
        If _invApp.ActiveDocument.DocumentType <> _
            DocumentTypeEnum.kAssemblyDocumentObject Then
          MsgBox("Need to have an Assembly document active")
          Return
        End If
     
        Dim asmDoc As AssemblyDocument = _invApp.ActiveDocument
        ' Get the attribute manager for the document
        Dim attbMan As AttributeManager = asmDoc.AttributeManager
     
        ' Find the objects with the attributes
        Dim objCol As ObjectCollection
        objCol = _
          attbMan.FindObjects("myPartGroup", "PartGroup1", "Group1")
     
        Dim attbSets As AttributeSets
     
        ' Delete the attribute from the ComponentOccurrence
        For Each obj As Object In objCol
          attbSets = obj.AttributeSets
          attbSets.Item("myPartGroup").Delete()
          ' Make the ComponentOccurrence visible
          obj.Visible = True
        Next
      End Sub

A Closer Look at the Code

In this lesson you created two different Sub-routines that you can call when a button is clicked instead of having the code directly in your Sub. The advantage of using a separate Sub is that similar code will not be duplicated in your project and your code will be easier to update in the future. In Lesson 5 you created a button that, when clicked, hid or displayed components after determining which to modify by searching for an attribute tag. You now have a Sub that can hide or show these components.

Public Sub HideOrShowGroup(ByVal hide As Boolean)
 
End Sub
You also have a Sub that can add components or remove them from the group.

Public Sub AddOrRemoveFromGroup(ByVal add As Boolean
 
End Sub
These Sub-routines can be called from anywhere in your project. You can pass in True or False to control the behavior. You were therefore able to reuse code that you created previously by making a few simple changes. You changed the code you pasted into the HideOrShowComponentsInGroup Sub to either hide or show the components based on the Boolean value passed in. You did this by changing the For Each loop simply to make the Visible property of the component equal to the argument passed into the Sub (hide).

For Each obj As Object In objsCol
    compOcc = obj
    compOcc.Visible = hide
Next
The changes you made to the code added to the AddOrRemoveFromGroup Sub were to change its behavior based on the value of the Boolean variable, add, that was passed into the Sub. For the case where add was True, you used the code you already had. For the case where add was False, you added code to delete the AttributeSet. You used an If Then Else End If statement to do this.  Similar to adding an AttributeSet, you do not want to try to delete an AttributeSet that does not exist, so you checked the NameIsUsed() method to test this. In the case the AttributeSet exists, you then used the Item property of the AttributeSets with your chosen name (“myPartGroup”), calling the Delete() method on the resultant AttributeSet to remove it from the component.

        If add Then
          ' Add the attributes to the ComponentOccurrence
          If Not attbSets.NameIsUsed("myPartGroup") Then
 
            Dim attbSet As AttributeSet = attbSets.Add("myPartGroup")
 
            Dim attb As Inventor.Attribute = _
              attbSet.Add( _
                "PartGroup1", ValueTypeEnum.kStringType, "Group1")
          End If
 
        Else
       ' Delete the attributes to the ComponentOccurrence
       If attbSets.NameIsUsed("myPartGroup") Then
           attbSets.Item("myPartGroup").Delete()
       End If
 
        End If
    Next
The code for Button 5, “Remove all components from group” – that you copied from another Sub – gets all the components with your AttributeSet attached and deletes the AttributeSet from each one (rather than deleting it only from the selected components). In this Sub, after you had an ObjectCollection with all of the components in the Assembly with your AttributeSet attached, you used a For Next loop to delete the AttributeSet from each component. You also needed to make each component visible: after the Sub had finished its work, these components no longer had your AttributeSet attached, and so would no longer be affected by the “Show group” button.

This brings you to the end of this lesson and to the practical section of this guide.

Congratulations! You have just completed all the lessons in this guide. You have completed the journey from being an Inventor product user to learning the basics of programming, getting a first look at the Inventor API and creating your first real-world plug-in for Inventor.

All that remains is to take a look at some of the resources you will find useful during your onwards journey with the Autodesk Inventor API. We wish you all the very best!