Question

I am relatively new at programming in Revit. I am currently getting a list of elements in my drawing that are of type door or window. What I want to do is cast these as an opening however I get an error when I try to cast them as a Autodesk.Revit.DB.Opening.
Code Below:

            // filter for current design option
        var designOptionFilter = S2E.Revit.Tools.Library.Cache.DesignOptionFilter;

        List<Element> elements = collector.WherePasses(designOptionFilter).ToElements().ToList();           

        var list = new List<Autodesk.Revit.DB.Opening>();

        foreach (var element in elements) {

            var opening = (Opening)element;
            if (opening.Host.Id == wallId) {
                list.Add(opening);
            }
        }

        return list;

As you can see I am testing if the id of the host matches the wall I am woking on. At least that is what I would like to do. All I am looking for is how to cast an element as an Opening.

Thanks, Rich

Was it helpful?

Solution

Considering a Door is a FamilyInstance and an Opening is not, I am unsure of how you would cast the door FamilyInstance to an opening type.

But, since the FamilyInstance has a Host parameter, just check that against the wall ID and it should work, no casting needed.

OTHER TIPS

It depends on what your filter is selecting on whether the cast you have will work. As you don't provide details on the exact error you are getting it is hard to be more precise.

Also you have in one place (Opening) used as your cast yet you use the full type name "Autodesk.Revit.DB.Opening" when you create your list. If you really need to do that maybe Opening is not the "Opening" you thought it was.

You can also use element.Cast() to perform the cast.

Likewise if you know that all your elements returned by the filter are only ever going to be Opening types then you can use

collector.WherePasses(designOptionFilter).Cast<Opening>()

to achieve the same thing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top