Set views to print with RevitAPI

中文链接

By Aaron Lu

If we want to change Revit print settings, the entry point is Document.PrintManager property, and the PrintManager.ViewSheetSetting is used to set which views you want to print.

However when calling PrintManager.ViewSheetSetting, we may get this kind of excpetion:

InvalidOperationException “This property is only available when user choose Select of Print Range.”

The solution is to set “Print Range” to “Selected views/sheets” in the dialog appeared after clicking “Print” menu.

Select View Range - en

After fix the problem above, we now can use ViewSheetSetting.InSession.Views to set the views to print.

Steps:

1. Create a new ViewSet

2. Filter all views

3. Check if the view is printable using View.CanBePrinted

4. Add the view to print to the new ViewSet

5. Set ViewSheetSetting.InSession.Views with the new ViewSet (note: remember to put this inside a transaction)

Code example:

RevitDoc = commandData.Application.ActiveUIDocument.Document;
var pm = RevitDoc.PrintManager;
try
{
var vss = pm.ViewSheetSetting;
ViewSet set = new ViewSet();
var classFilter = new ElementClassFilter(typeof(View));
FilteredElementCollector views =
new FilteredElementCollector(RevitDoc);
views = views.WherePasses(classFilter);
foreach (View view in views)
{
if (view.CanBePrinted)
{
set.Insert(view);
}
}
using (Transaction transaction = new Transaction(RevitDoc))
{
transaction.Start("Set in-session views");
vss.InSession.Views = set;
transaction.Commit();
}
}
catch (Exception ex)
{
TaskDialog.Show("ERROR", ex.ToString());
}

Comments

4 responses to “Set views to print with RevitAPI”

  1. Hi Aaron,
    Instead of setting Print Range in the Print Setting dialog, you can do the same using API.
    In the case with the Selected Views you have to just set PrintRange.Select before calling pm.ViewSheetSetting.
    For example:
    var printMgr = doc.PrintManager;
    printMgr.PrintRange = PrintRange.Select;
    var viewSheetSetrtings = printMgr.ViewSheetSetting;
    Have a nice day,
    Victor.

  2. Good point Victor, thanks!

  3. how to add user selected sheet to printing set
    Regards
    vinay

  4. This is work great with me but I want to add user selected sheet to this printing set is there any possibilities.

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading