Skip to content

Instantly share code, notes, and snippets.

@yiskang
Created June 13, 2019 03:16
Show Gist options
  • Save yiskang/feeb055ca27d1a143c8c269499f5ee24 to your computer and use it in GitHub Desktop.
Save yiskang/feeb055ca27d1a143c8c269499f5ee24 to your computer and use it in GitHub Desktop.
Find view plans where the section annotations appear in Revit
// (C) Copyright 2019 by Autodesk, Inc.
//
// Permission to use, copy, modify, and distribute this software
// in object code form for any purpose and without fee is hereby
// granted, provided that the above copyright notice appears in
// all copies and that both that copyright notice and the limited
// warranty and restricted rights notice below appear in all
// supporting documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK,
// INC. DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL
// BE UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is
// subject to restrictions set forth in FAR 52.227-19 (Commercial
// Computer Software - Restricted Rights) and DFAR 252.227-7013(c)
// (1)(ii)(Rights in Technical Data and Computer Software), as
// applicable.
//
// Revit CmdSectionAnnotationInViews
// by Eason Kang - Autodesk Forge & Autodesk Developer Network (ADN)
//
#region Namespaces
using System;
using System.Diagnostics;
using System.Collections.Generic;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.Exceptions;
using System.Linq;
#endregion
namespace CmdSectionAnnotationInViews
{
public class SectionRecord
{
public ElementId Section { get; set; }
public ElementId FloorPlanView { get; set; }
}
[Transaction(TransactionMode.Manual)]
public class Command : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
var uiapp = commandData.Application;
var app = uiapp.Application;
var uidoc = uiapp.ActiveUIDocument;
var doc = uidoc.Document;
// Find section views in current RVT document
var sectionViews = new FilteredElementCollector(doc)
.WhereElementIsNotElementType()
.OfCategory(BuiltInCategory.OST_Views)
.OfClass(typeof(ViewSection))
.Cast<ViewSection>()
.Where(v =>
{
var viewFamilyType = doc.GetElement(v.GetTypeId()) as ViewFamilyType;
if (viewFamilyType != null && viewFamilyType.ViewFamily == ViewFamily.Section)
return true;
return false;
});
var sectionViewMap = new Dictionary<ElementId, List<SectionRecord>>();
foreach (var sectionView in sectionViews)
{
sectionViewMap.Add(sectionView.Id, new List<SectionRecord>());
}
// Find floor plan views in current RVT document
var floorPlans = new FilteredElementCollector(doc)
.WhereElementIsNotElementType()
.OfCategory(BuiltInCategory.OST_Views)
.OfClass(typeof(ViewPlan))
.Cast<ViewPlan>()
.Where(v => !v.IsTemplate);
// Find possible section annoation inside each floor plan view, and record where it appears
foreach (var floorPlan in floorPlans)
{
var collector = new FilteredElementCollector(doc, floorPlan.Id)
.WhereElementIsNotElementType()
.OfCategory(BuiltInCategory.OST_Viewers);
foreach(var section in collector)
{
var viewFamilyType = doc.GetElement(section.GetTypeId()) as ViewFamilyType;
if (viewFamilyType != null && viewFamilyType.ViewFamily == ViewFamily.Section)
{
var viewParam = section.get_Parameter(BuiltInParameter.REFERENCED_VIEW);
if (viewParam == null)
continue;
var sectionView = doc.GetElement(viewParam.AsElementId());
if (sectionView == null)
continue;
var sectionRecords = sectionViewMap[sectionView.Id];
if (sectionRecords == null)
continue;
sectionRecords.Add(new SectionRecord
{
Section = section.Id,
FloorPlanView = floorPlan.Id
});
}
}
}
// Print result
var msg = string.Empty;
var secionViewIds = sectionViewMap.Keys;
foreach (var id in secionViewIds)
{
var sectionView = doc.GetElement(id) as ViewSection;
var sectionRecords = sectionViewMap[id];
if (sectionRecords.Count <= 0)
{
msg += string.Format("Associated views of Section `{0} ({1})` not found\n", sectionView.Name, sectionView.Id);
}
else
{
msg += string.Format("Section `{0} ({1})` appeared in:\n", sectionView.Name, sectionView.Id);
foreach (var record in sectionRecords)
{
var view = doc.GetElement(record.FloorPlanView) as ViewPlan;
var section = doc.GetElement(record.Section);
msg += string.Format("\t\t\t\t\t\t- {0} {1} ({2}) - Annotation ({3})\n", view.ViewType, view.Name, view.Id, section.Id);
}
}
}
TaskDialog.Show("Revit", msg);
return Result.Succeeded;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment