'Matching elements by their category
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();
Solution 1:[1]
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);
}
Solution 2:[2]
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
Solution 3:[3]
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);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Muhammed Ahsan |
Solution 2 | Mostafa Tarek Yassien |
Solution 3 |