当前位置: 首页 > news >正文

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选项卡上 + 菜单栏

  				//菜单
                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]);
                        
                    }
                }

在这里插入图片描述
工具栏上的下拉+快捷方式工具栏

在这里插入图片描述


                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.下级菜单

在这里插入图片描述

            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();

在这里插入图片描述

    #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

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

9.任务窗格

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

10.前导视图工具栏 未找到接口
11.
在这里插入图片描述
在这里插入图片描述

 #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. 对象的右键菜单
    在这里插入图片描述
  #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

相关文章:

  • 建筑安全员考试:“知识拓展” 关键词驱动的深度备考攻略
  • 物理环境与安全
  • 说说Vue 3.0中Treeshaking特性?举例说明一下?
  • 多线程synchronized——线程“八锁”
  • 数据通信学习笔记之OSPF其他内容1
  • 精益架构设计:深入理解与实践 C# 中的单一职责原则
  • 单播、广播、组播和任播
  • 浔川社团官方联合会维权成功
  • 单一职责原则开闭原则其他开发原则
  • 【HarmonyOS Next之旅】DevEco Studio使用指南(五) -> 添加/删除Module
  • MySql创建分区表并且按月分区
  • 数据库:一文掌握 Elasticsearch 的各种指令(Elasticsearch指令备忘)
  • 用户态内核态切换
  • 什么?获取到了未知的复位原因?
  • SOFABoot-07-版本查看
  • Linux系统管理与编程08:任务驱动综合应用
  • SAP SD学习笔记33 - 预詑品(寄售物料),预詑品引渡(KB),预詑品出库(KE)
  • S32k3XX MCU时钟配置
  • Gone v2 使用 goner/viper/remote链接远程的配置中心,支持etcd、consul、firestore、nats
  • 【Linux 下的 bash 无法正常解析, Windows 的 CRLF 换行符问题导致的】
  • 冰雹造成车损能赔吗?如何理赔?机构答疑
  • 人民日报仲音:大力纠治违规吃喝顽瘴痼疾
  • 来伊份深夜回应“粽子中吃出疑似创可贴”:拿到实物后会查明原因
  • 何立峰:中方坚定支持多边主义和自由贸易,支持世贸组织在全球经济治理中发挥更大作用
  • 菲律宾举行中期选举
  • 浙江省机电集团党委书记、董事长廉俊接受审查调查