问题
I am trying to automate the process of pdf printing in Revit 2017 using Revit API with Idling event handler. I am opening the revit document using OpenDocumentFile method and not using ActiveUIDocument anywhere. This process is generating Invalid Operation Exception. I am not sure why is it giving the exception or whether it is allowed using Revit API. Please help. Thanks.
Journal Output:
' 1:< 0 <-pushSettingsIntoDriver
' C 27-Sep-2016 14:45:22.641; 1:< ExportToRequestedFormat() : Pdf Print Exception : InvalidOperationExceptionAt least one view from the view set could not be printed.
' at Autodesk.Revit.DB.Document.Print(ViewSet views, View viewTemplate, Boolean useCurrentPrintSettings)
' at Autodesk.Revit.DB.Document.Print(ViewSet views, Boolean useCurrentPrintSettings)
' at RevitCommandListener.RevitCommandListenerService.ExportToRequestedFormat(UIApplication uiapp)
Code Behind:
using (FilteredElementCollector viewCollector = new FilteredElementCollector(doc))
{
ViewSet set = new ViewSet();
ElementClassFilter viewFilter = null;
PrintManager pm = PrinterDriverSettings.GetPrintManager(doc, _ifcSaveFile, PrinterDriver.Bullzip);
switch (_pdfExportSetting)
{
case PDFExportOptions.SheetsOnly:
viewFilter = new ElementClassFilter(typeof(Autodesk.Revit.DB.ViewSheet));
viewCollector.WherePasses(viewFilter);
foreach (Autodesk.Revit.DB.ViewSheet vw in viewCollector)
{
if (vw.CanBePrinted && !vw.IsTemplate)
set.Insert(vw);
}
break;
case PDFExportOptions.ViewsOnly:
viewFilter = new ElementClassFilter(typeof(Autodesk.Revit.DB.View));
viewCollector.WherePasses(viewFilter);
foreach (Autodesk.Revit.DB.View vw in viewCollector)
{
if (vw.CanBePrinted && !vw.IsTemplate && !(vw.GetType() == typeof(ViewSheet))) //Skip sheets
set.Insert(vw);
}
break;
case PDFExportOptions.ViewsAndSheets:
viewFilter = new ElementClassFilter(typeof(Autodesk.Revit.DB.View));
viewCollector.WherePasses(viewFilter);
foreach (Autodesk.Revit.DB.View vw in viewCollector)
{
if (vw.CanBePrinted && !vw.IsTemplate)
set.Insert(vw);
}
break;
case PDFExportOptions.Sections:
viewFilter = new ElementClassFilter(typeof(Autodesk.Revit.DB.ViewSection));
viewCollector.WherePasses(viewFilter);
foreach (Autodesk.Revit.DB.ViewSection vw in viewCollector)
{
if (vw.CanBePrinted && && !vw.IsTemplate !(vw.ViewType == ViewType.Elevation))
set.Insert(vw);
}
break;
case PDFExportOptions.Elevations:
viewFilter = new ElementClassFilter(typeof(Autodesk.Revit.DB.ViewSection));
viewCollector.WherePasses(viewFilter);
foreach (Autodesk.Revit.DB.ViewSection vw in viewCollector)
{
if (vw.CanBePrinted && !vw.IsTemplate && vw.ViewType == ViewType.Elevation)
{
set.Insert(vw);
}
}
break;
case PDFExportOptions.Schedules:
viewFilter = new ElementClassFilter(typeof(ViewSchedule));
viewCollector.WherePasses(viewFilter);
foreach (ViewSchedule vw in viewCollector)
{
if (vw.CanBePrinted && !vw.IsTemplate)
set.Insert(vw);
}
break;
}
if (_pdfExportSetting != PDFExportOptions.None && set.Size > 0)
{
Transaction tr = new Transaction(doc, "tr_pdf_print");
try
{
tr.Start();
doc.Print(set, true);
tr.Commit();
}
catch(Autodesk.Revit.Exceptions.InvalidOperationException iopex)
{
uiapp.Application.WriteJournalComment("ExportToRequestedFormat() : Pdf Print Exception : InvalidOperationException" + iopex.Message + Environment.NewLine + iopex.StackTrace, true);
}
catch(Autodesk.Revit.Exceptions.ArgumentNullException argsex)
{
uiapp.Application.WriteJournalComment("ExportToRequestedFormat() : Pdf Print Exception : ArgumentNullException" + argsex.Message + Environment.NewLine + argsex.StackTrace, true);
}
catch(Autodesk.Revit.Exceptions.ArgumentException arex)
{
uiapp.Application.WriteJournalComment("ExportToRequestedFormat() : Pdf Print Exception : ArgumentException" + arex.Message + Environment.NewLine + arex.StackTrace, true);
}
catch(Autodesk.Revit.Exceptions.ApplicationException appex)
{
uiapp.Application.WriteJournalComment("ExportToRequestedFormat() : Pdf Print Exception : ApplicationException" + appex.Message + Environment.NewLine + appex.StackTrace, true);
}
finally
{
set.Dispose();
viewFilter.Dispose();
viewCollector.Dispose();
pm.Dispose();
tr.Dispose();
}
}
回答1:
In addition to the IsPrintable field, the other thing you'll need to check for is whether the View is actually a View Template. Use IsTemplate to check that.
回答2:
This is much too convoluted for me, and probably for Revit as well.
You have a filtered element collector.
Inside your iteration over those element, you try to start a transaction plus print.
Keep it simple and smart!
First of all, separate filtered element iteration, transactions and printing into three completely independent operations.
Then figure how these three need to interact.
Minimise the interaction, and your problem should be solved.
来源:https://stackoverflow.com/questions/39720839/revit-to-pdf-conversion