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");
}
Advertisements

Print Sheets from Host & Linked RVT files

Jay suggested that it would be great to be able to print sheets from a linked RVT while in the host RVT.

Here’s a bit of code showing how the API can print the sheets both in the host file and the linked site file.

public void printLinked()
{
    Application app = this.Application;
    foreach (Document doc in app.Documents)
    {
       PrintManager printManager = doc.PrintManager;
       printManager.PrintToFile = true;

       printManager.SelectNewPrintDriver("CutePDF Writer");

        foreach (ViewSheet vs in new FilteredElementCollector(doc).OfClass(typeof(ViewSheet)).Cast<ViewSheet>())
        {
            printManager.PrintToFileName = @"C:\OutputFolder\" + Path.GetFileName(doc.PathName) + " " + vs.Name + " " + vs.SheetNumber + ".";
			printManager.SubmitPrint(vs);
		}
	}
}

Making Lines Not Print Improved (View Viz instead of Line Color)

David makes a great observation regarding my post at https://boostyourbim.wordpress.com/2012/12/12/making-lines-not-print-with-events/

‘That all works great if the background is in fact white. What happens if you have the same situation you were showing above with a floor tile pattern? You’d see the white line.”

The code I wrote in that post was I hope good for a relatively gentle introduction to the concept of Revit API Events. But this is not what anyone wants to see when they print.

whiteonpattern

I used the approach of changing the Object Styles – Line Color to white because it offered a straightforward way to globally change the appearance of the lines in all views. A better approach that requires greater API complexity could be to turn off the visibility of the Do Not Print sub-category in each view that is being printed.

vis

In the previous post’s code, myPrintingEvent and myPrintedEvent both had input arguments DocumentPrintingEventArgs and DocumentPrintedEventArgs that went unused. Every event has arguments like these that are used to pass data about what is happening in this event. For the printing events, this information includes the list of views to be printed and the list of views that were printed. This will be needed for the new approach of changing  category visibility for each view that is printed.

The structure of the code is the same as the previous post, but the CategoryLineColor method has been replaced with categoryVisInViews which uses View.SetVisibility(category, boolean).

public void PrintingEventRegister()
{
    // myPrintingEvent will run just BEFORE Revit prints
    this.Application.DocumentPrinting += new EventHandler<DocumentPrintingEventArgs>(myPrintingEvent);

    // myPrintedEvent will run just AFTER Revit prints
    this.Application.DocumentPrinted += new EventHandler<DocumentPrintedEventArgs>(myPrintedEvent);
}

private void myPrintingEvent(object sender, DocumentPrintingEventArgs args)
{
    // turn OFF visibility of the ""Generic Annotations - Do Not Print" subcategory in the views being printed
    categoryVisInViews(args.Document, args.GetViewElementIds(), false);
}

private void myPrintedEvent(object sender, DocumentPrintedEventArgs args)
{
    // list of views that both printed and failed to print
    List<ElementId> views = new List<ElementId>();
    views.AddRange(args.GetFailedViewElementIds());
    views.AddRange(args.GetPrintedViewElementIds());

    // turn ON visibility of the ""Generic Annotations - Do Not Print" subcategory in views that were printed & that failed to print
    categoryVisInViews(args.Document, views, true);
}

// This function takes as inputs:
// 1) The document
// 2) The list of views in which the "Do Not Print" subcategory visibility should be changed
// 3) A boolean (true or false) indicating the desired state of the category visibility 
private void categoryVisInViews(Document doc, IList<ElementId> viewList, bool visible)
{
    foreach (ElementId id in viewList)
    {
        View view = doc.GetElement(id) as View;
        // Get all categories in the document
        Categories categories = doc.Settings.Categories;

        // The "Generic Annotations" category
        Category genericAnnotations = categories.get_Item("Generic Annotations");

        // The ""Do Not Print" subcategory of the "Generic Annotations" category
        Category doNotPrint = genericAnnotations.SubCategories.get_Item("Do Not Print");

        // Create transaction with the view name & state in the transaction name
        using (Transaction t = new Transaction(doc,"Set Visibility: " + view.Name + ": " + visible))
        {            
            t.Start();
            // set the visibility of the category
            view.setVisibility(doNotPrint, visible);
            t.Commit();
        }            
    }
}

Making Lines Not Print (with Events)

A recurring question that has come up at least twice at AUGI (here and here) and many other times over the years has been how to put things in your Revit model that will not print. There were good reasons in the early days of Revit that we had a strong commitment to WYSIWYP (What You See Is What You Print) and there are also good reasons why Revit users do not want to print everything that they see in Revit. These “hide” options in the Print dialog are good, but often you will want to go beyond this.

printhide

Two API functions to the rescue!

  1. The ability to modify line colors defined in Object Styles and Line Styles
  2. Events, which let you run API commands before and after various file actions such as open, close, save, print

In this example, a Revit model contains two annotation families where the lines are assigned to a subcategory named “Do Not Print”

family

For this application, use two events:

  1. DocumentPrinting – runs API code just before Revit prints
  2. DocumentPrinted – runs API code just after Revit prints

Before Revit prints, the myPrintingEvent will change this subcategory’s line color to white.
After Revit prints, the myPrintedEvent will change this subcategory’s line color to black.

objectstyle

To enable the magic, run the macro PrintingEventRegister. This associates a function with each of the two print events mentioned above.

public void PrintingEventRegister()
{
    // myPrintingEvent will run just BEFORE Revit prints
    this.Application.DocumentPrinting += new EventHandler<DocumentPrintingEventArgs>(myPrintingEvent);

    // myPrintedEvent will run just AFTER Revit prints
    this.Application.DocumentPrinted += new EventHandler<DocumentPrintedEventArgs>(myPrintedEvent);
}

// These macro is private, instead of public, because it will not be run from the Macros dialog
// It will only be available to other macros
private void myPrintingEvent(object sender, DocumentPrintingEventArgs args)
{
    // set color to white (255, 255, 255)
    CategoryLineColor(new Color(Byte.MaxValue,Byte.MaxValue,Byte.MaxValue));
}

private void myPrintedEvent(object sender, DocumentPrintedEventArgs args)
{
    // set color to black (0, 0, 0)
    CategoryLineColor(new Color(Byte.MinValue,Byte.MinValue,Byte.MinValue));
}

The results of printing are shown in the PDF on the right side of this screenshot. The lines in the families are “invisible” because they are white! After the printing is completed they are set back to be black.

print

And here is the code for the CategoryLineColor function that sets the line color.

private void CategoryLineColor(Color newColor)
{
    Document doc = this.ActiveUIDocument.Document;

    // Get all categories in the document
    Categories categories = doc.Settings.Categories;

    // The "Generic Annotations" category
    Category genericAnnotations = categories.get_Item("Generic Annotations");

    // The ""Do Not Print" subcategory of the "Generic Annotations" category
    Category doNotPrint = genericAnnotations.SubCategories.get_Item("Do Not Print");

    // Create transaction, with the value of the color (0 or 255) in the transaction name
    using (Transaction t = new Transaction(doc,"Do Not Print color = " + newColor.Blue))
    {            
        t.Start();
        // set the style's line color to the "newColor"
        doNotPrint.LineColor = newColor;                
        t.Commit();
    }            
}