ExecuteCommand is now obsolete (C#)

ExecuteCommand is now obsolete (C#)

oransen
Collaborator Collaborator
741 Views
3 Replies
Message 1 of 4

ExecuteCommand is now obsolete (C#)

oransen
Collaborator
Collaborator

I've been looking at InventorAddIn1, and at a certain point I read:

 

public void ExecuteCommand(int commandID)
{
    // Note:this method is now obsolete, you should use the 
    // ControlDefinition functionality for implementing commands.
}

 

But for the life of me I can't find any C# examples of how to add a command (and presumably a button) using ControlDefinitions. I've found fragments, but I get lost in the disjointedness of it.

 

So far I've got to this...

 

        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;

            var cmdMgr = m_inventorApplication.CommandManager;
 
            Icon BigIcon = new Icon("Icon1.ico");
            Icon SmallIcon = new Icon("Icon1.ico");

            m_sampleButton  = cmdMgr.ControlDefinitions.AddButtonDefinition("Command 1",
                                                                            "Command 1",
                                                                            CommandTypesEnum.kFilePropertyEditCmdType,
                                                                            Guid.NewGuid().ToString(),
                                                                            "Command 1 description",
                                                                            "Command 1 Tooltip",
                                                                            BigIcon, SmallIcon);

            // TODO: Add ApplicationAddInServer.Activate implementation.
            MessageBox.Show ("Hello from icony");
        }

 

...and presumably there is a way of adding the button to the ribbon and a way of associating the button to a custom command (using the mysterious ControlDefinition method).

 

So,

Q1) how do I add a button to the ribbon?

Q2) how to I associate my command to the button?

 

Help!

 

 

 

 

 

 

 

 

 

0 Likes
Accepted solutions (1)
742 Views
3 Replies
Replies (3)
Message 2 of 4

JelteDeJong
Mentor
Mentor
Accepted solution

Something like this?

public void Activate(Inventor.ApplicationAddInSite addInSiteObject, bool firstTime)
{
    var cmdMgr = m_inventorApplication.CommandManager;

    Icon BigIcon = new Icon("Icon1.ico");
    Icon SmallIcon = new Icon("Icon1.ico");

    ButtonDefinition buttonDef = cmdMgr.ControlDefinitions.AddButtonDefinition(
                            "Command 1",
                            "Command 1",
                            CommandTypesEnum.kFilePropertyEditCmdType,
                            Guid.NewGuid().ToString(),
                            "Command 1 description",
                            "Command 1 Tooltip",
                            BigIcon, SmallIcon);

    buttonDef.OnExecute += ButtonDef_OnExecute;


    Ribbon partRibbon = m_inventorApplication.UserInterfaceManager.Ribbons["Part"]; 
    RibbonTab toolsTab = partRibbon.RibbonTabs["id_TabTools"];
    RibbonPanel customPanel = toolsTab.RibbonPanels.Add("Sample", "MysSample", Guid.NewGuid().ToString());
    customPanel.CommandControls.AddButton(buttonDef);

}

private void ButtonDef_OnExecute(NameValueMap Context)
{
    m_inventorApplication.CommandManager.ControlDefinitions["AppZoomallCmd"].Execute();
}

somethin

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

Message 3 of 4

oransen
Collaborator
Collaborator

Thanks for that, I've got a bit further along, but my plugin is not loaded because of my call to

 

 

            stdole.IPictureDisp okIconStandard = PictureDispConverter.ToIPictureDisp(new System.Drawing.Icon("PluginIcon.ico", new System.Drawing.Size(16, 16)));
            stdole.IPictureDisp okIconLarge = PictureDispConverter.ToIPictureDisp(new System.Drawing.Icon("PluginIcon.ico", new System.Drawing.Size(32, 32)));
#if false 
            m_sampleButton = cmdMgr.ControlDefinitions.AddButtonDefinition("Command 1",
                                                                            "Command 1",
                                                                            CommandTypesEnum.kFilePropertyEditCmdType,
                                                                            Guid.NewGuid().ToString(),
                                                                            "Command 1 description",
                                                                            "Command 1 Tooltip",
                                                                            okIconStandard,okIconLarge);

#endif 

 

AddButtonDefinition. If I don't make that call the plugin gets loaded.

 

We really need a working wizard for all this stuff!

 

 

0 Likes
Message 4 of 4

oransen
Collaborator
Collaborator

@JelteDeJong, again many thanks for your reply, after a bit of juggling I got this to work:

 

// Adding a button and a command into an Inventor ribbon
// 2021-10-18 : Started

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

namespace InventorAddIn1
{
    /// <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("bc7a9e92-f455-431d-9378-c791092de1cc")]
    public class StandardAddInServer : Inventor.ApplicationAddInServer
    {

        #region Data Members

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

        // The button I will add in...
        private ButtonDefinition m_sampleButton = null;

        #endregion

        public StandardAddInServer()
        {
        }

        #region ApplicationAddInServer Members


        // Inventor will call this if it finds a (this) DLL in the appropriate place, for example
        // C:\Users\ofr\AppData\Roaming\Autodesk\ApplicationPlugins\InventorAddIn1\InventorAddIn1.dll
        // We are passed Inventor application which me must hold and cherish...
        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.
            m_inventorApplication = addInSiteObject.Application;

            // I use try-catch because Inventor swallows the exceptions silently and does not
            // warn you or the user that anything has gone wrong. The catch will give you a clue
            // if something does go wrong...
            try
            {
                // This was me checking that m_inventorApplication is initialized properly
                MessageBox.Show("locale=" + m_inventorApplication.Locale.ToString()) ;
                var cmdMgr = m_inventorApplication.CommandManager;

                // Create the button (without icons, icons appear to be tricky)...
                m_sampleButton = cmdMgr.ControlDefinitions.AddButtonDefinition("Command 1", // Display name
                                                                               "Command 1", // Internal name
                                                                                CommandTypesEnum.kQueryOnlyCmdType, // No changes, just looking
                                                                                Guid.NewGuid().ToString(), // Invent an id for this command
                                                                                "Command 1 description", 
                                                                                "Command 1 Tooltip"); // shows when mouse hovers

                m_sampleButton.OnExecute += ButtonDef_OnExecute; // Tell the button what it should do when clicked, see function below

#if false
                // If you want show the name of each ribbon....
                foreach (Ribbon r in m_inventorApplication.UserInterfaceManager.Ribbons)
                {
                    MessageBox.Show(r.InternalName);
                }
#endif 

                // Which of these Ribbons (lines) you choose depends on whether you plug in handles Parts or Assemblies...
                // Ribbon TheRibbon = m_inventorApplication.UserInterfaceManager.Ribbons["Part"]; // command active when a part doc is active is open
                Ribbon TheRibbon = m_inventorApplication.UserInterfaceManager.Ribbons["Assembly"]; // command active when an assembly doc is active

#if false
                // If you want show the name of each tab in the ribbon....
                foreach (RibbonTab t in partRibbon.RibbonTabs)
                {
                    MessageBox.Show(t.InternalName);
                }
#endif 

                RibbonTab toolsTab = TheRibbon.RibbonTabs["id_TabTools"]; // tools is present in both parts and assemblies
                // MessageBox.Show ("tools tab internal name = " + toolsTab.InternalName);
                RibbonPanel customPanel = toolsTab.RibbonPanels.Add("Sample", "MysSample", Guid.NewGuid().ToString());
                // MessageBox.Show("Custom panel internal name = " + customPanel.InternalName);

                customPanel.CommandControls.AddButton(m_sampleButton);
            }
            catch (Exception Err)
            {
                MessageBox.Show("Error " + Err.Message);
            }

            MessageBox.Show ("Plugin loaded");
        }


        // This is the function you added to the button a few lines above
        private void ButtonDef_OnExecute(NameValueMap Context)
        {
            // This is a test of calling a standard Inventor command...
            m_inventorApplication.CommandManager.ControlDefinitions["AppZoomallCmd"].Execute();

            // This is my simple test of something I do...
            MessageBox.Show("My Command Called");
        }

        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;

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

        public void ExecuteCommand(int commandID)
        {
            // Note:this method is now obsolete, you should use the 
            // ControlDefinition functionality for implementing commands.
        }

        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

    }
}
0 Likes