Running rules with the Performance Adviser

With the Performance Adviser introduced in the last post, you can run one or more rules and learn which, if any, are being violated.

Here is a macro that runs the 3 default rules whose names include “View”. In my test model it results in 3 warnings:

ViewWarnings

public void PerfAdviserRunViewRules()
{
    Document doc = this.ActiveUIDocument.Document;
    PerformanceAdviser pa = PerformanceAdviser.GetPerformanceAdviser();

    // this list will contain the rules we want to run
    IList<PerformanceAdviserRuleId> ruleList = new List<PerformanceAdviserRuleId>();
    string s = "";

    // Loop through all rules
    foreach (PerformanceAdviserRuleId ruleId in pa.GetAllRuleIds())
    {
        // If the rule name contains "View" add it to the list
        if (pa.GetRuleName(ruleId).Contains("View"))
            ruleList.Add(ruleId);
    }

    TaskDialog.Show("d",ruleList.Count.ToString());

    // Run all rules in the list and loop through all resutling failure messages
    foreach (FailureMessage message in pa.ExecuteRules(doc,ruleList))
    {
        // for each failure get its description and the names & ids of the associated elements
        s += message.GetDescriptionText() + " " + getElementsFromList(doc, message.GetFailingElements()) + "\n\n";
    }
    TaskDialog.Show("Warnings",s);
}

private string getElementsFromList(Document doc, ICollection<ElementId> ids)
{
    string s = "";
    foreach (ElementId id in ids)
    {
        Element e = doc.GetElement(id);
        s += e.Name + " (" + id + "),";
    }
    // A comma is being added after each element id, but we don't want that comma after the last id
    return s.TrimEnd(',');
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s