AnsweredAssumed Answered

Add-in Callback and Enable Methods do not work.

Question asked by Arvin Nesarhosseini on Feb 22, 2019
Latest reply on Feb 23, 2019 by Arvin Nesarhosseini

I am trying to write my first Solidworks Add-In in C# but have faced an issue and could not find any answer for it. The connection to Solidworks works, I can create a menu/toolbar with a button but I cannot make the ButtonCallBack/MyMenuCallback work. It would be great if you can help me with that.

As you see in the following code,I have tried different methods AddMenuItem4,AddCommandItem2,AddToolbarCommand2 and created RVN1, RVN2 and RVN3 but had no luck with the Callbacks.

 

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using SolidWorks.Interop.sldworks;

using SolidWorks.Interop.swpublished;

using System.Runtime.InteropServices;

using Microsoft.Win32;

 

 

 

 

 

namespace MyFirstAddin

{

    public class Class1 : SwAddin  

    {

         

 

        SldWorks SwApp;

        CommandManager CmdMgr;

        int SessionCookie;

        #region Solidworks connection

        public bool ConnectToSW(object ThisSW, int Cookie)

        {

             

            SwApp = ThisSW as SldWorks;

            SwApp.SetAddinCallbackInfo(0, this, Cookie);

 

 

            CmdMgr = SwApp.GetCommandManager(Cookie);

            AddCommandMgr();

 

 

           SessionCookie = Cookie;

          

 

            int menuID = 0;

            string image = "Should be updated accordingly";

             

 

 

            menuID = SwApp.AddMenuItem4(3, Cookie, "RVN1", 0, "MyMenuCallback", "MyMenyEnableMethod", "Will Add Drawing Notes", image );

            menuID = SwApp.AddToolbar4(Cookie, "RVN2", image, image, 0, 8);

 

 

            bool mbool = true;

            mbool = SwApp.AddToolbarCommand2(Cookie, menuID, 0, "ButtonCallBack", "3", "Adds Notes From SalesApp", "Add Notes From SalesApp");

             

 

 

 

                  public void ButtonCallBack;

          {

                  SwApp.SendMsgToUser("Callback function called.");

              }

 

 

                 public void MyMenuCallback;

          {

                  SwApp.SendMsgToUser("Callback function called.");

              }

 

            return true;

        }

 

 

         

        public bool DisconnectFromSW()

        {

            

            RemoveCommandMgr();

            GC.Collect();

            SwApp = null;

            return true;

 

 

        }

        #endregion

 

 

        #region COM register and unregister functions

         

        [ComRegisterFunction]

        private static void RegisterAssembly(Type t)

 

 

        {

          // define the path of registry key

          //every class of com assembly is defined by a GUID

          string Path = String.Format(@"SOFTWARE\SolidWorks\AddIns\{0:b}", t);

 

 

            //using microsoft.win32 interface to create registry key

 

 

            RegistryKey Key = Registry.LocalMachine.CreateSubKey(Path);

 

 

            //startup int

            Key.SetValue(null, 1);

            Key.SetValue("Title", "RVN-AddIN");

            Key.SetValue("Description", "This is a Description");

        }

 

 

        [ComUnregisterFunction]

        private static void UnRegisterAssembly(Type t)

        {

            string Path = String.Format(@"SOFTWARE\SolidWorks\AddIns\{0:b}", t);

            Registry.LocalMachine.DeleteSubKey(Path);

 

 

        }

        #endregion

 

 

        public void AddCommandMgr()

        {

            ICommandGroup cmdGroup;

 

 

            cmdGroup = CmdMgr.CreateCommandGroup(1, "RVN3", "Click my items!", "My status description", 3);

 

 

            cmdGroup.AddCommandItem2("Create PMP", 0, "Creates a Property Page", "Click me!", 0, "_cbCreatePMP", "", 0, 2);

 

 

            cmdGroup.HasToolbar = true;

            cmdGroup.HasMenu = true;

            cmdGroup.Activate();

        }

 

 

        public void RemoveCommandMgr()

        {

            CmdMgr.RemoveCommandGroup(1);

        }

 

 

        public void _cbCreatePMP()

        {

            SwApp.SendMsgToUser("I will create a PropertyManagerPage item later");

        }

    }

}

Outcomes