C# SolidWorks 二次开发 -各种菜单命令增加方式

今天给大家讲一讲solidworks中各种菜单界面,如下图,大概有13处,也许还不完整哈。

  • 1.CommandManager选项卡
  • 2.下拉选项卡
  • 3.菜单栏
  • 4.下级菜单
  • 5.浮动工具栏
  • 6.快捷方式工具栏
  • 7.FeatureManager工具栏区域
  • 8.MontionManager区域 ModelView?
  • 9.任务窗格
  • 10.前导视图工具栏
  • 11.定制三方工具条
  • 12.PMP界面
  • 13.右键菜单


我们来看一下每个方式的创建方式及核心代码:

1+3+5.

CommandManager选项卡上 + 菜单栏

csharp 复制代码
  				//菜单
                List<int> cmdIndexs = new List<int>();

                //API提示的信息有误
                //第一个参数是菜单里面的名称
                //第三个参数是提示信息
                //第四个参数是工具条上的名称
                var tempCmdIndex1 = cmdGroup.AddCommandItem2("Cmd1", -1, "Cmd Tooltip1", "Cmd-1", 0, $"FunctionProxy({mainItemIds[0]})", $@"EnableFunction({mainItemIds[0]})", mainItemIds[0], menuToolbarOption);
                var tempCmdIndex2 = cmdGroup.AddCommandItem2("Cmd2", -1, "Cmd Tooltip2", "Cmd-2", 1, $"FunctionProxy({mainItemIds[1]})", $@"EnableFunction({mainItemIds[1]})", mainItemIds[1], menuToolbarOption);
                var tempCmdIndex3 = cmdGroup.AddCommandItem2("Cmd3", -1, "Cmd Tooltip3", "Cmd-3", 2, $"FunctionProxy({mainItemIds[2]})", $@"EnableFunction({mainItemIds[2]})", mainItemIds[2], menuToolbarOption);

                

                cmdIndexs.Add(tempCmdIndex1);
                cmdIndexs.Add(tempCmdIndex2);
                cmdIndexs.Add(tempCmdIndex3);

                cmdGroup.HasToolbar = true; //(浮动工具栏)
                cmdGroup.HasMenu = true;//(菜单栏)

                cmdGroup.Activate();
                
 //增加到工具条,是通过每个文档对象来增加的。 比如零件 装配 工程图
                bool bResult;

                foreach (int type in docTypes)
                {
                    CommandTab cmdTab;

                    cmdTab = iCmdMgr.GetCommandTab(type, Title);

                    //如果已经存在,并且id命令有变化,需要移除之后 ,重新增加。
                    if (cmdTab != null & !getDataResult && ignorePrevious)
                    {
                        bool res = iCmdMgr.RemoveCommandTab(TabToRemove: cmdTab);
                        cmdTab = null;
                    }

                    //工具栏为空时,重新增加
                    if (cmdTab == null)
                    {
                        cmdTab = iCmdMgr.AddCommandTab(type, Title);

                        CommandTabBox cmdBox = cmdTab.AddCommandTabBox();

                        List<int> cmdIDs = new List<int>();

                        //工具栏样式,
                        List<int> showTextType = new List<int>();
                                  

                        for (int i = 0; i < cmdIndexs.Count; i++)
                        {
                            cmdIDs.Add(cmdGroup.get_CommandID(i));
                            showTextType.Add((int)swCommandTabButtonTextDisplay_e.swCommandTabButton_TextBelow);
                        }

                        //把下拉式工具栏加到菜单里。
                        cmdIDs.Add(flyGroup1.CmdID);
                        showTextType.Add((int)swCommandTabButtonTextDisplay_e.swCommandTabButton_TextBelow);

                        cmdIDs.Add(flyGroup2.CmdID);
                        showTextType.Add((int)swCommandTabButtonTextDisplay_e.swCommandTabButton_TextBelow);

                        bResult = cmdBox.AddCommands(cmdIDs.ToArray(), showTextType.ToArray());

                        CommandTabBox cmdBox1 = cmdTab.AddCommandTabBox();

                        //这个是加分割线,记得从后往前,因为分割后最前的id集变少了。
                        //cmdTab.AddSeparator(cmdBox1, cmdIDs[0]);
                        
                    }
                }

工具栏上的下拉+快捷方式工具栏

csharp 复制代码
                FlyoutGroup flyGroup1 = iCmdMgr.CreateFlyoutGroup2(flyoutGroupID1, "FlyoutGroup1", "可下拉1", "工具栏说明",
                  cmdGroup.MainIconList, cmdGroup.IconList, $"FlyoutCallback(6000)", "FlyoutEnable");

     
                flyGroup1.FlyoutType = (int)swCommandFlyoutStyle_e.swCommandFlyoutStyle_Simple;

                var addResult = flyGroup1.AddContextMenuFlyout((int)swDocumentTypes_e.swDocPART, (int)swSelectType_e.swSelFACES);


                FlyoutGroup flyGroup2 = iCmdMgr.CreateFlyoutGroup2(flyoutGroupID2, "FlyoutGroup2", "可下拉2", "工具栏说明2",
                    cmdGroup.MainIconList, cmdGroup.IconList, $"FlyoutCallback(7000)", "FlyoutEnable");


                flyGroup2.FlyoutType = (int)swCommandFlyoutStyle_e.swCommandFlyoutStyle_Simple;


		public void FlyoutCallback(int gId)
        {
            if (gId==flyoutGroupID1)
            {
                FlyoutGroup flyGroup1 = iCmdMgr.GetFlyoutGroup(gId);
                flyGroup1.RemoveAllCommandItems();

                flyGroup1.AddCommandItem("AAA", "test", 0, $"FlyoutCommandItem1({gId+1})", $"FlyoutEnableCommandItem1({gId+1})");
                flyGroup1.AddCommandItem("BBB", "test", 0, $"FlyoutCommandItem1({gId+2})", $"FlyoutEnableCommandItem1({gId+2})");
                flyGroup1.AddCommandItem("CCC", "test", 0, $"FlyoutCommandItem1({gId+3})", $"FlyoutEnableCommandItem1({gId+3})");


            }
            if (gId == flyoutGroupID2)
            {
                FlyoutGroup flyGroup2 = iCmdMgr.GetFlyoutGroup(gId);
                flyGroup2.RemoveAllCommandItems();

                flyGroup2.AddCommandItem("XXX", "test", 0, $"FlyoutCommandItem1({gId + 1})", $"FlyoutEnableCommandItem1({gId+1})");
                flyGroup2.AddCommandItem("YYY", "test", 0, $"FlyoutCommandItem1({gId + 2})", $"FlyoutEnableCommandItem1({gId+2})");
          

            }

            if (gId==7000)
            {
                SwApp.SendMsgToUser("id==7000");
            }

        }

4.下级菜单

csharp 复制代码
            var cmdGroupSub = iCmdMgr.CreateCommandGroup2(mainSubCmdGroupID, $"Addin Study\\SubMenu", ToolTip, "", -1, ignorePrevious, ref cmdGroupErr);


                cmdGroupSub.MainIconList = mainIcons;
                cmdGroupSub.IconList = icons;

                cmdGroupSub.AddCommandItem2($@"SubCmd4", -1, "Cmd Tooltip4", "Cmd-4", 2, $"FunctionProxy({mainItemIds[2]})", $@"EnableFunction({mainItemIds[2]})", mainItemIds[2], menuToolbarOption);

                cmdGroupSub.Activate();
csharp 复制代码
    #region 工具菜单栏下面显示新菜单项

                var menuId = iSwApp.AddMenuItem5((int)swDocumentTypes_e.swDocPART, addinCookieID, "子菜单1@新菜单1", 0, "FlyoutCallback(7000)", "FlyoutEnable", "My menu item", icons);
                var menuId2 = iSwApp.AddMenuItem5((int)swDocumentTypes_e.swDocPART, addinCookieID, "子菜单3@子菜单2@新菜单1", 0, "FlyoutCallback(7000)", "FlyoutEnable", "My menu item", icons);
                var menuId3 = iSwApp.AddMenuItem5((int)swDocumentTypes_e.swDocPART, addinCookieID, "子菜单4@Addin Study", 0, "FlyoutCallback(7000)", "FlyoutEnable", "My menu item", icons);



                #endregion

8.ModelView

csharp 复制代码
            IModelDoc2 pDoc;
            pDoc = (IModelDoc2)iSwApp.ActiveDoc;
            IModelViewManager swModelViewMgr;
            swModelViewMgr = pDoc.ModelViewManager;
            ModelView1Control = new UserControl1();
            swModelViewMgr.DisplayWindowFromHandlex64("用户控件1", ModelView1Control.Handle.ToInt64(), false);

9.任务窗格

csharp 复制代码
            ITaskpaneView pTaskPanView;
            pTaskPanView = iSwApp.CreateTaskpaneView2("", "我的窗口");
            TaskPanWinFormControl = new Form1();
            pTaskPanView.DisplayWindowFromHandlex64(TaskPanWinFormControl.Handle.ToInt64());

10.前导视图工具栏 未找到接口


csharp 复制代码
 #region 新上下文菜单



                IFrame frame = (IFrame)SwApp.Frame();

                var imgPath1 = $@"{RegDllPath()}\icons\Pic1 (1).png";
                var imgPath2 = $@"{RegDllPath()}\icons\Pic1 (2).png";
                var imgPath3 = $@"{RegDllPath()}\icons\Pic1 (3).png";
                var imgPath4 = $@"{RegDllPath()}\icons\Pic1 (4).png";
                var imgPath5 = $@"{RegDllPath()}\icons\Pic1 (5).png";
                var imgPath6 = $@"{RegDllPath()}\icons\Pic1 (6).png";


                var resultCode = frame.AddMenuPopupIcon2((int)swDocumentTypes_e.swDocPART, (int)swSelectType_e.swSelNOTHING, "新上下文菜单", addinCookieID, "PopupCallbackFunction", "PopupEnable", "", imgPath1);

                // create and register the third party menu 创建并注册第三方组菜单
                registerID = SwApp.RegisterThirdPartyPopupMenu();


                // add a menu break at the top of the menu  在组菜单最上方增加一个不能点击的菜单
                resultCode = SwApp.AddItemToThirdPartyPopupMenu2(registerID, (int)swDocumentTypes_e.swDocPART, "我的菜单", addinCookieID, "", "", "", "", "", (int)swMenuItemType_e.swMenuItemType_Break);
                // add a couple of items to to the menu   增加菜单内的命令集
                resultCode = SwApp.AddItemToThirdPartyPopupMenu2(registerID, (int)swDocumentTypes_e.swDocPART, "命令测试1", addinCookieID, "FlyoutCallback(7000)", "FlyoutEnable", "", "Test1", imgPath2, (int)swMenuItemType_e.swMenuItemType_Default);
                resultCode = SwApp.AddItemToThirdPartyPopupMenu2(registerID, (int)swDocumentTypes_e.swDocPART, "命令测试2", addinCookieID, "FlyoutCallback(7000)", "FlyoutEnable", "", "Test4", imgPath3, (int)swMenuItemType_e.swMenuItemType_Default);
                // add a separator bar to the menu  增加分割线
                resultCode = SwApp.AddItemToThirdPartyPopupMenu2(registerID, (int)swDocumentTypes_e.swDocPART, "", addinCookieID, "", "", "", "", "", (int)swMenuItemType_e.swMenuItemType_Separator);

                //继续增加个命令
                resultCode = SwApp.AddItemToThirdPartyPopupMenu2(registerID, (int)swDocumentTypes_e.swDocPART, "命令测试3", addinCookieID, "FlyoutCallback(7000)", "FlyoutEnable", "", "Test5", imgPath4, (int)swMenuItemType_e.swMenuItemType_Default);

                // add an icon to the menu bar  给菜单条上面再加图标按钮
                resultCode = SwApp.AddItemToThirdPartyPopupMenu2(registerID, (int)swDocumentTypes_e.swDocPART, "", addinCookieID, "FlyoutCallback(7000)", "FlyoutEnable", "", "NoOp", imgPath5, (int)swMenuItemType_e.swMenuItemType_Default);


                #endregion
  1. PMP界面--请参考插件里的CreatePropertyManagerPage
  2. 对象的右键菜单
csharp 复制代码
  #region 右键菜单

                //右键菜单 (好像没看到图标的定义方式)  --选中草后 右键显示

                var popItem1 = SwApp.AddMenuPopupItem4((int)swDocumentTypes_e.swDocPART, addinCookieID, "ProfileFeature", "右键菜单",
                    "FlyoutCallback(7000)", "FlyoutEnable", "我的右键菜单", "");

                SwApp.AddMenuPopupItem4((int)swDocumentTypes_e.swDocPART, addinCookieID, "ProfileFeature", "子菜单For草图@右键子菜单",
                    "FlyoutCallback(7000)", "FlyoutEnable", "子菜单@右键菜单", "");




                #endregion

上面是我收集的到一些关于solidworks中二次开发里常用的一些增加自己的菜单或者命令的办法,当然也可能不是太准确。希望对大家有帮助。

代码还是保持开源风格,各位家人们自取

https://gitee.com/painezeng/SolidWorksAddinStudy

相关推荐
ok06011 分钟前
JavaScript(JS)单线程影响速度
开发语言·javascript·ecmascript
液态不合群15 分钟前
C# 中比较实用的关键字,基础高频面试题!
开发语言·c#
饼干帅成渣1 小时前
我又又又又又又又又更新了~~~纯手工编写C++画图,有注释~~~~~~
开发语言·c++
菜萝卜子1 小时前
【Go 】Go 语言中的 channel介绍
开发语言·后端·golang
techdashen1 小时前
Rust vs. Go: 在仅使用标准库时的性能测试
开发语言·golang·rust
小吴同学·1 小时前
NET6 WebApi第5讲:中间件(源码理解,俄罗斯套娃怎么来的?);Web 服务器 (Nginx / IIS / Kestrel)、WSL、SSL/TSL
中间件·c#·.net·.netcore·.net core
冷琅玕1 小时前
APL语言的压力测试
开发语言·后端·golang
Front_Yue1 小时前
Unity中MonoBehaviour的生命周期详解
3d·unity·c#
monstercl1 小时前
C# 元组
开发语言·c#
Antonio9152 小时前
【Q&A】策略模式在QT有哪些应用
开发语言·qt·策略模式