setting up API in Visual Basic using C#

setting up API in Visual Basic using C#

cstephens58F4Q
Contributor Contributor
1,002 Views
3 Replies
Message 1 of 4

setting up API in Visual Basic using C#

cstephens58F4Q
Contributor
Contributor

I have previously had a multiple successful Add-ins created using VB.net. I was told I need to start writing them in C# so our IT department can better assist. The build is successful, it opens inventor, shows in the Add-in manager, however it won't populate the button in any ribbon. I have all functions called out same as in the VB.net Add-ins just translated to C# language. Below is the full 'StandardAddInServer.cs" file any assistance would be greatly appreciated.

 

using Inventor;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Runtime.InteropServices;

namespace Furnace
{
    /// <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>
    [GuidAttribute("431205c6-c088-422e-82de-9bf8b0801f0c")]
  
    public class StandardAddInServer : Inventor.ApplicationAddInServer
    {

        // Inventor application object.
        private Inventor.Application m_inventorApplication;
        private UserInterfaceEvents m_uiEvents;
        private Inventor.ButtonDefinition buttonDefinition;
        private object AddClientID;

        public StandardAddInServer()
        {
            

        }

        #region ApplicationAddInServer Members

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

            // Initialize AddIn members.
            m_inventorApplication = addInSiteObject.Application;
            m_uiEvents = m_inventorApplication.UserInterfaceManager.UserInterfaceEvents;

            ControlDefinitions controlDefs = m_inventorApplication.CommandManager.ControlDefinitions;
            //IPictureDisp smallimage = 
            
            var commandManager = m_inventorApplication.CommandManager;

            buttonDefinition = commandManager.ControlDefinitions.AddButtonDefinition("Furnace Automation", "Automation", CommandTypesEnum.kQueryOnlyCmdType, AddClientID);



            // Connect to the user-interface events to handle a ribbon reset.

            buttonDefinition.OnExecute += ButtonDefinition_OnExecute;
            buttonDefinition.AutoAddToGUI();
        
            if (firstTime) 
            {
                AddToUserInterface();            
            }

        }

        private void ButtonDefinition_OnExecute(NameValueMap Context)
        {
            throw new NotImplementedException();
        }

        public void Deactivate()
        {
            // 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

            // TODO: Add ApplicationAddInServer.Deactivate implementation

            // Release objects.
            m_inventorApplication = null;
            m_uiEvents = null;
            buttonDefinition = null;
            buttonDefinition.OnExecute -= ButtonDefinition_OnExecute;

            GC.Collect();
            GC.WaitForPendingFinalizers();
        }

        public object Automation
        {
            // 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.

            get
            {
                // TODO: Add ApplicationAddInServer.Automation getter implementation
                return null;
            }
        }

        #endregion

        #region User interface definition

        private void AddToUserInterface()
        {

            Ribbon ZeroLoc = m_inventorApplication.UserInterfaceManager.Ribbons["ZeroDoc"];
            RibbonTab ToolLoc = ZeroLoc.RibbonTabs["id_TabTools"];
            string ClientID = null;
            RibbonPanel FinalLoc = ToolLoc.RibbonPanels.Add("Furnace Automation", "Automation2", ClientID);


            FinalLoc.CommandControls.AddButton(buttonDefinition);
        }

        public void ExecuteCommand(int CommandID)
        {
            throw new NotImplementedException();
        }

        #endregion

    }
}
0 Likes
Accepted solutions (1)
1,003 Views
3 Replies
Replies (3)
Message 2 of 4

Michael.Navara
Advisor
Advisor

Avoid to use ClientId = null. Here is modified version

 

using Inventor;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Runtime.InteropServices;

namespace Furnace
{
    /// <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>
    [GuidAttribute("3d2d5718-2cc0-4563-8275-cb8d02ab983a")]
    public class StandardAddInServer : ApplicationAddInServer
    {
        private /*object*/ const string AddClientID = "{3d2d5718-2cc0-4563-8275-cb8d02ab983a}";
        private ButtonDefinition buttonDefinition;

        // Inventor application object.
        private Application m_inventorApplication;
        private UserInterfaceEvents m_uiEvents;

        public StandardAddInServer()
        {
        }

        #region ApplicationAddInServer Members

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

            // Initialize AddIn members.
            m_inventorApplication = addInSiteObject.Application;
            m_uiEvents = m_inventorApplication.UserInterfaceManager.UserInterfaceEvents;

            var controlDefs = m_inventorApplication.CommandManager.ControlDefinitions;
            //IPictureDisp smallimage = 

            var commandManager = m_inventorApplication.CommandManager;

            buttonDefinition = commandManager.ControlDefinitions.AddButtonDefinition(
                "Furnace Automation", 
                "Automation",
                CommandTypesEnum.kQueryOnlyCmdType, 
                AddClientID);


            // Connect to the user-interface events to handle a ribbon reset.

            buttonDefinition.OnExecute += ButtonDefinition_OnExecute;
            buttonDefinition.AutoAddToGUI();

            if (firstTime) AddToUserInterface();
        }

        private void ButtonDefinition_OnExecute(NameValueMap Context)
        {
            throw new NotImplementedException();

        }

        public void Deactivate()
        {
            // 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

            // TODO: Add ApplicationAddInServer.Deactivate implementation

            // Release objects.
            m_inventorApplication = null;
            m_uiEvents = null;
            buttonDefinition = null;
            buttonDefinition.OnExecute -= ButtonDefinition_OnExecute;

            GC.Collect();
            GC.WaitForPendingFinalizers();
        }

        public object Automation =>
            // 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.
            // TODO: Add ApplicationAddInServer.Automation getter implementation
            null;

        #endregion

        #region User interface definition

        private void AddToUserInterface()
        {
            var ZeroLoc = m_inventorApplication.UserInterfaceManager.Ribbons["ZeroDoc"];
            var ToolLoc = ZeroLoc.RibbonTabs["id_TabTools"];
            string ClientID = AddClientID;
            var FinalLoc = ToolLoc.RibbonPanels.Add("Furnace Automation", "Automation2", ClientID);


            FinalLoc.CommandControls.AddButton(buttonDefinition);
        }

        public void ExecuteCommand(int CommandID)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
Message 3 of 4

cstephens58F4Q
Contributor
Contributor
Accepted solution

I figured out the issue was I was calling an old .dll file so all the changes I had done were not being updated in the active file in Inventor.

 

0 Likes
Message 4 of 4

cstephens58F4Q
Contributor
Contributor

Thanks for the pointer on that item. I am learning C# on the fly currently with this change.

0 Likes