In this post, we shall look the steps involved in executing a performance adviser rule.
While listing all the existing performance adviser rules, we can use the name of a rule to search for a specific rule that we wish to execute. Another approach for selecting rules for execution, is to create a dialog with a list of registered rules and provide the ability for users to select one or more rules for execution from the dialog. The Revit SDK sample called PerformanceAdviserControl sample uses a similar approach by providing a list of rules for the users to select from, for execution.
Once we know the rules that we are interested in, we can use PerformanceAdviser.ExecuteRules() method to execute the given rules using the rule ids. ExecuteRules() method takes a document and a set of rule ids as input arguments. It returns a list of FailureMessages, which we can iterate through and display, for instance, in a standard Revit warning dialog using Document.PostFailure() method.
The following command shows a simple example of executing “Overlapping walls” performance adviser rule:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.Attributes;
namespace PASample
{
[Transaction(TransactionMode.Manual)]
public class ExecutePerformanceRule : IExternalCommand
{
public Result Execute(ExternalCommandData commandData,
ref string message, ElementSet elements)
{
Document doc =
commandData.Application.ActiveUIDocument.Document;
// Get access to the Performance Adviser object
PerformanceAdviser adviser =
PerformanceAdviser.GetPerformanceAdviser();
// List all the rules
IList<PerformanceAdviserRuleId> ruleIds =
adviser.GetAllRuleIds();
// Create an empty list for the rules to be executed
IList<PerformanceAdviserRuleId> rulesIdsToExecute =
new List<PerformanceAdviserRuleId>();
// Iterate through each
foreach (PerformanceAdviserRuleId ruleId in ruleIds)
{
if (adviser.GetRuleName(ruleId).Equals("Overlapping walls"))
{
rulesIdsToExecute.Add(ruleId);
break;
}
}
// Now we need to post the results
IList<FailureMessage> failureMessages = adviser.ExecuteRules(
doc, rulesIdsToExecute);
foreach (FailureMessage fm in failureMessages)
{
Transaction trans = new Transaction(doc, "Failure Reporting");
trans.Start();
doc.PostFailure(fm);
trans.Commit();
}
return Result.Succeeded;
}
}
}
In the next post and the last post on this series on this topic, we shall look at defining a custom rule.