欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

AutoCAD .Net 创建Ribbon界面(一)

程序员文章站 2024-03-18 13:00:58
...

从 AutoCAD 2010 版开始,引入了 Ribbon 界面。
那怎样用代码创建 Ribbon 界面呢?
本篇文章讲解如何创建如下图所示的 Ribbon Tab 页:测试Ribbon页。
AutoCAD .Net 创建Ribbon界面(一)
如上图所示:
AutoCAD 的 Ribbon 界面由一系列 Tab 组成:默认、插入、注释……
测试Ribbon页 是我们创建的 Tab 页。
它由两个 Panel 组成:Panel1、Panel2。
Panel 下面就是三个按钮:直线、圆、Test。

除了需要添加 AutoCAD 引用: acmgd、acdbmgd、accoremgd(AutoCAD 2012版本及后续版本)。
还需要添加如下 AutoCAD 引用:
* AcWindows
* AdWindows

using System;
using System.Collections.Generic;
using System.Text;

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Ribbon;
using Autodesk.Windows;

//-----------------------------------------------
private static RibbonTab myRibbonTab = null;

[CommandMethod("MyRibbonTab")]
public static void MyRibbonTab()
{
    // 创建Ribbon Tab页
    if (myRibbonTab == null)
    {
        myRibbonTab = new RibbonTab();
        myRibbonTab.Title = "测试Ribbon页";
        myRibbonTab.Id = "MyRibbonTab";

        //
        RibbonPanel panel1 = new RibbonPanel();
        RibbonPanelSource panel1Src = new RibbonPanelSource();
        panel1Src.Title = "Panel 1";
        panel1.Source = panel1Src;
        myRibbonTab.Panels.Add(panel1);

        RibbonButton rbnBtnLine = NewRibbonBtn("直线");
        panel1Src.Items.Add(rbnBtnLine);
        RibbonButton rbnBtnCircle = NewRibbonBtn("圆");
        panel1Src.Items.Add(rbnBtnCircle);

        //
        RibbonPanel panel2 = new RibbonPanel();
        RibbonPanelSource panel2Src = new RibbonPanelSource();
        panel2Src.Title = "Panel 2";
        panel2.Source = panel2Src;
        myRibbonTab.Panels.Add(panel2);

        RibbonButton rbnBtnTest = NewRibbonBtn("Test");
        panel2Src.Items.Add(rbnBtnTest);
    }

    // 在AutoCAD的Ribbon窗口中显示
    RibbonControl ribCntrl = RibbonServices.RibbonPaletteSet.RibbonControl;
    ribCntrl.Tabs.Add(myRibbonTab);
}

private static RibbonButton NewRibbonBtn(string text)
{
    RibbonButton button = new RibbonButton();
    button.Text = text;
    button.ShowText = true;
    return button;
}

以 AutoCAD 2014 版本为例,以上代码编译时会报错,添加如下引用即可解决。
* WindowsBase
* PresentationCore
* PresentationFramework
* System.Xaml


以上部分,只是实现了 Ribbon 界面,但 Ribbon 按钮还没实现触发执行命令。
首先,我们实现一自定义类 AdskCommandHandler,通过该类关联 Ribbon 按钮和对应的命令。

using Autodesk.AutoCAD.ApplicationServices.Core;
using Autodesk.Windows;
using System;
using System.Threading;
using System.Windows.Input;

//namespace HelloAcad2014
//{
    public class AdskCommonHandler : ICommand
    {
        public event EventHandler CanExecuteChanged;

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public void Execute(object parameter)
        {
            RibbonButton button = parameter as RibbonButton;
            if (button != null)
            {
                Application.DocumentManager.MdiActiveDocument.SendStringToExecute(
                    (string)button.CommandParameter, true, false, true);
            }
        }
    }
//}

然后,更改 MyRibbonTab 代码如下:

using System;
using System.Collections.Generic;
using System.Text;

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Ribbon;
using Autodesk.Windows;

//-----------------------------------------------
private static RibbonTab myRibbonTab = null;

[CommandMethod("MyRibbonTab")]
public static void MyRibbonTab()
{
    // 创建Ribbon Tab页
    if (myRibbonTab == null)
    {
        myRibbonTab = new RibbonTab();
        myRibbonTab.Title = "测试Ribbon页";
        myRibbonTab.Id = "MyRibbonTab";

        //
        RibbonPanel panel1 = new RibbonPanel();
        RibbonPanelSource panel1Src = new RibbonPanelSource();
        panel1Src.Title = "Panel 1";
        panel1.Source = panel1Src;
        myRibbonTab.Panels.Add(panel1);

        RibbonButton rbnBtnLine = NewRibbonBtn("直线", "line ");
        panel1Src.Items.Add(rbnBtnLine);
        RibbonButton rbnBtnCircle = NewRibbonBtn("圆", "circle ");
        panel1Src.Items.Add(rbnBtnCircle);

        //
        RibbonPanel panel2 = new RibbonPanel();
        RibbonPanelSource panel2Src = new RibbonPanelSource();
        panel2Src.Title = "Panel 2";
        panel2.Source = panel2Src;
        myRibbonTab.Panels.Add(panel2);

        RibbonButton rbnBtnTest = NewRibbonBtn("Test", "MyCmdTest ");
        panel2Src.Items.Add(rbnBtnTest);
    }

    // 在AutoCAD的Ribbon窗口中显示
    RibbonControl ribCntrl = RibbonServices.RibbonPaletteSet.RibbonControl;
    bool isShow = false;
    foreach (RibbonTab rbnTab in ribCntrl.Tabs)
    {
        if (rbnTab == myRibbonTab)
        {
            isShow = true;
            break;
        }
    }
    if (!isShow)
    {
        ribCntrl.Tabs.Add(myRibbonTab);
    }
}

[CommandMethod("MyCmdTest")]
public static void MyCmdTest()
{
    Document doc = Application.DocumentManager.MdiActiveDocument;
    doc.Editor.WriteMessage("www.caxdev.com\n");
}

private static RibbonButton NewRibbonBtn(string text, string cmd)
{
    RibbonButton button = new RibbonButton();
    button.Text = text;
    button.ShowText = true;
    button.CommandHandler = new AdskCommonHandler();
    button.CommandParameter = cmd;
    return button;
}

注意:
RibbonButton 的 CommandParameter 一定要在命令字符串最后加空格。

参考文章:
AutoCAD中程序创建Ribbon界面执行AutoCAD命令

相关标签: AutoCAD