在基于AutoCAD做二次开发时,常见的教程讲的都是在DLL中定义一些自定义命令,然后通过netload命令加载这个DLL,通过执行自定义命令的方式来执行我们的自定义功能。这样的方式在在学习中是显得很简单,但用在正式产品中就显得太业余了,没有专业精神。当然更professional的当然是和AutoCAD一样,提供一些基于Ribbon的用户界面来调用我们的自定义功能才好。多啰嗦一句,这个方法同样适用基于AutoCAD的其他产品,比如Map 3D和Civil 3D。下面就讲讲如何实现创建Ribbon用户界面。

首先了解一下Ribbon的概念,下图是AutoCAD的一个Ribbon界面截图,它有一些Tab组成,比如Home,Insert等等;在一个Tab里面又有不同的Panel组成,即竖线分割的部分;panel里面就是button了,这些button来执行具体的功能。不是很准确,我理解大概就是这样回事了。:)

AutoCAD中程序创建Ribbon界面执行AutoCAD命令-风君雪科技博客

下面来通过程序创建一个tab,在这个tab里面加两个panel,在panel里面放两个button来执行自定义的AutoCAD命令。

新建一个class类库工程,需要添加的引用包括:

acmgd

acdbmgd

acCoreMgd(对于AutoCAD 2013)

acCui

AcWindows

AdWindows

下面是代码片段:

    
    private const string MY_TAB_ID = "MY_TAB_ID";

    [CommandMethod("addMyRibbon")]
    public void createRibbon()
    {
      Autodesk.Windows.RibbonControl ribCntrl =
                Autodesk.AutoCAD.Ribbon.RibbonServices.RibbonPaletteSet.RibbonControl;
      //can also be Autodesk.Windows.ComponentManager.Ribbon;     

      //add the tab
      RibbonTab ribTab = new RibbonTab();
      ribTab.Title = "My custom tab";
      ribTab.Id = MY_TAB_ID;
      ribCntrl.Tabs.Add(ribTab);

      //create and add both panels
      addPanel1(ribTab);
      addPanel2(ribTab);

      //set as active tab
      ribTab.IsActive = true;
    }

    private void addPanel2(RibbonTab ribTab)
    {
      //throw new NotImplementedException();
    }

    private void addPanel1(RibbonTab ribTab)
    {
      //throw new NotImplementedException();
    }

.csharpcode, .csharpcode pre { font-size: small; color: rgba(0, 0, 0, 1); font-family: consolas, “Courier New”, courier, monospace; background-color: rgba(255, 255, 255, 1) }
.csharpcode pre { margin: 0 }
.csharpcode .rem { color: rgba(0, 128, 0, 1) }
.csharpcode .kwrd { color: rgba(0, 0, 255, 1) }
.csharpcode .str { color: rgba(0, 96, 128, 1) }
.csharpcode .op { color: rgba(0, 0, 192, 1) }
.csharpcode .preproc { color: rgba(204, 102, 51, 1) }
.csharpcode .asp { background-color: rgba(255, 255, 0, 1) }
.csharpcode .html { color: rgba(128, 0, 0, 1) }
.csharpcode .attr { color: rgba(255, 0, 0, 1) }
.csharpcode .alt { background-color: rgba(244, 244, 244, 1); 100%; margin: 0 }
.csharpcode .lnum { color: rgba(96, 96, 96, 1) }

通过这些代码,应该已经可以创建一个空白的Tab,并把他设置当前活动Tab,如图:

AutoCAD中程序创建Ribbon界面执行AutoCAD命令-风君雪科技博客

现在往这个Tab里面添加一个Panel,然后加入一个button来执行我的自定义命令。

    private const string MY_TAB_ID = "MY_TAB_ID";

    [CommandMethod("addMyRibbon")]
    public void createRibbon()
    {
      Autodesk.Windows.RibbonControl ribCntrl =
                Autodesk.AutoCAD.Ribbon.RibbonServices.RibbonPaletteSet.RibbonControl;
      //can also be Autodesk.Windows.ComponentManager.Ribbon;     

      //add the tab
      RibbonTab ribTab = new RibbonTab();
      ribTab.Title = "My custom tab";
      ribTab.Id = MY_TAB_ID;
      ribCntrl.Tabs.Add(ribTab);

      //create and add both panels
      addPanel1(ribTab);
      addPanel2(ribTab);

      //set as active tab
      ribTab.IsActive = true;
    }

    private void addPanel2(RibbonTab ribTab)
    {
      //create the panel source
      RibbonPanelSource ribPanelSource = new RibbonPanelSource();
      ribPanelSource.Title = "Edit Registry";

      //create the panel
      RibbonPanel ribPanel = new RibbonPanel();
      ribPanel.Source = ribPanelSource;
      ribTab.Panels.Add(ribPanel);

      //create button1
      RibbonButton ribButtonDrawCircle = new RibbonButton();
      ribButtonDrawCircle.Text = "My Draw Circle";
      ribButtonDrawCircle.ShowText = true;
      //pay attention to the SPACE after the command name
      ribButtonDrawCircle.CommandParameter = "DrawCircle ";
      ribButtonDrawCircle.CommandHandler = new AdskCommandHandler();

      ribPanelSource.Items.Add(ribButtonDrawCircle);

    }

    private void addPanel1(RibbonTab ribTab)
    {
      //throw new NotImplementedException();
    }

    [CommandMethod("DrawCircle")]
    public void DrawCircle()
    {
      //画个圆,实现在此略去,这不是这篇blog的重点。
    }

.csharpcode, .csharpcode pre { font-size: small; color: rgba(0, 0, 0, 1); font-family: consolas, “Courier New”, courier, monospace; background-color: rgba(255, 255, 255, 1) }
.csharpcode pre { margin: 0 }
.csharpcode .rem { color: rgba(0, 128, 0, 1) }
.csharpcode .kwrd { color: rgba(0, 0, 255, 1) }
.csharpcode .str { color: rgba(0, 96, 128, 1) }
.csharpcode .op { color: rgba(0, 0, 192, 1) }
.csharpcode .preproc { color: rgba(204, 102, 51, 1) }
.csharpcode .asp { background-color: rgba(255, 255, 0, 1) }
.csharpcode .html { color: rgba(128, 0, 0, 1) }
.csharpcode .attr { color: rgba(255, 0, 0, 1) }
.csharpcode .alt { background-color: rgba(244, 244, 244, 1); 100%; margin: 0 }
.csharpcode .lnum { color: rgba(96, 96, 96, 1) }

注意上面的代码中我定义了一个ribButtonDrawCircle,指定他的CommandParameter为我的自定义命令名“DrawCircle”,并且指定他的CommandHandler 是AdskCommandHandler。这里的AdskCommandHandler是一个自定义的类,需要实现System.Windows.Input.ICommand接口。实现的方法就是在Execute时把commandParameter发送到AutoCAD命令行窗口执行。代码如下:

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


namespace AutoCAD_Debugger
{
  class AdskCommandHandler : System.Windows.Input.ICommand
  {
    public bool CanExecute(object parameter)
    {
      return true;
    }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
      //is from Ribbon Button
      RibbonButton ribBtn = parameter as RibbonButton;
      if (ribBtn != null)
      {
        //execute the command 
        Autodesk.AutoCAD.ApplicationServices.Application
          .DocumentManager.MdiActiveDocument
          .SendStringToExecute(
             (string)ribBtn.CommandParameter, true, false, true);
      }
    }
  }
}

.csharpcode, .csharpcode pre { font-size: small; color: rgba(0, 0, 0, 1); font-family: consolas, “Courier New”, courier, monospace; background-color: rgba(255, 255, 255, 1) }
.csharpcode pre { margin: 0 }
.csharpcode .rem { color: rgba(0, 128, 0, 1) }
.csharpcode .kwrd { color: rgba(0, 0, 255, 1) }
.csharpcode .str { color: rgba(0, 96, 128, 1) }
.csharpcode .op { color: rgba(0, 0, 192, 1) }
.csharpcode .preproc { color: rgba(204, 102, 51, 1) }
.csharpcode .asp { background-color: rgba(255, 255, 0, 1) }
.csharpcode .html { color: rgba(128, 0, 0, 1) }
.csharpcode .attr { color: rgba(255, 0, 0, 1) }
.csharpcode .alt { background-color: rgba(244, 244, 244, 1); 100%; margin: 0 }
.csharpcode .lnum { color: rgba(96, 96, 96, 1) }

执行结果如下图。注意如果你在测试是发现你的button只是把命令字符串发送到了AutoCAD命令行但没有执行,必须按回车才能执行,那你多半是忽略了CommandParameter 后面那个空格!

AutoCAD中程序创建Ribbon界面执行AutoCAD命令-风君雪科技博客

好了,基本过程应该就是这样,当然我们还可以为button添加图标,让他更好看一点。下来峻祁连将再介绍如何让AutoCAD自动就自动加载这个Ribbon菜单,这个就更方便了,敬请期待。

作者:峻祁连
邮箱:junqilian@163.com
出处:http://junqilian.cnblogs.com
转载请保留此信息。