1. RevitAPI 基礎(上)


一 重要的基本設置:

1. 類庫:revitAPI.DLL, revitAPIUI.DLL,個人理解前者包括了revit軟件所特有的數據類型及軟件中存在的全部后台數據,而后者是包含了大量與實現UI交互相關的接口,主要有IExternalCommand, IExternalApplication, Seletion選擇功能, 菜單制作與任務對話框的制作

2. IExternalCommand:規定了外部命令擴展功能的接口供類(class)使用,只有一個抽象函數Execute,其有三個參數,commandData(輸入參數,主要是Application, View, JournalData三種類型),message(輸出參數,以string類為主,主要用來返回錯誤信息),elements(錯誤發生后高亮顯示的元素)。

3.IExternalApplication:個人理解這個接口主要用於實現已有解決方案的拼接。這個接口本身定義了OnStartup和OnShutdown兩個函數用於拼接已存在的dll以及定制UI,其不提供直接操作軟件后台數據的方法。

4.IExternalDBApplication:這個口用於處理數據庫級別的事件(即不涉及UI交互的后台數據增刪改查),本質是3的一種特殊形式,其也通過上述兩個方法拼接解決方案,但未提供定制UI的方法。

5.在繼承IExternalCommand接口之前,必須定義事務Transaction的處理方式,[Autodesk.Revit.Attributes.Transaction(TransactionMode.Manual)]。除mannual外還有兩個readonly與Automatic兩種。

二  revit文檔的獲取(commandData, UIApplication, Application, DocumentSet, Documents, Document, ActiveDocument之間的關系)

commandData:revit后台數據的集合及相關的屬性信息   UIApplication:后台數據的集合,只能通過Application方法引用,commandData和兩種document都包含UIApplication。 UIDocument:個人理解其表示可交互的文檔中包含信息的集合,並提供了通過不同UI交互過程提取信息的方法,如seletion交互獲取文檔,只能通過uiapplication進行引用。ActiveDocument:當前的活動文檔,只能通過uidocument進行引用,是UIdocument的一部分。

Document:代表一個獨立的工程文件,不一定是最頂級的。ActiveUIDocument.Document代表引用了當前活動文檔。Documents代表后台中的全部文檔,只能用Application.Documents進行引用。

真的太亂了!還是畫圖吧!其實最亂的就是Document和UIDocument,其主要區別在於Document中不能用seletion方法,因而其主要用過濾器過濾元素,而UIDocument中出現了大量有交互有關的方法。

此外document類中規定了許多與文件創建相關的方法,其application屬性代表的是此document實例位於的應用實例,UIdocument也同理。

 

 三 最后貼一段代碼,主要是IExternalCommand的使用,遍歷了用seletion選擇的元素

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.Attributes;

namespace chapter2
{
    [Autodesk.Revit.Attributes.Transaction(TransactionMode.Manual)]
    public class class1 : IExternalCommand//讓class1繼承這個接口中的方法,即Execute
    {

        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            try
            {
                
                UIApplication uIApplication = commandData.Application;
                UIDocument uIDocument = uIApplication.ActiveUIDocument;
                Document document = uIDocument.Document;
                List<ElementId> selectedElem = new List<ElementId>();
                List<Element> walls = new List<Element>();
                List<Element> beams = new List<Element>();
                List<ElementId> beamid= new List<ElementId>();
                
                int countW = 0;int countBeam = 0;
                foreach ( var id in uIDocument.Selection.GetElementIds())
                {
                      Element element=uIDocument.Document.GetElement(id);
                    if(element is Wall)
                    {
                        selectedElem.Add(id);
                        walls.Add(element);
                        countW++;
                    }
                    if (element is BeamSystem)
                    {
                        beamid.Add(id);
                        beams.Add(element);
                        countBeam++;
                    }
                    
                }
                TaskDialog.Show("hello revit","模型中共有"+countW.ToString()+"個牆"+countBeam.ToString()+"個柱");
                foreach(var wall in walls)
                { TaskDialog.Show("牆的編號是",wall.Name);}

                TaskDialogResult taskDialogResult = TaskDialog.Show(
                    "revit",
                    "TES to continue," + "NO to cancel",
                    TaskDialogCommonButtons.Yes | TaskDialogCommonButtons.No);
                if(TaskDialogResult.Yes==taskDialogResult)
                {
                    return Result.Succeeded;
                }
                else if(TaskDialogResult.No == taskDialogResult)
                {
                    return Result.Cancelled;
                }
                else
                {
                    return Result.Failed;
                }
            }
            catch
            {
                message = "unexpected errors";
                return Result.Failed;
            }
            

            

            //throw new NotImplementedException();
        }
    }


}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM