Matching elements by their category

630 views Asked by At

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();
3

There are 3 answers

1
Muhammed Ahsan On

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);
    }


    
0
Mostafa Tarek Yassien On

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
Kliment Nechaev On

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);