1. 程式人生 > >Revit二次開發-根據視圖階段(Phase)創建房間

Revit二次開發-根據視圖階段(Phase)創建房間

new tran des pan tap yun gen 方法 task

最近開發業務中,有一個自動創建房間的功能,很自然的想到了Document.NewRooms2方法。但是當前功能的特殊之處在於,Revit項目視圖是分階段(Phase)的,不同階段的房間是互相獨立的。

官方API中創建房間提供了三個重載方法:

1         public ICollection<DB.ElementId> NewRooms2(Phase phase, int count);
2         public ICollection<DB.ElementId> NewRooms2(Level level);
3         public
ICollection<DB.ElementId> NewRooms2(Level level, Phase phase)

第二個以前經常用,所以自然先用這個方法創建房間。但是結果不遂人意,因為項目視圖是分階段的,所以用這個方法在創建房間時,在有些視圖是失敗的,功能不穩定,所以這個方法不適用。

第一個方法是帶有Phase參數的,但是個數又不確定,顯然不滿足需要。

第三個方法彌補第一個方法的不足,看上去應該滿足需要,但是結果跟第一個差不多,在有些視圖階段是創建不出來房間的,會提示 階段和當前視圖不匹配的錯誤。這個就讓人很困惑了。

沒辦法,只能求助百度了,剛好找到了網友的一片文章 https://blog.csdn.net/u010585773/article/details/83267904 其中介紹創建房間的方法,這篇博客講了通過閉合區間創建房間,讓做了好幾年Revit開發的我找到了新大陸,原來Revit竟然提供了這個方法,一直沒有研究過。於是系統的研究總結一下,提供下面兩種創建方式:

第一種是在當前業務中使用:

技術分享圖片
        private List<Room> CreateRooms()
        {
            List<Room> roomList = new List<Room>();
            List<Level> levelList = wrapper.Doc.GetLevels().OrderBy(p => p.Elevation).ToList();
            Level level = levelList[0];
            List<Phase> phaseList = wrapper.Doc.GetElements<Phase>();
            List
<ElementId> roomIdList = new List<ElementId>(); Phase viewPhase = wrapper.ActiveView.GetParameterElement(BuiltInParameter.VIEW_PHASE) as Phase; double minRoomArea = (2.0).ConvertToAPI(DisplayUnitType.DUT_SQUARE_METERS); Transaction trans = new Transaction(wrapper.Doc, "CreateRoom"); trans.Start(); try { PlanTopology planTopology = wrapper.Doc.get_PlanTopology(wrapper.ActiveView.GenLevel, viewPhase); if (planTopology != null) { var arry = planTopology.Circuits; foreach (PlanCircuit item in arry) { if (!item.IsRoomLocated && item.Area > minRoomArea) { Room room = wrapper.DocCreater.NewRoom(null, item); roomList.Add(room); } } } trans.Commit(); } catch (Exception ex) { trans.RollBack(); } return roomList; }
View Code

第二種是RevitAPI中官方示例:

技術分享圖片
Room InsertNewRoomInPlanCircuit(Autodesk.Revit.DB.Document document, Level level, Phase newConstructionPhase)
{
    // create room using Phase
    Room newScheduleRoom = document.Create.NewRoom(newConstructionPhase);

    // set the Room Number and Name
    string newRoomNumber = "101";
    string newRoomName = "Class Room 1";
    newScheduleRoom.Name = newRoomName;
    newScheduleRoom.Number = newRoomNumber;

    // Get a PlanCircuit
    PlanCircuit planCircuit = null;
    // first get the plan topology for given level
    PlanTopology planTopology = document.get_PlanTopology(level);

    // Iterate circuits in this plan topology
    foreach (PlanCircuit circuit in planTopology.Circuits)
    {
        // get the first circuit we find
        if (null != circuit)
        {
            planCircuit = circuit;
            break;
        }
    }

    Room newRoom2 = null;
    if (null != planCircuit)
    {
        using (Transaction transaction = new Transaction(document, "Create Room"))
        {
           if (transaction.Start() == TransactionStatus.Started)
           {
               // The input room must exist only in the room schedule, 
               // meaning that it does not display in any plan view.
               newRoom2 = document.Create.NewRoom(newScheduleRoom, planCircuit);
               // a model room with the same name and number is created in the 
               // view where the PlanCircuit is located
               if (null != newRoom2)
               {
                   // Give the user some information
                   TaskDialog.Show("Revit", "Room placed in Plan Circuit successfully.");
               }
               transaction.Commit();
           }
        }
    }

    return newRoom2;
}
View Code

Revit二次開發-根據視圖階段(Phase)創建房間