Get all Views containing a FamilyInstance
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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;
}
}