Skip to content

Commit

Permalink
Add polygon export to CSV and POL
Browse files Browse the repository at this point in the history
  • Loading branch information
magnesj committed Mar 1, 2024
1 parent 424e629 commit 06fbc5c
Show file tree
Hide file tree
Showing 9 changed files with 311 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ set(SOURCE_GROUP_HEADER_FILES
${CMAKE_CURRENT_LIST_DIR}/RicImportPolygonFileFeature.h
${CMAKE_CURRENT_LIST_DIR}/RicReloadPolygonFileFeature.h
${CMAKE_CURRENT_LIST_DIR}/RicDuplicatePolygonFeature.h
${CMAKE_CURRENT_LIST_DIR}/RicExportPolygonCsvFeature.h
${CMAKE_CURRENT_LIST_DIR}/RicExportPolygonPolFeature.h
)

set(SOURCE_GROUP_SOURCE_FILES
${CMAKE_CURRENT_LIST_DIR}/RicCreatePolygonFeature.cpp
${CMAKE_CURRENT_LIST_DIR}/RicImportPolygonFileFeature.cpp
${CMAKE_CURRENT_LIST_DIR}/RicReloadPolygonFileFeature.cpp
${CMAKE_CURRENT_LIST_DIR}/RicDuplicatePolygonFeature.cpp
${CMAKE_CURRENT_LIST_DIR}/RicExportPolygonCsvFeature.cpp
${CMAKE_CURRENT_LIST_DIR}/RicExportPolygonPolFeature.cpp
)

list(APPEND COMMAND_CODE_HEADER_FILES ${SOURCE_GROUP_HEADER_FILES})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2024 Equinor ASA
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////

#include "RicExportPolygonCsvFeature.h"

#include "RiaGuiApplication.h"
#include "RiaLogging.h"

#include "Polygons/RimPolygon.h"
#include "Polygons/RimPolygonInView.h"
#include "Polygons/RimPolygonTools.h"

#include "RiuFileDialogTools.h"

#include "cafSelectionManager.h"

#include <QAction>
#include <QFileInfo>

CAF_CMD_SOURCE_INIT( RicExportPolygonCsvFeature, "RicExportPolygonCsvFeature" );

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicExportPolygonCsvFeature::onActionTriggered( bool isChecked )
{
auto sourcePolygon = caf::SelectionManager::instance()->selectedItemOfType<RimPolygon>();
if ( !sourcePolygon )
{
auto sourcePolygonInView = caf::SelectionManager::instance()->selectedItemOfType<RimPolygonInView>();
if ( sourcePolygonInView )
{
sourcePolygon = sourcePolygonInView->polygon();
}
}

if ( !sourcePolygon ) return;

auto app = RiaGuiApplication::instance();
auto fallbackPath = app->lastUsedDialogDirectory( "BINARY_GRID" );
auto polygonPath = app->lastUsedDialogDirectoryWithFallback( RimPolygonTools::polygonCacheName(), fallbackPath );
auto polygonFileName = polygonPath + "/" + sourcePolygon->name() + ".csv";

auto fileName = RiuFileDialogTools::getSaveFileName( nullptr,
"Select File for Polygon Export to CSV",
polygonFileName,
"CSV Files (*.csv);;All files(*.*)" );

if ( !RimPolygonTools::exportPolygonCsv( sourcePolygon, fileName ) )
{
RiaLogging::error( "Failed to export polygon to " + fileName );
}

RiaLogging::info( "Completed polygon export to " + fileName );
RiaApplication::instance()->setLastUsedDialogDirectory( RimPolygonTools::polygonCacheName(), QFileInfo( fileName ).absolutePath() );
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicExportPolygonCsvFeature::setupActionLook( QAction* actionToSetup )
{
actionToSetup->setText( "Export Polygon CSV" );
actionToSetup->setIcon( QIcon( ":/Save.svg" ) );
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2024 Equinor ASA
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////

#pragma once

#include "cafCmdFeature.h"

//==================================================================================================
///
//==================================================================================================
class RicExportPolygonCsvFeature : public caf::CmdFeature
{
CAF_CMD_HEADER_INIT;

protected:
void onActionTriggered( bool isChecked ) override;
void setupActionLook( QAction* actionToSetup ) override;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2024 Equinor ASA
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////

#include "RicExportPolygonPolFeature.h"

#include "RiaGuiApplication.h"
#include "RiaLogging.h"

#include "Polygons/RimPolygon.h"
#include "Polygons/RimPolygonInView.h"
#include "Polygons/RimPolygonTools.h"

#include "RiuFileDialogTools.h"

#include "cafSelectionManager.h"

#include <QAction>
#include <QFileInfo>

CAF_CMD_SOURCE_INIT( RicExportPolygonPolFeature, "RicExportPolygonPolFeature" );

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicExportPolygonPolFeature::onActionTriggered( bool isChecked )
{
auto sourcePolygon = caf::SelectionManager::instance()->selectedItemOfType<RimPolygon>();
if ( !sourcePolygon )
{
auto sourcePolygonInView = caf::SelectionManager::instance()->selectedItemOfType<RimPolygonInView>();
if ( sourcePolygonInView )
{
sourcePolygon = sourcePolygonInView->polygon();
}
}

if ( !sourcePolygon ) return;

auto app = RiaGuiApplication::instance();
auto fallbackPath = app->lastUsedDialogDirectory( "BINARY_GRID" );
auto polygonPath = app->lastUsedDialogDirectoryWithFallback( RimPolygonTools::polygonCacheName(), fallbackPath );
auto polygonFileName = polygonPath + "/" + sourcePolygon->name() + ".pol";

auto fileName = RiuFileDialogTools::getSaveFileName( nullptr,
"Select File for Polygon Export to POL",
polygonFileName,
"POL Files (*.pol);;All files(*.*)" );

if ( !RimPolygonTools::exportPolygonPol( sourcePolygon, fileName ) )
{
RiaLogging::error( "Failed to export polygon to " + fileName );
}

RiaLogging::info( "Completed polygon export to " + fileName );
RiaApplication::instance()->setLastUsedDialogDirectory( RimPolygonTools::polygonCacheName(), QFileInfo( fileName ).absolutePath() );
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicExportPolygonPolFeature::setupActionLook( QAction* actionToSetup )
{
actionToSetup->setText( "Export Polygon POL" );
actionToSetup->setIcon( QIcon( ":/Save.svg" ) );
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2024 Equinor ASA
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////

#pragma once

#include "cafCmdFeature.h"

//==================================================================================================
///
//==================================================================================================
class RicExportPolygonPolFeature : public caf::CmdFeature
{
CAF_CMD_HEADER_INIT;

protected:
void onActionTriggered( bool isChecked ) override;
void setupActionLook( QAction* actionToSetup ) override;
};
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "Polygons/RimPolygon.h"
#include "Polygons/RimPolygonCollection.h"
#include "Polygons/RimPolygonFile.h"
#include "Polygons/RimPolygonTools.h"
#include "RimOilField.h"
#include "RimProject.h"

Expand All @@ -39,18 +40,21 @@ CAF_CMD_SOURCE_INIT( RicImportPolygonFileFeature, "RicImportPolygonFileFeature"
//--------------------------------------------------------------------------------------------------
void RicImportPolygonFileFeature::onActionTriggered( bool isChecked )
{
RiaApplication* app = RiaApplication::instance();
QString defaultDir = app->lastUsedDialogDirectory( "BINARY_GRID" );
QStringList fileNames = RiuFileDialogTools::getOpenFileNames( Riu3DMainWindowTools::mainWindowWidget(),
RiaApplication* app = RiaApplication::instance();

auto fallbackPath = app->lastUsedDialogDirectory( "BINARY_GRID" );
auto polygonPath = app->lastUsedDialogDirectoryWithFallback( RimPolygonTools::polygonCacheName(), fallbackPath );

QStringList fileNames = RiuFileDialogTools::getOpenFileNames( Riu3DMainWindowTools::mainWindowWidget(),
"Import Polygons",
defaultDir,
polygonPath,
"Polylines (*.csv *.dat *.pol);;Text Files (*.txt);;Polylines "
"(*.dat);;Polylines (*.pol);;Polylines (*.csv);;All Files (*.*)" );
"(*.dat);;Polylines (*.pol);;Polylines (*.csv);;All Files (*.*)" );

if ( fileNames.isEmpty() ) return;

// Remember the path to next time
app->setLastUsedDialogDirectory( "BINARY_GRID", QFileInfo( fileNames.last() ).absolutePath() );
app->setLastUsedDialogDirectory( RimPolygonTools::polygonCacheName(), QFileInfo( fileNames.last() ).absolutePath() );

auto proj = RimProject::current();
auto polygonCollection = proj->activeOilField()->polygonCollection();
Expand Down
3 changes: 3 additions & 0 deletions ApplicationLibCode/ProjectDataModel/Polygons/RimPolygon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ void RimPolygon::appendMenuItems( caf::CmdFeatureMenuBuilder& menuBuilder ) cons
menuBuilder << "RicDuplicatePolygonFeature";
menuBuilder << "RicNewPolygonIntersectionFeature";
menuBuilder << "RicNewPolygonFilterFeature";
menuBuilder << "Separator";
menuBuilder << "RicExportPolygonCsvFeature";
menuBuilder << "RicExportPolygonPolFeature";
}

//--------------------------------------------------------------------------------------------------
Expand Down
64 changes: 64 additions & 0 deletions ApplicationLibCode/ProjectDataModel/Polygons/RimPolygonTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,70 @@ void RimPolygonTools::selectPolygonInView( RimPolygon* polygon, caf::PdmObject*
}
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RimPolygonTools::exportPolygonCsv( const RimPolygon* polygon, const QString& filePath )
{
if ( !polygon ) return false;

QFile file( filePath );
if ( !file.open( QIODevice::WriteOnly | QIODevice::Text ) )
{
return false;
}

QTextStream out( &file );
const int precision = 8;
out.setRealNumberPrecision( precision );

out << "X,Y,Z" << endl;

for ( const auto& point : polygon->pointsInDomainCoords() )
{
out << point.x() << "," << point.y() << "," << -point.z() << endl;
}

file.close();

return true;
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RimPolygonTools::exportPolygonPol( const RimPolygon* polygon, const QString& filePath )
{
if ( !polygon ) return false;

QFile file( filePath );
if ( !file.open( QIODevice::WriteOnly | QIODevice::Text ) ) return false;

QTextStream out( &file );
const int precision = 8;
out.setRealNumberPrecision( precision );

for ( const auto& point : polygon->pointsInDomainCoords() )
{
out << point.x() << " " << point.y() << " " << -point.z() << endl;
}

const double endOfPolygon = 999.0;
out << endOfPolygon << " " << endOfPolygon << " " << endOfPolygon << endl;

file.close();

return true;
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString RimPolygonTools::polygonCacheName()
{
return "POLYGON";
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ class RimPolygonTools
public:
static void enableEditOfPolygonInView( RimPolygon* polygon, caf::PdmObject* sourceObject );
static void selectPolygonInView( RimPolygon* polygon, caf::PdmObject* sourceObject );
static bool exportPolygonCsv( const RimPolygon* polygon, const QString& filePath );
static bool exportPolygonPol( const RimPolygon* polygon, const QString& filePath );

static QString polygonCacheName();

private:
static RimPolygonInView* findPolygonInView( RimPolygon* polygon, caf::PdmObject* sourceObject );
Expand Down

0 comments on commit 06fbc5c

Please sign in to comment.