We all want to be efficient in our code writing and using design patterns can help with this. A design pattern is utility code that will solve a commonly recurring task. For Inventor AddIns creating the ribbon elements is a common task that can be expedited by using a design pattern. My colleagues Jan Liska and Philippe Leefsma created this example design pattern named AdnRibbonBuilder. This is XML based and provides these advantages:
- Define Ribbon elements much faster and more easily
- Generate the Ribbon items dynamically at loading
- Use external XML that can be replaced or modified by user
To use this design pattern you add the AdnRibbonBuilder class to your project. This class has functions that will create the Ribbon elements by parsing an xml file. Once you have added the class to your project there are two things you need to do.
- Create the xml file with the settings that will be parsed. There are tags in the xml file that will allow you control where the ribbon element is created and what the ribbon element is.
- Call the CreateRibbon or CreateRibbonFromFile methods of the AdnRibbonBuilder class from the Activate method of the AddIn. One of the CreateRibbon parameters is a string that is the name of a resource. (This resource is an embedded xml file). CreateRibbonFromFile takes a string that is a name of an xml file on disk.
Here is the xml code from the project in this zip file that creates a button when it is parsed by AdnRibbonBuilder. Notice that it is going to add the button to the Modify panel on the Part ribbon. The Button will use the ButtonDefintion named Autodesk:RibbonVBTest:Button4. (This ButtonDefinition needs to be already created in the project before the xml file is parsed).
<RibbonTab ribbons="Part"
internalName="id_TabModel"
displayName=""
targetTab=""
insertBefore="false">
<RibbonPanel internalName="id_PanelP_ModelModify"
displayName=""
targetPanel=""
insertBefore="false">
<Button internalName="Autodesk:RibbonVBTest:Button4"
useLargeIcon="false"
isInSlideout="false"
targetControl=""
insertBefore="false"/>
</RibbonPanel>
</RibbonTab>
Below is the code from the attached project that calls CreateRibbonFromFile or CreateRibbon. Having the ability to use an xml file on disk makes it easy to create a different layout of the ribbon elements. If you want to try using the file, rename this file so the extension is only “xml”. (Ribbon_By_Xml_Example.ribbons.override.rename_to_just_xml)
The tags in the xml file cause the buttons to be created as a slide out instead of on the main ribbon.
(isInSlideout="true")
If this xml file is found (this example is looking for it at the same location of the AddIn dll) it will be used instead of the xml resource.
Here is the code:
Dim asmName As String = _ Assembly.GetExecutingAssembly().GetName().Name If (System.IO.File.Exists _ (asmName + ".ribbons.override.xml")) Then AdnRibbonBuilder.CreateRibbonFromFile( m_inventorApplication, Me.GetType, asmName + ".ribbons.override.xml") Else AdnRibbonBuilder.CreateRibbon( m_inventorApplication, Me.GetType, "Ribbon_By_Xml_Example.Ribbons.xml") End If
You can explore the AdnRibbonBuilder class to see how it creates the ribbon elements based on the tags in the xml file. The zip file with the project also has an xml file named ribbons_template.xml. This file has examples of xml tags that will create a few other ribbon elements. Here is the complete list of ribbon elements that AdnRibbonBuilder can create. (From the ProcessPanelNode Sub Procedure).
Case "Button" processMethod = _ New Action(Of RibbonPanel, XmlNode) _ (AddressOf ProcessButtonNode) Exit Select Case "Separator" processMethod = _ New Action(Of RibbonPanel, XmlNode) _ (AddressOf ProcessSeparatorNode) Exit Select Case "ButtonPopup" processMethod = _ New Action(Of RibbonPanel, XmlNode) _ (AddressOf ProcessButtonPopupNode) Exit Select Case "Popup" processMethod = _ New Action(Of RibbonPanel, XmlNode) _ (AddressOf ProcessPopupNode) Exit Select Case "ComboBox" processMethod = _ New Action(Of RibbonPanel, XmlNode) _ (AddressOf ProcessComboBoxNode) Exit Select Case "Gallery" processMethod = _ New Action(Of RibbonPanel, XmlNode) _ (AddressOf ProcessGalleryNode) Exit Select Case "Macro" processMethod = _ New Action(Of RibbonPanel, XmlNode) _ (AddressOf ProcessMacroNode) Exit Select Case "SplitButton" processMethod = _ New Action(Of RibbonPanel, XmlNode) _ (AddressOf ProcessSplitButtonNode) Exit Select Case "SplitButtonMRU" processMethod = _ New Action(Of RibbonPanel, XmlNode) _ AddressOf ProcessSplitButtonMRUNode) Exit Select Case "TogglePopup" processMethod = _ New Action(Of RibbonPanel, XmlNode) _ (AddressOf ProcessTogglePopupNode) Exit Select Case Else Continue For
Wayne Brill

Leave a Reply