第一章节:Revit API基本概念

本章我们只要记录下载我本人学习Revit二次开发过程的API基本概念
1.外部命令:IExternalCommand
2.外部应用:IExternalApplication
3.插件的属性(Transaction、Journaling)
4.Revit的应用类和文档类(Application和Document)
3.Revit插件的注册安装到revit
4.应用实例

一、外部命令:IExternalCommand
外部命令是IEXternalCommand是Revit API提供给开发者的通过外部命令来扩展Revit时必须要实现的外部命令实现的接口。在IExternalCommand接口中必须重写其中的抽象函数:Excute();换句话说,该函数是IExternalCommand接口的入口函数,必须重写。一下是该函数的具体形式

  [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    public class FileterElement : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            //1.获取当视图的文档
            Autodesk.Revit.UI.UIApplication uiapp = commandData.Application.ActiveUIDocument.Application;
            Autodesk.Revit.ApplicationServices.Application app = uiapp.Application;
            Document uidoc = uiapp.ActiveUIDocument.Document;
        ElementClassFilter classfileter = new ElementClassFilter(typeof(FamilyInstance));

        ElementCategoryFilter cateoryfilter = new ElementCategoryFilter(BuiltInCategory.OST_Windows);

        LogicalAndFilter logicand = new LogicalAndFilter(classfileter,cateoryfilter);

        FilteredElementCollector collection = new FilteredElementCollector(uidoc);
        IList<Element> list = collection.WherePasses(cateoryfilter).ToElements();
        string info = "选择的窗子:个数为:"+list.Count;
        foreach(Element ele in list)
        {

            info += "\n\t" + ele.ToString();
        }
        TaskDialog.Show("提示",info);
        return Result.Succeeded;
    }
}

从上面我们看得出外部命令的实现方式是在自己定义的类后面实现该接口IExtrenalCommand接口,之后再重写Excute()方法,该方法返回的是result集合;
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
}
在该方法中有三个参数分别是commandData、message、elements;下面分别介绍这三个函数参数的具体用法和功能
1.输入参数(commandData):IExtrenalCommand对象包含了我们外部命令所需要的Applicaton以及一些视图的引用,在外部命令中,所有的RTevit外部所需要数据都可以从中该参数中获取
例如在上面的代码中,我们可以获得Revit中的Application和Document的对象

//1.获取当视图的文档
            Autodesk.Revit.UI.UIApplication uiapp = commandData.Application.ActiveUIDocument.Application;
            Autodesk.Revit.ApplicationServices.Application app = uiapp.Application;
            Document uidoc = uiapp.ActiveUIDocument.Document;

2.输出参数message的用法和功能
外部命令可以通过这个参数来返回执行过程中的错误信息。这个参数作用于整个外部命令的执行过程,当外部命令的Excute的返回值是Failed或者 canceled的时候,这个错误信息将会被显示在UI上
例如一下例子中我们返回failed的时候,会在Revit界面高亮我们所选择的墙

   [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    public class highlightselection : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {

            message = "请注意墙的高亮";
            FilteredElementCollector collector = new FilteredElementCollector(commandData.Application.ActiveUIDocument.Document);
            IList<Element> list = collector.OfClass(typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_Windows).ToElements();

            foreach (Element e in list)
            {
                elements.Insert(e);

            }
            //高亮选择
            return Result.Failed;
        }
    }

3.输出参数elements
和message中一样,当我们的execute函数的返回值是Failed或者是Canceled的时候,并且message的参数 不为空的时候。错误的信息会显示出来在对话框中,点击上面的显示按钮,element中的元素就会被高亮
以上代码的实际效果:
这里写图片描述

这里写图片描述

——————————————-章节分割线——————————————————

第二节:外部应用IExternalApplication的具体 实现
相对于外部命令而言,外部应用的实现更为直接,我们需要在addin模块中进行注册该外部应用;外部应用随着revit程序的启动而启动,随着revit关闭而退出。因此这个函数的实现在实现IExternalApplication的接口之后,需要重写OnStartup()函数和OnShutDown()这两个函数。下面我们看一下这个IExternalnal接口函数的定义形式:

public  interface IExternalApplication{
    Autodesk.Revit.UI.Result OnShutdown(UIControlledApplication application);
    Autodesk.Revit.UI.Result OnStartUp(UIControlledApplication application);
    }

我们 看到在这两函数中的参数的类型都是UIControlledApplication;该类是比较特殊的一个应用类型。它只在OnShutdown、OnStartUp函数的范围内起作用。UIControlledApplication提供对UI事件的访问定制和注册事件的方法。换句话说就是它不提供类似UIApplication、Aplication类型访问Revit文档的方法。
下面是一个我们的具体外部应用定制UI的实现代码

/// <remarks>
    /// This application's main class. The class must be Public.
    /// </remarks>
    public class CsAddPanel : IExternalApplication
    {


        // Both OnStartup and OnShutdown must be implemented as public method
        public Result OnStartup(UIControlledApplication application)
        {
            // Add a new ribbon panel
            RibbonPanel ribbonPanel = application.CreateRibbonPanel("善水面板实例");

            // Create a push button to trigger a command add it to the ribbon panel.
            //string thisAssemblyPath = Assembly.GetExecutingAssembly().Location;
            string thisAssemblyPath = @"c:\users\administrator\documents\visual studio 2015\Projects\添加到面板\添加到面板\bin\Debug\添加到面板.dll";
            PushButtonData buttonData = new PushButtonData("cmdHelloWorld",
               "Hello 善水", thisAssemblyPath, "Walkthrough.HelloWorld");

            PushButton pushButton = ribbonPanel.AddItem(buttonData) as PushButton;

            // Optionally, other properties may be assigned to the button
            // a) tool-tip
            pushButton.ToolTip = "善水revit插件.";

            // b) large bitmap
            Uri uriImage = new Uri(@"C:\Users\Administrator\Pictures\周星驰\skype.png");
            BitmapImage image = new BitmapImage(uriImage);
            pushButton.LargeImage = image;
         return Result.Succeeded;
      }

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

    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    public class HelloWorld : IExternalCommand
    {
        // The main Execute method (inherited from IExternalCommand) must be public
        public Result Execute(ExternalCommandData revit,
            ref string message, ElementSet elements)
        {
            TaskDialog.Show("Revit", "Hello World,善水");
            return Result.Succeeded;
        }
    }

最后的实现结果就是在revit的附加模块中添加了面板:
这里写图片描述

猜你喜欢

转载自blog.csdn.net/sinat_38321889/article/details/78591304