ViewSheet

ViewSheet

A sheet contains views and a title block. When creating a sheet view with the ViewSheet.Create() method, a title block family symbol Id is a required parameter for the method. A title block family symbol can be found using a FilteredElementCollector.

Code Region: ViewSheet.Create()

public static ViewSheet ViewSheet.Create(Document document, ElementId titleBlockTypeId);

The newly created sheet has no views. The Viewport.Create() method is used to add views. The Viewport class is used to add regular views to a view sheet, i.e. plan, elevation, drafting and three dimensional. To add schedules to a view, use ScheduleSheetInstance.Create() instead.

Code Region: Add two views aligned at left corner

public static void PlaceAlignedViewsAtLeftCorner(Document doc)
{
    FilteredElementCollector fec = new FilteredElementCollector(doc);
    fec.OfClass(typeof(ViewPlan));
    var viewPlans = fec.Cast<ViewPlan>().Where<ViewPlan>(vp => !vp.IsTemplate && vp.ViewType == ViewType.CeilingPlan);

    ViewPlan vp1 = viewPlans.ElementAt(0);
    ViewPlan vp2 = viewPlans.ElementAt(1);

    using (Transaction t = new Transaction(doc, "Place on sheet"))
    {
        t.Start();

        // Add two viewports distinct from one another
        ViewSheet vs = ViewSheet.Create(doc, ElementId.InvalidElementId);
        Viewport viewport1 = Viewport.Create(doc, vs.Id, vp1.Id, new XYZ(0, 0, 0));
        Viewport viewport2 = Viewport.Create(doc, vs.Id, vp2.Id, new XYZ(0, 5, 0));

        doc.Regenerate();

        // Calculate the necessary move vector to align the lower left corner
        Outline outline1 = viewport1.GetBoxOutline();
        Outline outline2 = viewport2.GetBoxOutline();
        XYZ boxCenter = viewport2.GetBoxCenter();
        XYZ vectorToCenter = boxCenter - outline2.MinimumPoint;
        XYZ newCenter = outline1.MinimumPoint + vectorToCenter;

        // Move the viewport to the new location
        viewport2.SetBoxCenter(newCenter);

        t.Commit();
    }
}

Each sheet has a unique sheet number in the complete drawing set. The number is displayed before the sheet name in the Project Browser. It is convenient to use the sheet number in a view title to cross-reference the sheets in your drawing set. You can retrieve or modify the number using the SheetNumber property. The number must be unique; otherwise an exception is thrown when you set the number to a duplicate value.

The following example illustrates how to create and print a sheet view. Begin by finding an available title block in the document (using a filter in this case) and use it to create the sheet view. Next, add a 3D view. The view is placed with its lower left-hand corner at the center of the sheet. Finally, print the sheet by calling the View.Print() method.

Code Region: Creating a sheet view

private void CreateSheetView(Autodesk.Revit.DB.Document document, View3D view3D)
{

    // Get an available title block from document
    FilteredElementCollector collector = new FilteredElementCollector(document);
    collector.OfClass(typeof(FamilySymbol));
    collector.OfCategory(BuiltInCategory.OST_TitleBlocks);

    FamilySymbol fs = collector.FirstElement() as FamilySymbol;
    if (fs != null)
    {
        using (Transaction t = new Transaction(document, "Create a new ViewSheet"))
        {
            t.Start();
            try
            {
                // Create a sheet view
                ViewSheet viewSheet = ViewSheet.Create(document, fs.Id);
                if (null == viewSheet)
                {
                    throw new Exception("Failed to create new ViewSheet.");
                }

                // Add passed in view onto the center of the sheet
                UV location = new UV((viewSheet.Outline.Max.U - viewSheet.Outline.Min.U) / 2,
                                        (viewSheet.Outline.Max.V - viewSheet.Outline.Min.V) / 2);

                //viewSheet.AddView(view3D, location);
                Viewport.Create(document, viewSheet.Id, view3D.Id, new XYZ(location.U, location.V, 0));

                // Print the sheet out
                if (viewSheet.CanBePrinted)
                {
                    TaskDialog taskDialog = new TaskDialog("Revit");
                    taskDialog.MainContent = "Print the sheet?";
                    TaskDialogCommonButtons buttons = TaskDialogCommonButtons.Yes | TaskDialogCommonButtons.No;
                    taskDialog.CommonButtons = buttons;
                    TaskDialogResult result = taskDialog.Show();

                    if (result == TaskDialogResult.Yes)
                    {
                        viewSheet.Print();
                    }
                }

                t.Commit();
            }
            catch
            {
                t.RollBack();
            }
        }
    }
}
Note: You cannot add a sheet view to another sheet and you cannot add a view to more than one sheet; otherwise an argument exception occurs.

Revisions on Sheets

The ViewSheet class has several methods for working with revision and revision clouds on sheets.
  • GetAllRevisionIds()- Gets the ordered array of Revisions which participate in the sheet's revision schedules.
  • GetAdditionalRevisionIds()- Gets the Revisions that are additionally included in the sheet's revision schedules.
  • SetAdditionalRevisionIds()- Sets the Revisions to additionally include in the sheet's revision schedules.
  • GetCurrentRevision()- Returns the most recent numbered Revision shown on this ViewSheet.
  • GetRevisionCloudNumberOnSheet()- Gets the Revision Number for a RevisionCloud on this sheet when the numbering in the project is by sheet.
  • GetRevisionNumberOnSheet() - Gets the Revision Number for a particular Revision as it will appear on this sheet when the numbering in the project is by sheet.

The Revisions are ordered according to the revision sequence in the project. Additionally included Revisions will always participate in the sheet's revision schedules. Normally a Revision is scheduled in the revision schedule because one of its associated RevisionClouds is present on the sheet.

The following code sample demonstrates how to add additional revisions to the sheet that match a given criteria.

Code Region: Add additional revisions to sheet

public static void AddAdditionalRevisionsToSheet(ViewSheet viewSheet, String toMatch)
{
    Document doc = viewSheet.Document;

    ICollection<ElementId> revisions = viewSheet.GetAdditionalRevisionIds();

    // Find revisions whose description matches input string
    FilteredElementCollector collector = new FilteredElementCollector(doc);
    collector.OfCategory(BuiltInCategory.OST_Revisions);
    collector.WhereElementIsNotElementType();
    if (revisions.Count > 0)
        collector.Excluding(revisions);

    // Check if revision should be added
    foreach (Element revision in collector)
    {
        Parameter descriptionParam = revision.get_Parameter(BuiltInParameter.PROJECT_REVISION_REVISION_DESCRIPTION);
        String description = descriptionParam.AsString();
        if (description.Contains(toMatch))
            revisions.Add(revision.Id);
    }

    if (revisions.Count > 0)
    {
        // Apply the new list of revisions
        using (Transaction t = new Transaction(doc, "Add revisions to sheet"))
        {
            t.Start();
            viewSheet.SetAdditionalRevisionIds(revisions);
            t.Commit();
        }
    }
}

Printer Setup

You may want to change the settings of the printer before printing a sheet. The API exposes the settings for the printer with the PrintManager class, and related Autodesk.Revit.DB classes:

Class

Functionality

Autodesk.Revit.DB.PrintManager

Represents the Print information in Print Dialog (File->Print) within the Revit UI.

Autodesk.Revit.DB.PrintParameters

An object that contains settings used for printing the document.

Autodesk.Revit.DB.PrintSetup

Represents the Print Setup (File->Print Setup...) within the Revit UI.

Autodesk.Revit.DB.PaperSize

An object that represents a Paper Size of Print Setup within the Autodesk Revit project.

Autodesk.Revit.DB.PaperSizeSet

A set that can contain any number of paper size objects.

Autodesk.Revit.DB.PaperSource

An object that represents a Paper Source of Print Setup within the Autodesk Revit project.

Autodesk.Revit.DB.PaperSourceSet

A set that can contain any number of paper source objects.

Autodesk.Revit.DB.ViewSheetSetting

Represents the View/Sheet Set (File->Print) within the Revit UI.

Autodesk.Revit.DB.PrintSetting

Represents the Print Setup (File->Print Setup...) within the Revit UI.

For an example of code that uses these objects, see the ViewPrinter sample application that is included with the Revit Platform SDK.