日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

AutoCAD中程序创建Ribbon界面执行AutoCAD命令

發(fā)布時間:2023/12/13 综合教程 37 生活家
生活随笔 收集整理的這篇文章主要介紹了 AutoCAD中程序创建Ribbon界面执行AutoCAD命令 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

在基于AutoCAD做二次開發(fā)時,常見的教程講的都是在DLL中定義一些自定義命令,然后通過netload命令加載這個DLL,通過執(zhí)行自定義命令的方式來執(zhí)行我們的自定義功能。這樣的方式在在學習中是顯得很簡單,但用在正式產(chǎn)品中就顯得太業(yè)余了,沒有專業(yè)精神。當然更professional的當然是和AutoCAD一樣,提供一些基于Ribbon的用戶界面來調(diào)用我們的自定義功能才好。多啰嗦一句,這個方法同樣適用基于AutoCAD的其他產(chǎn)品,比如Map 3D和Civil 3D。下面就講講如何實現(xiàn)創(chuàng)建Ribbon用戶界面。

首先了解一下Ribbon的概念,下圖是AutoCAD的一個Ribbon界面截圖,它有一些Tab組成,比如Home,Insert等等;在一個Tab里面又有不同的Panel組成,即豎線分割的部分;panel里面就是button了,這些button來執(zhí)行具體的功能。不是很準確,我理解大概就是這樣回事了。:)

下面來通過程序創(chuàng)建一個tab,在這個tab里面加兩個panel,在panel里面放兩個button來執(zhí)行自定義的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) }

通過這些代碼,應該已經(jīng)可以創(chuàng)建一個空白的Tab,并把他設(shè)置當前活動Tab,如圖:

現(xiàn)在往這個Tab里面添加一個Panel,然后加入一個button來執(zhí)行我的自定義命令。

    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()
    {
      //畫個圓,實現(xiàn)在此略去,這不是這篇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是一個自定義的類,需要實現(xiàn)System.Windows.Input.ICommand接口。實現(xiàn)的方法就是在Execute時把commandParameter發(fā)送到AutoCAD命令行窗口執(zhí)行。代碼如下:

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) }

執(zhí)行結(jié)果如下圖。注意如果你在測試是發(fā)現(xiàn)你的button只是把命令字符串發(fā)送到了AutoCAD命令行但沒有執(zhí)行,必須按回車才能執(zhí)行,那你多半是忽略了CommandParameter 后面那個空格!

好了,基本過程應該就是這樣,當然我們還可以為button添加圖標,讓他更好看一點。下來峻祁連將再介紹如何讓AutoCAD自動就自動加載這個Ribbon菜單,這個就更方便了,敬請期待。

作者:峻祁連
郵箱:junqilian@163.com
出處:http://junqilian.cnblogs.com
轉(zhuǎn)載請保留此信息。

總結(jié)

以上是生活随笔為你收集整理的AutoCAD中程序创建Ribbon界面执行AutoCAD命令的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。