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.
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.
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();
}
}
}
harry- I appreciate the various approaches, as well as keeping things basic (which is what I love about what you’re doing here)! thanks!
[…] in the Revit API, particularly printing events, and in doing so showed a couple of ways (here and here) to implement a no plot type functionality in Revit. We took that kernel of an idea and decided to […]
Harry – Old time C programmer from the Autolisp days. Trying to get back in to programming and feel like I’m learning to ride a bike again. You’re blog has been invaluable. Great macros.
2 comments:
1. I ran the noprint macro. I can’t believe Revit does not have this functionality. When I ran it in 2014 it kept giving me error messages. The debugger fixed whatever the problems was.
2. Is it possible to make this work with other components, lines, text, etc?
Hi Tom,
Glad you’ve been enjoying the blog. You might also find my video lectures at bit.ly/revitapi to be helpful.
What were the errors you were getting in 2014?
The visibility of different items can be controlled in different ways. This post sets category visibility. You might also use View.HideElements if there are specific elements you want to not display without turning off the whole category.
Regards
Harry
Harry, just found your website. I’m a BIM Manager but new to Macros and have no programming experience. Is it possible to provide a few steps for a newbie on where to copy and paste the macro and how to run the macro? This would truly help as a lot of users i’m working with still reminisce the CAD days and would like this functionality.
Hi
Here are a couple posts that I hope will be helpful
https://boostyourbim.wordpress.com/2013/01/02/how-to-use-these-macros-basic-overview/
https://boostyourbim.wordpress.com/2013/02/15/how-to-use-boost-your-bim-source-code-to-create-working-macros/
There is also a ton of great information for newbies interested in learning about the Revit API in my online video course at https://www.udemy.com/course/revitapi/learn/lecture/12475742?couponCode=164DD9A243D60927BBD4#overview