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.
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()); }