#14772 Python: Add View.export_contour_map_to_text as object method and forward exportContourMapToText to it

Rim3dView_exportContourMapToText exports the contour map of an Eclipse
or GeoMech contour map view. The implementation is extracted from the
RICF_SOURCE_INIT feature RicExportContourMapToTextFeature into a static
helper returning std::expected, used by both the object method and the
legacy command.

The Python EclipseContourMap/GeoMechContourMap.export_to_text() wrappers
are kept as aliases of the generated method.
This commit is contained in:
Kristian Bendiksen committed 2026-09-22 14:19:23 +02:00
1 parent b1a822e7ac
commit ebf7a061d8
6 files changed
+145 -64

No files matched your search

@@ -274,11 +274,8 @@ void RicExportContourMapToTextFeature::setupActionLook( QAction* actionToSetup )
caf::PdmScriptResponse RicExportContourMapToTextFeature::execute()
{
caf::PdmScriptResponse response;
QStringList errorMessages, warningMessages;
RiaApplication* app = RiaApplication::instance();
RimProject* proj = app->project();
RimProject* proj = RimProject::current();
CAF_ASSERT( proj );
Rim3dView* myView = nullptr;
@@ -296,10 +293,31 @@ caf::PdmScriptResponse RicExportContourMapToTextFeature::execute()
return response;
}
auto result = exportContourMapToText( myView,
m_exportFileName(),
m_exportLocalCoordinates.value(),
m_undefinedValueLabel.value(),
m_excludeUndefinedValues.value() );
if ( !result )
{
response.updateStatus( caf::PdmScriptResponse::COMMAND_ERROR, result.error() );
}
return response;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::expected<void, QString> RicExportContourMapToTextFeature::exportContourMapToText( Rim3dView* view,
const QString& exportFileName,
bool exportLocalCoordinates,
const QString& undefinedValueLabel,
bool excludeUndefinedValues )
{
RimContourMapProjection* contourMapProjection = nullptr;
RimEclipseContourMapView* existingEclipseContourMap = dynamic_cast<RimEclipseContourMapView*>( myView );
RimGeoMechContourMapView* existingGeoMechContourMap = dynamic_cast<RimGeoMechContourMapView*>( myView );
CAF_ASSERT( existingEclipseContourMap || existingGeoMechContourMap );
RimEclipseContourMapView* existingEclipseContourMap = dynamic_cast<RimEclipseContourMapView*>( view );
RimGeoMechContourMapView* existingGeoMechContourMap = dynamic_cast<RimGeoMechContourMapView*>( view );
QString contourMapName;
if ( existingEclipseContourMap )
@@ -312,30 +330,22 @@ caf::PdmScriptResponse RicExportContourMapToTextFeature::execute()
contourMapProjection = existingGeoMechContourMap->contourMapProjection();
contourMapName = existingGeoMechContourMap->createAutoName();
}
CAF_ASSERT( contourMapProjection );
QFile exportFile( m_exportFileName );
if ( !exportFile.open( QIODevice::WriteOnly | QIODevice::Text ) )
{
errorMessages << QString( "Export Contour Map to Text : Could not open the file: %1" ).arg( m_exportFileName );
}
else
{
QString tableText;
QTextStream stream( &exportFile );
writeMetaDataToStream( stream, contourMapProjection, contourMapName, m_exportLocalCoordinates.value() );
writeContourMapToStream( stream,
contourMapProjection,
m_exportLocalCoordinates.value(),
m_undefinedValueLabel.value(),
m_excludeUndefinedValues.value() );
return std::unexpected( "The view is not a contour map view" );
}
for ( QString errorMessage : errorMessages )
if ( !contourMapProjection ) return std::unexpected( "The contour map view has no projection" );
QFile exportFile( exportFileName );
if ( !exportFile.open( QIODevice::WriteOnly | QIODevice::Text ) )
{
response.updateStatus( caf::PdmScriptResponse::COMMAND_ERROR, errorMessage );
return std::unexpected( QString( "Export Contour Map to Text : Could not open the file: %1" ).arg( exportFileName ) );
}
return response;
QTextStream stream( &exportFile );
writeMetaDataToStream( stream, contourMapProjection, contourMapName, exportLocalCoordinates );
writeContourMapToStream( stream, contourMapProjection, exportLocalCoordinates, undefinedValueLabel, excludeUndefinedValues );
return {};
}
@@ -22,9 +22,12 @@
#include "cafCmdFeature.h"
#include "cafPdmField.h"
#include <expected>
#include <utility>
class RimContourMapProjection;
class Rim3dView;
class RimEclipseContourMapView;
class RimGeoMechContourMapView;
@@ -43,6 +46,13 @@ public:
static std::pair<RimEclipseContourMapView*, RimGeoMechContourMapView*> findContourMapView();
/// Export the contour map of the given view (an Eclipse or GeoMech contour map view) to a text file.
static std::expected<void, QString> exportContourMapToText( Rim3dView* view,
const QString& exportFileName,
bool exportLocalCoordinates,
const QString& undefinedValueLabel,
bool excludeUndefinedValues );
protected:
bool isCommandEnabled() const override;
void onActionTriggered( bool isChecked ) override;
@@ -21,6 +21,8 @@
#include "RiaApplication.h"
#include "RiaKeyValueStoreUtil.h"
#include "RicExportContourMapToTextFeature.h"
#include "Polygons/RimPolygon.h"
#include "Polygons/RimPolygonInViewCollection.h"
#include "Rim3dView.h"
@@ -312,3 +314,51 @@ std::expected<caf::PdmObjectHandle*, QString> Rim3dView_setTimeStep::execute()
return nullptr;
}
CAF_PDM_OBJECT_METHOD_SOURCE_INIT( Rim3dView, Rim3dView_exportContourMapToText, "exportContourMapToText" );
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
Rim3dView_exportContourMapToText::Rim3dView_exportContourMapToText( caf::PdmObjectHandle* self )
: caf::PdmVoidObjectMethod( self )
{
CAF_PDM_InitObject( "Export Contour Map To Text", "", "", "Export the contour map of a contour map view to a text file" );
CAF_PDM_InitScriptableField( &m_exportFileName, "ExportFileName", QString(), "Export File Name", "", "", "Full path of the file to write" );
CAF_PDM_InitScriptableField( &m_exportLocalCoordinates,
"ExportLocalCoordinates",
false,
"Export Local Coordinates",
"",
"",
"Export local coordinates instead of UTM" );
CAF_PDM_InitScriptableField( &m_undefinedValueLabel,
"UndefinedValueLabel",
QString( "NaN" ),
"Undefined Value Label",
"",
"",
"Text written for undefined values" );
CAF_PDM_InitScriptableField( &m_excludeUndefinedValues, "ExcludeUndefinedValues", false, "Exclude Undefined Values", "", "", "Skip undefined values" );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::expected<caf::PdmObjectHandle*, QString> Rim3dView_exportContourMapToText::execute()
{
auto* view = self<Rim3dView>();
if ( !view ) return std::unexpected( "No view is available." );
if ( m_exportFileName().isEmpty() ) return std::unexpected( "No export file name specified." );
auto result = RicExportContourMapToTextFeature::exportContourMapToText( view,
m_exportFileName(),
m_exportLocalCoordinates(),
m_undefinedValueLabel(),
m_excludeUndefinedValues() );
if ( !result ) return std::unexpected( result.error() );
return nullptr;
}
@@ -127,3 +127,22 @@ public:
private:
caf::PdmField<int> m_timeStep;
};
//==================================================================================================
/// Export the contour map of a contour map view (Eclipse or GeoMech) to a text file.
//==================================================================================================
class Rim3dView_exportContourMapToText : public caf::PdmVoidObjectMethod
{
CAF_PDM_HEADER_INIT;
public:
Rim3dView_exportContourMapToText( caf::PdmObjectHandle* self );
std::expected<caf::PdmObjectHandle*, QString> execute() override;
private:
caf::PdmField<QString> m_exportFileName;
caf::PdmField<bool> m_exportLocalCoordinates;
caf::PdmField<QString> m_undefinedValueLabel;
caf::PdmField<bool> m_excludeUndefinedValues;
};