// // (C) Copyright 2003-2019 by Autodesk, Inc. // // Permission to use, copy, modify, and distribute this software in // object code form for any purpose and without fee is hereby granted, // provided that the above copyright notice appears in all copies and // that both that copyright notice and the limited warranty and // restricted rights notice below appear in all supporting // documentation. // // AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS. // AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF // MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC. // DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE // UNINTERRUPTED OR ERROR FREE. // // Use, duplication, or disclosure by the U.S. Government is subject to // restrictions set forth in FAR 52.227-19 (Commercial Computer // Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii) // (Rights in Technical Data and Computer Software), as applicable. // using System; using System.Collections.Generic; using System.Linq; using System.Text; using Autodesk.Revit.UI; namespace Revit.SDK.Samples.PerformanceAdviserControl.CS { /// /// A class that implements IPerformanceAdviserRule. This class implements several methods that will be /// run automatically when PerformanceAdviser::ExecuteRules or ExecuteAllRules is called. /// 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(); 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(); Autodesk.Revit.DB.PerformanceAdviser.GetPerformanceAdviser().PostWarning(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 #region Other instance methods /// /// This method is used by PerformanceAdviser to get the /// ID of the rule. It returns a global static field to make sharing the ID in different places /// in the application easier. /// /// The Rule ID of this rule public Autodesk.Revit.DB.PerformanceAdviserRuleId getRuleId() { return FlippedDoorCheck.Id; } #endregion /// /// The rule ID for this rule; /// public static Autodesk.Revit.DB.PerformanceAdviserRuleId Id { get { return m_Id; } } #region Data /// /// A list of all family instances in the document that have the FaceFlipped property set to true; /// private List m_FlippedDoors; /// /// A short name for the rule /// private string m_name; /// /// A short description of the rule /// private string m_description; /// /// The rule ID for this rule; /// private static Autodesk.Revit.DB.PerformanceAdviserRuleId m_Id = new Autodesk.Revit.DB.PerformanceAdviserRuleId((new Guid("BC38854474284491BD03795675AC7386"))); /// /// The ID of the failure definition for our API-based door flip check rule /// private Autodesk.Revit.DB.FailureDefinitionId m_doorWarningId; /// /// The failure definition for our API-based door flip check rule /// private Autodesk.Revit.DB.FailureDefinition m_doorWarning; #endregion } }