RevitLookup.Test.TestElements.ViewToNewSheetHardwired C# (CSharp) Method

ViewToNewSheetHardwired() public method

Add the Level 1 (type = Floor Plan) view to a newly created sheet.
public ViewToNewSheetHardwired ( ) : void
return void
        public void ViewToNewSheetHardwired()
        {
            Revit.Document doc = m_revitApp.ActiveUIDocument.Document;

              // Collect all available views
              ViewSet allViews = Utils.View.GetAllViews( doc );

              // Collection of views that have been added to existing sheets.
              ViewSet usedViews = new ViewSet();

              // Collect all the existing sheets. It would be nice to have some API to do this.
              Revit.ElementSet allSheets = Utils.View.GetAllSheets( doc );

              // Create a new sheet
              ViewSheet sheet = ViewSheet.Create( doc, new FilteredElementCollector( doc ).OfClass( typeof( FamilySymbol ) ).OfCategory( BuiltInCategory.OST_TitleBlocks ).Cast<FamilySymbol>().LastOrDefault().Id );

              Random rnd = new Random();
              String name = "RevitLookup_Sheet" + rnd.Next().ToString();
              sheet.Name = name;

              foreach( ViewSheet viewSheet in allSheets )
              {
            // jeremy migrated from Revit 2014 to 2015:
            // 'Autodesk.Revit.DB.ViewSheet.Views' is obsolete: 'This property is obsolete in Revit 2015.  Use GetAllPlacedViews() instead.'
            //foreach( Autodesk.Revit.DB.View usedView in viewSheet.Views )

            foreach( ElementId id in viewSheet.GetAllPlacedViews() )
            {
              Autodesk.Revit.DB.View usedView = doc.GetElement( id )
            as Autodesk.Revit.DB.View;

              usedViews.Insert( usedView );
            }
              }

              // Note: The application does not allow the addition of a view to a new sheet if that view has already been added to
              // an existing sheet. I realized this when a programmatic attempt to do so resulted in an exception being thrown.
              // So running this hardwired test a second time would result in an exception (As "Level 1 (FloorPlan) would have already been added)".
              // The workaround that has been used here is that first all the available sheets and their added views are gathered.
              // Before adding a view it is compared with the used views. arj - 1/17/07
              //
              foreach( Autodesk.Revit.DB.View view in allViews )
              {
            // Has been hardwired for the following view.
            if( ( view.ViewName == "Level 1" ) && ( view.ViewType == ViewType.FloorPlan ) )
            {
              UV point = view.Outline.Max;

              // Display notification message if view is already being used.
              foreach( Autodesk.Revit.DB.View usedView in usedViews )
              {
            if( ( usedView.ViewName == view.ViewName ) && ( usedView.ViewType == view.ViewType ) )
            {
              MessageBox.Show( "View already added to an existing sheet", "RevitLookup Test", MessageBoxButtons.OK, MessageBoxIcon.Information );
              return;
            }
              }
              Viewport.Create( view.Document, sheet.Id, view.Id, new XYZ( point.U, point.V, 0 ) );
            }
              }
        }