stuck in BuiltInCategory enum

stuck in BuiltInCategory enum

ahmed.samy661993
Contributor Contributor
1,577 Views
3 Replies
Message 1 of 4

stuck in BuiltInCategory enum

ahmed.samy661993
Contributor
Contributor

i have problem in convert 'user category selected' to 'item in BuiltInCategory enum'

thanks for your help ...

 

Capture1.PNG

 

Capture2.PNG

 

Capture3.PNG

 

0 Likes
Accepted solutions (1)
1,578 Views
3 Replies
Replies (3)
Message 2 of 4

Omar_Amen
Advocate
Advocate
Accepted solution

Hi Ahmed,
as shown in your screenshots, form1.selectedcategory is a String type variable,
but the ElementCategoryFilter() Constructor takes a BuiltInCategory enum value!
so what you need is to convert your string to a BuiltInCategory  enum value,
you can try this:

 

 

BuiltInCategory BIC = (BuiltInCategory) BuiltInCategory.Parse(typeof(BuiltInCategory), form1.selectedcategory) ;
ElementCategoryFilter f = new ElementCategoryFilter(BIC);

 

 


Note that form1.selectedcategory must be equal to the enum string value not the direct Revit category name, e.g.
Revit category name : "StructuralFraming"
enum string value : "OST_StructuralFraming"
you must use "OST_StructuralFraming" not "StructuralFraming"!
to get that you can make a dictionary to convert the value of the string
this link will help you a lot to achieve this:
 https://docs.google.com/spreadsheets/.... 

I hope that will help you! 

Message 3 of 4

guillain.jolivet
Contributor
Contributor

what kind of string do you use in your ComboBox? (ex : "OST_Walls" or "Walls"?). If you use the first one, you have to parse the built in category like @Omar_Amen said.  If you use the second one (category name), you can iterate over the built in category enumeration, get their names and check if it's the same as the combo box value.

 

 

 

BuiltInCategory BuiltInCategoryFromName(Document doc, string category_name)
{
    BuiltInCategory ost = BuiltInCategory.INVALID;
    Categories categories = doc.Settings.Categories;
    foreach (BuiltInCategory bic in System.Enum.GetValues(typeof(BuiltInCategory)))
    {
        try
        {         
            if (categories.get_Item(bic).Name == category_name)
            {
                ost = bic;
                break;
            }
        }
        catch
        {
        }
    }
    return ost;
}

 

Message 4 of 4

ahmed.samy661993
Contributor
Contributor

it's working

Thank you dear omaramen