Get all Views containing a FamilyInstance

Get all Views containing a FamilyInstance

DanielKP2Z9V
Advocate Advocate
271 Views
1 Reply
Message 1 of 2

Get all Views containing a FamilyInstance

DanielKP2Z9V
Advocate
Advocate

I'm trying to list all views containing a family instance. I was hoping to obtain viewIds with something like that:

 

instance.GetValidViewIds()

 

or

 

IEnumerable<ElementId> viewIds = instance.GetValidTypes().SelectMany(t => instance.GetValidTypesInView(t));

 

but none of these methods exist. Is there another way to get viewIds?

 

Full code so far:

 

[Transaction(TransactionMode.Manual)]
[Regeneration(RegenerationOption.Manual)]
public class family_refs : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
// Get the active Revit application
UIApplication uiApp = commandData.Application;
UIDocument uiDoc = uiApp.ActiveUIDocument;
Document doc = uiDoc.Document;

// Select a family instance
Reference pickedRef = uiDoc.Selection.PickObject(ObjectType.Element, "Select a family instance");
Element selectedElement = doc.GetElement(pickedRef);

// Get the family type of the selected instance
FamilyInstance selectedInstance = selectedElement as FamilyInstance;
FamilySymbol familySymbol = selectedInstance.Symbol;

// Find all views where instances of the selected family type are placed
List<Autodesk.Revit.DB.View> views = new List<Autodesk.Revit.DB.View>();
FilteredElementCollector collector = new FilteredElementCollector(doc);
ICollection<Element> instancesOfType = collector.OfCategory(BuiltInCategory.OST_GenericModel).OfClass(typeof(FamilyInstance)).WhereElementIsNotElementType().ToList();
foreach (FamilyInstance instance in instancesOfType)
{
if (instance.Symbol.Id == familySymbol.Id)
{
// Get all views where the current instance is visible
// doesn't work
IEnumerable<ElementId> viewIds = instance.GetValidTypes().SelectMany(t => instance.GetValidTypesInView(t));

foreach (ElementId viewId in selectedElement.GetValidViewIds())
{
var view = doc.GetElement(viewId) as Autodesk.Revit.DB.View;
if (view != null && !views.Contains(view))
{
views.Add(view);
}
}
}
}

// Display the list of views in a WinForms ListBox
if (views.Count > 0)
{
var viewListForm = new System.Windows.Forms.Form();
viewListForm.Text = "Views Containing Instances of Selected Family Type";
ListBox listBox = new ListBox();
listBox.Dock = DockStyle.Fill;
foreach (Autodesk.Revit.DB.View view in views)
{
listBox.Items.Add(view.Name);
}
viewListForm.Controls.Add(listBox);
viewListForm.ShowDialog();
}
else
{
TaskDialog.Show("No Views Found", "No views contain instances of the selected family type.");
}

return Result.Succeeded;
}
}

0 Likes
272 Views
1 Reply
Reply (1)
Message 2 of 2

jeremy_tammik
Alumni
Alumni

You should be able to use a filtered element collector to retrieve all elements in a view, and thus check if a given view contains any family instances like this:

  

if( 0 
  < new FilteredElementCollector( doc, view.id)
    .OfClass(typeof(FamilyInstance))
    .GetElementCount())

  

Iterate over all views and pick the ones that return a family instance element  count larger than zero.

  

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