title: Revit View Filter
body: <p>I am working on a routine for Revit 2015 shown below. The routine is for selecting an element and creating a view filter that would automatically hide all elements on that system. It only works once in a project due to the fact that a view filter can only be created once in a project and then reused for other views. Does anyone know how i can collect all the viewfilters within the model and compare the name to the view filter I would like to create?</p>
  
  <pre><code>public void HideSystems()
          {
  
  
              UIDocument uidoc = this.ActiveUIDocument; 
              Document doc = uidoc.Document; 
  
              Reference r = uidoc.Selection.PickObject(ObjectType.Element,new SystemElementFilter(),"Select an element of a system");
                  if (r == null)
                  {
                  return;
              }   
  
               Element elem = doc.GetElement(r.ElementId); 
  
  
               var systemNameParam =elem.get_Parameter(BuiltInParameter.RBS_DUCT_PIPE_SYSTEM_ABBREVIATION_PARAM); 
               if (systemNameParam == null) 
  
               { 
  
  
               } 
  
  
               var view = doc.ActiveView; 
  
  
               var categoriesWithSystem = 
                   GetCategoriesApplicableForParameter(doc, BuiltInParameter.RBS_DUCT_PIPE_SYSTEM_ABBREVIATION_PARAM); 
  
  
               var categoriesWithoutSystemNameParameter = 
                   GetCategoriesApplicableForParameter(doc, BuiltInParameter.RBS_DUCT_PIPE_SYSTEM_ABBREVIATION_PARAM, true); 
  
  
               var systemName = systemNameParam.AsString(); 
  
  
               // An element can be assigned to the several systems 
               // In this case System Name parameter has a comma separated 
               // list of the systems 
               // Create several rules             
               IList&lt;FilterRule&gt; rules = new List&lt;FilterRule&gt;(); 
  
  
               var systems = systemName.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); 
  
  
               foreach (var system in systems) 
               { 
                  rules.Add(ParameterFilterRuleFactory.CreateContainsRule(new ElementId(BuiltInParameter.RBS_DUCT_PIPE_SYSTEM_ABBREVIATION_PARAM),system.Trim(),true));
  
               } 
  
  
  
  
               using (var t = new Transaction(doc, "System hide")) 
               { 
                   t.Start(); 
                   string sysName=string.Concat("Remove ",systemName);
  
                   // Hide elements which are in the selected system                 
                   ParameterFilterElement filter2 = ParameterFilterElement.Create(doc,sysName,categoriesWithSystem,rules); 
  
                   view.AddFilter(filter2.Id); 
                   view.SetFilterVisibility(filter2.Id, false); 
  
   vi
  
                   t.Commit(); 
               } 
  
  
  
  
  
          }
  
  
   /// &lt;summary&gt;         
   /// Returns the list of the categories what can be used in filter  
   /// by the specific parameter 
   /// &lt;/summary&gt; 
   /// &lt;param name="doc"&gt;Document on which the filter is applying&lt;/param&gt; 
   /// &lt;param name="bip"&gt;BuiltInParameter&lt;/param&gt; 
   /// &lt;param name="inverse"&gt;If true, the list will be inverted. I.e. you will get  
   /// the list of the categories what cannot be used in filter  
   /// by the specific parameter &lt;/param&gt; 
   /// &lt;returns&gt;&lt;/returns&gt; 
    ICollection&lt;ElementId&gt; GetCategoriesApplicableForParameter(Document doc,BuiltInParameter bip,bool inverse = false) 
   { 
       // All categories available for filter 
       var allCategories = ParameterFilterUtilities.GetAllFilterableCategories(); 
       allCategories.Remove(new ElementId(BuiltInCategory.OST_DuctTerminal));
       allCategories.Remove(new ElementId(BuiltInCategory.OST_PlumbingFixtures));
       ICollection&lt;ElementId&gt; retResult = new List&lt;ElementId&gt;(); 
  
  
  
       foreach (ElementId categoryId in allCategories) 
       { 
           // get the list of parameters, compatible with the category.                 
           var applicableParameters = 
               ParameterFilterUtilities.GetFilterableParametersInCommon(doc, new[] {categoryId}); 
  
  
           // if the parameter we are interested in the collection 
           // add it to the result 
           if (applicableParameters.Contains(new ElementId(bip))) 
           { 
               retResult.Add(categoryId); 
           } 
       } 
  
  
       // Invert if needed.  
       if (inverse) 
           retResult = 
               allCategories.Where(x =&gt; !retResult.Contains(x)).ToList(); 
  
  
       return retResult; 
   } 
       } 
  
  
       /// &lt;summary&gt; 
       /// Allow to select only element what has the parameter  
       /// 'System Name' and has value. 
       /// &lt;/summary&gt; 
       public class SystemElementFilter : ISelectionFilter 
       { 
           public bool AllowElement(Element elem) 
           { 
               var systemNameParam = 
                   elem.get_Parameter(BuiltInParameter.RBS_DUCT_PIPE_SYSTEM_ABBREVIATION_PARAM); 
               return systemNameParam != null &amp;&amp; systemNameParam.HasValue; 
           } 
  
  
           public bool AllowReference(Reference reference, XYZ position) 
           { 
               throw new NotImplementedException(); 
           } 
       } 
      } 
  `
  </code></pre>