From e1c19818308c25538bd052ef1a4dd35e5666b63a Mon Sep 17 00:00:00 2001 From: Gaute Lindkvist Date: Tue, 21 Jan 2020 12:36:22 +0100 Subject: [PATCH] Support PDF-export from Python --- .../RicfExportSnapshots.cpp | 14 +++++++++++++- .../RicfExportSnapshots.h | 19 ++++++++++++++----- .../RicSnapshotAllPlotsToFileFeature.cpp | 5 +++-- .../RicSnapshotAllPlotsToFileFeature.h | 5 +++-- .../RicSnapshotViewToFileFeature.cpp | 7 ++++++- .../GrpcInterface/GrpcProtos/Commands.proto | 17 ++++++++++++----- .../rips/PythonExamples/export_plots.py | 1 + .../GrpcInterface/Python/rips/plot.py | 6 ++++-- .../GrpcInterface/Python/rips/project.py | 5 +++-- 9 files changed, 59 insertions(+), 20 deletions(-) diff --git a/ApplicationCode/CommandFileInterface/RicfExportSnapshots.cpp b/ApplicationCode/CommandFileInterface/RicfExportSnapshots.cpp index 323e5cc574..398ad28ff3 100644 --- a/ApplicationCode/CommandFileInterface/RicfExportSnapshots.cpp +++ b/ApplicationCode/CommandFileInterface/RicfExportSnapshots.cpp @@ -35,6 +35,14 @@ CAF_PDM_SOURCE_INIT( RicfExportSnapshots, "exportSnapshots" ); namespace caf { +template <> +void RicfExportSnapshots::PreferredOutputFormatEnum::setUp() +{ + addItem( RicfExportSnapshots::PlotOutputFormat::PNG, "PNG", "PNG" ); + addItem( RicfExportSnapshots::PlotOutputFormat::PDF, "PDF", "PDF" ); + setDefault( RicfExportSnapshots::PlotOutputFormat::PNG ); +} + template <> void RicfExportSnapshots::SnapshotsTypeEnum::setUp() { @@ -55,6 +63,7 @@ RicfExportSnapshots::RicfExportSnapshots() RICF_InitField( &m_caseId, "caseId", -1, "Case Id", "", "", "" ); RICF_InitField( &m_viewId, "viewId", -1, "View Id", "", "", "" ); RICF_InitField( &m_exportFolder, "exportFolder", QString(), "Export Folder", "", "", "" ); + RICF_InitFieldNoDefault( &m_plotOutputFormat, "plotOutputFormat", "Output Format", "", "", "" ); } //-------------------------------------------------------------------------------------------------- @@ -109,9 +118,12 @@ RicfCommandResponse RicfExportSnapshots::execute() QApplication::processEvents(); } + QString fileSuffix = ".png"; + if ( m_plotOutputFormat == PlotOutputFormat::PDF ) fileSuffix = ".pdf"; RicSnapshotAllPlotsToFileFeature::exportSnapshotOfPlotsIntoFolder( absolutePathToSnapshotDir, m_prefix, - m_viewId() ); + m_viewId(), + fileSuffix ); } mainWnd->loadWinGeoAndDockToolBarLayout(); diff --git a/ApplicationCode/CommandFileInterface/RicfExportSnapshots.h b/ApplicationCode/CommandFileInterface/RicfExportSnapshots.h index af9a4f7d80..2c710edb0e 100644 --- a/ApplicationCode/CommandFileInterface/RicfExportSnapshots.h +++ b/ApplicationCode/CommandFileInterface/RicfExportSnapshots.h @@ -34,6 +34,14 @@ class RicfExportSnapshots : public RicfCommandObject public: // Values are exposed in gRPC .proto. Do not change without also changing .proto + + enum class PlotOutputFormat + { + PNG, + PDF + }; + typedef caf::AppEnum PreferredOutputFormatEnum; + enum SnapshotsType { VIEWS, @@ -48,9 +56,10 @@ public: RicfCommandResponse execute() override; private: - caf::PdmField m_type; - caf::PdmField m_prefix; - caf::PdmField m_caseId; - caf::PdmField m_viewId; - caf::PdmField m_exportFolder; + caf::PdmField m_type; + caf::PdmField m_prefix; + caf::PdmField m_caseId; + caf::PdmField m_viewId; + caf::PdmField m_exportFolder; + caf::PdmField m_plotOutputFormat; }; diff --git a/ApplicationCode/Commands/ExportCommands/RicSnapshotAllPlotsToFileFeature.cpp b/ApplicationCode/Commands/ExportCommands/RicSnapshotAllPlotsToFileFeature.cpp index 3626695243..d74f812dcf 100644 --- a/ApplicationCode/Commands/ExportCommands/RicSnapshotAllPlotsToFileFeature.cpp +++ b/ApplicationCode/Commands/ExportCommands/RicSnapshotAllPlotsToFileFeature.cpp @@ -68,7 +68,8 @@ void RicSnapshotAllPlotsToFileFeature::saveAllPlots() //-------------------------------------------------------------------------------------------------- void RicSnapshotAllPlotsToFileFeature::exportSnapshotOfPlotsIntoFolder( const QString& snapshotFolderName, const QString& prefix, - int viewId ) + int viewId, + const QString& preferredFileSuffix /*=".png"*/ ) { RiaApplication* app = RiaApplication::instance(); @@ -98,7 +99,7 @@ void RicSnapshotAllPlotsToFileFeature::exportSnapshotOfPlotsIntoFolder( const QS fileName.replace( " ", "_" ); - QString absoluteFileName = caf::Utils::constructFullFileName( absSnapshotPath, fileName, ".png" ); + QString absoluteFileName = caf::Utils::constructFullFileName( absSnapshotPath, fileName, preferredFileSuffix ); RicSnapshotViewToFileFeature::saveSnapshotAs( absoluteFileName, viewWindow ); } diff --git a/ApplicationCode/Commands/ExportCommands/RicSnapshotAllPlotsToFileFeature.h b/ApplicationCode/Commands/ExportCommands/RicSnapshotAllPlotsToFileFeature.h index 6dfd3450b5..2df7dbce22 100644 --- a/ApplicationCode/Commands/ExportCommands/RicSnapshotAllPlotsToFileFeature.h +++ b/ApplicationCode/Commands/ExportCommands/RicSnapshotAllPlotsToFileFeature.h @@ -33,8 +33,9 @@ public: static void saveAllPlots(); static void exportSnapshotOfPlotsIntoFolder( const QString& snapshotFolderName, - const QString& prefix = "", - int viewId = -1 ); + const QString& prefix = "", + int viewId = -1, + const QString& preferredFileSuffix = ".png" ); protected: // Overrides diff --git a/ApplicationCode/Commands/ExportCommands/RicSnapshotViewToFileFeature.cpp b/ApplicationCode/Commands/ExportCommands/RicSnapshotViewToFileFeature.cpp index bd1264b604..3b33d92feb 100644 --- a/ApplicationCode/Commands/ExportCommands/RicSnapshotViewToFileFeature.cpp +++ b/ApplicationCode/Commands/ExportCommands/RicSnapshotViewToFileFeature.cpp @@ -50,7 +50,12 @@ CAF_CMD_SOURCE_INIT( RicSnapshotViewToFileFeature, "RicSnapshotViewToFileFeature //-------------------------------------------------------------------------------------------------- void RicSnapshotViewToFileFeature::saveSnapshotAs( const QString& fileName, RimViewWindow* viewWindow ) { - if ( viewWindow ) + RimPlotWindow* plotWindow = dynamic_cast( viewWindow ); + if ( plotWindow && fileName.endsWith( ".pdf" ) ) + { + savePlotPDFReportAs( fileName, plotWindow ); + } + else if ( viewWindow ) { QImage image = viewWindow->snapshotWindowContent(); saveSnapshotAs( fileName, image ); diff --git a/ApplicationCode/GrpcInterface/GrpcProtos/Commands.proto b/ApplicationCode/GrpcInterface/GrpcProtos/Commands.proto index 016ddf2377..a550d07151 100644 --- a/ApplicationCode/GrpcInterface/GrpcProtos/Commands.proto +++ b/ApplicationCode/GrpcInterface/GrpcProtos/Commands.proto @@ -45,6 +45,12 @@ message ExportMultiCaseRequest string gridListFile = 1; } +enum PlotOutputFormat +{ + PNG = 0; + PDF = 1; +} + enum SnapshotType { VIEWS = 0; @@ -54,11 +60,12 @@ enum SnapshotType message ExportSnapshotsRequest { - SnapshotType type = 1; - string prefix = 2; - int32 caseId = 3; - int32 viewId = 4; - string exportFolder = 5; + SnapshotType type = 1; + string prefix = 2; + int32 caseId = 3; + int32 viewId = 4; + string exportFolder = 5; + PlotOutputFormat plotOutputFormat = 6; } message ExportPropertyRequest diff --git a/ApplicationCode/GrpcInterface/Python/rips/PythonExamples/export_plots.py b/ApplicationCode/GrpcInterface/Python/rips/PythonExamples/export_plots.py index 1949cb3a47..475e92503c 100644 --- a/ApplicationCode/GrpcInterface/Python/rips/PythonExamples/export_plots.py +++ b/ApplicationCode/GrpcInterface/Python/rips/PythonExamples/export_plots.py @@ -14,6 +14,7 @@ print("Exporting to: " + export_folder) for plot in plots: plot.export_snapshot(export_folder=export_folder) + plot.export_snapshot(export_folder=export_folder, output_format='PDF') well_log_plot = rips.WellLogPlot.from_pdm_object(plot) if well_log_plot is not None: well_log_plot.export_data_as_las(export_folder=export_folder) diff --git a/ApplicationCode/GrpcInterface/Python/rips/plot.py b/ApplicationCode/GrpcInterface/Python/rips/plot.py index 2f23ab7f37..8614603dd2 100644 --- a/ApplicationCode/GrpcInterface/Python/rips/plot.py +++ b/ApplicationCode/GrpcInterface/Python/rips/plot.py @@ -16,18 +16,20 @@ class Plot(PdmObject): PdmObject.__init__(self, pdm_object.pb2_object(), pdm_object.channel(), pdm_object.project()) self.view_id = pdm_object.get_value("ViewId") - def export_snapshot(self, export_folder='', file_prefix='', ): + def export_snapshot(self, export_folder='', file_prefix='', output_format='PNG'): """ Export snapshot for the current plot Arguments: export_folder(str): The path to export to. By default will use the global export folder prefix (str): Exported file name prefix + output_format(str): Enum string. Can be 'PNG' or 'PDF'. """ return self._execute_command( exportSnapshots=Cmd.ExportSnapshotsRequest(type='PLOTS', prefix=file_prefix, viewId=self.view_id, - exportFolder=export_folder)) + exportFolder=export_folder, + plotOutputFormat=output_format)) \ No newline at end of file diff --git a/ApplicationCode/GrpcInterface/Python/rips/project.py b/ApplicationCode/GrpcInterface/Python/rips/project.py index 79a1cb7e18..240bbf7527 100644 --- a/ApplicationCode/GrpcInterface/Python/rips/project.py +++ b/ApplicationCode/GrpcInterface/Python/rips/project.py @@ -224,16 +224,17 @@ class Project(PdmObject): exportMultiCaseSnapshot=Cmd.ExportMultiCaseRequest( gridListFile=grid_list_file)) - def export_snapshots(self, snapshot_type='ALL', prefix=''): + def export_snapshots(self, snapshot_type='ALL', prefix='', plot_format='PNG'): """ Export all snapshots of a given type Arguments: snapshot_type (str): Enum string ('ALL', 'VIEWS' or 'PLOTS') prefix (str): Exported file name prefix + plot_format(str): Enum string, 'PNG' or 'PDF' """ return self._execute_command( exportSnapshots=Cmd.ExportSnapshotsRequest( - type=snapshot_type, prefix=prefix, caseId=-1, viewId=-1)) + type=snapshot_type, prefix=prefix, caseId=-1, viewId=-1, plotOutputFormat=plot_format)) def export_well_paths(self, well_paths=None, md_step_size=5.0): """ Export a set of well paths