CAD二次开发(7)- 实现Ribbon选项卡,面板,功能按钮的添加

news2024/10/6 8:27:51

1. 创建工程

在这里插入图片描述

2. 需要引入的依赖

在这里插入图片描述

在这里插入图片描述
如图,去掉依赖复制到本地
在这里插入图片描述

3. 代码实现

RibbonTool.cs

实现添加Ribbon选项卡,添加面板,以及给面板添加下拉组合按钮。

using Autodesk.Windows;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace _18Ribbon界面
{
    public static partial class RibbonTool
    {
        /// <summary>
        /// 添加Ribbon选项卡
        /// </summary>
        /// <param name="ribbonCtrl">Ribbon控制器</param>
        /// <param name="title">选项卡标题</param>
        /// <param name="ID">选项卡ID</param>
        /// <param name="isActive">是否置为当前</param>
        /// <returns>RibbonTab</returns>
        public static RibbonTab AddTab(this RibbonControl ribbonCtrl, string title, string ID, bool isActive)
        {
            RibbonTab tab = new RibbonTab();
            tab.Title = title;
            tab.Id = ID;
            ribbonCtrl.Tabs.Add(tab);
            tab.IsActive = isActive;
            return tab;
        }
        /// <summary>
        /// 添加面板
        /// </summary>
        /// <param name="tab">Ribbon选项卡</param>
        /// <param name="title">面板标题</param>
        /// <returns>RibbonPanelSource</returns>
        public static RibbonPanelSource AddPanel(this RibbonTab tab, string title)
        {
            RibbonPanelSource panelSource = new RibbonPanelSource();
            panelSource.Title = title;
            RibbonPanel ribbonPanel = new RibbonPanel();
            ribbonPanel.Source = panelSource;
            tab.Panels.Add(ribbonPanel);
            return panelSource;
        }
        /// <summary>
        /// 给面板添加下拉组合按钮
        /// </summary>
        /// <param name="panelSource"></param>
        /// <param name="text"></param>
        /// <param name="size"></param>
        /// <param name="orient"></param>
        /// <returns></returns>
        public static RibbonSplitButton AddSplitButton(this RibbonPanelSource panelSource,string text,RibbonItemSize size,Orientation orient)
        {
            RibbonSplitButton splitBtn = new RibbonSplitButton();
            splitBtn.Text = text;
            splitBtn.ShowText = true;
            splitBtn.Size = size;
            splitBtn.ShowImage = true;
            splitBtn.Orientation = orient;
            panelSource.Items.Add(splitBtn);
            return splitBtn;
        }
    }
}

RibbonCommandHandler.cs

命令执行的拦截器

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.Windows;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _18Ribbon界面
{
    public class RibbonCommandHandler:System.Windows.Input.ICommand
    {
        //定义用于确定此命令是否可以在其当前状态下执行的方法。
        public bool CanExecute(object parameter)
        {
            return true;
        }
        public event EventHandler CanExecuteChanged;
        // 定义在调用此命令时调用的方法。
        public void Execute(object parameter)
        {
            
            if (parameter is RibbonButton)
            {
                RibbonButton btn = (RibbonButton)parameter;
                if (btn.CommandParameter != null)
                {
                    Document doc = Application.DocumentManager.MdiActiveDocument;
                    doc.SendStringToExecute(btn.CommandParameter.ToString(), true, false, false);
                }
            }
        }
    }
}

RibbonButtonEX.cs

按钮的属性设置,鼠标事件设置。

using Autodesk.Windows;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Media.Imaging;

namespace _18Ribbon界面
{
    public class RibbonButtonEX:RibbonButton
    {
        //正常显示的图片
        private string imgFileName = "";
        public string ImgFileName
        {
            get { return imgFileName; }
            set { imgFileName = value; }
        }
        //鼠标进入时的图片
        private string imgHoverFileName = "";
        public string ImgHoverFileName
        {
            get { return imgHoverFileName; }
            set { imgHoverFileName = value; }
        }

        public RibbonButtonEX(string name,RibbonItemSize size,Orientation orient,string cmd)
            : base()
        {
            this.Name = name;//按钮的名称
            this.Text = name;
            this.ShowText = true; //显示文字
            this.MouseEntered += this_MouseEntered;
            this.MouseLeft += this_MouseLeft;
            this.Size = size; //按钮尺寸
            this.Orientation = orient; //按钮排列方式
            this.CommandHandler = new RibbonCommandHandler(); //给按钮关联命令
            this.CommandParameter = cmd + " ";
            this.ShowImage = true; //显示图片
        }
        public void SetImg(string imgFileName)
        {
            Uri uri = new Uri(imgFileName);
            BitmapImage bitmapImge = new BitmapImage(uri);
            this.Image = bitmapImge; //按钮图片
            this.LargeImage = bitmapImge; //按钮大图片
            this.imgFileName = imgFileName;
        }
        /// <summary>
        /// 鼠标离开事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void this_MouseLeft(object sender, EventArgs e)
        {
            if (this.ImgFileName !="")
            {
                RibbonButton btn = (RibbonButton)sender;
                string imgFileName = this.ImgFileName;
                Uri uri = new Uri(imgFileName);
                BitmapImage bitmapImge = new BitmapImage(uri);
                btn.Image = bitmapImge; //按钮图片
                btn.LargeImage = bitmapImge; //按钮大图片
            }
           
        }
        /// <summary>
        /// 鼠标进入事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void this_MouseEntered(object sender, EventArgs e)
        {
            if (this.ImgHoverFileName !="")
            {
                RibbonButton btn = (RibbonButton)sender;
                string imgFileName = this.ImgHoverFileName;
                Uri uri = new Uri(imgFileName);
                BitmapImage bitmapImge = new BitmapImage(uri);
                btn.Image = bitmapImge; //按钮图片
                btn.LargeImage = bitmapImge; //按钮大图片
            }
            
        }
    }
}

CurPath.cs

图片路径地址设置,一般设置为空,我们到时候会获取执行文件的加载路径来赋值。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _18Ribbon界面
{
    public static class CurPath
    {
        public static string curPath = "";
    }
}

RibbonButtonInfos.cs

实现自定义的仿CAD的直线按钮功能,多段线按钮功能。

using Autodesk.Windows;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Media.Imaging;

namespace _18Ribbon界面
{
    public static class RibbonButtonInfos
    {
        //直线按钮
        private static RibbonButtonEX lineBtn;
        public static RibbonButtonEX LineBtn
        {
            get
            {
                lineBtn = new RibbonButtonEX("直线",RibbonItemSize.Large,Orientation.Vertical,"Line");
                lineBtn.SetImg(CurPath.curPath + "Images\\Line.PNG");//设置按钮图片
                //添加提示对象
                RibbonToolTip toolTip = new RibbonToolTip();
                toolTip.Title = "直线";
                toolTip.Content = "创建直线段";
                toolTip.Command = "LINE";
                toolTip.ExpandedContent = "是用LINE命令,可以创建一些列连续的直线段。每条线段都是可以单独进行编辑的直线对象。";
                string imgToolTipFileName = CurPath.curPath + "Images\\LineTooTip.PNG";
                Uri toolTipUri = new Uri(imgToolTipFileName);
                BitmapImage toolTipBitmapImge = new BitmapImage(toolTipUri);
                toolTip.ExpandedImage = toolTipBitmapImge;
                lineBtn.ToolTip = toolTip;
                //鼠标进入时的图片
                lineBtn.ImgHoverFileName = CurPath.curPath + "Images\\LineHover.PNG";
                return lineBtn;
            }
        }
        //多段线按钮
        private static RibbonButtonEX polylineBtn;
        public static RibbonButtonEX PolylineBtn
        {
            get
            {
                polylineBtn = new RibbonButtonEX("多段线", RibbonItemSize.Large, Orientation.Vertical, "Pline");
                polylineBtn.SetImg(CurPath.curPath + "Images\\Polyline.PNG");//设置按钮图片
                //添加提示对象
                RibbonToolTip toolTip = new RibbonToolTip();
                toolTip.Title = "多段线";
                toolTip.Content = "创建二维多段线";
                toolTip.Command = "PLINE";
                toolTip.ExpandedContent = "二维多段线是作为单个平面对象创建的相互连接的线段序列。可以创建直线段、圆弧段或者两者的组合线段。";
                string imgToolTipFileName = CurPath.curPath + "Images\\PolylineToolTip.PNG";
                Uri toolTipUri = new Uri(imgToolTipFileName);
                BitmapImage toolTipBitmapImge = new BitmapImage(toolTipUri);
                toolTip.ExpandedImage = toolTipBitmapImge;
                polylineBtn.ToolTip = toolTip;
                //鼠标进入时的图片
                polylineBtn.ImgHoverFileName = CurPath.curPath + "Images\\PolylineHover.PNG";
                return polylineBtn;
            }
        }
        //圆心半径按钮
        private static RibbonButtonEX circleCRBtn;
        public static RibbonButtonEX CircleCRBtn
        {
            get
            {
                circleCRBtn = new RibbonButtonEX("圆心,半径", RibbonItemSize.Large, Orientation.Horizontal, "Circle");
                circleCRBtn.SetImg(CurPath.curPath + "Images\\CircleCR.PNG");//设置按钮图片
                circleCRBtn.ShowText = false;
                //添加提示对象
                RibbonToolTip toolTip = new RibbonToolTip();
                toolTip.Title = "圆心,半径";
                toolTip.Content = "用圆心和半径创建圆";
                toolTip.Command = "CIRCLE";
                toolTip.ExpandedContent = "用圆心和半径创建圆。\n\n示例:";
                string imgToolTipFileName = CurPath.curPath + "Images\\CircleCDHover.PNG";
                Uri toolTipUri = new Uri(imgToolTipFileName);
                BitmapImage toolTipBitmapImge = new BitmapImage(toolTipUri);
                toolTip.ExpandedImage = toolTipBitmapImge;
                circleCRBtn.ToolTip = toolTip;
                //鼠标进入时的图片
                circleCRBtn.ImgHoverFileName = CurPath.curPath + "Images\\CircleToolTip.PNG";
                return circleCRBtn;
            }
        }
    }
}

测试

using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.Windows;
using System.Linq;
using System.Windows.Interop;
using System.Windows.Media.Imaging;
using System.IO;
using System.Windows.Shapes;
using _18Ribbon界面;
using Path = System.IO.Path;

namespace RibbonTest01
{
    public class Class1
    {
        [CommandMethod("RibbonCmd")]
        public void RibbonCmd()
        {
            RibbonControl ribbonCtrl = ComponentManager.Ribbon; //获取cad的Ribbon界面
            RibbonTab tab = ribbonCtrl.AddTab("选项卡1", "Acad.RibbonId1", true); //给Ribbon界面添加一个选项卡
            CurPath.curPath = Path.GetDirectoryName(this.GetType().Assembly.Location) + "\\"; //获取程序集的加载路径
            RibbonPanelSource panelSource = tab.AddPanel("绘图"); //给选项卡添加面板
            panelSource.Items.Add(RibbonButtonInfos.LineBtn); //添加直线命令按钮
            panelSource.Items.Add(RibbonButtonInfos.PolylineBtn); //添加多段线命令按钮
        }
        
    }
}

结果

启动并自动加载CAD,输入netload,加载自定义执行文件,并输入RibbonCmd命令。

在这里插入图片描述

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

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

相关文章

怎么避免电脑磁盘数据泄露?磁盘数据保护方法介绍

电脑磁盘是电脑存储数据的基础&#xff0c;而为了避免磁盘数据泄露&#xff0c;我们需要保护电脑磁盘。下面我们就来了解一下磁盘数据保护的方法。 磁盘加密 磁盘加密可以通过专业的加密算法来加密保护磁盘数据&#xff0c;避免电脑磁盘数据泄露。在这里小编推荐使用文件夹只读…

使用hw-probe来记录机器硬件@FreeBSD

HW PROBE&#xff08;hw-probe-1.6.5 Probe for hardware, check operability, and find drivers&#xff09;是一款功能强大的硬件探测工具&#xff0c;当前在FreeBSD里是1.6.5版本。 hw-probe能够详尽地检测电脑硬件&#xff0c;包括处理器、内存、硬盘、显卡等各个组件的信息…

SQL语句练习每日5题(二)

题目1——查找学校是北大的学生信息 筛选出所有北京大学的学生进行用户调研&#xff0c;请你从用户信息表中取出满足条件的数据&#xff0c;结果返回设备id和学校。 解法&#xff1a;考察where条件语句 select device_id,university from user_profile where university北京…

聚焦Cayman 环二核苷酸(CDNs)

环二核苷酸CDNs 环二核苷酸&#xff08;cyclic dinucleotides&#xff0c;CDNs&#xff09;是一类天然的环状RNA分子&#xff0c;细菌衍生的CDNs分子包括c-di-GMP、c-di-AMP和3,3-cGAMP&#xff0c;它们介导对恶性、病毒性和细菌性疾病的先天免疫的保护作用&#xff0c;并在自…

11.盛水最多的容器

给定一个长度为 n 的整数数组 height 。有 n 条垂线&#xff0c;第 i 条线的两个端点是 (i, 0) 和 (i, height[i]) 。 找出其中的两条线&#xff0c;使得它们与 x 轴共同构成的容器可以容纳最多的水。 返回容器可以储存的最大水量。 说明&#xff1a;你不能倾斜容器。 示例 1&a…

STM32—按键控制LED(定时器)

目录 1 、 电路构成及原理图 2 、编写实现代码 main.c exit.c 3、代码讲解 4、烧录到开发板调试、验证代码 5、检验效果 此笔记基于朗峰 STM32F103 系列全集成开发板的记录。 1 、 电路构成及原理图 EXTI&#xff08;External interrupt/event controller&#xff…

通用树查找算法

想要一个树形控件来显示数据&#xff0c;却发现Racket的GUI库竟然没有提供这个控件。既然没有&#xff0c;那就自己手搓一个吧。没想到&#xff0c;在做这个控件中竟然有了新发现&#xff01; 树形控件有一个功能是查找树中指定的节点。这就是接下来的故事的起点。 1 找外援 不…

“神经网络之父”和“深度学习鼻祖”Geoffrey Hinton

“神经网络之父”和“深度学习鼻祖”Geoffrey Hinton在神经网络领域数十年如一日的研究&#xff0c;对深度学习的推动和贡献显著。 一、早期贡献与突破 反向传播算法的引入&#xff1a;Hinton是将反向传播&#xff08;Backpropagation&#xff09;算法引入多层神经网络训练的…

SOLIDWORKS参数化设计插件 慧德敏学

SOLIDWORKS软件是法国达索公司的产品&#xff0c;最初是满足欧美一些工程师产品设计需要而开发的&#xff0c;并没有考虑中国的企业实际情况。我们为满足国内客户的需要&#xff0c;对SOLIDWORKS进行了二次开发&#xff0c;借助SolidKits.AutoWorks参数化工具&#xff0c;通过一…

106、python-第四阶段-3-设计模式-单例模式

不是单例类&#xff0c;如下&#xff1a; class StrTools():pass str1StrTools() str2StrTools() print(str1) print(str2) 运用单例&#xff0c;先创建一个test.py class StrTools():pass str1StrTools()然后创建一个hello.py&#xff0c;在这个文件中引用test.py中的对象&a…

多尺度注意力创新

深度之眼17种多尺度注意力创新

蓝牙资讯|2024年Q1全球个人智能音频设备出货量达到9000万台

Canalys 发布了最新研究报告&#xff1a;2024 年第一季度&#xff0c;全球个人智能音频设备市场呈回暖的迹象&#xff0c;同比增长 6%&#xff0c;出货量超 9000 万台。数据显示&#xff0c;本季度的增长主要得益于 TWS 真无线蓝牙耳机和无线头戴式耳机的强劲表现&#xff0c;两…

Thesios: Synthesizing Accurate Counterfactual I/O Traces from I/O Samples——论文泛读

ASPLOS 2024 Paper 论文阅读笔记整理 问题 在设计大规模分布式存储系统时&#xff0c;I/O活动的建模至关重要。具有代表性的/O跟踪&#xff0c;可以对现有硬件、配置和策略进行详细的性能评估。假设跟踪进一步支持分析假设情况&#xff0c;例如部署新的存储硬件、更改配置和修…

EverWeb 强大的零基础Mac网页设计制作软件

搜索Mac软件之家下载EverWeb 强大的零基础Mac网页设计制作软件 EverWeb 4.2是非专业网页设计师的绝佳网页制作工具&#xff0c;无需编码即可创建美观、响应迅速的网站。只需拖放自己的图像、文本和其他任何html元素到网页布局的任何位置。 EverWeb的功能特性&#xff1a; 下…

企业公户验证API在Java、Python、PHP中的使用教程

在金融和商业领域&#xff0c;企业公户验证API是一种用于验证企业对公账户的真实性和合法性的技术解决方案。这种API通常由金融机构或第三方服务提供商提供&#xff0c;旨在帮助企业加快账户认证流程&#xff0c;提高效率&#xff0c;降低审核成本&#xff0c;并确保符合法规要…

手机短信删除怎么恢复?快速找回的3个秘密武器

手机&#xff0c;这个我们每天离不开的小玩意儿&#xff0c;有时候也会让我们头疼不已。比如&#xff0c;你一不小心&#xff0c;或者为了清理点空间&#xff0c;就把那些重要的短信给删了。这些短信可能是你和好友的深夜聊天&#xff0c;或者是重要的工作信息。一旦删除&#…

matplotlib绘制三维曲面图时遇到的问题及解决方法

在使用 Matplotlib 绘制三维曲面图时&#xff0c;可能会遇到一些常见的问题。今天我将全程详细讲解下遇到问题并且找到应对方法的全部过程&#xff0c;希望能帮助大家。 1、问题背景 在使用 matplotlib 绘制三维曲面图时&#xff0c;遇到了一个问题。代码如下&#xff1a; im…

走进 Apache 世界的另一扇大门

引言 作为热爱技术的你&#xff0c;是否也羡慕 Apache PMC 或者 Committer&#xff0c;此篇文章渣渣皮带你迈出如何成为技术大牛的第一步。 当然我现在还是一枚小小的 code contributor&#xff0c;在成为 committer 的路上还在奋力打码中&#xff0c;写这篇文章也是为大家有…

NAT技术

目录 前言一、NAT的基本思想二、NAT的局限性总结 前言 IP地址短缺问题并不是一个只有在将来某个时候可能发生的理论问题。现在&#xff0c;此时此地&#xff0c;这个问题已经发生。对于整个Internet而言&#xff0c;长期的解决方案是迁移到IPV6&#xff0c;它有128位地址。这个…

如何免费使用(白瞟)最新的开源大模型?

下面介绍两个可以免费白瞟开源大模型的网站&#xff0c;一个是国内的ModelScope ,点击链接注册后进入右上方的司南评测即可&#xff0c;界面效果如下&#xff0c;最新开源的Qwen2-72B也可用的噢&#xff01; 另外一个 是LMSYS和UC伯克利分校联合开发的全球大模型测评平台Chatbo…