- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
At first, I could not replicate the error. But I think I found the problem. The devil seems to be in the details. I could reproduce the problem with the following steps:
- Start Inventor.
- Load the addin with the add-in manager.
- Notice that the addin loads as expected!
- Unload the addin with the add-in manager.
- Notice that the addin is not really unloaded. The button is still on the ribbon and still works.
- Load the addin with the add-in manager.
- Now we get the exception!
The problem is that the ButtonDefinition and CommandControl are not unloaded and therefore they can't be recreated. For normal users, this should not be a problem because they rarely reload addins. And I don't know what your use case is but you can solve the problem by unloading the ButtonDefinition and CommandControl explicitly on deactivation of the addin.
Cahnge the StandardAddInServer.Deactivate() to look like this:
Public Sub Deactivate() Implements ApplicationAddInServer.Deactivate
_myButton.Unload()
_myButton = Nothing
End Sub
And your class MyButton should look like this.
Imports Inventor
Public Class MyButton
Private _inventor As Inventor.Application
Private _settingsButton As ButtonDefinition
Private _control As CommandControl
Public Sub New(inventor As Inventor.Application)
_inventor = inventor
SetupButtonDefinition()
AddButtonDefinitionToRibbon()
End Sub
Public Sub Unload()
_control.Delete()
_control = Nothing
_settingsButton.Delete()
_settingsButton = Nothing
End Sub
Private Sub SetupButtonDefinition()
Dim conDefs As ControlDefinitions = _inventor.CommandManager.ControlDefinitions
_settingsButton = conDefs.AddButtonDefinition(
"MyButton DisplayName",
"MyButton InternalName",
CommandTypesEnum.kEditMaskCmdType,
Guid.NewGuid().ToString(),
"MyButton DescriptionText",
"MyButton ToolTipText",)
AddHandler _settingsButton.OnExecute, AddressOf MyButton_OnExecute
_settingsButton.StandardIcon = PictureDispConverter.ToIPictureDisp(My.Resources.MyImage16x16)
_settingsButton.LargeIcon = PictureDispConverter.ToIPictureDisp(My.Resources.MyImage32x32)
End Sub
Private Sub AddButtonDefinitionToRibbon()
Dim ribbon As Ribbon = _inventor.UserInterfaceManager.Ribbons.Item("Assembly")
Dim ribbonTab As RibbonTab = ribbon.RibbonTabs.Item("id_TabManage")
Dim ribbonPanel As RibbonPanel = ribbonTab.RibbonPanels.Item("iLogic.RibbonPanel")
_control = ribbonPanel.CommandControls.AddButton(_settingsButton)
End Sub
Private Sub MyButton_OnExecute(Context As NameValueMap)
Try
Dim rule As New ThisRule()
rule.ThisApplication = _inventor
rule.Main()
Catch ex As Exception
MsgBox("Something went wrong while runing rule. Message: " & ex.Message)
End Try
End Sub
End Class
you can view the changes here:
https://github.com/hjalte79/MyILogicAddin/commit/6b566804dbe8ae50048f85f92b968192705245d8
Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Blog: hjalte.nl - github.com