public Result Execute(
ExternalCommandData commandData,
ref string message,
ElementSet elements)
{
UIApplication app = commandData.Application;
Document doc = app.ActiveUIDocument.Document;
FilteredElementCollector sheets
= new FilteredElementCollector( doc );
sheets.OfClass( typeof( ViewSheet ) );
// map with key = sheet element id and
// value = list of viewport element ids:
Dictionary<ElementId, List<ElementId>>
mapSheetToViewport =
new Dictionary<ElementId, List<ElementId>>();
// map with key = viewport element id and
// value = sheet element id:
Dictionary<ElementId, ElementId>
mapViewportToSheet =
new Dictionary<ElementId, ElementId>();
foreach( ViewSheet sheet in sheets )
{
//int n = sheet.Views.Size; // 2014
ISet<ElementId> viewIds = sheet.GetAllPlacedViews(); // 2015
int n = viewIds.Count;
Debug.Print(
"Sheet {0} contains {1} view{2}: ",
Util.ElementDescription( sheet ),
n, Util.PluralSuffix( n ) );
ElementId idSheet = sheet.Id;
int i = 0;
foreach( ElementId id in viewIds )
{
View v = doc.GetElement( id ) as View;
BoundingBoxXYZ bb;
bb = v.get_BoundingBox( doc.ActiveView );
Debug.Assert( null == bb,
"expected null view bounding box" );
bb = v.get_BoundingBox( sheet );
Debug.Assert( null == bb,
"expected null view bounding box" );
Element viewport = GetViewport( sheet, v );
// null if not in active view:
bb = viewport.get_BoundingBox( doc.ActiveView );
BoundingBoxUV outline = v.Outline;
Debug.WriteLine( string.Format(
" {0} {1} bb {2} outline {3}",
++i, Util.ElementDescription( v ),
(null == bb ? "<null>" : Util.BoundingBoxString( bb )),
Util.BoundingBoxString( outline ) ) );
if( !mapSheetToViewport.ContainsKey( idSheet ) )
{
mapSheetToViewport.Add( idSheet,
new List<ElementId>() );
}
mapSheetToViewport[idSheet].Add( v.Id );
Debug.Assert(
!mapViewportToSheet.ContainsKey( v.Id ),
"expected viewport to be contained"
+ " in only one single sheet" );
mapViewportToSheet.Add( v.Id, idSheet );
}
}
return Result.Cancelled;
}