0

I am trying to create an ElementFilter in my project that will select only elements where a certain Shared Parameter is of a certain value.

Consulting the documentation (here: https://www.revitapidocs.com/2023/b231dc85-516a-5e75-c634-c6cd81b43fc5.htm) it seems to be the case that I will need the id of the corresponding Parameter.

Within the same entry, it seems to be implied that the only way to recover a Parameter is by querying an Element, any Element, using, for example, the guid of the Shared Parameter. I am not sure I understand the underlying logic, does this mean that a Parameter does not exist without Elements? This seems counterintuitive with the existence of ElementFilters prior to the existence of Elements in the Document.

Does anyone know how to create an ElementFilter without having an Element before? Otherwise it seems to me I will have to go through the process of creating an Element, check the Parameter and then remove the Element.

1
  • Just to clarify, are you looking to query the model for elements given their parameter values? I assumed this was the case in my answer, since you are looking for an ElementFilter. However, your link is for a FilterElement, which is to create a filter for a given view in the UI, which would only show elements in that view that pass the filter. If you see the comment at the bottom of that article, this is an easy confusion revitapidocs.com/2015/… Commented Feb 13, 2024 at 13:49

1 Answer 1

0

The API is a little confusing here. There are ElementFilter's which can be used to query the Revit document for specific Element's, and there are FilterElement's which can be used to create a filter for a View within the document.

Querying the Document

If you are attempting to query the model for elements given their parameter value you can do this using either get_Parameter() or LookUpParameter() methods for that element. get_Parameter() will work for built-in parameters or shared parameters where you know the GUID. You should be able to get the GUID of your SharedParameter either by looking at the SharedParameter file or using the Revit LoookUp tool. If you only know the name of the parameter, you can user LookupParameter(), but do know that this method is a little slower.

For example, if you are trying to get all walls with a height greater than 5', you could do the following:

var fec = new FilteredElementCollector(doc)
    .OfClass(typeof(Wall))
    // Replace BuiltInParameter with your SharedParameter's GUID
    .Where(x => x.get_Parameter(BuiltInParameter.WALL_USER_HEIGHT_PARAM)?.AsDouble() > 5)
    .ToList();

If you only know the name of the paramter, you could do the following:

var fec = new FilteredElementCollector(doc)
    .OfClass(typeof(Wall))
    .Where(x => x.LookupParameter("Unconnected Height")?.AsDouble() > 5)
    .ToList();

Applying a filter to a view

If you are looking to apply a filter to a view, then you will need to use a FilterElement and apply that to a view. For example, the following will hide all of the walls with the text "some value" in MySharedParam in the current view:

using (var t = new Transaction(doc, "Add filter"))
{
    t.Start();

    var parameter = new FilteredElementCollector(doc)
        .OfClass(typeof(SharedParameterElement))
        .OfType<SharedParameterElement>()
        .Where(x => x.GuidValue == new Guid("cf973001-e6e2-4e80-b502-ff74918165c9")) // if you know the GUID
        .Where(x => x.Name == "MySharedParam") // if you know the name
        .FirstOrDefault();

    var filterRule = ParameterFilterRuleFactory.CreateContainsRule(parameter.Id, "some value", false);
    var parameterFilter = new ElementParameterFilter(filterRule);
    var categories = new List<ElementId> { new ElementId(BuiltInCategory.OST_Walls) };

    var filterElement = ParameterFilterElement.Create(doc, "Filter", categories, parameterFilter);
    view.SetFilterVisibility(filterElement.Id, false);

    t.Commit();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.