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

22 thoughts on “Print Sheets from Host & Linked RVT files

  1. Thanks Harry. I suppose you could also bring up a list of sheets from the linked model to print also? Regarding printing, I was wondering if you could use the PickBox method in order to print part of a view, similar to how AutoCAD has the “Select Window” print option. Thanks.

    • Yes, all that is possible. The user interface will take more time to implement than the printing mechanism.
      Regarding PickBox, the API would have to use the same Visible Portion of Current View that you could use in the UI. How are you hoping to improve the printing workflow beyond that existing option?

      • Essentially, my users are usually dismayed to find out that there is no “Select Window” option. But functionally, it takes time to get the portion you want centered on the sheet size you are printing to, plus you may want to exclude nearby objects that may be on the screen in order to draw focus on the portion you do want. Thanks.

  2. Hi,
    I am trying to get this code to work but seem to be having a problem with the public void printlinked() line. I am getting the error “Expected class, delegate, enum, interface, or struct” any help would be greatly appreciated.

    • Try using the New Macro button in the Revit UI and then paste this code inside the {} of the new macro. Also make sure you create the macro in an Application module if you want to use this code as-is.

      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())
      {
      printManager.PrintToFileName = @”C:\OutputFolder\” + Path.GetFileName(doc.PathName) + ” ” + vs.Name + ” ” + vs.SheetNumber + “.”;
      printManager.SubmitPrint(vs);
      }
      }

  3. hi Mat, i tried your code in non_macro environment, pdf SaveAs dialogbox will display and path is wrong, any reason and fix? see below code for your reference, thanks.

    foreach (Document openedDoc in commandData.Application.Application.Documents)
    {
    PrintManager printManager = openedDoc.PrintManager;
    printManager.PrintToFile = true;
    printManager.SelectNewPrintDriver(“CutePDF Writer”);
    foreach (ViewSheet vs in new FilteredElementCollector(openedDoc).OfClass(typeof(ViewSheet)).Cast())
    {
    printManager.PrintToFileName = Environment.GetEnvironmentVariable(“USERPROFILE”) + @”\documents\” + Path.GetFileName(openedDoc.PathName) + ” ” + vs.Name + ” ” + vs.SheetNumber + “.”;
    printManager.SubmitPrint(vs);
    }
    }

  4. Hi Harry,
    I am trying to run this macro in 2015 and get this error when I try and build the solution…

    ‘Autodesk.Revit.UI.UIApplication.Application’ is a ‘property’ but is used like a ‘type’ (CS0118) – C:\ProgramData\Autodesk\Revit\Macros\2015\Revit\AppHookup\Printing\Source\Printing\ThisApplication.cs:41,4

    Any help would be greatly appreciated.

        • Does this work with the 2015 and 2016 API?
          I am getting an
          Autodesk.Revit.Exceptions.ArgumentOutOfRangeException:
          The inout view can not be printed.
          at MacroModule.executeMacro_(MacroModule*,AString*macroName)
          at MacroModule.executeMacro_(MacroModule*,AString*)
          atUIMacroGeneralManager.runMacro(UIMacroGeneralManager*,MacroModule*pModule,AString*macroName)
          It is probably my beginner level of API programming.

          • I have it working now, just opened the link and reset up the print set, saved and then reloaded the link in the parent file, and all good. Not too sure where the problem was, but it is working now. Thanks a lot for your help.

  5. Here is my code for printing based on your example.

    using System;
    using System.Diagnostics;
    using System.IO;
    using System.Text;
    using Autodesk.Revit.UI;
    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;
    using System.Collections.Generic;
    using System.Linq;

    namespace PrintLinked2
    {
    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(openddoc).OfClass(typeof(ViewSheet)).Cast())
    {
    printManager.PrintToFileName = Environment.GetEnvironmentVariable(“userprofile”)+ @”/documents/”+Path.GetFileName(openedDoc.PathName)+vs.Name + + vs.SheetNumber + .;
    printManager.SubmitPrint(vs);

    }
    }

    I am getting the following two errors that I can’t fix. Could you please review and direct me to my error?
    “A namespace cannot directly contain members such as fields or methods (CS0116)” – C:\ProgramData\Autodesk\Revit\Macros\2014\Revit\AppHookup\PrintLinked2\Source\PrintLinked2\ThisApplication.cs:26,2
    “Expected class, delegate, enum, interface, or struct (CS1518)” – C:\ProgramData\Autodesk\Revit\Macros\2014\Revit\AppHookup\PrintLinked2\Source\PrintLinked2\ThisApplication.cs:34,32

  6. Hello Harry,

    First of all many thanks for the above script. It works great for all linked files. I have linked all my individual project Revit files to a new Revit file so that it should print sheets from all those linked Revit files at once. Also, few files are interlinked with each other. The problem I am facing here is after running the macro, it will start printing all sheets from linked Revit file but only show elements which are modeled inside that file and skipping the linked files inside that Revit file. Basically, it’s printing all elements which are modeled inside those linked files and skip any other data which is not modeled inside.

    Do you have any suggestion for my problem? Any help will be appreciated. Thank you!

      • Thanks Harry for reply. I have tried the Overlay/Attachment to see if that will work but looks like not working. Process:

        Project 01 has Project 2 (overlayed) inside.
        Project 2 has Project 3 linked(attached) inside.

        After printing from Project 01 file it will look for all sheets inside linked files including Project 01, 02 and 03 but while printing only project 01 file will show the project 02 and 03. Rest Project 02 and 03 will not show any linked file content on print. Does it make sense? let me know.

        I also thought to open individual revit files from directory through macro and look for specific view/sheet set and run print just on those views and close the revit file. Thank you!

  7. I am creating a application module then (with Sharpdevelop still open), creating a macro within it and then inputting this code

    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 print
    {
    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    [Autodesk.Revit.DB.Macros.AddInId(“ECCCF65F-734E-4A45-84FA-D127C3EF55C0”)]
    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(“CutePDF Writer”);

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

    The final line has 2 errors, both are; } expected (CS1513)

    Please advise and forgive my ignorance as I am new to Macros

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s