简体   繁体   English

按类别匹配元素

[英]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".一旦选择了一个类别,我们点击按钮,类别的名称“进入”这个名为“selected”的变量。 Now, how to put inside of ElementCategoryFilter that variable containing the necessary category?现在,如何将包含必要类别的变量放入 ElementCategoryFilter 中? 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.您可以在Button_Click事件中直接将所选字符串分配给 ElementCategoryFilter。

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 有 4 个构造函数

ElementCategoryFilter(BuiltInCategory category)

ElementCategoryFilter(ElementId CategoryId)

and 2 more constructors which take the same parameters plus an additional boolean to invert the filter.和另外 2 个构造函数,它们采用相同的参数加上一个额外的 boolean 来反转过滤器。 Here you can find the documentation for it [reference link]: https://www.revitapidocs.com/2019/41234622-8696-4b43-5ffa-3d92567f8318.htm在这里您可以找到它的文档 [参考链接]: 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.如果在将 AllTheCategories.SelectedItem 转换为字符串之前,它的类型是 Autodesk.Revit.DB.Category,那么您应该像这样使用它。

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.如果不是,那么我猜您正在访问 ComboBox.SelectedItem 属性,这是错误的方法,您应该检查 MVVM 模式和数据绑定。

However, there is another way you can get category id it is by BuiltInCategory enum and Document但是,还有另一种获取类别 ID 的方法是通过 BuiltInCategory 枚举和 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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