Strange behavior between List<BuiltInCategory> vs BuiltInCategory[]

Strange behavior between List<BuiltInCategory> vs BuiltInCategory[]

Mr.Borges
Enthusiast Enthusiast
184 Views
2 Replies
Message 1 of 3

Strange behavior between List<BuiltInCategory> vs BuiltInCategory[]

Mr.Borges
Enthusiast
Enthusiast

I've found a strange behavior between List and Array of Enums, not sure if it a c# issue or API issue, I have the below function:

        public static IList<Element> GetElements(this Document document,
                                                 IEnumerable<BuiltInCategory> categories)
        {
            List<BuiltInCategory> categoryList = categories.ToList();
            var multiCategoryFilter = new ElementMulticategoryFilter(categoryList);
            var collector = new FilteredElementCollector(document);

            var elements = collector
                           .WhereElementIsNotElementType()
                           .WherePasses(multiCategoryFilter)
                           .ToElements();

            return elements;
        }

 If I call it like this, it doesn't works, neither rise any exception:

 (But it should works, as Arrays implements IEnumerable)

var myElements = document.GetElements(new BuiltInCategory[] { BuiltInCategory.OST_Rebar, BuiltInCategory.OST_Walls });

 

 If I call it like this, it  works:

var myElements = document.GetElements(new List<BuiltInCategory> { BuiltInCategory.OST_Rebar, BuiltInCategory.OST_Walls });

I've checked the debug information at categoryList = categories.ToList() for the array version, and the Value of items is a number (the Enum int) instead of something like this: OST_Rebar as shown for the List version. Tested in .NetFramework version of API.

Any thoughts?

0 Likes
185 Views
2 Replies
Replies (2)
Message 2 of 3

longt61
Advocate
Advocate

Strange, I tried your code, called it as normal method as well as extension method, and it worked fine. Maybe this is some magic with the memory of your PC, perhaps.

As for the debug value, they are all display as text values of BuiltinCategory enumeration. 

0 Likes
Message 3 of 3

ricaun
Advisor
Advisor

What version of Revit API are you using? 

 

The only thing I know that create problem with array and BuiltInCategory is related with Revit 2024 API that changes BuiltInCategory to long. In older versions BuiltInCategory is a int.

 

And if you build your dll with Revit 2023 reference and load the dll inside Revit 2024 could create problems, don't know if that is your issue.

Luiz Henrique Cassettari

ricaun.com - Revit API Developer

AppLoader EasyConduit WireInConduit ConduitMaterial CircuitName ElectricalUtils

0 Likes