Making Lines Not Print (with Events)

Making Lines Not Print (with Events)

Eduardo.SaezDRTV4
Participant Participant
533 Views
5 Replies
Message 1 of 6

Making Lines Not Print (with Events)

Eduardo.SaezDRTV4
Participant
Participant

I came across this post (https://boostyourbim.wordpress.com/2012/12/12/making-lines-not-print-with-events/) a while back and thought I'd give it a go whilst it's a little quiet due to Xmas.
Running it on Revit 2022
As a complete newbie to C# I cannot workout why I'm getting the following errors
Inconsistent accessibility: parameter type 'DocumentPrintingEventArgs' is less accessible than method 'AuscoNoPrint.ThisDocument.myPrintingEvent(object, DocumentPrintingEventArgs)' (CS0051) 
'DocumentPrintingEventArgs' is inaccessible due to its protection level (CS0122)
Inconsistent accessibility: parameter type 'DocumentPrintedEventArgs' is less accessible than method 'AuscoNoPrint.ThisDocument.myPrintedEvent(object, DocumentPrintedEventArgs)' (CS0051)
'DocumentPrintedEventArgs' is inaccessible due to its protection level (CS0122)

/*
 * Created by SharpDevelop.
 * User: Saezed
 * Date: 19/12/2023
 * Time: 11:28 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
//using Autodesk.Revit.DB.Events;
using Autodesk.Revit.UI.Selection;
using System.Collections.Generic;
using System.Linq;

namespace AuscoNoPrint
{
    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    [Autodesk.Revit.DB.Macros.AddInId("CB5C92FC-F218-4D2A-A260-BAEDEEC7A7B0")]
	public partial class ThisDocument
	{		
		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();
		        }            
		    }
		}
		
		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();
		    }            
		}
//		private void Module_Startup(object sender, EventArgs e)
//		{
//
//		}
//
//		private void Module_Shutdown(object sender, EventArgs e)
//		{
//
//		}
//
//		#region Revit Macros generated code
//		private void InternalStartup()
//		{
//			this.Startup += new System.EventHandler(Module_Startup);
//			this.Shutdown += new System.EventHandler(Module_Shutdown);
//		}
//		#endregion
		
//		public void GetSheetParams()
//        {

Any assistance would be greatful.
Thanks
 

0 Likes
534 Views
5 Replies
Replies (5)
Message 2 of 6

jeremy_tammik
Alumni
Alumni

It may be due to the fact that the Revit Events namespace has been commented out:

  

//using Autodesk.Revit.DB.Events;

  

Try uncommenting that.

     

By the way, welcome to programming. You might have to learn a little bit in order to really start enjoying it.

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 3 of 6

Eduardo.SaezDRTV4
Participant
Participant

Thanks Jeremy,
I'll make that change as noted.

Also thanks for the welcome, I'm aiming to learn C# this year as last year it was python... 😁🤞

0 Likes
Message 4 of 6

Eduardo.SaezDRTV4
Participant
Participant

I've uncommented that line and commented the 3 below.
Now I'm only getting one error.
The type or namespace name 'IList' could not be found (are you missing a using directive or an assembly reference?) (CS0246)

using System;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Events;
//using Autodesk.Revit.UI.Selection;
//using System.Collections.Generic;
//using System.Linq;

namespace AuscoNoPrint
{
    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    [Autodesk.Revit.DB.Macros.AddInId("CB5C92FC-F218-4D2A-A260-BAEDEEC7A7B0")]
    public partial class ThisDocument
    {        
        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();
                }            
            }
        }
        
        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();
            }            
        }
0 Likes
Message 5 of 6

jeremy_tammik
Alumni
Alumni

Yes, that is due to another missing namespace. It is easy to determine which one it is. Hint: 

  

  

Hint number 2:

  

Learn to search for answers yourself. One of the main activities of a programmer nowadays. It is very improbable and actually almost impossible for you to run into a problem that has not already been encountered, raised, discussed and answered.

  

Your plan sounds great and I am sure you will enjoy it and have fun. Knowing any programming language will simplify and enrichen the learning path for the next, Python is a brilliant starting point, and C# a brilliant next step.

  

Happy New Year!

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 6 of 6

Eduardo.SaezDRTV4
Participant
Participant

Thanks for the hints.. appreciate your assistance.

0 Likes