Casting Element as a view (whose category is BuiltInCategory.OST_Viewers)

Casting Element as a view (whose category is BuiltInCategory.OST_Viewers)

EATREVITPOOPCAD
Collaborator Collaborator
492 Views
3 Replies
Message 1 of 4

Casting Element as a view (whose category is BuiltInCategory.OST_Viewers)

EATREVITPOOPCAD
Collaborator
Collaborator

My understanding is that BuiltInCategory.OST_Viewers is a category for Views. However, having an issue with casting the element as a View.

 

                    if (sourceElement.Category?.Id.IntegerValue == (int)BuiltInCategory.OST_Viewers)
                    {
                        TaskDialog.Show("News", "Its a view!");

                        View v = sourceElement as View;

                        if (v.ViewType == ViewType.Section)
                        {
                            TaskDialog.Show("News", "Its a section!");
                        }

                        if (v.ViewType == ViewType.Elevation)
                        {
                            TaskDialog.Show("News", "Its an Elevation!");
                        }
                    }

 

This code works by letting me know "It's a view!" but fails at letting me know if it is a section or an elevation. I get an exception saying can't cast Element as a View. (If I do the (View) cast at least, in the example above v is null)

 

 

The definition of insanity is doing the same thing over and over again and expecting different results
0 Likes
Accepted solutions (1)
493 Views
3 Replies
Replies (3)
Message 2 of 4

jeremy_tammik
Alumni
Alumni
Accepted solution

Yes, indeed. Unfortunately, your assumption is wrong. Revit element categories mostly apply to both element instances and element types. For instance the Wall category includes Wall elements as well as WallType elements. So, for views, the Views category probably includes both different types of views as well as view types. Snooping your BIM database using RevitLookup and other API-based exploration tools provides the safest and most direct way to determine what elements you are dealing with, their category, properties and relationships with other elements.

  

To filter for a section view, for instance, you can also check the element .NET class in addition to its category, and restrict the filter to ViewSection objects.

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
Message 3 of 4

EATREVITPOOPCAD
Collaborator
Collaborator

Thanks for explaining that. I started with a user-selected object so didn't do the filter. Found 'Family' parameter had what I needed.


 

if (sourceElement.Category?.Id.IntegerValue == (int)BuiltInCategory.OST_Viewers)
{
    TaskDialog.Show("News", "It's a view!");

    Parameter familyParam = sourceElement.LookupParameter("Family");
    if (familyParam != null)
    {
        string familyValue = familyParam.AsValueString();

        if (familyValue == "Section")
        {
            TaskDialog.Show("News", "It's a Section!");
        }

        if (familyValue == "Elevation")
        {
            TaskDialog.Show("News", "Its an Elevation!");
        }      
    }
}
The definition of insanity is doing the same thing over and over again and expecting different results
Message 4 of 4

jeremy_tammik
Alumni
Alumni

Cool. If your intention is to select a specific type of object, e.g., a specific view type, you can also add a selection filter argument to the PickObject call:

  

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open