AutoCAD二次开发三种添加插件按钮的方法之二

上一篇相关文章主要借助了cuix配置文件来制作插件按钮,但是对于纯码农来说还是喜欢以代码来说话,今天这篇文章就来讲讲纯代码添加按钮。

开发IDE:VS2010

环境:.Net Framework4.0

AutoCAD版本:2014中/EN

今天介绍的代码主要借助的是AcCui.dll这个动态链接库,因为在我的了解中,CAD的开发库中有很多类似的类,又没有相关的介绍API的文档(你不是专业人员真是心累~),都是自己尝试或者在AutoDesk社区中找到的相关内容。

下面先说说思路,再添加核心代码,如果你有需要,欢迎关注我或者联系我,我很愿意与你共享资源与共同学习进步大笑

1.首先还是要找到相关的主cuix文件,就是AutoCAD二次开发三种添加插件按钮的方法之一中介绍的acad.cuix文件(我觉得添加图片排版太不方便了,所以尽量减少图片~);

                //获取到ACAD.cuix
                CustomizationSection cs = new CustomizationSection((string)Application.GetSystemVariable("MENUNAME"));
                string strCurWorkspace = (string)Application.GetSystemVariable("WSCURRENT");
                //workspace的操作
                Workspace curWorkspace = cs.getWorkspace(strCurWorkspace);
                if (IsExistPluginTab(ed, curWorkspace))//如果自定义工作空间存在指定的Tab
                {
                    //初始化功能按钮
                    InitialRibbonBtn(ed, cs);
                }

2.由于我们是需要将按钮添加到‘插件’这个Tab中,而这个Tab是在Ribbon中的(而AutoCAD又是可以自定义是否显示Ribbon的,而且不同的工作空间显示的Ribbon还不同,所以我这里尽量简化了,不讨论那些了),这个Tab的ElementID叫做RBN_00012112,我们可以根据此判断这个Tab是否存在还是被删掉了(默认安装时存在的);这里只讨论存在的咯。  

3.如果存在,我们直接获取到这个Tab,在其中在其中添加一个Panel按钮;

        /// <summary>
        /// 在acad.cuix中的(选项卡)Tab中添加panel
        /// </summary>
        /// <param name="cs">acad.cuix的引用</param>
        /// <param name="tabName">Tab的中文名字</param>
        /// <param name="tabEnName">Tab的英文版名称</param>
        /// <param name="panelName">新建的Panel名称(自定义)</param>
        /// <returns></returns>
        private  Autodesk.AutoCAD.Customization.RibbonPanelSource AddRibbonPanelToTab(CustomizationSection cs, string tabName,string tabEnName, string panelName)
        {
            RibbonRoot root = cs.MenuGroup.RibbonRoot;
            Autodesk.AutoCAD.Customization.RibbonPanelSourceCollection panels = root.RibbonPanelSources;
            RibbonTabSource rts = root.FindTab("RBN_00012112");//ElementID
            if (rts == null)
            {
                Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("未找到指定的插件Tab");
            }
            if (rts.Name == tabName || rts.Name == tabEnName)
            {
                //创建一个panel并添加到panels集合中
                Autodesk.AutoCAD.Customization.RibbonPanelSource panelSrc = new Autodesk.AutoCAD.Customization.RibbonPanelSource(root);
                panelSrc.Text = panelSrc.Name = panelName;
                panelSrc.ElementID = panelSrc.Id = panelName + "_PanelSourceID";
                panels.Add(panelSrc);

                RibbonPanelSourceReference ribPanelSourceRef = new RibbonPanelSourceReference(rts);
                ribPanelSourceRef.PanelId = panelSrc.ElementID;
                rts.Items.Add(ribPanelSourceRef);

                return panelSrc;
            }
            return null;
        }

4.在这个自定义的Panel直接添加按钮,最后重新加载所有menu。

        private void InitialRibbonBtn(Editor ed, CustomizationSection cs)
        {
            Autodesk.AutoCAD.Customization.RibbonPanelSource panelSrc = AddRibbonPanelToTab(cs, "插件", "Plug-ins","测试Panel");
            MacroGroup macGroup = cs.MenuGroup.MacroGroups[0];
            //int macroCount = cs.MenuGroup.MacroGroups.Count;
            //foreach (MacroGroup macro in cs.MenuGroup.MacroGroups)
            //{
            //    string name = macro.Name;
            //}
            RibbonRow row = new RibbonRow();
            panelSrc.Items.Add((Autodesk.AutoCAD.Customization.RibbonItem)row);

            RibbonCommandButton button1 = new RibbonCommandButton(row);
            button1.Text = "测试LargeBtn1";
            MenuMacro menuMac1 = macGroup.CreateMenuMacro("Button1_Macro", "^C^CButton1_Command ", "Button1_Tag", "Button1_Help",
                                MacroType.Any, "RibbonImages//test16.png", "RibbonImages//test32.png", "Button1_Label_Id");
            button1.MacroID = menuMac1.ElementID;
            button1.ButtonStyle = RibbonButtonStyle.LargeWithText;
            button1.KeyTip = "Button1 Key Tip";
            button1.TooltipTitle = "Button1 Tooltip Title!";
            row.Items.Add((Autodesk.AutoCAD.Customization.RibbonItem)button1);

            RibbonCommandButton button2 = new RibbonCommandButton(row);
            button2.Text = "测试SmallBtn1";
            MenuMacro menuMac2 = macGroup.CreateMenuMacro("Button2_Macro", "^C^CButton2_Command ", "Button2_Tag", "Button2_Help",
                                MacroType.Any, "RibbonImages//test16.png", "RibbonImages//test32.png", "Button2_Label_Id");
            button2.MacroID = menuMac2.ElementID;//ID不能少
            button2.ButtonStyle = RibbonButtonStyle.SmallWithText;
            button2.KeyTip = "Button2 Key Tip";
            button2.TooltipTitle = "Button2 Tooltip Title!";
            row.Items.Add((Autodesk.AutoCAD.Customization.RibbonItem)button2);

            RibbonCommandButton button3 = new RibbonCommandButton(row);
            button3.Text = "测试LargeBtn3";
            MenuMacro menuMac3 = macGroup.CreateMenuMacro("Button3_Macro", "^C^CButton3_Command ", "Button3_Tag", "Button3_Help",
                                MacroType.Any, "RibbonImages//test16.png", "RibbonImages//test32.png", "Button3_Label_Id");
            button3.MacroID = menuMac3.ElementID;
            button3.ButtonStyle = RibbonButtonStyle.LargeWithText;
            button3.KeyTip = "Button3 Key Tip";
            button3.TooltipTitle = "Button3 Tooltip Title!";
            row.Items.Add((Autodesk.AutoCAD.Customization.RibbonItem)button3);

            cs.Save();

            Application.ReloadAllMenus();//重加载所有的按钮
            ed.WriteMessage("Add buttons successed!");
        }

最后,效果和第一篇最后的效果是一样的哦!~

 

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值