前言
PerformanceAdviserControl 介绍如何在文件中,根据 Revit 自带的规则或者用户自定义的规则对文件进行检查。
内容
运行的界面,列出了Revit 自带的规则,以及这个例子加入的规则。
如何加入规则?
参考官网文档:Performance Adviser
必须在 IExternalApplication::OnStartup
创建和注册自定义规则。
public Autodesk.Revit.UI.Result OnStartup(Autodesk.Revit.UI.UIControlledApplication application)
{
#region Add command button
// 增加按钮,省略。。。
#endregion
#region 创建和注册新的规则 (FlippedDoorCheck)
m_FlippedDoorApiRule = new FlippedDoorCheck();
Autodesk.Revit.DB.PerformanceAdviser.GetPerformanceAdviser().AddRule(m_FlippedDoorApiRule.getRuleId(), m_FlippedDoorApiRule);
#endregion
return Autodesk.Revit.UI.Result.Succeeded;
}
如何执行规则?
可通过静态方法 PerformanceAdviser.GetPerformanceAdviser()
获取 PerformanceAdviser
。
通过 ExecuteAllRules 执行所有的规则:
// PerformanceAdviser
public static PerformanceAdviser GetPerformanceAdviser();
public IList<FailureMessage> ExecuteAllRules(Document document);
例子中的代码:
如何自定义规则?
实现接口 IPerformanceAdviserRule
:
namespace Autodesk.Revit.DB
{
public interface IPerformanceAdviserRule
{
void ExecuteElementCheck(Document document, Element element);
void FinalizeCheck(Document document);
string GetDescription();
ElementFilter GetElementFilter(Document document);
string GetName();
void InitCheck(Document document);
bool WillCheckElements();
}
}
对于类 FlippedDoorCheck : Autodesk.Revit.DB.IPerformanceAdviserRule
,系统的大致逻辑:
- 注册和初始化
IExternalApplication::OnStartup
- 初始化具体的规则
FlippedDoorCheck.InitCheck
- 过滤获取需要进行规则判断的构件
FlippedDoorCheck.GetElementFilter
- 对构件进行规则判断
FlippedDoorCheck.ExecuteElementCheck
- 对判断结果进行处理
FlippedDoorCheck.FinalizeCheck
初始化具体的规则
FlippedDoorCheck.InitCheck
public void InitCheck(Autodesk.Revit.DB.Document document)
{
if (m_FlippedDoors == null)
m_FlippedDoors = new List<Autodesk.Revit.DB.ElementId>();
else
m_FlippedDoors.Clear();
}
过滤获取需要进行规则判断的构件
FlippedDoorCheck.GetElementFilter
,过滤获取所有的门。
public Autodesk.Revit.DB.ElementFilter GetElementFilter(Autodesk.Revit.DB.Document document)
{
return new Autodesk.Revit.DB.ElementCategoryFilter(Autodesk.Revit.DB.BuiltInCategory.OST_Doors);
}
对构件进行规则判断
FlippedDoorCheck.ExecuteElementCheck
,记录所有反向的门。
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);
}
}
对判断结果进行处理
FlippedDoorCheck.FinalizeCheck
,如果没有反向的门,测试通过;如果有反向的门,则通过PerformanceAdviser.PostWarning
报错。
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();
Autodesk.Revit.DB.PerformanceAdviser.GetPerformanceAdviser().PostWarning(fm);
failureReportingTransaction.Commit();
m_FlippedDoors.Clear();
}
}
运行结果
最终程序会提示和高亮不符合规则的门。