Revit API 2012 provides a new Performance Advisor Rule API with which we can access to all the existing native Revit peformance advisor rules, execuate any of them, and create our own ones when necessary and hook them up into the system.
Performance Advisor sounds a bit counfusing, as it does not really seem to have something to do with the Revit performance. In fact, the Performance Advisor Rule API provides us a way to monitor Revit modeling, warn for something not as expected, and suggest how to correct it if necessary, so Modeling Monitor may sound better.
Anyway, the naming is not the major concern here. Let’s see how to execute a particular native Performance Advisor Rule (PerformanceAdviserRule) in this article. Here we go:
PerformanceAdviser adviser = PerformanceAdviser.GetPerformanceAdviser();
string guid = "cbb14baa-a57c-48ee-aab4-4137fcad779e"; //"Overlapping walls" rule
PerformanceAdviserRuleId ruleId = adviser.GetAllRuleIds()
.FirstOrDefault(r => r.Guid.ToString()
.Equals(guid, StringComparison.InvariantCultureIgnoreCase));
if (ruleId != null)
{
IList<FailureMessage> msgList = adviser.ExecuteRules(CachedDoc, new List<PerformanceAdviserRuleId> { ruleId });
Transaction tr = new Transaction(CachedDoc, "FailureMessage Posting");
tr.Start();
foreach (FailureMessage m in msgList)
{
CachedDoc.PostFailure(m);
}
tr.Commit();
}
Some highlights about the code may be more helpeful.
1. Readers may wonder how the GUID of the specific Revit native Performance Advisor Rule (PerformanceAdviserRule) was figured out in the above code. Very easy, creating a dumper for all the Performance Advisor Rules is the way to go, as demonstrated in the previous post.
2. Though the PerformanceAdviser provides a GetRuleId() method but it only accepts an integer index, which makes the API useless actually. If the IList index is supposed to be unique and constant for each PerformanceAdviserRuleId, why it has to carray a GUID. That explains why we did not even care about the index at all when dumping out all the PerformanceAdviserRule information previously.
3. No worry! We can find it out from the whole list using the GUID we sorted out and the cool LINQ technology.
4. We’d better use the StringComparison.InvariantCultureIgnoreCase option when comparing the GUID strings as they are hexidecial values instead of real characters.
5. Do not bother to set all other PerformanceAdviserRule instances as disabled through calling the SetRuleEnabled call repeatedly and call the ExecuteAllRules() finally to execuate all the rules (enabled or disabled) so as to execuate the single rule of concern.
6. The ExecuteRules() method has already been there to help for a task like such.
7. A transaction has to be started for the failure posting operations though the reason is not so clear. However, anyway, if we don’t do so, an exception is there waiting for us, “Autodesk.Revit.Exceptions.ModificationOutsideTransactionException: Modifying is forbidden because the document has no open transaction. at Autodesk.Revit.DB.Document.PostFailure(FailureMessage failure)”
8. For the performance sake, maybe not a big deal in out case since we only care about a single PerformanceAdviserRule here, it is obviously a good idea to start and commit the transaction only once outside of the FailureMessage loop, so performance issues could be avoided in the Performance Advisor API usages!
Now it’s time to put the code into an External Command and trigger it in a model with some overlapping walls. Here is what will happen:
The failure message has been reported as expected. And better we can show the problematic wall or erase it when necessary using the same dialog.
By the way, in terms of readers wondering what are those on top of the highlighted walls, they are twin walls generated automatically using some other Revit APIs such as DBExternalApplication, IUpdater, Subtransaction and so on. All these have alredy been covered in another article posted earlier.
Revit Addin Wizard (RevitAddinWizard) provides a PerformanceAdvisor Rule Creator to help implement custom performance advisor rules automatically and quickly. RevitAddinWizard can be downloaded from the Download link at the bottom of the blog index page.
Recent Comments