Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

view filter

8 REPLIES 8
SOLVED
Reply
Message 1 of 9
Ning_Zhou
1757 Views, 8 Replies

view filter

i assume you cannot directly manipulate view filters for specific view like turn on/off, set override, etc. in 2014 and 2015? but you can do it through view template? this means you have to temporarily create/modify view template?

8 REPLIES 8
Message 2 of 9
Anonymous
in reply to: Ning_Zhou

Not specific to the view, but you can create a custom view filter and then apply it to any view you like. I do this quite regularly, I'd advise creating a management framework for them to handle naming conflicts and the clean up of unused filters. If you need a few code snippets, let me know I'll get you started.

Message 3 of 9
Ning_Zhou
in reply to: Anonymous

thanks Scott, code snippets will be very much appreciated!
Message 4 of 9
Anonymous
in reply to: Ning_Zhou

Here's some basic code that will generate a filter that matches for equality with a particular parameter value. It also checks for an existing filter with the same name and re-uses an existing one if found, this allows updating the rules of a filter without having to specifically update each affected view.

 

 

public ParameterFilterElement CreateViewFilter(Document dbDoc)
{
    Parameter filterParam = someParameterFromSomewhere;
    ElementId paramId = filterParam.Id;

    List<ElementId> catIds = new List<ElementId>();
    catIds.Add(new ElementId(BuiltInCategory.OST_GenericModel));
    catIds.Add(new ElementId(BuiltInCategory.OST_Roofs));
    catIds.Add(new ElementId(BuiltInCategory.OST_Walls));

    List<FilterRule> filterRules = new List<FilterRule>();
    filterRules.Add(ParameterFilterRuleFactory.CreateEqualsRule(paramId, "Desired Parameter Value", true));

    ParameterFilterElement elemFilter = null;

    String filterName = "My New Filter";

    // Search for existing filters with same name
    List<FilterElement> filterList = new FilteredElementCollector(dbDoc).WherePasses(new ElementClassFilter(typeof(FilterElement))).ToElements().Cast<FilterElement>().ToList()

    foreach(ParameterFilterElement filter in filterList)
    {
        if(filter.Name == filterName) // Filter Exists, update rules and use existing filter
        {
            elemFilter = filter;
            elemFilter.ClearRules();
            elemFilter.SetRules(filterRules);
            break;
        }
    }

    if(elemFilter == null) // Doesn't Exist, create new filter
    {
        elemFilter = ParameterFilterElement.Create(dbDoc, filterName, catIds, filterRules);
    }

    return elemFilter;
}

 

Now to apply this filter to a view you do something like this.

 

public void AddFilterToView(Document dbDoc, View viewToFilter)
{
    ParameterFilterElement newFilter = CreateViewFilter(dbDoc);
    viewToFilter.AddFilter(newFilter.id);
    viewToFilter.SetFilterVisibility(newFilter.Id, true);
}

 

 

That should be enough to get you motoring. 

  

 

Message 5 of 9
Ning_Zhou
in reply to: Anonymous

works great! thanks Scott
Message 6 of 9
Anonymous
in reply to: Ning_Zhou

Sorry I am coming back to this, but my code just doesn't work.

 

I want to create a filter on a PlanView that filters all the structural columns below the current level. Here my code so far:

 

private static ParameterFilterElement CreateViewFilter(Document doc, BuiltInCategory category, Level CurrentLevel, Level LowerLevel)
{
    ElementId paramId = new ElementId(BuiltInParameter.FAMILY_BASE_LEVEL_PARAM);
    List<ElementId> catIds = new List<ElementId>();
    catIds.Add(new ElementId(category));
    List<FilterRule> filterRules = new List<FilterRule>();

    filterRules.Add(ParameterFilterRuleFactory.CreateEqualsRule(paramId, LowerLevel.Id));            
    ParameterFilterElement elemFilter = null;
    string filterName = CurrentLevel.Name + "_ExcludeColumnsBelow";

    // Search for existing filters with same name
    List<FilterElement> filterList = new FilteredElementCollector(doc).WherePasses(new ElementClassFilter(typeof(FilterElement))).ToElements().Cast<FilterElement>().ToList();
    foreach (ParameterFilterElement filter in filterList)
    {
        if (filter.Name == filterName) // Filter Exists, update rules and use existing filter
        {
            elemFilter = filter;
            elemFilter.ClearRules();
            elemFilter.SetRules(filterRules);
            break;
        }
    }
            
    if (elemFilter == null) // Doesn't Exist, create new filter
    {
        elemFilter = ParameterFilterElement.Create(doc, filterName, catIds, filterRules);
    }
    return elemFilter;
}

The error message I get, is: One of the given rules refers to a parameter that does not apply to this filter's categories.

 

The category is BuiltInCategory.OST_StructuralColumns;

 

I am lost. Would be great if someone has an idea.

 

regards

Christian

Message 7 of 9
Anonymous
in reply to: Anonymous

Nobody an idea, what I am doing wrong.

 

Thanks

Christian

Message 8 of 9
Anonymous
in reply to: Anonymous

Maybe to illustrate a bit. This is what I would like to do.

 

FilterRule.png

Any ideas, whats wrong with the code?

 

regards

Christian

Message 9 of 9
Anonymous
in reply to: Anonymous

I got it:

 

ElementId paramId_base = new ElementId(BuiltInParameter.SCHEDULE_BASE_LEVEL_PARAM);
ElementId paramId_top = new ElementId(BuiltInParameter.SCHEDULE_TOP_LEVEL_PARAM);

and its not BuiltInParameter.FAMILY_BASE_LEVEL_PARAM or BuiltInParameter.FAMILY_TOP_LEVEL_PARAM.

 

Holy cow.

Christian

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Rail Community