Search code examples
c#revit-apirevit

Determining If Element is In Dependent View (Revit API)


I'm trying to write a macro for Revit 2020 and 2018 in C#. So far, I have written a program that takes selected elements and filters them if they are within a particular view. However, issues arise when using dependent views because when selecting all elements in a view, it also selects everything in the other dependent views. I don't want this to happen. I'd like to find a way of filtering elements that are only within one particular dependent view.

I've tried having the user select from all available views, but it still treats a dependent view as though it were the larger superview.

The easiest way to solve this would be if there were a way to check membership to a particular view. However, I haven't figured out how to do this. Any suggestions?


Solution

  • Copied from Tray Gates' answer to check if the parameter exist - check if the view is dependent or callout:

    You can check to see if a view is a dependent view using GetPrimaryViewId from a view element.

    If the result is -1, it is NOT a dependent view.

    If it is any other integer, it is a dependent.

    Here is an example:

      var views = new FilteredElementCollector(doc)
        .OfClass(typeof(View));
    
      foreach (View view in views)
      {
        ElementId parentId = view.GetPrimaryViewId();
    
        if (parentId.IntegerValue == -1 && !view.IsTemplate)
        {
          // View is NOT a dependent
        }
        else if (parentId.IntegerValue != -1 && !view.IsTemplate)
        {
          // View is dependent
        }
      }
    

    Note that -1 is shorthand for ElementId.InvalidElementId.

    Later: I see that you asked the same question in the Revit API forum thread on Determining If Element is in Dependent View, received other answers there, and discovered that your need is in fact different and more complicated...