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; }
第二种是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; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构