2017-07-28 04:28:55 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Copyright (C) 2017 Statoil ASA
|
2019-09-06 03:40:57 -05:00
|
|
|
//
|
2017-07-28 04:28:55 -05:00
|
|
|
// 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.
|
2019-09-06 03:40:57 -05:00
|
|
|
//
|
2017-07-28 04:28:55 -05:00
|
|
|
// 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.
|
2019-09-06 03:40:57 -05:00
|
|
|
//
|
|
|
|
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
|
2017-07-28 04:28:55 -05:00
|
|
|
// for more details.
|
|
|
|
//
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "RicfReplaceCase.h"
|
|
|
|
|
|
|
|
#include "RiaApplication.h"
|
|
|
|
#include "RiaLogging.h"
|
|
|
|
#include "RiaProjectModifier.h"
|
|
|
|
|
2018-02-05 01:47:10 -06:00
|
|
|
#include "RicfCommandFileExecutor.h"
|
|
|
|
|
|
|
|
#include "RimProject.h"
|
|
|
|
|
2020-08-21 04:13:07 -05:00
|
|
|
#include "cafPdmFieldScriptingCapability.h"
|
2020-03-10 08:11:22 -05:00
|
|
|
|
2019-07-18 01:50:58 -05:00
|
|
|
#include <QDir>
|
|
|
|
#include <QFileInfo>
|
|
|
|
|
2019-09-06 03:40:57 -05:00
|
|
|
CAF_PDM_SOURCE_INIT( RicfSingleCaseReplace, "replaceCase" );
|
2017-07-28 04:28:55 -05:00
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
///
|
2017-07-28 04:28:55 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2018-02-05 01:47:10 -06:00
|
|
|
RicfSingleCaseReplace::RicfSingleCaseReplace()
|
2017-07-28 04:28:55 -05:00
|
|
|
{
|
2021-11-14 07:15:12 -06:00
|
|
|
CAF_PDM_InitScriptableField( &m_caseId, "caseId", -1, "Case ID" );
|
|
|
|
CAF_PDM_InitScriptableField( &m_newGridFile, "newGridFile", QString(), "New Grid File" );
|
2017-07-28 04:28:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
///
|
2017-07-28 04:28:55 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2018-02-05 01:47:10 -06:00
|
|
|
int RicfSingleCaseReplace::caseId() const
|
|
|
|
{
|
|
|
|
return m_caseId;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
///
|
2018-02-05 01:47:10 -06:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
QString RicfSingleCaseReplace::filePath() const
|
|
|
|
{
|
|
|
|
return m_newGridFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
///
|
2018-02-05 01:47:10 -06:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-03-10 08:11:22 -05:00
|
|
|
caf::PdmScriptResponse RicfSingleCaseReplace::execute()
|
2018-02-05 01:47:10 -06:00
|
|
|
{
|
2020-09-23 02:49:42 -05:00
|
|
|
QString projectPath = RimProject::current()->fileName();
|
|
|
|
if ( projectPath.isEmpty() )
|
2019-07-16 10:12:08 -05:00
|
|
|
{
|
2020-09-23 02:49:42 -05:00
|
|
|
QString lastProjectPath = RicfCommandFileExecutor::instance()->getLastProjectPath();
|
|
|
|
if ( lastProjectPath.isNull() )
|
|
|
|
{
|
|
|
|
QString errMsg( "replaceCase: The project must be saved as a file before calling 'replaceCase'." );
|
|
|
|
RiaLogging::error( errMsg );
|
|
|
|
return caf::PdmScriptResponse( caf::PdmScriptResponse::COMMAND_ERROR, errMsg );
|
|
|
|
}
|
|
|
|
projectPath = lastProjectPath;
|
2019-07-16 10:12:08 -05:00
|
|
|
}
|
|
|
|
|
2020-10-22 01:55:26 -05:00
|
|
|
cvf::ref<RiaProjectModifier> projectModifier = cvf::make_ref<RiaProjectModifier>();
|
2018-02-05 01:47:10 -06:00
|
|
|
|
2019-09-06 03:40:57 -05:00
|
|
|
QString filePath = m_newGridFile();
|
|
|
|
QFileInfo casePathInfo( filePath );
|
|
|
|
if ( !casePathInfo.exists() )
|
2019-07-18 01:50:58 -05:00
|
|
|
{
|
2019-09-06 03:40:57 -05:00
|
|
|
QDir startDir( RiaApplication::instance()->startDir() );
|
|
|
|
filePath = startDir.absoluteFilePath( m_newGridFile() );
|
2019-07-18 01:50:58 -05:00
|
|
|
}
|
|
|
|
|
2019-09-06 03:40:57 -05:00
|
|
|
if ( m_caseId() < 0 )
|
2019-07-16 10:12:08 -05:00
|
|
|
{
|
2019-09-06 03:40:57 -05:00
|
|
|
projectModifier->setReplaceCaseFirstOccurrence( filePath );
|
2019-07-16 10:12:08 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-09-06 03:40:57 -05:00
|
|
|
projectModifier->setReplaceCase( m_caseId(), filePath );
|
2019-07-16 10:12:08 -05:00
|
|
|
}
|
2018-02-05 01:47:10 -06:00
|
|
|
|
2020-09-23 02:49:42 -05:00
|
|
|
if ( !RiaApplication::instance()->loadProject( projectPath,
|
2020-04-23 16:08:14 -05:00
|
|
|
RiaApplication::ProjectLoadAction::PLA_NONE,
|
|
|
|
projectModifier.p() ) )
|
2019-07-16 10:12:08 -05:00
|
|
|
{
|
2019-09-06 03:40:57 -05:00
|
|
|
QString errMsg( "Could not reload project" );
|
|
|
|
RiaLogging::error( errMsg );
|
2020-03-10 08:11:22 -05:00
|
|
|
return caf::PdmScriptResponse( caf::PdmScriptResponse::COMMAND_ERROR, errMsg );
|
2019-07-16 10:12:08 -05:00
|
|
|
}
|
2018-02-05 01:47:10 -06:00
|
|
|
|
2020-03-10 08:11:22 -05:00
|
|
|
return caf::PdmScriptResponse();
|
2019-07-16 10:12:08 -05:00
|
|
|
}
|
2018-02-05 01:47:10 -06:00
|
|
|
|
2019-09-06 03:40:57 -05:00
|
|
|
CAF_PDM_SOURCE_INIT( RicfMultiCaseReplace, "replaceMultipleCases" );
|
2018-02-05 01:47:10 -06:00
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
///
|
2018-02-05 01:47:10 -06:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-02-12 04:13:38 -06:00
|
|
|
RicfMultiCaseReplace::RicfMultiCaseReplace()
|
|
|
|
{
|
|
|
|
}
|
2018-02-05 01:47:10 -06:00
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
///
|
2018-02-05 01:47:10 -06:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
void RicfMultiCaseReplace::setCaseReplacePairs( const std::map<int, QString>& caseIdToGridFileNameMap )
|
2018-02-05 01:47:10 -06:00
|
|
|
{
|
|
|
|
m_caseIdToGridFileNameMap = caseIdToGridFileNameMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
///
|
2018-02-05 01:47:10 -06:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-03-10 08:11:22 -05:00
|
|
|
caf::PdmScriptResponse RicfMultiCaseReplace::execute()
|
2018-02-05 01:47:10 -06:00
|
|
|
{
|
2019-09-06 03:40:57 -05:00
|
|
|
if ( m_caseIdToGridFileNameMap.empty() )
|
2017-07-28 04:28:55 -05:00
|
|
|
{
|
2019-09-06 03:40:57 -05:00
|
|
|
QString errMsg( "replaceCaseImpl: No replacements available." );
|
|
|
|
RiaLogging::error( errMsg );
|
2020-03-10 08:11:22 -05:00
|
|
|
return caf::PdmScriptResponse( caf::PdmScriptResponse::COMMAND_ERROR, errMsg );
|
2017-07-28 04:28:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
QString lastProjectPath = RicfCommandFileExecutor::instance()->getLastProjectPath();
|
2019-09-06 03:40:57 -05:00
|
|
|
if ( lastProjectPath.isNull() )
|
2017-07-28 04:28:55 -05:00
|
|
|
{
|
2019-09-06 03:40:57 -05:00
|
|
|
QString errMsg( "replaceCase: 'openProject' must be called before 'replaceCase' to specify project file to "
|
|
|
|
"replace case in." );
|
|
|
|
RiaLogging::error( errMsg );
|
2020-03-10 08:11:22 -05:00
|
|
|
return caf::PdmScriptResponse( caf::PdmScriptResponse::COMMAND_ERROR, errMsg );
|
2017-07-28 04:28:55 -05:00
|
|
|
}
|
|
|
|
|
2020-10-22 01:55:26 -05:00
|
|
|
cvf::ref<RiaProjectModifier> projectModifier = cvf::make_ref<RiaProjectModifier>();
|
2019-09-06 03:40:57 -05:00
|
|
|
for ( const auto& a : m_caseIdToGridFileNameMap )
|
2017-07-28 04:28:55 -05:00
|
|
|
{
|
2019-09-06 03:40:57 -05:00
|
|
|
const int caseId = a.first;
|
|
|
|
QString filePath = a.second;
|
|
|
|
QFileInfo casePathInfo( filePath );
|
|
|
|
if ( !casePathInfo.exists() )
|
2019-07-18 01:50:58 -05:00
|
|
|
{
|
2019-09-06 03:40:57 -05:00
|
|
|
QDir startDir( RiaApplication::instance()->startDir() );
|
|
|
|
filePath = startDir.absoluteFilePath( filePath );
|
2019-07-18 01:50:58 -05:00
|
|
|
}
|
|
|
|
|
2019-09-06 03:40:57 -05:00
|
|
|
if ( caseId < 0 )
|
2018-02-05 01:47:10 -06:00
|
|
|
{
|
2019-09-06 03:40:57 -05:00
|
|
|
projectModifier->setReplaceCaseFirstOccurrence( filePath );
|
2018-02-05 01:47:10 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-09-06 03:40:57 -05:00
|
|
|
projectModifier->setReplaceCase( caseId, filePath );
|
2018-02-05 01:47:10 -06:00
|
|
|
}
|
2017-07-28 04:28:55 -05:00
|
|
|
}
|
|
|
|
|
2020-04-23 16:08:14 -05:00
|
|
|
if ( !RiaApplication::instance()->loadProject( lastProjectPath,
|
|
|
|
RiaApplication::ProjectLoadAction::PLA_NONE,
|
|
|
|
projectModifier.p() ) )
|
2019-05-23 06:59:19 -05:00
|
|
|
{
|
2019-09-06 03:40:57 -05:00
|
|
|
QString errMsg( "Could not reload project" );
|
|
|
|
RiaLogging::error( errMsg );
|
2020-03-10 08:11:22 -05:00
|
|
|
return caf::PdmScriptResponse( caf::PdmScriptResponse::COMMAND_ERROR, errMsg );
|
2019-05-23 06:59:19 -05:00
|
|
|
}
|
|
|
|
|
2020-03-10 08:11:22 -05:00
|
|
|
return caf::PdmScriptResponse();
|
2017-07-28 04:28:55 -05:00
|
|
|
}
|