杉木的园子

Revit二次开发-根据视图阶段(Phase)创建房间

       最近开发业务中,有一个自动创建房间的功能,很自然的想到了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

 

posted @ 2019-02-14 12:04  杉木2019  阅读(876)  评论(0编辑  收藏  举报