应用程序: RoofsRooms

Revit 平台: Architecture, MEP

Revit 版本: 2011.0

首次发布于: 2009.1

编程语言: C#

技能水平: 中等

类别: Rooms/Spaces

类型: ExternalCommand

主题: 对于屋顶和房间(空间)的几何检查

摘要:本示例展示了如何检查一个房间/空间是否有一个边界屋顶。

相关类:

Autodesk.Revit.DB.Mechanical.SpaceFilter

Autodesk.Revit.DB.Architecture.RoomFilter

Autodesk.Revit.DB.ElementCategoryFilter

Autodesk.Revit.DB.FilteredElementCollector

Autodesk.Revit.DB.LogicalOrFilter

Autodesk.Revit.DB.Architecture.Room

Autodesk.Revit.DB.Mechanical.Space

Autodesk.Revit.DB.BuiltInCategory

Autodesk.Revit.DB.SpatialElementGeometryCalculator

Autodesk.Revit.DB.SpatialElementGeometryResults

Autodesk.Revit.DB.SpatialEleme

项目文件:

Command.cs 文件包含了一个 RoofsRooms 类,该类继承自 IExternalCommand 接口并实现 Execute 方法;该类的一些方法用于检索房间/空间和屋顶元素。

描述:

功能:

·获取当前项目中的所有房间和屋顶,并进行单独检查。

·使用SparialElementGeometryCalculator获取房间/空间的边界面。

·检查边界面相关元素是否为屋顶。

 

实现:

·为了在当前文档中检索所有房间和空间元素,在创建单独的RoomFilterSpaceFilter后,为它们创建LogicalOrFilter

·为了检索房间和空间的几何形状,使用SparialElementGeometryCalculator。构造此对象,并将每个元素传递到其CalculateSpatialElementGeometry()方法中。

·从SpatialElementGeometryResults.GetGeometry()中获取表示房间/空间的实体。

·使用Solid.Faces属性获取固体对象的面。

·对于每个面,从SpatialElementGeometryResults.GetBoundaryFaceInfo()中获取相关的SpatialElementBoundarySubface(s)

·使用ElementFilter.PassesElement()ElementCategoryFilter (BuiltInCategory对于屋顶和屋顶底板分别为OST_RoofsOST_RoofSoffit)来查看边界元素是否为屋顶。

·Elements.Face是一个基类,派生类包括PlanarFacePlanarFaceConicalFaceCylindricalFaceRevolvedFaceHermiteFaceRuledFace

·PlanarFace.OriginNormal属性将用于检查两个Planar Faces是否重合。可能面的法线是相反的,然后我们可以使用Face.Project(XYZ)方法计算投影距离以检查点是否在面内。

·ConicalFace.AxisOrigin属性和get_Radius(int)方法将用于检查两个ConicalFaces是否重合。

·CylindricalFace.AxisOrigin属性和get_Radius(int)方法将用于检查两个CylindricalFaces是否重合。

·RevolvedFace.AxisOrigin属性和Curve属性将用于检查两个RevolvedFaces是否重合;要获取曲线点,请使用Curve.Tessellate()方法。

·HermiteFace.PointsMixedDerivs属性可用于检查两个HermiteFaces是否重合。

·RuledFace.get_Curve(int)get_Point(int)方法可用于检索RuledFace的曲线和点,然后使用这些点和曲线检查两个RuledFaces是否重合。

说明:

1. 运行Revit,绘制一些房间(空间),然后将屋顶绑定到它们上面,您可以调整房间(空间)的“上限”和“限位偏移”参数、“屋顶”和“距离层高的基准偏移”参数。在示例项目文件夹下提供了一个带有房间的One Architecture示例模型(RoofsRooms.rvt)和一个带有空间的MEP示例模型(MEPRoofsRooms.rvt)。

2. 运行外部命令,命令将弹出边界检查结果。

注意:

1. 运行此外部命令之前,请确保您在[设置]-[区域和体积计算...]-“计算”选项卡中打开了“区域和体积”选项。

2. 确保在屋顶的元素属性对话框中勾选了“房间边界”参数,否则屋顶将无法成为房间的边界。

3. 目前,此示例不支持具有斜面玻璃屋顶的模型。

源代码:

完整的源代码请加入QQ群649037449,在群文件中下载RevitSDK.exe,解压后在文件夹中搜索本文中应用程序名称即可获得完整源码

Command.cs

//
// (C) Copyright 2003-2019 by Autodesk, Inc.
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
//

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Reflection;

using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.DB.Mechanical;

namespace Revit.SDK.Samples.RoofsRooms.CS
{
/// <summary>
/// This class inherits from IExternalCommand,
/// used to check if room can cut roof by geometry relationship
/// </summary>
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.ReadOnly)]
[Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
[Autodesk.Revit.Attributes.Journaling(Autodesk.Revit.Attributes.JournalingMode.UsingCommandData)]
public class Command : IExternalCommand
{
#region Class Variables
// Revit application
Autodesk.Revit.ApplicationServices.Application m_application;
// Current document in Revit
Document m_document;
#endregion

#region Implement IExternalCommand
///<summary>
/// Implement this method as an external command for Revit.
/// </summary>
/// <param name="commandData">An object that is passed to the external application
/// which contains data related to the command,
/// such as the application object and active view.</param>
/// <param name="message">A message that can be set by the external application
/// which will be displayed if a failure or cancellation is returned by
/// the external command.</param>
/// <param name="elements">A set of elements to which the external application
/// can add elements that are to be highlighted in case of failure or cancellation.</param>
/// <returns>Return the status of the external command.
/// A result of Succeeded means that the API external method functioned as expected.
/// Cancelled can be used to signify that the user cancelled the external operation
/// at some point. Failure should be returned if the application is unable to proceed with
/// the operation.</returns>
public Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData,
ref string message, Autodesk.Revit.DB.ElementSet elements)
{
string assemblyLocation = Assembly.GetExecutingAssembly().Location;
string log = assemblyLocation + "." + DateTime.Now.ToString("yyyyMMdd") + ".log";
if (File.Exists(log)) File.Delete(log);
TraceListener txtListener = new TextWriterTraceListener(log);
Trace.Listeners.Add(txtListener);
try
{
// variable initialization
m_application = commandData.Application.Application;
m_document = commandData.Application.ActiveUIDocument.Document;
Environment.CurrentDirectory = Path.GetDirectoryName(assemblyLocation);

FindRoomBoundingRoofs(ref message, elements);

// Not show TaskDialog in regression mode
if (0 == commandData.JournalData.Count)
{
TaskDialog.Show("Roofs Rooms", message);
}

// Insert result to journal data for regression purpose.
const string DataKey = "Results";
if (!commandData.JournalData.ContainsKey(DataKey))
{
// In normal/recording mode
commandData.JournalData.Add(DataKey, message);
}
else
{
// In regression/replaying mode
commandData.JournalData[DataKey] = message;
}

return Autodesk.Revit.UI.Result.Succeeded;
}
catch (Exception ex)
{
Trace.WriteLine(ex.ToString());
message = ex.ToString();
return Autodesk.Revit.UI.Result.Failed;
}
finally
{
Trace.Flush();
txtListener.Close();
Trace.Close();
Trace.Listeners.Remove(txtListener);
}
}

/// <summary>
/// Test whether each room has a roof to bound it.
/// </summary>
/// <param name="message">Error message to be dumped.</param>
/// <param name="elements">Some elements to return.</param>
/// <returns></returns>
private bool FindRoomBoundingRoofs(ref string message, Autodesk.Revit.DB.ElementSet elements)
{
// Get all rooms
List<Element> rooms = GetRoomsElements();
if (rooms.Count == 0)
{
message = "Unable to identify any rooms, please create room first!";
return false;
}

// Represents the criteria for boundary elements to be considered bounding roofs
LogicalOrFilter categoryFilter = new LogicalOrFilter(new ElementCategoryFilter(BuiltInCategory.OST_Roofs),
new ElementCategoryFilter(BuiltInCategory.OST_RoofSoffit));

// Calculator for room/space geometry.
SpatialElementGeometryCalculator calculator = new SpatialElementGeometryCalculator(m_document);

// Stores the resulting room->roof relationships
Dictionary<Element, List<ElementId>> roomsAndRoofs = new Dictionary<Element, List<ElementId>>();

foreach (Element room in rooms)
{
// Get room geometry & boundaries
SpatialElementGeometryResults results = calculator.CalculateSpatialElementGeometry((SpatialElement)room);

// Get solid geometry so we can examine each face
Solid geometry = results.GetGeometry();

foreach (Face face in geometry.Faces)
{
// Get list of roof boundary subfaces for a given face
IList<SpatialElementBoundarySubface> boundaryFaces = results.GetBoundaryFaceInfo(face);
foreach (SpatialElementBoundarySubface boundaryFace in boundaryFaces)
{
// Get boundary element
LinkElementId boundaryElementId = boundaryFace.SpatialBoundaryElement;

// Only considering local file room bounding elements
ElementId localElementId = boundaryElementId.HostElementId;

// Evaluate if element meets criteria using PassesFilter()
if (localElementId != ElementId.InvalidElementId && categoryFilter.PassesFilter(m_document, localElementId))
{
// Room already has roofs, add more
if (roomsAndRoofs.ContainsKey(room))
{
List<ElementId> roofs = roomsAndRoofs[room];
if (!roofs.Contains(localElementId))
roofs.Add(localElementId);
}
// Room found first roof
else
{
List<ElementId> roofs = new List<ElementId>();
roofs.Add(localElementId);
roomsAndRoofs.Add(room, roofs);
}
break;
}
}
}
}

// Format results
if (roomsAndRoofs.Count > 0)
{
String logs = String.Format("Rooms that have a bounding roof:");
message += logs + "\t\r\n";
Trace.WriteLine(logs);
foreach (KeyValuePair<Element, List<ElementId>> kvp in roomsAndRoofs)
{
// remove this room from all rooms list
rooms.Remove(kvp.Key);

List<ElementId> roofs = kvp.Value;
String roofsString;

// Single roof boundary
if (roofs.Count == 1)
{
Element roof = m_document.GetElement(roofs[0]);
roofsString = String.Format("Roof: Id = {0}, Name = {1}", roof.Id.IntegerValue, roof.Name);
}
// Multiple roofs
else
{
roofsString = "Roofs ids = " + string.Join(", ", Array.ConvertAll<ElementId, string>(roofs.ToArray(), i => i.ToString()));
}

// Save results
logs = String.Format(
" Room: Id = {0}, Name = {1} --> {2}",
kvp.Key.Id.IntegerValue, kvp.Key.Name, roofsString);
message += logs + "\t\r\n";
Trace.WriteLine(logs);
}
}

// Format the rooms that have no bounding roof
Trace.WriteLine("Geometry relationship checking finished...");
if (rooms.Count != 0)
{
String logs = String.Format("Below rooms don't have bounding roofs:");
message += logs + "\t\r\n";
Trace.WriteLine(logs);
foreach (Element room in rooms)
{
elements.Insert(room);
logs = String.Format(" Room Id: {0}, Room Name: {1}",
room.Id.IntegerValue, room.Name);
message += logs + "\t\r\n";
Trace.WriteLine(logs);
}
}

return true;
}

/// <summary>
/// Retrieve all Rooms and Spaces elements from active document.
/// </summary>
/// <returns>Element list retrieved from current document.</returns>
private List<Element> GetRoomsElements()
{
List<Element> array = new List<Element>();
ElementFilter roomSpaceFilter = new LogicalOrFilter(new RoomFilter(), new SpaceFilter());
FilteredElementCollector collector = new FilteredElementCollector(m_document);
array.AddRange(collector.WherePasses(roomSpaceFilter).ToElements());
return array;
}

#endregion
}
}