Files
ResInsight/ApplicationLibCode/CommandFileInterface/RicfReplaceCase.cpp
T
Kristian Bendiksen 3eb63c0b8e #14772 Python: Add Case.replace_grid and forward replaceCase to it
First step in modernizing the command file interface: move the
implementation of legacy Ricf commands into object-oriented Rimc
methods, and let the Ricf command forward to them.

- Add RimCase_replaceGrid (PdmVoidObjectMethod on RimCase) with
  NewGridFile and optional ProjectFile parameters. Works for both
  Eclipse result cases and GeoMech cases. Generated in Python as
  Case.replace_grid().
- Add RicfCommandForwarding helpers for resolving ids to objects and
  converting std::expected results to PdmScriptResponse.
- RicfSingleCaseReplace::execute() now resolves the case and the
  executor's last project path, then forwards to RimCase_replaceGrid.
  Command file syntax and Commands.proto are unchanged.
- Case.replace() in Python is kept as a thin wrapper over
  replace_grid() and emits a DeprecationWarning.
2026-09-22 14:19:23 +02:00

163 lines
6.0 KiB
C++

/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2017 Statoil 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 "RicfReplaceCase.h"
#include "RiaApplication.h"
#include "RiaLogging.h"
#include "RiaProjectModifier.h"
#include "RicfCommandFileExecutor.h"
#include "RicfCommandForwarding.h"
#include "RimCase.h"
#include "RimProject.h"
#include "RimcCase.h"
#include "cafPdmFieldScriptingCapability.h"
#include <QDir>
#include <QFileInfo>
CAF_PDM_SOURCE_INIT( RicfSingleCaseReplace, "replaceCase" );
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RicfSingleCaseReplace::RicfSingleCaseReplace()
{
CAF_PDM_InitScriptableField( &m_caseId, "caseId", -1, "Case ID" );
CAF_PDM_InitScriptableField( &m_newGridFile, "newGridFile", QString(), "New Grid File" );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
int RicfSingleCaseReplace::caseId() const
{
return m_caseId;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString RicfSingleCaseReplace::filePath() const
{
return m_newGridFile;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
caf::PdmScriptResponse RicfSingleCaseReplace::execute()
{
const QString commandName = classKeyword();
// The legacy command file interface allows replacing a case in a project that has been opened by
// 'openProject' but is not saved. Resolve the project path here and pass it on explicitly.
QString projectPath = RimProject::current()->fileName();
if ( projectPath.isEmpty() )
{
QString lastProjectPath = RicfCommandFileExecutor::instance()->getLastProjectPath();
if ( lastProjectPath.isNull() )
{
return RicfForwarding::errorResponse( "The project must be saved as a file before calling 'replaceCase'.", commandName );
}
projectPath = lastProjectPath;
}
auto rimCase = RicfForwarding::findCase( m_caseId() );
if ( !rimCase ) return RicfForwarding::errorResponse( rimCase.error(), commandName );
RimCase_replaceGrid method( rimCase.value() );
method.setNewGridFile( m_newGridFile() );
method.setProjectFile( projectPath );
return RicfForwarding::toScriptResponse( method.execute(), commandName );
}
CAF_PDM_SOURCE_INIT( RicfMultiCaseReplace, "replaceMultipleCases" );
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RicfMultiCaseReplace::RicfMultiCaseReplace()
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicfMultiCaseReplace::setCaseReplacePairs( const std::map<int, QString>& caseIdToGridFileNameMap )
{
m_caseIdToGridFileNameMap = caseIdToGridFileNameMap;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
caf::PdmScriptResponse RicfMultiCaseReplace::execute()
{
if ( m_caseIdToGridFileNameMap.empty() )
{
QString errMsg( "replaceCaseImpl: No replacements available." );
RiaLogging::error( errMsg.toStdString() );
return caf::PdmScriptResponse( caf::PdmScriptResponse::COMMAND_ERROR, errMsg );
}
QString lastProjectPath = RicfCommandFileExecutor::instance()->getLastProjectPath();
if ( lastProjectPath.isNull() )
{
QString errMsg( "replaceCase: 'openProject' must be called before 'replaceCase' to specify project file to "
"replace case in." );
RiaLogging::error( errMsg.toStdString() );
return caf::PdmScriptResponse( caf::PdmScriptResponse::COMMAND_ERROR, errMsg );
}
cvf::ref<RiaProjectModifier> projectModifier = cvf::make_ref<RiaProjectModifier>();
for ( const auto& a : m_caseIdToGridFileNameMap )
{
const int caseId = a.first;
QString filePath = a.second;
QFileInfo casePathInfo( filePath );
if ( !casePathInfo.exists() )
{
QDir startDir( RiaApplication::instance()->startDir() );
filePath = startDir.absoluteFilePath( filePath );
}
if ( caseId < 0 )
{
projectModifier->setReplaceCaseFirstOccurrence( filePath );
}
else
{
projectModifier->setReplaceCase( caseId, filePath );
}
}
if ( !RiaApplication::instance()->loadProject( lastProjectPath, RiaApplication::ProjectLoadAction::PLA_NONE, projectModifier.p() ) )
{
QString errMsg( "Could not reload project" );
RiaLogging::error( errMsg.toStdString() );
return caf::PdmScriptResponse( caf::PdmScriptResponse::COMMAND_ERROR, errMsg );
}
return caf::PdmScriptResponse();
}