Addin Dynamically add/remove toolbar buttons

Addin Dynamically add/remove toolbar buttons

pball
Mentor Mentor
841 Views
4 Replies
Message 1 of 5

Addin Dynamically add/remove toolbar buttons

pball
Mentor
Mentor

I've been wanting to add the ability for users to choose which addin buttons are shown and in what order. I'm wondering if anyone has experience doing this because I couldn't figure out how to reload the buttons properly.

 

From my previous failures of doing this I remember something about button definitions already existing so I believe all of the addin buttons would have to be removed and then they can be added again. What would really help me is if someone could say make a function X that removes buttons like this and then do something else and finally run the "Private Sub AddToUserInterface()" to add buttons again, if that's how it works. Thanks

Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style
0 Likes
842 Views
4 Replies
Replies (4)
Message 2 of 5

pball
Mentor
Mentor

Well I'm halfway there and still just as stuck. I figured out how to delete the ribbon bar groups that my addin creates. Can't figure out how to add the buttons back after that though.

 

Some parts of my code to illustrate what's happening.

 

'Stick this in Public Class StandardAddInServer

Public Sub reset_ui() Dim PartRibbon As Ribbon = g_inventorApplication.UserInterfaceManager.Ribbons.Item("Part") PartRibbon.RibbonTabs.Item("id_TabModel").RibbonPanels.Item("MCRI").Delete() PartRibbon.RibbonTabs.Item("id_TabSheetMetal").RibbonPanels.Item("MCRI").Delete() PartRibbon.RibbonTabs.Item("id_TabTools").RibbonPanels.Item("MCRI").Delete()

'Up to here works

AddToUserInterface() 'Doesn't work

'Lets try to run activate instead
Activate(Nothing, True)

End Sub

Private Sub AddToUserInterface()
'Ribbon bar in assembly file
  Dim AssemblyRibbon As Ribbon = g_inventorApplication.UserInterfaceManager.Ribbons.Item("Assembly")
'Tools tab in assembly file
Dim AssemblyPanel_Tools As RibbonPanel = AssemblyRibbon.RibbonTabs.Item("id_TabTools").RibbonPanels.Add("MCRI", "MCRI", AddInClientID)

'This fails because Edit_IProps_Button is nothing
AssemblyPanel_Tools.CommandControls.AddButton(Edit_IProps_Button)
End Sub

Public Sub Activate(......)
......

Dim smallIcon As stdole.IPictureDisp = smallIcon = PictureDispConverter.ToIPictureDisp(My.Resources.edit_iprops)
'Line below fails, maybe because edit_iprops_button already exists????
Edit_IProps_Button = controlDefs.AddButtonDefinition("Edit iProperties", "Edit_IProps", CommandTypesEnum.kQueryOnlyCmdType, AddInClientID, "Allows easy editing of mutltiple iProperties.", "Edit iProperties", smallIcon)


End Sub

 

According to a comment from the template included with the api sdk tools the AddToUserInterface() sub is called when the add-in is loaded or the user interface is reset. So there should be some way to run it again to add the buttons back.

 

Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style
0 Likes
Message 3 of 5

Anonymous
Not applicable

Hi pball,

The reason you may be encountering an error is because your button object already exists as a control definition even though you removed the panel on which it was placed. As for adding buttons back into the ribbon dynamically, I have found that deleting the buttons and adding them back tends to be a bit unreliable. This is especially true if the button objects are not disposed of properly in your code. What I do now that is far more dependable and requires less code is to make the buttons invisible. In my own projects, controlling button visibility mirrors the desired behavior of deletion without having to recreate that button object each time.

0 Likes
Message 4 of 5

asiteur
Collaborator
Collaborator

I believe that Inventor only updates the toolbar buttons on load. So I would suggest you require the user to restart the application after changing their preferences.



Alexander Siteur
Project Engineer at MARIN | NL
LinkedIn

0 Likes
Message 5 of 5

pball
Mentor
Mentor

jwterminator1

 

I'm guessing that hiding and unhiding buttons will not allow the order of the buttons to be changed. Being able to reorder the buttons is a big part of what I'm trying to do.

 

asiteur

 

I know that an add-in should be able to add it's buttons again if a reset of the ribbon bar is done. So it should be possible to remove them and add them again inside of the add-in. I just cannot find any documentation on this since it doesn't seem to be a popular thing to do. So I'd rather not resort to forcing a restart until other options are exhausted.

Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style
0 Likes