Revit 2011 二次开发之Ribbon

Revit 2011之后取消了菜单和工具条,全部使用Ribbon即按钮组,如图



步骤一:
新建类库项目Ribbon
修改命名空间为HY
修改Class1.cs为Ribbon.cs用来控制按钮显示
添加一个类AddInCommand.cs用来响应命令

步骤二:
引入类库
RevitAPI.dll
RevitAPIUI.dll
System.Windows.Forms;
PresentationCore
WindowBase

Ribbon.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;
using System.Windows.Forms;
using System.Windows.Media.Imaging;

using Autodesk.Revit.UI;
using Autodesk.Revit.Attributes;

namespace HY
{
    [Transaction(TransactionMode.Automatic)]
    [Regeneration(RegenerationOption.Manual)]
    [Journaling(JournalingMode.NoCommandData)]
    
public class Ribbon : IExternalApplication
    {
        
// 程序集路径
        static string AddInPath = typeof(Ribbon).Assembly.Location;
        
// 按钮图标目录
        static string ButtonIconsFolder = Path.GetDirectoryName(AddInPath);
        
// uiApplication
        
//static UIApplication uiApplication = null;

        
#region IExternalApplication Members

        
public Autodesk.Revit.UI.Result OnStartup(UIControlledApplication application)
        {
            
try
            {
                CreateRibbonSamplePanel(application);

                
return Autodesk.Revit.UI.Result.Succeeded;
            }
            
catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), 
"Ribbon Sample");

                
return Autodesk.Revit.UI.Result.Failed;
            }
        }

        
public Autodesk.Revit.UI.Result OnShutdown(UIControlledApplication application)
        {
            
return Autodesk.Revit.UI.Result.Succeeded;
        }

        
#endregion

        
private void CreateRibbonSamplePanel(UIControlledApplication application)
        {
            
//RibbonPanel显示一个大按钮,图标为第一个按钮的图标
            string firstPanelName = "Ribbon 实例";//面板底部文字提示
            RibbonPanel ribbonSamplePanel = application.CreateRibbonPanel(firstPanelName);

            
#region 创建墙和结构墙

            
//RibbonPanel(面板)->SplitButton(按钮组)->PushButton(按钮)
            SplitButtonData splitButtonData = new SplitButtonData("NewWallSplit""创建墙");//按钮数据,按钮组显示的文字为第一个按钮的文字
            SplitButton splitButton = ribbonSamplePanel.AddItem(splitButtonData) as SplitButton;//添加到面板

            
//PushButton pushButton = splitButton.AddPushButton(new PushButtonData("WallPush", "普通墙", AddInPath, "Revit.SDK.Samples.Ribbon.CS.CreateWall"));
            
//最后一个参数是执行按钮事件的响应类名
            PushButtonData pushButtonDataWall = new PushButtonData("WallPush""普通墙", AddInPath, "HY.CreateWall");
            PushButton pushButton 
= splitButton.AddPushButton(pushButtonDataWall);
            
//大图标,小图标
            pushButton.LargeImage = new BitmapImage(new Uri(Path.Combine(ButtonIconsFolder, "CreateWall.png"), UriKind.Absolute));
            pushButton.Image 
= new BitmapImage(new Uri(Path.Combine(ButtonIconsFolder, "CreateWall-S.png"), UriKind.Absolute));
            
//提示文字,提示图片
            pushButton.ToolTip = "Creates a partition wall in the building model.";
            pushButton.ToolTipImage 
= new BitmapImage(new Uri(Path.Combine(ButtonIconsFolder, "CreateWallTooltip.bmp"), UriKind.Absolute));

            
//按钮组里第二个按钮
            pushButton = splitButton.AddPushButton(new PushButtonData("StrWallPush""结构墙", AddInPath, "HY.CreateStructureWall"));
            pushButton.LargeImage 
= new BitmapImage(new Uri(Path.Combine(ButtonIconsFolder, "StrcturalWall.png"), UriKind.Absolute));
            pushButton.Image 
= new BitmapImage(new Uri(Path.Combine(ButtonIconsFolder, "StrcturalWall-S.png"), UriKind.Absolute));

            
#endregion

            ribbonSamplePanel.AddSeparator();
        }
    }
}
AddInCommand.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows.Forms;

using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.Attributes;

namespace HY
{
    [Transaction(TransactionMode.Automatic)]
    [Regeneration(RegenerationOption.Manual)]
    [Journaling(JournalingMode.NoCommandData)]
    
public class CreateWall : IExternalCommand
    {
        
public Result Execute(ExternalCommandData revit,ref string message,ElementSet elements)
        {
            MessageBox.Show(
"创建墙");

            
return Autodesk.Revit.UI.Result.Succeeded;
        }
    }

    [Transaction(TransactionMode.Automatic)]
    [Regeneration(RegenerationOption.Manual)]
    
public class CreateStructureWall : CreateWall
    {
    }
}
Ribbon.addin
<?xml version="1.0" encoding="utf-8"?>
<RevitAddIns>
  
<AddIn Type="Application">
    
<Name>External Tool</Name>
    
<Assembly>F:\Ribbon\Ribbon\bin\Debug\Ribbon.dll</Assembly>
    
<ClientId>cb5c768a-dc2f-4289-a803-58ee5afc5708</ClientId>
    
<FullClassName>HY.Ribbon</FullClassName>
  
</AddIn>
</RevitAddIns>
参考上篇:http://www.cnblogs.com/greatverve/archive/2011/03/08/revit-api-begin.html
源码:https://files.cnblogs.com/greatverve/Ribbon.rar
posted @ 2011-03-09 17:14  大气象  阅读(5858)  评论(10编辑  收藏  举报
http://www.tianqiweiqi.com