第一章節: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的附加模塊中添加了面板:
這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章