性能顾问

Revit API的性能顾问功能旨在分析文档,并为用户标记可能导致性能下降的任何元素和/或设置。Performance Adviser命令执行一组规则并在标准审阅警告对话框中显示其结果。

性能顾问的API由两个类组成:

  • PerformanceAdviser -一个应用程序范围的对象,具有双重角色,既可以作为运行的规则注册表,以检测潜在的性能问题,也可以作为执行这些问题的引擎
  • IPerformanceAdviserRule - 允许您为Performance Adviser定义新规则的接口

性能顾问

PerformanceAdviser用于添加或删除要检查的规则、启用和禁用规则、获取有关列表中规则的信息以及执行列表中的部分或全部规则。创建新规则的应用程序应在应用程序启动期间使用AddRule()注册新规则,并在应用程序关闭期间使用DeleteRule()取消注册。ExecuteAllRules()将对给定文档执行列表中的所有规则,而ExecuteRules()可用于执行文档中的选定规则。这两种方法都将返回一个失败消息列表,解释在文档中检测到的性能问题。

下面的示例演示循环遍历所有Performance Adviser规则并执行文档的所有规则。

代码区域:性能顾问

1
2
3
4
5
6
//Get the name of each registered PerformanceRule and then execute all of them.
foreach (PerformanceAdviserRuleId id in PerformanceAdviser.GetPerformanceAdviser().GetAllRuleIds())
{
string ruleName = PerformanceAdviser.GetPerformanceAdviser().GetRuleName(id);
}
PerformanceAdviser.GetPerformanceAdviser().ExecuteAllRules(document);

IPerformanceAdviserRule

创建IPerformanceAdviserRule接口的实例,以便为Performance Adviser创建新规则。规则可以特定于元素,也可以是文档范围的规则。需要实施以下方法:

  • GetName() - 命名规则的短字符串
  • GetDescription() - 规则的一到两句描述
  • InitCheck() -在检查开始时由性能顾问调用一次的方法。如果rule检查整个文档而不是特定元素,则应在此方法中执行检查。
  • FinalizeCheck() -在检查结束时由性能顾问调用一次的方法。在规则执行期间发现的任何有问题的结果都可以在此消息期间使用FailureMessage报告
  • WillCheckElements() - 指示规则是否需要在单个元素上执行
  • GetElementFilter() - 检索一个过滤器以限制要检查的元素
  • ExecuteElementCheck() - 性能顾问为每个要检查的元素调用的方法

The following excerpt from the PerformanceAdviserControl sample in the Revit API SDK Samples folder demonstrates the 以下摘录摘自Revit API SDK Samples文件夹中的PerformanceAdviserControl示例,演示了用于标识文档中任何正面翻转的门的自定义规则的实现。(在示例项目查看完整类实现。)

代码区域:实现IPerformanceAdviserRule

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
public class FlippedDoorCheck : Autodesk.Revit.DB.IPerformanceAdviserRule
{
#region Constructor
///
/// Set up rule name, description, and error handling
///
public FlippedDoorCheck()
{
m_name = "Flipped Door Check";
m_description = "An API-based rule to search for and return any doors that are face-flipped";
m_doorWarningId = new Autodesk.Revit.DB.FailureDefinitionId(new Guid("25570B8FD4AD42baBD78469ED60FB9A3"));
m_doorWarning = Autodesk.Revit.DB.FailureDefinition.CreateFailureDefinition(m_doorWarningId, Autodesk.Revit.DB.FailureSeverity.Warning, "Some doors in this project are face-flipped.");
}
#endregion

#region IPerformanceAdviserRule implementation
///
/// Does some preliminary work before executing tests on elements. In this case,
/// we instantiate a list of FamilyInstances representing all doors that are flipped.
///
/// The document being checked
public void InitCheck(Autodesk.Revit.DB.Document document)
{
if (m_FlippedDoors == null)
m_FlippedDoors = new List<Autodesk.Revit.DB.ElementId>();
else
m_FlippedDoors.Clear();
return;
}

///
/// This method does most of the work of the IPerformanceAdviserRule implementation.
/// It is called by PerformanceAdviser.
/// It examines the element passed to it (which was previously filtered by the filter
/// returned by GetElementFilter() (see below)). After checking to make sure that the
/// element is an instance, it checks the FacingFlipped property of the element.
///
/// If it is flipped, it adds the instance to a list to be used later.
///
/// The active document
/// The current element being checked
public void ExecuteElementCheck(Autodesk.Revit.DB.Document document, Autodesk.Revit.DB.Element element)
{
if ((element is Autodesk.Revit.DB.FamilyInstance))
{
Autodesk.Revit.DB.FamilyInstance doorCurrent = element as Autodesk.Revit.DB.FamilyInstance;
if (doorCurrent.FacingFlipped)
m_FlippedDoors.Add(doorCurrent.Id);
}

}

///
/// This method is called by PerformanceAdviser after all elements in document
/// matching the ElementFilter from GetElementFilter() are checked by ExecuteElementCheck().
///
/// This method checks to see if there are any elements (door instances, in this case) in the
/// m_FlippedDoor instance member. If there are, it iterates through that list and displays
/// the instance name and door tag of each item.
///
/// The active document
public void FinalizeCheck(Autodesk.Revit.DB.Document document)
{
if (m_FlippedDoors.Count == 0)
System.Diagnostics.Debug.WriteLine("No doors were flipped. Test passed.");
else
{
//Pass the element IDs of the flipped doors to the revit failure reporting APIs.
Autodesk.Revit.DB.FailureMessage fm = new Autodesk.Revit.DB.FailureMessage(m_doorWarningId);
fm.SetFailingElements(m_FlippedDoors);
Autodesk.Revit.DB.Transaction failureReportingTransaction = new Autodesk.Revit.DB.Transaction(document, "Failure reporting transaction");
failureReportingTransaction.Start();
document.PostFailure(fm);
failureReportingTransaction.Commit();
m_FlippedDoors.Clear();
}
}

///
/// Gets the description of the rule
///
/// The rule description
public string GetDescription()
{
return m_description;
}

///
/// This method supplies an element filter to reduce the number of elements that PerformanceAdviser
/// will pass to GetElementCheck(). In this case, we are filtering for door elements.
///
/// The document being checked
/// A door element filter
public Autodesk.Revit.DB.ElementFilter GetElementFilter(Autodesk.Revit.DB.Document document)
{
return new Autodesk.Revit.DB.ElementCategoryFilter(Autodesk.Revit.DB.BuiltInCategory.OST_Doors);
}

///
/// Gets the name of the rule
///
/// The rule name
public string GetName()
{
return m_name;
}

///
/// Returns true if this rule will iterate through elements and check them, false otherwise
///
/// True
public bool WillCheckElements()
{
return true;
}

#endregion
}

注:翻译自Revit API Developers Guide