C# plugins, adding a button and a command

Top  Previous  Next

I've found that I can use the VS 2017 Inventor Plugin Maker wizard and then simply move all the sources over to VS 2019. I haven't found a VS2019 compatible wizard.

 

Many thanks to Jelte de Jong for getting me going on this subject.

 

I add the "Sample" panel to the "Tools" tab of the "Assembly" ribbon:

 

Add-button

 

Here is the full code of a C# program which adds in a command, copiously commented.

 

// 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

   {

 

       // Inventor application object...

       private Inventor.Application m_inventorApplication;

 

       // The button I will add in...

       private ButtonDefinition m_sampleButton = null;

 

       public StandardAddInServer()

       {

       }

 

       // 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;

           }

       }

   }

}

 

 

 

Text, images and diagrams © 2021 Owen F. Ransen. All rights reserved. (But copy the source code as much as you want!)