1

Once a category is selected we hit the button and the name of the category "goes into" this variable called "selected". Now, how to put inside of ElementCategoryFilter that variable containing the necessary category? selected category

public void Button_Click(object sender, RoutedEventArgs e)
    {
        string selected = AllTheCategories.SelectedItem.ToString();
    }

    ElementCategoryFilter filter = new ElementCategoryFilter();
fysharp
  • 101
  • 7
  • Does `selected` match up to one of [these built-in categories](https://www.revitapidocs.com/2015/ba1c5b30-242f-5fdc-8ea9-ec3b61e6e722.htm)? In that case your question is how to [parse an enum from a string](https://stackoverflow.com/questions/16100/convert-a-string-to-an-enum-in-c-sharp) – Crowcoder Apr 28 '21 at 20:46

3 Answers3

0

You can directly assign selected string to your ElementCategoryFilter inside your Button_Click event.

ElementCategoryFilter filter = new ElementCategoryFilter();

public void Button_Click(object sender, RoutedEventArgs e)
    {
        string selected = AllTheCategories.SelectedItem.ToString();
        filter = new ElementCategoryFilter(selected);
    }


    
  • Thanks for providing code. Unfortunatelly it doesn't work. When hovering over "ElementCategoryFilter()" it says "ElementCategoryFilter does not contain a constructor that takes 0 arguments" and when hovering over "(selected)" it says "cannot convert from string to 'Autodesk.Revit.DB.BuiltinCategory' ". Maybe it is because I work inside xaml.cs file but I am not sure how to transfer this code to the "class.cs" file where all of my main commands are – fysharp Apr 29 '21 at 05:50
0

ElementCategoryFilter has 4 Constructors

ElementCategoryFilter(BuiltInCategory category)

ElementCategoryFilter(ElementId CategoryId)

and 2 more constructors which take the same parameters plus an additional boolean to invert the filter. Here you can find the documentation for it [reference link]: https://www.revitapidocs.com/2019/41234622-8696-4b43-5ffa-3d92567f8318.htm

0

You should use it like this.

    ElementCategoryFilter filter = new ElementCategoryFilter();

    public void Button_Click(object sender, RoutedEventArgs e)
    {
        // selected type should be Category
        var selected = AllTheCategories.SelectedItem;
        filter = new ElementCategoryFilter(selected.Id);
        
    }

There is another way you can get category id, by BuiltInCategory enum and Document

like this:

    Document doc =  /* some code to get document */;
    ElementId categoryId = 
    doc.Settings.Categories.get_Item(BuiltInCategory.OST_DuctAccessory /* or any category you want */).Id;
    ElementCategoryFilter collector = new ElementCategoryFilter(categoryId);
Kliment Nechaev
  • 480
  • 1
  • 6
  • 13