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.
Two API functions to the rescue!
- The ability to modify line colors defined in Object Styles and Line Styles
- 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”
For this application, use two events:
- DocumentPrinting – runs API code just before Revit prints
- 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.
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)
private void myPrintedEvent(object sender, DocumentPrintedEventArgs args)
// set color to black (0, 0, 0)
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.
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))
// set the style's line color to the "newColor"
doNotPrint.LineColor = newColor;