Add custom created Ribbon Tab with VS

Add custom created Ribbon Tab with VS

ADAAAM
Participant Participant
3,911 Views
15 Replies
Message 1 of 16

Add custom created Ribbon Tab with VS

ADAAAM
Participant
Participant

Hi! I tied to add new Ribbon Tab for Inventor, but I don't show my created tab if I open an assembly. I tied to add panel for Tools Tab and it is worked. (I changed in code "id_TabSample" to id_TabTools"). I am using VS2017 and Inventor Pro 2020.

Public Class StandardAddInServer
Implements Inventor.ApplicationAddInServer

Private WithEvents m_uiEvents As UserInterfaceEvents
Private WithEvents m_sampleButton As ButtonDefinition



#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.Pic)
Dim smallIcon As stdole.IPictureDisp = PictureDispConverter.ToIPictureDisp(My.Resources.Pic1)
Dim controlDefs As Inventor.ControlDefinitions = g_inventorApplication.CommandManager.ControlDefinitions
m_sampleButton = controlDefs.AddButtonDefinition("Command Name1", "Internal Name", CommandTypesEnum.kShapeEditCmdType, AddInClientID)


' Add to the user interface, if it's the first time.
If firstTime Then
AddToUserInterface()
End If


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"
' 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("Assembly")

'Add ribbonTab
Dim oTab As RibbonTab = partRibbon.RibbonTabs.Add("Sample", "Sample12", Guid.NewGuid().ToString())


' Get the "Tools" tab.
Dim toolsTab As RibbonTab = partRibbon.RibbonTabs.Item("id_TabSample")

' Create a new panel.
Dim customPanel As RibbonPanel = toolsTab.RibbonPanels.Add("Sample21", "MysSample", AddInClientID)

' Add a button.
customPanel.CommandControls.AddButton(m_sampleButton)
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

' Sample handler for the button.
'Private Sub m_sampleButton_OnExecute(Context As NameValueMap) Handles m_sampleButton.OnExecute
' MsgBox("Button was clicked.")
'End Sub
#End Region

End Class
0 Likes
3,912 Views
15 Replies
Replies (15)
Message 2 of 16

yan.gauthier
Advocate
Advocate
This is what I did in C# to create a ribbon in the assembly environment. Hope it helps !
 
using System;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Windows.Forms;
using Inventor;
using Microsoft.Win32;
using System.Linq;

namespace GenikAddin
{
    /// <summary>
    /// This is the primary AddIn Server class that implements the ApplicationAddInServer interface
    /// that all Inventor AddIns are required to implement. The communication between Inventor and
    /// the AddIn is via the methods on this interface.
    /// </summary>
    
    public class AddInParameters
    {
        //public static string addInCLSIDString;
    }

    
    [GuidAttribute("7AD36B50-4F53-4314-84E7-5F47BEF796C8")]
    public class AddInServer : Inventor.ApplicationAddInServer
    {
        #region Data Members
        
        //Inventor application object
        private Inventor.Application m_inventorApplication;

        //buttons
        private DimGenButton m_DimGenButton;
        private BomGeneratorButton m_BomGeneratorButton;
        private DrawingToolsButton m_DrawingToolsButton;
        private LoadProp.LoadPropButton m_LoadPropButton;


        //user interface event
        private UserInterfaceEvents m_userInterfaceEvents;

        // ribbon panel
        RibbonPanel m_drawingAnnotateDimGenRibbonPanel;
        RibbonPanel m_AssemblyOrganikRibbonPanel;

        //event handler delegates
        

        private Inventor.UserInterfaceEventsSink_OnResetCommandBarsEventHandler UserInterfaceEventsSink_OnResetCommandBarsEventDelegate;
        private Inventor.UserInterfaceEventsSink_OnResetEnvironmentsEventHandler UserInterfaceEventsSink_OnResetEnvironmentsEventDelegate;
        private Inventor.UserInterfaceEventsSink_OnResetRibbonInterfaceEventHandler UserInterfaceEventsSink_OnResetRibbonInterfaceEventDelegate;

        public static string addInCLSIDString;
        #endregion

        public AddInServer()
        {
        }

        #region ApplicationAddInServer Members

        public void Activate(Inventor.ApplicationAddInSite addInSiteObjectbool firstTime)
        {
            try
            {
                //the Activate 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

                //initialize AddIn members
                m_inventorApplication = addInSiteObject.Application;
                Button.InventorApplication = m_inventorApplication;

                //initialize event delegates
                m_userInterfaceEvents = m_inventorApplication.UserInterfaceManager.UserInterfaceEvents;

                UserInterfaceEventsSink_OnResetCommandBarsEventDelegate = new UserInterfaceEventsSink_OnResetCommandBarsEventHandler(UserInterfaceEvents_OnResetCommandBars);
                m_userInterfaceEvents.OnResetCommandBars += UserInterfaceEventsSink_OnResetCommandBarsEventDelegate;

                UserInterfaceEventsSink_OnResetEnvironmentsEventDelegate = new UserInterfaceEventsSink_OnResetEnvironmentsEventHandler(UserInterfaceEvents_OnResetEnvironments);
                m_userInterfaceEvents.OnResetEnvironments += UserInterfaceEventsSink_OnResetEnvironmentsEventDelegate;

                UserInterfaceEventsSink_OnResetRibbonInterfaceEventDelegate = new UserInterfaceEventsSink_OnResetRibbonInterfaceEventHandler(UserInterfaceEvents_OnResetRibbonInterface);
                m_userInterfaceEvents.OnResetRibbonInterface += UserInterfaceEventsSink_OnResetRibbonInterfaceEventDelegate;
                
                //load image icons for UI items
                Icon BomGeneratorIcon = new Icon (new Icon(this.GetType(), "Organik.ico"), new Size(16,16));
                Icon LoadPropIcon = new Icon(this.GetType(), "LoadProp1.ico");
    
                //retrieve the GUID for this class
                GuidAttribute addInCLSID
                addInCLSID = (GuidAttribute)GuidAttribute.GetCustomAttribute(typeof(AddInServer), typeof(GuidAttribute));
                
                addInCLSIDString = "{" + addInCLSID.Value + "}";


                m_BomGeneratorButton = new BomGeneratorButton("Bom Generator""Autodesk:Organik:BomGenCmdBtn"CommandTypesEnum.kShapeEditCmdTypeaddInCLSIDString"BomQuery""Bom Generator"BomGeneratorIconBomGeneratorIconButtonDisplayEnum.kDisplayTextInLearningMode);
                m_LoadPropButton = new LoadProp.LoadPropButton("LoadProp""Autodesk:Organik:LoadPropBtn"CommandTypesEnum.kShapeEditCmdTypeaddInCLSIDString"LoadProp""LoadProp Form"LoadPropIconLoadPropIconButtonDisplayEnum.kDisplayTextInLearningMode);




                //create the command category
                CommandCategory bomCategory = m_inventorApplication.CommandManager.CommandCategories.Add("Bom""Autodesk:Organik:Bom"addInCLSIDString);


                bomCategory.Add(m_BomGeneratorButton.ButtonDefinition);
                bomCategory.Add(m_LoadPropButton.ButtonDefinition);
                
                if (firstTime == true)
                {
                    //access user interface manager
                    UserInterfaceManager userInterfaceManager;
                    userInterfaceManager = m_inventorApplication.UserInterfaceManager;

                    InterfaceStyleEnum interfaceStyle;
                    interfaceStyle = userInterfaceManager.InterfaceStyle;

                    //create the UI for classic interface
                    if (interfaceStyle == InterfaceStyleEnum.kClassicInterface)
                    {

                        CommandBar bomCommandBar;
                        bomCommandBar = userInterfaceManager.CommandBars.Add("BOM""Autodesk:Organik:BomGenToolBar"CommandBarTypeEnum.kRegularCommandBaraddInCLSIDString);


                        bomCommandBar.Controls.AddButton(m_BomGeneratorButton.ButtonDefinition0);
                        bomCommandBar.Controls.AddButton(m_LoadPropButton.ButtonDefinition0);
     

                        //Get the drawing environment base object
                        Inventor.Environment drawingEnvironment;
                        drawingEnvironment = userInterfaceManager.Environments["DLxDrawingEnvironment"];

                        //make this command bar accessible in the panel menu for the drawing environment.
                        drawingEnvironment.PanelBar.CommandBarList.Add(dimGenCommandBar);    

                        //Get Assembly environment base object
                        Inventor.Environment assemblyEnvironment;
                        assemblyEnvironment = userInterfaceManager.Environments["AMxAssemblyEnvironment"];

                        //make this command bar accessible in the panel menu for the drawing environment.
                        assemblyEnvironment.PanelBar.CommandBarList.Add(bomCommandBar);
                    }
                    //create the UI for ribbon interface
                    else
                    {
                        //get the ribbon associated with Drawing document
                        Inventor.Ribbons ribbons;
                        ribbons = userInterfaceManager.Ribbons;

                        //get the ribbon Associated with Assembly document
                        Inventor.Ribbon assemblyRibbon;
                        assemblyRibbon = ribbons["Assembly"];

                        //get tabs associated with assembly ribbon
                        RibbonTabs assemblyRibbonTabs;
                        assemblyRibbonTabs = assemblyRibbon.RibbonTabs;

                        RibbonTab assemblyRibbonTab;
                        assemblyRibbonTab = assemblyRibbonTabs.Add("Organik""Autodesk:Organik:Bom"addInCLSIDString);

                        //Create new panel with the tab
                        RibbonPanels assemblyRibbonPannels;
                        assemblyRibbonPannels = assemblyRibbonTab.RibbonPanels;

                        m_AssemblyOrganikRibbonPanel = assemblyRibbonPannels.Add("Organik""Autodesk:Organik:AssemblyRibbonPanel"Guid.NewGuid().ToString());

                        //add controls to the Organik Assembly Pannel
                        CommandControls organikAssemblyRibbonPanelCtrls;
                        organikAssemblyRibbonPanelCtrls = m_AssemblyOrganikRibbonPanel.CommandControls;

                        CommandControl bomGeneratorButtonControl;
                        bomGeneratorButtonControl = organikAssemblyRibbonPanelCtrls.AddButton(m_BomGeneratorButton.ButtonDefinition);

                        CommandControl WPFFormButtonControl;
                        WPFFormButtonControl = organikAssemblyRibbonPanelCtrls.AddButton(m_LoadPropButton.ButtonDefinition);


                    }
                }

                //Dummy connection to server to prevent first load delay when running
                string dummy;
                
                using (var context = new OrganikDB.OrganikSQL(@"Data Source = M005\GENIKERP; Initial Catalog = OrganikSQL; Persist Security Info = True; User ID = SecuriteOrganik; Password = genik123"))
                {
                    dummy = context.tabTmtGrp.Select(x => x.Materiel).FirstOrDefault();

                }

                //MessageBox.Show ("To access the commands of the sample addin, activate a 2d sketch of a part \n document and select the \"AddInSlot\" toolbar within the panel menu");
            }
            catch(Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }

        public void Deactivate()
        {
            //the Deactivate 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
        
            try
            {
                m_userInterfaceEvents.OnResetCommandBars -= UserInterfaceEventsSink_OnResetCommandBarsEventDelegate;
                m_userInterfaceEvents.OnResetEnvironments -= UserInterfaceEventsSink_OnResetEnvironmentsEventDelegate;

                UserInterfaceEventsSink_OnResetCommandBarsEventDelegate = null;
                UserInterfaceEventsSink_OnResetEnvironmentsEventDelegate = null;
                m_userInterfaceEvents = null;
                
                if (m_AssemblyOrganikRibbonPanel != null)
                {
                    m_AssemblyOrganikRibbonPanel.Delete();
                }

                //release inventor Application object
                    Marshal.ReleaseComObject(m_inventorApplication);
                m_inventorApplication = null;

                GC.WaitForPendingFinalizers();
                GC.Collect();
            }
            catch(Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }

        public void ExecuteCommand(int CommandID)
        {
            //this method was used to notify when an AddIn command was executed
            //the CommandID parameter identifies the command that was executed
    
            //Note:this method is now obsolete, you should use the new
            //ControlDefinition objects to implement commands, they have
            //their own event sinks to notify when the command is executed
        }

        public object Automation
        {
            //if you want to return an interface to another client of this addin,
            //implement that interface in a class and return that class object 
            //through this property

            get
            {
                return null;
            }
        }

        private void UserInterfaceEvents_OnResetCommandBars(ObjectsEnumerator commandBarsNameValueMap context)
        {
            try
            {
                CommandBar commandBar;
                for (int commandBarCt = 1commandBarCt <= commandBars.CountcommandBarCt++)
                {
                    commandBar = (Inventor.CommandBar)commandBars[commandBarCt];
                    
                    if (commandBar.InternalName == "Autodesk:Organik:BomGenToolBar")
                    {

                        //add buttons to toolbar
                        commandBar.Controls.AddButton(m_BomGeneratorButton.ButtonDefinition0);
                        commandBar.Controls.AddButton(m_LoadPropButton.ButtonDefinition0);
                        return;
                    }
                }               
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }

        private void UserInterfaceEvents_OnResetEnvironments(ObjectsEnumerator environmentsNameValueMap context)
        {
            try
            {                
                Inventor.Environment environment;
                for (int environmentCt = 1environmentCt <= environments.CountenvironmentCt++)
                {

                    if (environment.InternalName == "AMxAssemblyEnvironment")
                    {
                        //make this command bar accessible in the panel menu for the 3d Assembly environment.
                        environment.PanelBar.CommandBarList.Add(m_inventorApplication.UserInterfaceManager.CommandBars["Autodesk:Organik:BomGenToolBar"]);

                        return;
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }

        private void UserInterfaceEvents_OnResetRibbonInterface(NameValueMap context)
        {
            try
            {

                UserInterfaceManager userInterfaceManager;
                userInterfaceManager = m_inventorApplication.UserInterfaceManager;

                //get the ribbon associated with Drawing document
                Inventor.Ribbons ribbons;
                ribbons = userInterfaceManager.Ribbons;

                //get the ribbon Associated with Assembly document
                Inventor.Ribbon assemblyRibbon;
                assemblyRibbon = ribbons["Assembly"];

                //get tabs associated with assembly ribbon
                RibbonTabs assemblyRibbonTabs;
                assemblyRibbonTabs = assemblyRibbon.RibbonTabs;

                RibbonTab assemblyRibbonTab;
                assemblyRibbonTab = assemblyRibbonTabs.Add("Organik""Autodesk:Organik:Bom"Guid.NewGuid().ToString());

                //Create new panel with the tab
                RibbonPanels assemblyRibbonPannels;
                assemblyRibbonPannels = assemblyRibbonTab.RibbonPanels;

                m_AssemblyOrganikRibbonPanel = assemblyRibbonPannels.Add("Organik""Autodesk:Organik:AssemblyRibbonPanel"Guid.NewGuid().ToString());

                //add controls to the Organik Assembly Pannel
                CommandControls organikAssemblyRibbonPanelCtrls;
                organikAssemblyRibbonPanelCtrls = m_AssemblyOrganikRibbonPanel.CommandControls;

                CommandControl bomGeneratorButtonControl;
                bomGeneratorButtonControl = organikAssemblyRibbonPanelCtrls.AddButton(m_BomGeneratorButton.ButtonDefinition);

                CommandControl WPFFormButtonControl;
                WPFFormButtonControl = organikAssemblyRibbonPanelCtrls.AddButton(m_LoadPropButton.ButtonDefinition);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }



        #endregion
    }
}
0 Likes
Message 3 of 16

yan.gauthier
Advocate
Advocate

This is what I did in C# to create a new ribbon in the assembly. hope it helps

0 Likes
Message 4 of 16

ADAAAM
Participant
Participant

Hi!

Thank you for your help!

I pasted your code in VS. Unfortunately, it doesn't work my project. I set target framework: .NET Framework 4 and I added references (windows.form and drawing). Can you help me? I attached a screenshot.

 

screen.PNG

0 Likes
Message 5 of 16

yan.gauthier
Advocate
Advocate

Hi,

 

Sorry it's part of the code I forgot to clean before pasting it for you. I am using a button base class for my button (based of the simple addin code example).

 

Add these three files to your project.

 

Button.cs is simply a base class. You don't need to edit it.

Rename the .ico to be the same as the string calling it in the addin server main file. (set its Build Action property to embedded ressource)

DimGenButton.cs is an example of a button class. It contains the button constructor and its event handler. From there you could call you other funtions or forms. Edit the content of the onExecute event to perform the action you need)

 

You can use my calls of my buttons as an example but will need to remove/rename to match project

 

Here is a cleaned up version of the code !

 

using System;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Windows.Forms;
using Inventor;
using Microsoft.Win32;
using System.Linq;

namespace GenikAddin
{
    /// <summary>
    /// This is the primary AddIn Server class that implements the ApplicationAddInServer interface
    /// that all Inventor AddIns are required to implement. The communication between Inventor and
    /// the AddIn is via the methods on this interface.
    /// </summary>
    
    public class AddInParameters
    {
        //public static string addInCLSIDString;
    }

    
    [GuidAttribute("7AD36B50-4F53-4314-84E7-5F47BEF796C8")]
    public class AddInServer : Inventor.ApplicationAddInServer
    {
        #region Data Members
        
        //Inventor application object
        private Inventor.Application m_inventorApplication;

        //Define your button here. you will first need to create a new class button the button as base clase (e.g.: internal class DimGenButton : Button)

        //private DimGenButton m_DimGenButton;
        //private BomGeneratorButton m_BomGeneratorButton;
        //private DrawingToolsButton m_DrawingToolsButton;
        //private LoadProp.LoadPropButton m_LoadPropButton;


        //user interface event
        private UserInterfaceEvents m_userInterfaceEvents;

        // ribbon panel
        RibbonPanel m_drawingAnnotateDimGenRibbonPanel;
        RibbonPanel m_AssemblyOrganikRibbonPanel;

        //event handler delegates
        

        private Inventor.UserInterfaceEventsSink_OnResetCommandBarsEventHandler UserInterfaceEventsSink_OnResetCommandBarsEventDelegate;
        private Inventor.UserInterfaceEventsSink_OnResetEnvironmentsEventHandler UserInterfaceEventsSink_OnResetEnvironmentsEventDelegate;
        private Inventor.UserInterfaceEventsSink_OnResetRibbonInterfaceEventHandler UserInterfaceEventsSink_OnResetRibbonInterfaceEventDelegate;

        public static string addInCLSIDString;
        #endregion

        public AddInServer()
        {
        }

        #region ApplicationAddInServer Members

        public void Activate(Inventor.ApplicationAddInSite addInSiteObject, bool firstTime)
        {
            try
            {
                //the Activate 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

                //initialize AddIn members
                m_inventorApplication = addInSiteObject.Application;
                Button.InventorApplication = m_inventorApplication;

                //initialize event delegates
                m_userInterfaceEvents = m_inventorApplication.UserInterfaceManager.UserInterfaceEvents;

                UserInterfaceEventsSink_OnResetCommandBarsEventDelegate = new UserInterfaceEventsSink_OnResetCommandBarsEventHandler(UserInterfaceEvents_OnResetCommandBars);
                m_userInterfaceEvents.OnResetCommandBars += UserInterfaceEventsSink_OnResetCommandBarsEventDelegate;

                UserInterfaceEventsSink_OnResetEnvironmentsEventDelegate = new UserInterfaceEventsSink_OnResetEnvironmentsEventHandler(UserInterfaceEvents_OnResetEnvironments);
                m_userInterfaceEvents.OnResetEnvironments += UserInterfaceEventsSink_OnResetEnvironmentsEventDelegate;

                UserInterfaceEventsSink_OnResetRibbonInterfaceEventDelegate = new UserInterfaceEventsSink_OnResetRibbonInterfaceEventHandler(UserInterfaceEvents_OnResetRibbonInterface);
                m_userInterfaceEvents.OnResetRibbonInterface += UserInterfaceEventsSink_OnResetRibbonInterfaceEventDelegate;
                
                //load image icons for UI items
                // Remember to use .ico files en change their build action property to embedded ressource
                //Icon BomGeneratorIcon = new Icon (new Icon(this.GetType(), "Organik.ico"), new Size(16,16));
                //Icon LoadPropIcon = new Icon(this.GetType(), "LoadProp1.ico");
    
                //retrieve the GUID for this class
                GuidAttribute addInCLSID; 
                addInCLSID = (GuidAttribute)GuidAttribute.GetCustomAttribute(typeof(AddInServer), typeof(GuidAttribute));
                
                addInCLSIDString = "{" + addInCLSID.Value + "}";

                //Here you assign your button and give their icon
                //m_BomGeneratorButton = new BomGeneratorButton("Bom Generator", "Autodesk:Organik:BomGenCmdBtn", CommandTypesEnum.kShapeEditCmdType, addInCLSIDString, "BomQuery", "Bom Generator", BomGeneratorIcon, BomGeneratorIcon, ButtonDisplayEnum.kDisplayTextInLearningMode);
                //m_LoadPropButton = new LoadProp.LoadPropButton("LoadProp", "Autodesk:Organik:LoadPropBtn", CommandTypesEnum.kShapeEditCmdType, addInCLSIDString, "LoadProp", "LoadProp Form", LoadPropIcon, LoadPropIcon, ButtonDisplayEnum.kDisplayTextInLearningMode);




                //create the command category
                // Command category act like a group for your buttons
                CommandCategory bomCategory = m_inventorApplication.CommandManager.CommandCategories.Add("Bom", "Autodesk:Organik:Bom", addInCLSIDString);

                // Add your button to the category
                //bomCategory.Add(m_BomGeneratorButton.ButtonDefinition);
                //bomCategory.Add(m_LoadPropButton.ButtonDefinition);
                
                if (firstTime == true)
                {
                    //access user interface manager
                    UserInterfaceManager userInterfaceManager;
                    userInterfaceManager = m_inventorApplication.UserInterfaceManager;

                    InterfaceStyleEnum interfaceStyle;
                    interfaceStyle = userInterfaceManager.InterfaceStyle;

                    //create the UI for classic interface
                    if (interfaceStyle == InterfaceStyleEnum.kClassicInterface)
                    {
                        // Also add your button to a command bar.
                        
                        CommandBar bomCommandBar;
                        bomCommandBar = userInterfaceManager.CommandBars.Add("BOM", "Autodesk:Organik:BomGenToolBar", CommandBarTypeEnum.kRegularCommandBar, addInCLSIDString);


                        //bomCommandBar.Controls.AddButton(m_BomGeneratorButton.ButtonDefinition, 0);
                        //bomCommandBar.Controls.AddButton(m_LoadPropButton.ButtonDefinition, 0);
     

                        //Get the drawing environment base object
                        // This expose the drawing environnement in order to add tab to the drawing context ribbon
                        Inventor.Environment drawingEnvironment;
                        drawingEnvironment = userInterfaceManager.Environments["DLxDrawingEnvironment"];

                        //make this command bar accessible in the panel menu for the drawing environment.
                        //Add a defined command bar to your drawing environment
                        //drawingEnvironment.PanelBar.CommandBarList.Add(dimGenCommandBar);    

                        //Get Assembly environment base object
                        Inventor.Environment assemblyEnvironment;
                        assemblyEnvironment = userInterfaceManager.Environments["AMxAssemblyEnvironment"];

                        //make this command bar accessible in the panel menu for the drawing environment.
                        
                        assemblyEnvironment.PanelBar.CommandBarList.Add(bomCommandBar);
                    }
                    //create the UI for ribbon interface
                    else
                    {
                        // New environement contains ribbons and tab !

                        //get the ribbon associated with Drawing document
                        Inventor.Ribbons ribbons;
                        ribbons = userInterfaceManager.Ribbons;

                        //get the ribbon Associated with Assembly document
                        Inventor.Ribbon assemblyRibbon;
                        assemblyRibbon = ribbons["Assembly"];

                        //get tabs associated with assembly ribbon
                        RibbonTabs assemblyRibbonTabs;
                        assemblyRibbonTabs = assemblyRibbon.RibbonTabs;

                        // BOOM ! new tab in the assembly environment !
                        RibbonTab assemblyRibbonTab;
                        assemblyRibbonTab = assemblyRibbonTabs.Add("Organik", "Autodesk:Organik:Bom", addInCLSIDString);

                        //Create new panel with the tab
                        RibbonPanels assemblyRibbonPannels;
                        assemblyRibbonPannels = assemblyRibbonTab.RibbonPanels;

                        m_AssemblyOrganikRibbonPanel = assemblyRibbonPannels.Add("Organik", "Autodesk:Organik:AssemblyRibbonPanel", Guid.NewGuid().ToString());

                        //add controls to the Organik Assembly Pannel
                        CommandControls organikAssemblyRibbonPanelCtrls;
                        organikAssemblyRibbonPanelCtrls = m_AssemblyOrganikRibbonPanel.CommandControls;

                        //Add your buttons there too !
                        //CommandControl bomGeneratorButtonControl;
                        //bomGeneratorButtonControl = organikAssemblyRibbonPanelCtrls.AddButton(m_BomGeneratorButton.ButtonDefinition);

                        //CommandControl WPFFormButtonControl;
                        //WPFFormButtonControl = organikAssemblyRibbonPanelCtrls.AddButton(m_LoadPropButton.ButtonDefinition);


                    }
                }

            }
            catch(Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }

        public void Deactivate()
        {
            //the Deactivate 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
        
            try
            {
                m_userInterfaceEvents.OnResetCommandBars -= UserInterfaceEventsSink_OnResetCommandBarsEventDelegate;
                m_userInterfaceEvents.OnResetEnvironments -= UserInterfaceEventsSink_OnResetEnvironmentsEventDelegate;

                UserInterfaceEventsSink_OnResetCommandBarsEventDelegate = null;
                UserInterfaceEventsSink_OnResetEnvironmentsEventDelegate = null;
                m_userInterfaceEvents = null;
                
                if (m_AssemblyOrganikRibbonPanel != null)
                {
                    m_AssemblyOrganikRibbonPanel.Delete();
                }

                //release inventor Application object
                    Marshal.ReleaseComObject(m_inventorApplication);
                m_inventorApplication = null;

                GC.WaitForPendingFinalizers();
                GC.Collect();
            }
            catch(Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }

        public void ExecuteCommand(int CommandID)
        {
            //this method was used to notify when an AddIn command was executed
            //the CommandID parameter identifies the command that was executed
    
            //Note:this method is now obsolete, you should use the new
            //ControlDefinition objects to implement commands, they have
            //their own event sinks to notify when the command is executed
        }

        public object Automation
        {
            //if you want to return an interface to another client of this addin,
            //implement that interface in a class and return that class object 
            //through this property

            get
            {
                return null;
            }
        }

        private void UserInterfaceEvents_OnResetCommandBars(ObjectsEnumerator commandBars, NameValueMap context)
        {
            try
            {
                CommandBar commandBar;
                for (int commandBarCt = 1; commandBarCt <= commandBars.Count; commandBarCt++)
                {
                    commandBar = (Inventor.CommandBar)commandBars[commandBarCt];
                    
                    if (commandBar.InternalName == "Autodesk:Organik:BomGenToolBar")
                    {

                        //add buttons to toolbar
                        //commandBar.Controls.AddButton(m_BomGeneratorButton.ButtonDefinition, 0);
                        //commandBar.Controls.AddButton(m_LoadPropButton.ButtonDefinition, 0);
                        return;
                    }
                }               
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }

        private void UserInterfaceEvents_OnResetEnvironments(ObjectsEnumerator environments, NameValueMap context)
        {
            try
            {                
                Inventor.Environment environment;
                for (int environmentCt = 1; environmentCt <= environments.Count; environmentCt++)
                {

                    if (environment.InternalName == "AMxAssemblyEnvironment")
                    {
                        //make this command bar accessible in the panel menu for the 3d Assembly environment.
                        environment.PanelBar.CommandBarList.Add(m_inventorApplication.UserInterfaceManager.CommandBars["Autodesk:Organik:BomGenToolBar"]);

                        return;
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }

        private void UserInterfaceEvents_OnResetRibbonInterface(NameValueMap context)
        {
            try
            {

                UserInterfaceManager userInterfaceManager;
                userInterfaceManager = m_inventorApplication.UserInterfaceManager;

                //get the ribbon associated with Drawing document
                Inventor.Ribbons ribbons;
                ribbons = userInterfaceManager.Ribbons;

                //get the ribbon Associated with Assembly document
                Inventor.Ribbon assemblyRibbon;
                assemblyRibbon = ribbons["Assembly"];

                //get tabs associated with assembly ribbon
                RibbonTabs assemblyRibbonTabs;
                assemblyRibbonTabs = assemblyRibbon.RibbonTabs;

                RibbonTab assemblyRibbonTab;
                assemblyRibbonTab = assemblyRibbonTabs.Add("Organik", "Autodesk:Organik:Bom", Guid.NewGuid().ToString());

                //Create new panel with the tab
                RibbonPanels assemblyRibbonPannels;
                assemblyRibbonPannels = assemblyRibbonTab.RibbonPanels;

                m_AssemblyOrganikRibbonPanel = assemblyRibbonPannels.Add("Organik", "Autodesk:Organik:AssemblyRibbonPanel", Guid.NewGuid().ToString());

                //add controls to the Organik Assembly Pannel
                CommandControls organikAssemblyRibbonPanelCtrls;
                organikAssemblyRibbonPanelCtrls = m_AssemblyOrganikRibbonPanel.CommandControls;

                //Same thing here be we handle the reset
                //CommandControl bomGeneratorButtonControl;
                //bomGeneratorButtonControl = organikAssemblyRibbonPanelCtrls.AddButton(m_BomGeneratorButton.ButtonDefinition);

                //CommandControl WPFFormButtonControl;
                //WPFFormButtonControl = organikAssemblyRibbonPanelCtrls.AddButton(m_LoadPropButton.ButtonDefinition);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }



        #endregion
    }
}

 I added some comments and put other part of the code in comments to avoid problems on your side

0 Likes
Message 6 of 16

ADAAAM
Participant
Participant

Hi!

Thank you for your help!

I added your 3 files to my project. I set the Build action to embredded resource in the StandardAddinServer.cs. I added your picture to resources, but I didn't rename. Maybe, that's the problem. Unfortunately still indicates errors. Errors: 'stdole' and 'AutoDwg'

Can you help me again? Sorry, because of many questions. 

I attached 2 screenshot.

  

screenshot resources.PNG

screenshot errors.PNG

 

 

0 Likes
Message 7 of 16

yan.gauthier
Advocate
Advocate

Hi,

 

You are right about the .ico file. Simply rename it to match what is called in the addinserver file. Also, I meant to make the .ico file embedded to your solution.

embedded ressource.png

 

the stdole is a modification i did to the sample from autodesk as it was using depreciated code refering to vb6.. To fix it, add reference to stdole.dll (right click on reference --> add reference --> search for stdole)

 

stdole.png

Lastly, remove all the code in the DimGenButton.cs that is under the function: override protected void ButtonDefinition_OnExecute

 

It refers to my code which i set with the namespace AutoDwg

 

Regards,

0 Likes
Message 8 of 16

ADAAAM
Participant
Participant

Hi!

Thank you!

The good news is run over your code. VS created the dll file, but nothing happen in Inventor when I open an assembly.

First time I got a popup message about the new addin. Then I changed from "Block" to "Loaded/Unloaded" and "Load Automatically".  Then Inventor change back Unloaded status. (pictures)

screen.PNG

  

screen1.PNG

Maybe that's the problem, but I don't know. Have you idea, what is the problem? I attached my project file.

 

Really thank you, I appreciate your help!

 

 

0 Likes
Message 9 of 16

Martin-Winkler-Consulting
Advisor
Advisor

@ADAAAM 

if the addin does not load an error is included. Unfortunately you do not get an error message and it can be very difficult to find this error. You write that you are using Inventor 2020. Then you have to set the .Net Framework to 4.8 in any case. Maybe that's the mistake.

Martin Winkler
CAD Developer
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.


EESignature

0 Likes
Message 10 of 16

ADAAAM
Participant
Participant

Hi!

Thank you for reply!

Yeah It very difficult to find the errors.

I downloaded .NET Framework 4.8. Unfortunately, it didn't help. 

0 Likes
Message 11 of 16

JelteDeJong
Mentor
Mentor

Can u share your complete visual studio project? (on github or by zip) then i could test it onmy computer.

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.

EESignature


Blog: hjalte.nl - github.com

0 Likes
Message 12 of 16

ADAAAM
Participant
Participant

Hi!

I shared it in my previous post.

MyProject.zip

0 Likes
Message 13 of 16

yan.gauthier
Advocate
Advocate

Hi,

I finally found your issue. Your standardAddinServer.cs file was set as embedded ressource instead of compile. I did a lot of modification to your source code while looking for the issue. Also, you did not modify what I had commented so nothing would have happen anyway.

 

This solution works and also support the possibility to run in debug mode (you need to run VS as admin). Simply paste the .addin file to C:\programdata\autodesk\inventor2020\addins. Dll is set to be built directly at this location.

 

cheers,

 

 

0 Likes
Message 14 of 16

ADAAAM
Participant
Participant

Hi!

Thank you for your reply. Really sorry for inconvenience.

 

I am really new in VS. I just wrote some VB macros for Inventor and that's all. I have never seen VS before.

 

Unfortunately, I can't run your project, because the metadata file is missing. I think something wrong with the file path.

 

ADAAAM_0-1589223725182.png

 

 

I tried to solve it, but I failed. I deleted the Autodesk.Inventor.Interop from references, then I browsed again the file but didn't help.

Sorry again!

0 Likes
Message 15 of 16

genikinc1
Explorer
Explorer

Hi,

 

 

0 Likes
Message 16 of 16

yan.gauthier
Advocate
Advocate

Hi,

 

It's strange. Since you have inventor 2020, the dll should be there. Try and remove the reference for Autodesk.Inventor.Interop.dll and browse for the dll and then try to add inventor assembly reference using the add reference dialog.

 

I am not using it since I have both Inventor 2020 and 2016 installed and the mapping was not working properly. It should work for you.

 

Cheers,

0 Likes