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!