In response to the comment on this blog-post where there was a query on how to hide sections in all the plan views using the API, I wrote this short code snippet to illustrate the workflow:
As a first step, we need to filter out the plan views. Then on each plan view, we filter the sections created on them. This is done by checking the elements of category OST_Viewers and their Type Name to be Building Section. With this list of Ids of Sections on a given plan view, we can simply call HideElements() to hide the sections on that plan view.
The code snippet is included here:
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
namespace Revit.SDK.Samples.HelloRevit.CS
{
[Transaction(TransactionMode.Manual)]
public class Command : IExternalCommand
{
public Result Execute(ExternalCommandData commandData,
ref string message,
ElementSet elements)
{
UIApplication uiApp = commandData.Application;
Document doc = uiApp.ActiveUIDocument.Document;
// Filter out the ViewPlans
FilteredElementCollector collector =
new FilteredElementCollector(doc);
collector.OfClass(typeof(ViewPlan));
using (Transaction trans =
new Transaction(doc, "Dimension"))
{
trans.Start();
foreach (ViewPlan v in collector)
{
// Ensure it is a real viewplan
if (v != null && !v.IsTemplate)
{
// Filter out Sections
FilteredElementCollector sections =
new FilteredElementCollector(doc, v.Id).
OfCategory(BuiltInCategory.OST_Viewers);
// Check for the Type name of section to
// be Building Section
IEnumerable<ElementId> elemIds =
from Element d in sections
where doc.GetElement(
d.GetTypeId()).Name.Equals(
"Building Section")
select d.Id;
// Convert Enumerable to Collection
ICollection<ElementId> elemIdsColl =
(ICollection<ElementId>)
elemIds.Cast<ElementId>().ToList();
// If there are sections to hide ...
if (elemIdsColl.Count > 0)
// ... go ahead
v.HideElements(elemIdsColl);
// Clear the collection for next ViewPlan
elemIdsColl.Clear();
}
}
// Commit the changes
trans.Commit();
}
return Result.Succeeded;
}
}
}