Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Custom ribbon tab activate event

8 REPLIES 8
SOLVED
Reply
Message 1 of 9
TA.Fehr
2006 Views, 8 Replies

Custom ribbon tab activate event

I know I'm close, but can't seem to figure out what I'm missing.

I've got a custom ribbon tab (Inspection) that shows when a Drawing is active.

However, I have a few docked windows that need to be associated with the inspection tab. When it's active they show up, when it's not they disappear.

 

I used Philippe's example to create a hook on the "ZeroDoc" environment, but when the event handler is called, the custom tab is not yet visible. When the custom tab is visible the component manager has already been initialized. I tried setting the addhandler when I loaded the custom tab, but it would seem as though the "activated" handle is not available.

 

Here is the sample code which fails because the "RibbonTab" is not the same object as the "Ribbons.item" that is used to create the tab, but I'm not sure how else to go about it.

        Private Sub AddToUserInterface()
            ' This is where you'll add code to add buttons to the ribbon.

            ' Get the part ribbon.
            Dim partRibbon As Ribbon = g_inventorApplication.UserInterfaceManager.Ribbons.Item("Drawing")
            Dim InspectionTab As Inventor.RibbonTab
            ' Get the "Inspection" tab.

            InspectionTab = partRibbon.RibbonTabs.Add("Inspection", "TAB_Inspection", Guid.NewGuid().ToString)
                For Each Tab As Autodesk.Windows.RibbonTab In Autodesk.Windows.ComponentManager.Ribbon.Tabs
                    If (Tab.Id = "id_Inspection") Then
                        AddHandler Tab.Activated, AddressOf Me.Tab_Activated
                    End If
                Next
            ' Create a new panel.
            Dim AddNew As Inventor.RibbonPanel = InspectionTab.RibbonPanels.Add("Add New", "Insp_Add_New", Guid.NewGuid().ToString)
            ' Add a button.
            AddNew.CommandControls.AddButton(m_AddNew, True, True)
        End Sub
8 REPLIES 8
Message 2 of 9
dgreatice
in reply to: TA.Fehr

Hi,

 

basic to ADD ribbon as I know is, because I use VS this my example, and you can find this Code in Inventor Addin Template :

 

Imports Inventor
Imports System.Runtime.InteropServices

-------------------------------------------------------------------------------------------------

Namespace MyCustomAddin

<ProgIdAttribute("MyCustomAddIn.StandardAddInServer"),
GuidAttribute("36568e3e-5cb8-468a-95f3-cdc8a6658a7e")>

 

Public Class StandardAddInServer
Implements ApplicationAddInServer

 

Private m_IA As Inventor.Application

Private m_UIEvents As UserInterfaceEvents

Private m_clientID As String

 

Public Sub Activate(ByVal AddInSiteObject As ApplicationAddInSite, ByVal FirstTime As Boolean) Implements ApplicationAddInServer.Activate

 

Try

m_IA = AddInSiteObject.Application

m_UIEvents = m_IA.UserInterfaceManager.UserInterfaceEvents

 

 

Dim addInCLSID As GuidAttribute
addInCLSID = CType(System.Attribute.GetCustomAttribute(GetType(StandardAddInServer), GetType(GuidAttribute)), GuidAttribute)

 

m_clientID = "{" & addInCLSID.Value & "}"

 

       If FirstTime = True Then
               AddOrUpdateUserInterface()

       End If
Catch ex As Exception
        MessageBox.Show(ex.ToString)
End Try

 

Private Sub AddOrUpdateUserInterface()

Dim oUIM As UserInterfaceManager = m_IA.UserInterfaceManager

Dim oIFS As InterfaceStyleEnum = oUIM.InterfaceStyle

 

If oIFS = InterfaceStyleEnum.kRibbonInterface Then

     Dim oRibs As Ribbons = oUIM.Ribbons

     Dim oAssyRib As Ribbon = oRibs.Item("Assembly")

     Dim oAssyRibTabs As RibbonTabs = oAssyRib.RibbonTabs
     Dim oAssyRibTab As RibbonTab = oAssyRibTabs.Add("MyRibbon", "id_MyCustomRibbon", m_clientID, "id_TabVault", True)

     'Next Command Control

end sub

 

End Class

End Namespace

 

 

In DrawingIn Drawing

In DrawingIn DrawingIn PartIn PartIn AssemblyIn Assembly

Please use the ACCEPT AS SOLUTION or KUDOS button if my Idea helped you to solve the problem.

Autodesk Inventor Professional Certified 2014
Message 3 of 9
TA.Fehr
in reply to: dgreatice

Thanks for the reply, but my issue is not with adding a tab or ribbon button. This I have done.

What I'd like to do is raise an event when the custom tab is selected/deselected.

 

 

In the screencast, using Philippe's code, I can create the event with on the "Tools" tab, but only in the "ZeroDoc" state, I cannot create the same event with the "Tools" tab in any other environment.

I'd like to have the exact same capability with the custom tab I've loaded called "Inspection"

 

 

 

Message 4 of 9
dgreatice
in reply to: TA.Fehr

Put you all code, so anyone can learn it and help you.

Please use the ACCEPT AS SOLUTION or KUDOS button if my Idea helped you to solve the problem.

Autodesk Inventor Professional Certified 2014
Message 5 of 9
CAD-e-Shack
in reply to: dgreatice

The code necessary is included in Philippes example which I've linked to in the original post.
I've included what is necessary that I use to create the custom tab:

Imports Inventor
Imports System.Runtime.InteropServices
Imports Microsoft.Win32
Imports Autodesk.Windows

Namespace Inspection
    <ProgIdAttribute("Inspection.StandardAddInServer"),
    GuidAttribute("5df622b8-f73c-4238-91d0-97e2b5739f0c")>
    Public Class StandardAddInServer
        Implements Inventor.ApplicationAddInServer
        Dim dgvDimValues As Windows.Forms.DataGridView
        Dim oUserIntrerfaceMgr As UserInterfaceManager
        Dim oWindowValue_Table As DockableWindow
        Dim oWindowCharacteristics As DockableWindow
        Dim WithEvents m_AppEvents As ApplicationEvents
        Private WithEvents m_uiEvents As UserInterfaceEvents
        Private WithEvents m_AddNew As ButtonDefinition
        Private WithEvents m_Show As ButtonDefinition
        Private WithEvents m_Hide As ButtonDefinition
        Private WithEvents oInteraction As InteractionEvents
        Private WithEvents oSelect As SelectEvents
#Region "ApplicationAddInServer Members"
        ' This method is called by Inventor when it loads the AddIn. The AddInSiteObject provides access  
        ' to the Inventor Application object. The FirstTime flag indicates if the AddIn is loaded for
        ' the first time. However, with the introduction of the ribbon this argument is always true.
        Public Sub Activate(ByVal addInSiteObject As Inventor.ApplicationAddInSite, ByVal firstTime As Boolean) Implements Inventor.ApplicationAddInServer.Activate
            ' Initialize AddIn members.
            g_inventorApplication = addInSiteObject.Application
            ' Connect to the user-interface events to handle a ribbon reset.
            m_uiEvents = g_inventorApplication.UserInterfaceManager.UserInterfaceEvents

            ' TODO: Add button definitions.

            ' Sample to illustrate creating a button definition.
            ' Dim largeIcon As stdole.IPictureDisp = PictureDispConverter.ToIPictureDisp(My.Resources.inspdimlarge)
            Dim smallIcon As stdole.IPictureDisp = PictureDispConverter.ToIPictureDisp(My.Resources.inspdimsmall)
            Dim controlDefs As Inventor.ControlDefinitions = g_inventorApplication.CommandManager.ControlDefinitions
            m_AddNew = controlDefs.AddButtonDefinition(
                "Insp Dim",
                "Inspection Dimension",
                CommandTypesEnum.kShapeEditCmdType,
                AddInClientID,
                "Insp Dim",
                "Insp Dim",
                PictureDispConverter.ToIPictureDisp(My.Resources.inspdimsmall),
                PictureDispConverter.ToIPictureDisp(My.Resources.inspdimlarge),
                ButtonDisplayEnum.kDisplayTextInLearningMode)
            m_Show = controlDefs.AddButtonDefinition(
                "Show",
                "Show Details",
                CommandTypesEnum.kShapeEditCmdType,
                AddInClientID,
                "Show",
                "Show",
                PictureDispConverter.ToIPictureDisp(My.Resources.inspdimsmall),
                PictureDispConverter.ToIPictureDisp(My.Resources.inspdimlarge),
                ButtonDisplayEnum.kDisplayTextInLearningMode)
            m_Hide = controlDefs.AddButtonDefinition(
                "Hide",
                "Hide Details",
                CommandTypesEnum.kShapeEditCmdType,
                AddInClientID,
                "Hide",
                "Hide",
                PictureDispConverter.ToIPictureDisp(My.Resources.inspdimsmall),
                PictureDispConverter.ToIPictureDisp(My.Resources.inspdimlarge),
                ButtonDisplayEnum.kDisplayTextInLearningMode)
            ' Add to the user interface, if it's the first time.
            If firstTime Then
                AddToUserInterface()
            End If
            AddHandler Autodesk.Windows.ComponentManager.ItemInitialized, AddressOf Me.ComponentManager_ItemInitialized
        End Sub

        ' This method is called by Inventor when the AddIn is unloaded. The AddIn will be
        ' unloaded either manually by the user or when the Inventor session is terminated.
        Public Sub Deactivate() Implements Inventor.ApplicationAddInServer.Deactivate

            ' TODO:  Add ApplicationAddInServer.Deactivate implementation

            ' Release objects.
            m_uiEvents = Nothing
            g_inventorApplication = Nothing

            System.GC.Collect()
            System.GC.WaitForPendingFinalizers()
        End Sub

        ' This property is provided to allow the AddIn to expose an API of its own to other 
        ' programs. Typically, this  would be done by implementing the AddIn's API
        ' interface in a class and returning that class object through this property.
        Public ReadOnly Property Automation() As Object Implements Inventor.ApplicationAddInServer.Automation
            Get
                Return Nothing
            End Get
        End Property

        ' Note:this method is now obsolete, you should use the 
        ' ControlDefinition functionality for implementing commands.
        Public Sub ExecuteCommand(ByVal commandID As Integer) Implements Inventor.ApplicationAddInServer.ExecuteCommand
        End Sub

#End Region

#Region "User interface definition"
        Private CharacteristicsForm As Settings
        Private Value_TableForm As Value_Table
        ' 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.

            ' Get the part ribbon.
            Dim partRibbon As Ribbon = g_inventorApplication.UserInterfaceManager.Ribbons.Item("Drawing")
            Dim InspectionTab As Inventor.RibbonTab
            ' Get the "Inspection" tab.

            InspectionTab = partRibbon.RibbonTabs.Add("Inspection", "TAB_Inspection", Guid.NewGuid().ToString)
            For Each Tab As Autodesk.Windows.RibbonTab In Autodesk.Windows.ComponentManager.Ribbon.Tabs
                If (Tab.Id = "id_Inspection") Then
                    AddHandler Tab.Activated, AddressOf Me.Tab_Activated
                End If
            Next
            ' Create a new panel.
            Dim AddNew As Inventor.RibbonPanel = InspectionTab.RibbonPanels.Add("Add New", "Insp_Add_New", Guid.NewGuid().ToString)

            ' Add a button.
            AddNew.CommandControls.AddButton(m_AddNew, True, True)
            AddNew.CommandControls.AddButton(m_Show, False, True)
            AddNew.CommandControls.AddButton(m_Hide, False, True)
        End Sub
        Private Sub m_uiEvents_OnResetRibbonInterface(Context As NameValueMap) Handles m_uiEvents.OnResetRibbonInterface
            ' The ribbon was reset, so add back the add-ins user-interface.
            AddToUserInterface()
        End Sub
        Private Sub ComponentManager_ItemInitialized(ByVal sender As Object, ByVal e As RibbonItemEventArgs)
            'now one Ribbon item is initialized, but the Ribbon control
            'may not be available yet, so check before
            If (Not (Autodesk.Windows.ComponentManager.Ribbon) Is Nothing) Then
                For Each Tab As Autodesk.Windows.RibbonTab In Autodesk.Windows.ComponentManager.Ribbon.Tabs
                    If (Tab.Id = "id_TabTools") Then 'id_Inspection") Then
                        AddHandler Tab.Activated, AddressOf Me.Tab_Activated
                    End If

                Next
                'and remove the event handler
                RemoveHandler Autodesk.Windows.ComponentManager.ItemInitialized, AddressOf Me.ComponentManager_ItemInitialized
            End If

        End Sub

        Private Sub Tab_Activated(ByVal sender As Object, ByVal e As EventArgs)
            System.Windows.Forms.MessageBox.Show(("Tab " _
                        + (ComponentManager.Ribbon.ActiveTab.Id + " Activated!")))
        End Sub

#End Region

    End Class
End Namespace


Public Module Globals
    ' Inventor application object.
    Public g_inventorApplication As Inventor.Application

#Region "Function to get the add-in client ID."
    ' This function uses reflection to get the GuidAttribute associated with the add-in.
    Public Function AddInClientID() As String
        Dim guid As String = ""
        Try
            Dim t As Type = GetType(Inspection.StandardAddInServer)
            Dim customAttributes() As Object = t.GetCustomAttributes(GetType(GuidAttribute), False)
            Dim guidAttribute As GuidAttribute = CType(customAttributes(0), GuidAttribute)
            guid = "{" + guidAttribute.Value.ToString() + "}"
        Catch
        End Try

        Return guid
    End Function
#End Region

#Region "Image Converter"
    ' Class used to convert bitmaps and icons from their .Net native types into
    ' an IPictureDisp object which is what the Inventor API requires. A typical
    ' usage is shown below where MyIcon is a bitmap or icon that's available
    ' as a resource of the project.
    '
    ' Dim smallIcon As stdole.IPictureDisp = PictureDispConverter.ToIPictureDisp(My.Resources.MyIcon)

    Public NotInheritable Class PictureDispConverter
        <DllImport("OleAut32.dll", EntryPoint:="OleCreatePictureIndirect", ExactSpelling:=True, PreserveSig:=False)>
        Private Shared Function OleCreatePictureIndirect(
            <MarshalAs(UnmanagedType.AsAny)> ByVal picdesc As Object,
            ByRef iid As Guid,
            <MarshalAs(UnmanagedType.Bool)> ByVal fOwn As Boolean) As stdole.IPictureDisp
        End Function

        Shared iPictureDispGuid As Guid = GetType(stdole.IPictureDisp).GUID

        Private NotInheritable Class PICTDESC
            Private Sub New()
            End Sub

            'Picture Types
            Public Const PICTYPE_BITMAP As Short = 1
            Public Const PICTYPE_ICON As Short = 3

            <StructLayout(LayoutKind.Sequential)>
            Public Class Icon
                Friend cbSizeOfStruct As Integer = Marshal.SizeOf(GetType(PICTDESC.Icon))
                Friend picType As Integer = PICTDESC.PICTYPE_ICON
                Friend hicon As IntPtr = IntPtr.Zero
                Friend unused1 As Integer
                Friend unused2 As Integer

                Friend Sub New(ByVal icon As System.Drawing.Icon)
                    Me.hicon = icon.ToBitmap().GetHicon()
                End Sub
            End Class

            <StructLayout(LayoutKind.Sequential)>
            Public Class Bitmap
                Friend cbSizeOfStruct As Integer = Marshal.SizeOf(GetType(PICTDESC.Bitmap))
                Friend picType As Integer = PICTDESC.PICTYPE_BITMAP
                Friend hbitmap As IntPtr = IntPtr.Zero
                Friend hpal As IntPtr = IntPtr.Zero
                Friend unused As Integer

                Friend Sub New(ByVal bitmap As System.Drawing.Bitmap)
                    Me.hbitmap = bitmap.GetHbitmap()
                End Sub
            End Class
        End Class

        Public Shared Function ToIPictureDisp(ByVal icon As System.Drawing.Icon) As stdole.IPictureDisp
            Dim pictIcon As New PICTDESC.Icon(icon)
            Return OleCreatePictureIndirect(pictIcon, iPictureDispGuid, True)
        End Function

        Public Shared Function ToIPictureDisp(ByVal bmp As System.Drawing.Bitmap) As stdole.IPictureDisp
            Dim pictBmp As New PICTDESC.Bitmap(bmp)
            Return OleCreatePictureIndirect(pictBmp, iPictureDispGuid, True)
        End Function
    End Class
#End Region
End Module

 

Message 6 of 9
dgreatice
in reply to: CAD-e-Shack

Hi,

 

I don't understand what do you want.

 

You want create Custom Tab Inspection in ZeroDoc? or,

You just want show message? Tab_Inspection is activate message box?

 

if you want both are showing in ZeroDoc and Drawing, you must declare twice in AddUserInterface component.

#Region ZeroDoc

 

Dim ZeroRibbon As Ribbon = g_inventorApplication.UserInterfaceManager.Ribbons.Item("ZeroDoc")

#End Region

 

 

#Region Drawing

 

Dim DwgRibbon As Ribbon = g_inventorApplication.UserInterfaceManager.Ribbons.Item("Drawing")

#End Region

 

Please use the ACCEPT AS SOLUTION or KUDOS button if my Idea helped you to solve the problem.

Autodesk Inventor Professional Certified 2014
Message 7 of 9
TA.Fehr
in reply to: dgreatice

As I stated in the original post, my goal is to have some docked windows appear and dock on the screen only when the "Inspection" tab is active. In all other cases the docked windows should hide. (Much like how the bottom docked window in the "presentation" scene only shows up when you have a presentation file as the active  file).

 

In order to do this I need to create an event for when the "Inspection" tab is activated. I used the example to show that I know it's possible, but I can only create an active tab event in the "ZeroDoc" screen. I cannot create the event in the "Drawing" screen because after that point the ComponentManger has already initialized (which is where Phillipe used as a trigger to setup his addhandler event).

 

The "Inspection" tab needs to be in the drawing environment, and I need to be able to trigger an event when that tab is activated and deactivated.

Message 8 of 9
ADAAAM
in reply to: dgreatice

Hi!

 

I would like to create a custom RibbonTab. I used your code, but unfortunately it doesn't appear the tab. When I open Inventor it "MyRibbon" appears just for 1 sec then disappear. How can I solve it? Can you help me?

 

#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
            Dim oUIM As UserInterfaceManager = m_IA.UserInterfaceManager
            Dim oIFS As InterfaceStyleEnum = oUIM.InterfaceStyle



            If oIFS = InterfaceStyleEnum.kRibbonInterface Then
                Dim oRibs As Ribbons = oUIM.Ribbons
                Dim oAssyRib As Ribbon = oRibs.Item("ZeroDoc")
                Dim oAssyRibTabs As RibbonTabs = oAssyRib.RibbonTabs
                Dim oAssyRibTab As RibbonTab = oAssyRibTabs.Add("MyRibbon", "id_MyCustomRibbon", m_clientID, "id_TabTools", True)
            End If

 I'm using Inventor 2020 and VS2017.

Message 9 of 9
TA.Fehr
in reply to: TA.Fehr

Hate to resurrect an old thread, but I've literally been searching for years, and no solution has yet been posted to this question.

 

The solution lies in keeping the addhandler active until the correct tab has been initiated.

The sample provided removes the handler as soon as the first ribbon is activated (ZeroDoc) however, as this isn't the tab desired, we need to keep the handler open until that tab is created.

 

Below is an example where an event is raised whenever the "Tools" tab is activated in the "Drawing" and "Assembly" environments.

 

It may be necessary, depending on the application, to include a "deactivate" event as well.

For good measure in my add-in I also included an event to "OnActivateDocument" from the ApplicationEvents properties inherent inside the Inventor Application. That way if someone switches between documents, the event will still fire even if the "Tools" tab is already activated.

 

Hope this helps anyone who runs into this problem in the future.

 

        Dim AddhandlerDrawing As Boolean = False
        Dim addhandlerAssembly As Boolean = false

 

 

 

        Public Sub Activate(ByVal addInSiteObject As Inventor.ApplicationAddInSite, ByVal firstTime As Boolean) Implements Inventor.ApplicationAddInServer.Activate
                AddHandler Autodesk.Windows.ComponentManager.ItemInitialized, AddressOf Me.ComponentManager_ItemInitialized
        End Sub
 Private Sub ComponentManager_ItemInitialized(ByVal sender As Object, ByVal e As RibbonItemEventArgs)
            'now one Ribbon item is initialized, but the Ribbon control
            'may not be available yet, so check before
            If (Not (Autodesk.Windows.ComponentManager.Ribbon) Is Nothing) Then
                If ComponentManager.Ribbon.ToString = "Drawing"  Then
                    For Each Tab As Autodesk.Windows.RibbonTab In Autodesk.Windows.ComponentManager.Ribbon.Tabs
                        If (Tab.Id = "id_TabTools") Then
                            AddHandler Tab.Activated, AddressOf Me.Tab_Activated
                            AddhandlerDrawing = true
                        End If                        
                    Next
                    'and remove the event handler
                    
                Else if componentmanager.ribbon.tostring = "Assembly" then
                     For Each Tab As Autodesk.Windows.RibbonTab In Autodesk.Windows.ComponentManager.Ribbon.Tabs
                        If (Tab.Id = "id_TabTools") Then
                            AddHandler Tab.Activated, AddressOf Me.Tab_Activated
                            addhandlerAssembly = true
                        End If
                        
                    Next
                End If
               if addhandlerdrawing = True AndAlso
                        addhandlerAssembly = True Then RemoveHandler ComponentManager.ItemInitialized, AddressOf Me.ComponentManager_ItemInitialized
                
            End If

        End Sub

 

        Private Sub Tab_Activated(ByVal sender As Object, ByVal e As EventArgs)
           msgbox("Tab activated")
        End Sub

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Technology Administrators


Autodesk Design & Make Report