Macro to Print Linked Views

Macro to Print Linked Views

H4TSDESQ
Enthusiast Enthusiast
1,176 Views
4 Replies
Message 1 of 5

Macro to Print Linked Views

H4TSDESQ
Enthusiast
Enthusiast

I came across the post here: Link about how to create a macro to plot views from a linked model. 

 

I have modified it a little, mostly what printer and the base filename, but I'm only able to get a single page within the host file to print. I've changed the print range to include multiple views & still it only prints 1 sheet. Also note, the page that prints is the same all the time, even if it isn't open or set to be printed.

 

When using a Print set & the <in-session> I get the following error after the first page:

 

in session.PNG

 

 

/*
 * Created by SharpDevelop.
 * User: H4TSDESQ
 * Date: 8/29/2017
 * Time: 11:13 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.UI.Events;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.ApplicationServices;

namespace printLinked
{
    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    [Autodesk.Revit.DB.Macros.AddInId("C86EF7BE-C396-4D36-9578-38595A99709C")]
	public partial class ThisApplication
	{
		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 printLinked()
		{
			Application app = this.Application;
				
    foreach (Document doc in app.Documents)
    {
       PrintManager printManager = doc.PrintManager;
       printManager.PrintToFile = true;

       printManager.SelectNewPrintDriver("Adobe PDF");

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

		}
		}}}

 

Any suggestions for what might be causing this?

 

Revit build:

16.0.428.0
20150220_1215(x64)

0 Likes
Accepted solutions (2)
1,177 Views
4 Replies
Replies (4)
Message 2 of 5

RPTHOMAS108
Mentor
Mentor
Accepted solution

Some possible causes are as below:

 

1) Some ViewSheets are placeholders and can't be printed. The ViewSheet class has a 'IsPlaceHolder' property so check this is false.

2) Some views are not ready to be printed due to application/view state the 'ViewSheet.CanBePrinted property should be checked for true.

3) I don't believe a ViewSheet can be a template but if you are printing views in general you should also check the View.IsTemplate = false.

 

Printing sheets from links doesn't sound ideal to me but I've never tried so can't say it inherantly has problems. 

 

I'm not sure you need this many using statements considering the limited namespaces you are using.

 

I should also say that I've found the Adobe PDF writer impossible to set the printed filename on dynamically. Usually you can set the print to file path but it will ignore it and just prompt you for a filename. This is obviously annoying in a batch printing situation.

 

 

using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.UI.Events;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.ApplicationServices;

namespace printLinked
{
	[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
	[Autodesk.Revit.DB.Macros.AddInId("C86EF7BE-C396-4D36-9578-38595A99709C")]
	public partial class ThisApplication
	{

		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 printLinked()
		{
			Application app = this.Application;

			foreach (Document doc in app.Documents) {
				PrintManager printManager = doc.PrintManager;
				printManager.PrintToFile = true;
				printManager.SelectNewPrintDriver("Adobe PDF");

				FilteredElementCollector FEC = new FilteredElementCollector(doc);
				ElementClassFilter ECF = new ElementClassFilter(typeof(ViewSheet));
				List<ElementId> VS_Ids = FEC.WherePasses(ECF).WhereElementIsNotElementType.ToElementIds;

				foreach (ElementId EID in VS_Ids) {
					ViewSheet VS = doc.GetElement(EID) as ViewSheet;
					if (VS == null)
						continue;
					if (VS.IsPlaceholder == true)
						continue;
					if (VS.CanBePrinted != true)
						continue;

					printManager.PrintToFileName = "C:\\Temp\\" + VS.SheetNumber + ".";
					printManager.SubmitPrint(VS);

				}
			}
		}
	}
}
Message 3 of 5

JimJia
Alumni
Alumni

Thanks a lot for RPTHOMAS108's detailed sharing! I believe this code should work for desired views. 

 

Note that Revit doesn't support printing views which are from linked document, if you want to print them, you have to open the linked document and print viewsheet from there. 


Jim Jia
Autodesk Forge Evangelist
https://forge.autodesk.com
Developer Technical Services
Autodesk Developer Network
Email: Jim.Jia@autodesk.com
0 Likes
Message 4 of 5

H4TSDESQ
Enthusiast
Enthusiast

JimJia, when you say "Revit doesn't support printing views", are you referring to specific views & not actual sheets? I ask because there are times when I run this macro and sheets within a linked model do print in addition to the sheets within the "master" model while only having the "master" open. 

 

I also get the following error with the code posted:

 

Error CS0119: 'Autodesk.Revit.DB.FilteredElementCollector.WhereElementIsNotElementType()' is a 'method', which is not valid in the given context

0 Likes
Message 5 of 5

RPTHOMAS108
Mentor
Mentor
Accepted solution

Sorry this was a conversion error that crept in when I converted to C#

 

change the line:
List<ElementId> VS_Ids = FEC.WherePasses(ECF).WhereElementIsNotElementType.ToElementIds;
to
ICollection<ElementId> VS_Ids = FEC.WherePasses(ECF).WhereElementIsNotElementType().ToElementIds();

 

If the macro works for you and your expectations of it are at the right level then that is all that really matters. However even in a single model there are lots of printing pitfalls (alliteration) that a simple macro isn't going to handle i.e. worksharing (views and print settings), so called non-pumping waits; where long running tasks make OS think Revit has become unresponsive.

 

I've also found that some views are not ready to be printed, I'm not sure if there is a way to make such views print ready. In the past I tried opening them before hand, not sure if that had a benefit in forcing a regeneration of them? Would be nice to have clarity on this i.e. to know if there is a way to make views print ready?

0 Likes