Any given Revit model can contain multiple sections and these sections can be represented in a number of views (for example – in various floor plans). Is there a way to use the API to extract the information of which section symbol is displayed in which view? The approach to handle this requirement would be to first list all the section symbols from the Revit model (using ElementFiltering) and then access the OwnerViewId() of each of the section symbol element. This will provide access to the ID of the view that owns/contains the section symbol element.
So far so good - but this helps only if the section symbol is present in only one view. What if same section is present in different views (floor plans)?
For this scenario, we will have a use a different approach. For this case, we will have to iterate through each view and get all the section symbols in each view. To do this, we can create a list of the views in the model (using element filtering) and then for each view, pass on the view id to the FilteredElementCollector to create a list of building section symbols. In the following code, for simplicity, I am only printing out the name of the building section in the active view. The key point to note here is to use the overload of FilteredElementCollector which accepts the view id for the element filtering.
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.ApplicationServices;
namespace HelloRevit.CS
{
[Transaction(TransactionMode.Manual)]
public class Command : IExternalCommand
{
public Result Execute(ExternalCommandData commandData,
ref string message,
ElementSet elements)
{
UIDocument uiDoc = commandData.Application.ActiveUIDocument;
UIApplication uiApp = uiDoc.Application;
FilteredElementCollector coll =
new FilteredElementCollector(
uiDoc.Document,
uiApp.ActiveUIDocument.Document.ActiveView.Id);
coll.OfCategory(BuiltInCategory.OST_Viewers);
var targetElems =
from element in coll
where uiDoc.Document.GetElement(
element.GetTypeId()).Name.Equals("Building Section")
select element;
IList<Element> elems = targetElems.ToList();
foreach (Element ele in elems)
{
TaskDialog.Show("Name", ele.Name);
}
return Result.Succeeded;
}
}
}