Skip to content

Instantly share code, notes, and snippets.

@yiskang
Created May 31, 2019 03:20
Show Gist options
  • Save yiskang/889ef689c79df12e3d5ee24b59d9e445 to your computer and use it in GitHub Desktop.
Save yiskang/889ef689c79df12e3d5ee24b59d9e445 to your computer and use it in GitHub Desktop.
// (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
{
[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 sectionMap = new Dictionary<ElementId, List<ElementId>>();
foreach (var sectionView in sectionViews)
{
sectionMap.Add(sectionView.Id, new List<ElementId>());
}
// 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 map = sectionMap[sectionView.Id];
if (map != null)
map.Add(floorPlan.Id);
}
}
}
// Print result
var msg = string.Empty;
var secionIds = sectionMap.Keys;
foreach (var id in secionIds)
{
var section = doc.GetElement(id) as ViewSection;
var viewIds = sectionMap[id];
if (viewIds.Count <=0)
{
msg += string.Format("Associated views of Section `{0} ({1})` not found\n", section.Name, section.Id);
}
else
{
msg += string.Format("Section `{0} ({1})` appeared in:\n", section.Name, section.Id);
foreach (var vid in viewIds)
{
var view = doc.GetElement(vid) as ViewPlan;
msg += string.Format("\t\t\t\t\t\t- {0} View {1} ({2})\n", view.ViewType, view.Name, view.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