我们要修改Revit的打印设置,该设置可以使用Document.PrintManager属性来访问,其中ViewSheetSetting对应的就是打印哪些视图的设置,
但是使用API访问Document.PrintManager.ViewSheetSetting的时候会抛出异常:
InvalidOperationException "This property is only available when user choose Select of Print Range."
解决的办法是:在打印之前的对话框里面,把”打印范围“设置为”所选视图/图纸"。见下图:
上面的问题解决之后,我们就可以通过ViewSheetSetting.InSession.Views来设置需要打印的视图了。
做法就是:
1. 新建一个ViewSet
2. 过滤出所有的View
3. 判断View.CanBePrinted值为true
4. 把你想要打印的view加到一个ViewSet里面
5. 设置ViewSheetSetting.InSession.Views为新建的ViewSet (需要放到Transaction里)
代码示例:
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());
}