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

news2025/5/11 8:38:43

今天给大家讲一讲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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2324243.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【RocketMQRocketMQ Dashbord】Springboot整合RocketMQ

【RocketMQ&&RocketMQ Dashbord】Springboot整合RocketMQ 【一】Mac安装RocketMQ和RocketMQ Dashbord【1】安装RocketMQ&#xff08;1&#xff09;下载&#xff08;2&#xff09;修改 JVM 参数&#xff08;3&#xff09;启动测试&#xff08;4&#xff09;关闭测试&…

《白帽子讲 Web 安全》之跨站请求伪造

引言 在数字化时代&#xff0c;网络已深度融入人们生活的方方面面&#xff0c;Web 应用如雨后春笋般蓬勃发展&#xff0c;为人们提供着便捷高效的服务。然而&#xff0c;繁荣的背后却潜藏着诸多安全隐患&#xff0c;跨站请求伪造&#xff08;CSRF&#xff09;便是其中极为隐蔽…

K8S学习之基础五十:k8s中pod时区问题并通过kibana查看日志

k8s中pod默认时区不是中国的&#xff0c;挂载一个时区可以解决 vi pod.yaml apiVersion: v1 kind: Pod metadata:name: counter spec:containers:- name: countimage: 172.16.80.140/busybox/busybox:latestimagePullPolicy: IfNotPresentargs: [/bin/sh,-c,i0;while true;do …

nginx代理前端请求

一&#xff0c;项目配置 我在 ip 为 192.168.31.177 的机器上使用 vue3 开发前端项目&#xff0c;项目中使用 axios 调用后端接口。 这是 axios 的配置&#xff1a; import axios from axios;const request axios.create({baseURL: http://192.168.31.177:8001,// 设置请求…

Android生态大变革,谷歌调整开源政策,核心开发不再公开

“开源”这个词曾经是Android的护城河&#xff0c;如今却成了谷歌的烫手山芋。最近谷歌宣布调整Android的开源政策&#xff0c;核心开发将全面转向私有分支。翻译成人话就是&#xff1a;以后Android的核心更新&#xff0c;不再公开共享了。 这操作不就是开源变节吗&#xff0c;…

银行分布式新核心的部署架构(两地三中心)

银行的核心系统对可用性和性能要求均非常严苛&#xff0c;所以一般都采用两地三中心部署模式。 其中&#xff1a; 同城两个主数据中心各自部署一套热备&#xff0c;平时两个中心同时在线提供服务&#xff0c;进行负载均衡假如其中一个数据中心出现异常&#xff0c;则由另外一个…

MantisBT在Windows10上安装部署详细步骤

MantisBT 是一款基于 Web 的开源缺陷跟踪系统&#xff0c;以下是在 Windows 10 上安装部署 MantisBT 的详细步骤&#xff1a; 1. 安装必要的环境 MantisBT 是一个基于 PHP 的 Web 应用程序&#xff0c;因此需要安装 Web 服务器&#xff08;如 Apache&#xff09;、PHP 和数据…

9.4分漏洞!Next.js Middleware鉴权绕过漏洞安全风险通告

今日&#xff0c;亚信安全CERT监控到安全社区研究人员发布安全通告&#xff0c;Next.js 存在一个授权绕过漏洞&#xff0c;编号为 CVE-2025-29927。攻击者可能通过发送精心构造的 x-middleware-subrequest 请求头绕过中间件安全控制&#xff0c;从而在未授权的情况下访问受保护…

OpenCV图像拼接(5)图像拼接模块的用于创建权重图函数createWeightMap()

操作系统&#xff1a;ubuntu22.04 OpenCV版本&#xff1a;OpenCV4.9 IDE:Visual Studio Code 编程语言&#xff1a;C11 算法描述 cv::detail::createWeightMap 是 OpenCV 库中用于图像拼接模块的一个函数&#xff0c;主要用于创建权重图。这个权重图在图像拼接过程中扮演着重…

CTF类题目复现总结-[MRCTF2020]ezmisc 1

一、题目地址 https://buuoj.cn/challenges#[MRCTF2020]ezmisc二、复现步骤 1、下载附件&#xff0c;得到一张图片&#xff1b; 2、利用010 Editor打开图片&#xff0c;提示CRC值校验错误&#xff0c;flag.png应该是宽和高被修改了&#xff0c;导致flag被隐藏掉&#xff1b;…

linux打包前端vue,后端springboot项目

第一步先对整个项目进行通过maven进行clean在进行compile 第二步直接进行打包package和install都可以 第三部把对应的jar放到服务器上 把jar包放到服务器上某个地址下&#xff0c;然后cd到这个目录下&#xff0c;然后执行命令 nohup java -jar ruoyi-admin.jar > springbo…

Elasticsearch:使用 AI SDK 和 Elastic 构建 AI 代理

作者&#xff1a;来自 Elastic Carly Richmond 你是否经常听到 AI 代理&#xff08;AI agents&#xff09;这个词&#xff0c;但不太确定它们是什么&#xff0c;或者如何在 TypeScript&#xff08;或 JavaScript&#xff09;中构建一个&#xff1f;跟我一起深入了解 AI 代理的概…

Docker 快速入门指南

Docker 快速入门指南 1. Docker 常用指令 Docker 是一个轻量级的容器化平台&#xff0c;可以帮助开发者快速构建、测试和部署应用程序。以下是一些常用的 Docker 命令。 1.1 镜像管理 # 搜索镜像 docker search <image_name># 拉取镜像 docker pull <image_name>…

自顶向下学习K8S--部署Agones

本文在本人博客&#xff0c;原文地址&#xff1a;http://viogami.tech/index.php/blog/346/ 我是gopher&#xff0c;离不开云原生&#xff0c;自然也逃不了理解docker和K8S这俩。今天抽空想玩下agones&#xff0c;进而对K8S有实践性的理解。 学一个新事物从底层理论学肯定是最…

unity中Xcharts图表鼠标悬浮表现异常

鼠标悬浮在面板附近&#xff0c;只显示单独的一个项目 而且无论鼠标如何移动&#xff0c;根本没有效果。 解决方案&#xff1a; 需要在对应的Canvas上绑定主相机才可以 鼠标移动到项目上就有信息展示了

【Java SE】包装类 Byte、Short、Integer、Long、Character、Float、Double、Boolean

参考笔记&#xff1a;java 包装类 万字详解&#xff08;通俗易懂)_java包装类-CSDN博客 目录 1.简介 2.包装类的继承关系图 3.装箱和拆箱 3.1 介绍 3.2 手动拆装箱 3.3. 自动拆装箱 ​4.关于String类型的转化问题 4.1 String类型和基本类型的相互转化 4.1.1 String —…

口腔种植全流程AI导航系统及辅助诊疗与耗材智能化编程分析

一、系统架构与编程框架设计 口腔种植全流程人工智能导航系统的开发是一项高度复杂的多学科融合工程,其核心架构需在医学精准性、工程实时性与临床实用性之间实现平衡。系统设计以模块化分层架构为基础,结合高实时性数据流与多模态协同控制理念,覆盖从数据采集、智能决策到…

Java 集合中ArrayList与LinkedList的性能比较

一、需求&#xff1a; 头部插入‌&#xff1a;向列表头部插入10万个整数。‌随机访问‌&#xff1a;从列表中间位置连续获取1万个元素。‌头部删除‌&#xff1a;从列表头部连续删除10万个元素。 二、 使用ArrayList与LinkedList测试 //常量定义&#xff0c;用于测试操作的次数…

漏洞发现:AWVS 联动 XRAY 图形化工具.(主动+被动 双重扫描)

漏洞发现&#xff1a;AWVS 联动 XRAY 图形化工具. 漏洞发现是网络安全领域的关键环节&#xff0c;指通过技术手段识别计算机系统、网络设备或软件中存在的设计缺陷、配置错误或代码漏洞的过程。这些漏洞可能被攻击者利用&#xff0c;导致数据泄露、服务中断或权限提升等风险。…

Linux ping/telnet/nc命令

在Linux操作系统中&#xff0c;ping命令用于测试网络连接和发送数据包到目的主机。 然而&#xff0c;ping命令默认情况下只能测试IP地址和域名&#xff0c;而无法直接测试端口号。 ping www.baidu.comping 192.168.0.1 测试端口 如果你想测试特定端口是否开放并响应&#xff…