Create a View/Sheet Set


public void CreateSheetSet()
{
    Document doc = this.ActiveUIDocument.Document;

    // create a new ViewSet - add views to it that match the desired criteria
    ViewSet myViewSet = new ViewSet();
    string match = "S";
    foreach (ViewSheet vs in new FilteredElementCollector(doc).OfClass(typeof(ViewSheet)).Cast<ViewSheet>()
             .Where(q => q.SheetNumber.Contains(match)))
    {
        myViewSet.Insert(vs);
    }

    // get the PrintManger from the current document
    PrintManager printManager = doc.PrintManager;

    // set this PrintManager to use the "Selected Views/Sheets" option
    printManager.PrintRange = PrintRange.Select;

    // get the ViewSheetSetting which manages the view/sheet set information of current document
    ViewSheetSetting viewSheetSetting = printManager.ViewSheetSetting;

    // set the views in this ViewSheetSetting to the newly created ViewSet
    viewSheetSetting.CurrentViewSheetSet.Views = myViewSet;

    if (myViewSet.Size == 0)
    {
        TaskDialog.Show("Error", "No sheet numbers contain '" + match + "'.");
        return;
    }

    using (Transaction t = new Transaction(doc,"Create ViewSet"))
    {
        t.Start();
        string setName = "'" + match +"' Sheets";
        try
        {
            // Save the current view sheet set to another view/sheet set with the specified name.
            viewSheetSetting.SaveAs(setName);
        }
        // handle the exception that will occur if there is already a view/sheet set with this name
        catch (Autodesk.Revit.Exceptions.InvalidOperationException)
        {
            TaskDialog.Show("Error", setName + " is already in use");
            t.RollBack();
            return;
        }
        t.Commit();
    }
    TaskDialog.Show("View Set", myViewSet.Size + " sheets added to set");
}

20 thoughts on “Create a View/Sheet Set

  1. Your brilliant! I was looking for a way to do exactly this. But I was getting confused between all the ViewSet, ViewSheetSet, ViewSheetSets, and SheetSettings classes. Thanks!

  2. Harry,

    In this section:

    // handle the exception that will occur if there is already a view/sheet set with this name
    catch (Autodesk.Revit.Exceptions.InvalidOperationException)
    {
    TaskDialog.Show(“Error”, setName + ” is already in use”);
    t.RollBack();
    return;
    }

    Is it possible to get the code to update (or ask if you want to update) the sheet set with the new myViewSet so that if more sheets are added it can be updated easily? I’m thinking you would use the ViewSheetSetting.Delete method and then add it back. Thanks!

  3. Harry amazing methods, however i tried to develop it to make it suites my needs,, but i am still can’t get it right with the ViewSheetSetting.Delete method… can you please teach me how to call for a set and delete it? also what is the different between dispose and delete?

    • The ViewSheetSet class is a sub-class of Element, so you can get the elementId of the ViewSheetSet and then call the Document.Delete Method (ElementId). Dispose is for .NET memory management and is something you will probably never need to use.

  4. Hello,

    Very interesting solution.

    ItĀ“s possible to select via code to create sets using the sheet size (maybe using the name of type)?

    Thank you in advance.

  5. Admiral Mattison:

    I can understand if you’ve retired from coding for Revit, after that program’s owners ‘flattered’ you last month … I’d be very disgusted …

    However, if you haven’t retired, perhaps you can make an adjustment to this macro you have, then put the thing on Exchange, and copyright it.

    In Revit, we can make a special schedule called a Sheet List, which is just a schedule of sheets. Often, we make these sheet lists for drawing revisions, and it works well. However, once the Sheet List is made, and we want to print out that set of drawings, we have to manually add those sheets to a Sheet Set under the Print function. Is there any way to write a routine that will generate a Sheet Set (for printing) from a Sheet List?

  6. Hi Harry,
    I have created an add-in that collects ViewSheets and renumbers them. Unfortunately after the renumber operation the numbers have changed in the sheet properties and not in the project browser. I have tried to use doc.Regenrate(); This has no affect. Also If I renumber a sheet manually they update to the sheet numbers created using an add-in

    • Sounds like a bug if the Project Browser is not updating when the sheet numbers change. Does setting the active view to the new sheet (using UIDocument.ActiveView) help?

  7. Harry,

    Thanks for the great information. I have adapted what you have above to create sheet sets based on revisions (so that there is no chance of forgetting a sheet when manually picking them). It has always been an annoyance that Revit will order the sheets for printing based on the sheet number, so I figured I would order the sheets in my list before adding them to the viewset. However, it seems the ViewSet.Insert() method has some kind of sort on it, as the order I insert them is not the order they end up in (which matches the order they print in). Can you verify that there is not a way to change or turn off this default sorting?

    • Hi Paul,

      The API does not seem to have any way to specify where in the set the view is inserted. Maybe you could remove all views from the set and then add them all back in the order you want.

      Harry

      • Thanks for your quick reply. I actually am starting with a new (empty) ViewSet and add the sheets in the order I want them to print. But they are ordered differently than that which they are added, so it seems I am out of luck. I can find no indication of what the insert function actually does. I may change my sheets to use my own parameter for sheet number and use revits sheet number as a sequential print order.

  8. Has anyone had issues creating viewsheetsets in revit 2022. The set saves but the viewsets are always empty. Finding they are read only.

    • Hi Nick – The code in this post is working fine for me in 2022. I created a sheet named S101, ran the macro, and that sheet was added to a newly created sheet set named “‘S’ Sheets”

      • hi Harry, thanks for the comment! This is so weird, I have tried so many things including the example code above in versions 2019 to 2023 via my addins and macros and each time it will not save the sheets in the list. Saves a new set with a given name, but no sheets are selected when going back to the dialog. Doing my head in lol.

        • got it working, Revit seems very particular how the viewsheetsets are referenced when making the changes, and creating a variable for each object seemed to have done the trick.

Leave a reply to harrymattison Cancel reply