Ribbon Icon Enable or Disable per document

Ribbon Icon Enable or Disable per document

bwang-tecoustics
Collaborator Collaborator
1,052 Views
7 Replies
Message 1 of 8

Ribbon Icon Enable or Disable per document

bwang-tecoustics
Collaborator
Collaborator

Hi, I was able to follow Inventor SDK sample file to add custom buttons on the ribbon. Now I want the button to be Enable or Disable per a parameter of the active document. It's similar to the Inventor Copy and Cut buttons. When you select something, the buttons will be enable, otherwise it shows as disable. Any help will be appreciated.

0 Likes
Accepted solutions (1)
1,053 Views
7 Replies
Replies (7)
Message 2 of 8

etaCAD
Advocate
Advocate

Hi, you should capture and react on Inventor's events. A good help is the EventWatcher. It helps you to find the correct events you have to react on and located in your SDK\DeveloperTools\Tools. The source code is included as well when you need an example.

 

Andreas

etaCAD.de

Andreas
etaCAD

0 Likes
Message 3 of 8

bwang-tecoustics
Collaborator
Collaborator

Thx. I checked the EventWatchers and try to use the Document OnActive event

 

Private Sub m_docEvents_OnActive(BeforeOrAfter As EventTimingEnum, Context As NameValueMap, HandlingCode As HandlingCodeEnum) Handles m_docEvents.OnActivate

 

 

But I got an error says the method does not have a compatible signature with the original event. I can check the parameter and set the button to disable by clicking the button. But just can't initialize it when the document is loaded.

0 Likes
Message 4 of 8

bwang-tecoustics
Collaborator
Collaborator

Also, after I added the document event handler, the add-in dll is not loaded. I think there are some handlers for the Ribbons only.

0 Likes
Message 5 of 8

etaCAD
Advocate
Advocate

When your add-in doesn't load I think there is an error in your code. Perhaps you want connect to an document event when there isn't any open document yet. Without your code it is just like reading from a glass bowl for me.

 

Andreas

etaCAD.de

Andreas
etaCAD

0 Likes
Message 6 of 8

bwang-tecoustics
Collaborator
Collaborator

Here is part of my code. It's basicly copied from the sample. You can see I can set one of the button to disable (fourth last row). But I just don't know how to set it enable or disable dynamically depending a condition. Once I add a If statement, the add-in won't load.

#Region "User interface definition"
        ' Sub where the user-interface creation is done.  This is called when
        ' the add-in loaded and also if the user interface is reset.
        Private Sub AddToUserInterface()
            ' This is where you'll add code to add buttons to the ribbon.

            '** Sample to illustrate creating a button on a new panel of the Tools tab of the Part ribbon.

            '** Sample to illustrate creating a button on a new panel of the Tools tab of the Assembly ribbon.
            ' Get the Assembly ribbon.
            Dim assmRibbon As Ribbon = g_inventorApplication.UserInterfaceManager.Ribbons.Item("Assembly")

            ' Get the "Tools" tab.
            Dim assmToolsTab As RibbonTab = assmRibbon.RibbonTabs.Item("id_TabTools")

            ' Create a new panel.
            Dim assmToolPanel As RibbonPanel = assmToolsTab.RibbonPanels.Add("BoMTool", "id_TECO_BoM_Tool", AddInClientID)

            ' Add a button.
            assmToolPanel.CommandControls.AddButton(asmBtnBomNorm, True)
            'asmBtnBomNorm.Enabled = Not (g_inventorApplication.ActiveDocument.ComponentDefinition.BOMStructure = BOMStructureEnum.kNormalBOMStructure)

            assmToolPanel.CommandControls.AddButton(asmBtnBomInsep, True)
            'asmBtnBomInsep.Enabled = Not (g_inventorApplication.ActiveDocument.ComponentDefinition.BOMStructure = BOMStructureEnum.kInseparableBOMStructure)
            'UpdateBoMButtons()

            '** Sample to illustrate creating a button on a new panel of the Tools tab of the Drawing ribbon.

            ' Get the Drawing ribbon.
            Dim dwgRibbon As Ribbon = g_inventorApplication.UserInterfaceManager.Ribbons.Item("Drawing")

            ' Get the "Tools" tab.
            'Dim dwgToolsTab As RibbonTab = dwgRibbon.RibbonTabs.Item("id_TabTools")
            Dim dwgAnnoteTab As RibbonTab = dwgRibbon.RibbonTabs.Item("id_TabAnnotate") ' Move the Buttons to under Annotation

            ' Create a new panel.
            Dim dwgAnnotePanel As RibbonPanel = dwgAnnoteTab.RibbonPanels.Add("TECO", "id_TECO_ToolBox", AddInClientID)

            ' Add a button.
            dwgAnnotePanel.CommandControls.AddButton(m_dwgBtnFind, True)
            dwgAnnotePanel.CommandControls.AddButton(m_dwgBtnSpell, True)
            m_dwgBtnSpell.Enabled = False


            'UpdateBoMButtons()
        End Sub
0 Likes
Message 7 of 8

etaCAD
Advocate
Advocate
Accepted solution

Your code doesn't show how you capture and work on Inventor's events. Below is a rough pattern what you code structure should look like.

 

 

Public Class YourClassName

' Declare event
Private WithEvents m_ApplicationsEvents As Inventor.ApplicationEvents


Public Sub Activate(ByVal siteObj As Inventor.ApplicationAddInSite, ByVal loaded1stTime As Boolean) Implements ApplicationAddInServer.Activate

...

' Connect to the representations interface events
        m_ApplicationsEvents = siteObj.ApplicationEvents

...

'Creating the buttons

...

End Sub


' Reacting on events

 Private Sub m_ApplicationsEvents_OnActivateDocument(DocumentObject As _Document, _
                                                        BeforeOrAfter As EventTimingEnum, _
                                                        Context As NameValueMap, _
                                                        ByRef HandlingCode As HandlingCodeEnum) Handles m_ApplicationsEvents.OnActivateDocument
        If BeforeOrAfter = EventTimingEnum.kAfter Then
           ActivateOrDeactivateButtonAccordingToYourConditions()
        End If
 End Sub


End Class

Use the EventWatcher to look on which event you should have look on. I think you should focus on ApplicatonEvents and DocumentEvents. There are several examples in the SDK and have a look in the help.

 

Andreas
etaCAD

0 Likes
Message 8 of 8

bwang-tecoustics
Collaborator
Collaborator

Thx a lot. This works. I was thinking the event on active documents would be as a document event. But actually it's an application event. I also referred to this site: http://modthemachine.typepad.com/my_weblog/2013/07/inventor-events-using-net-3-examples.html

 

Now my button can be set to enable or disable depends on the property of the opening document.

0 Likes