简体   繁体   中英

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

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


    

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

If before converting AllTheCategories.SelectedItem to string, its type was Autodesk.Revit.DB.Category then 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);
    
}

If not then i guess you are accessing ComboBox.SelectedItem property and this is a wrong way to do this, and you should checkout MVVM pattern and data binding.

However, there is another way you can get category id it is 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM