diff --git a/ApplicationLibCode/CommandFileInterface/RicfCommandForwarding.cpp b/ApplicationLibCode/CommandFileInterface/RicfCommandForwarding.cpp index 6e83dd4fdc..e3c0e3e2ca 100644 --- a/ApplicationLibCode/CommandFileInterface/RicfCommandForwarding.cpp +++ b/ApplicationLibCode/CommandFileInterface/RicfCommandForwarding.cpp @@ -42,18 +42,9 @@ std::expected RicfForwarding::findCase( int caseId ) RimProject* project = RimProject::current(); if ( !project ) return std::unexpected( "No project is available." ); - std::vector allCases = project->allGridCases(); - if ( caseId < 0 ) - { - // Matches RiaProjectModifier::firstCaseId(): the first occurrence is the first Eclipse result case - for ( RimCase* rimCase : allCases ) - { - if ( dynamic_cast( rimCase ) ) return rimCase; - } - return std::unexpected( "No Eclipse result cases found in project." ); - } + if ( caseId < 0 ) return std::unexpected( "No case id specified." ); - for ( RimCase* rimCase : allCases ) + for ( RimCase* rimCase : project->allGridCases() ) { if ( rimCase && rimCase->caseId() == caseId ) return rimCase; } @@ -61,6 +52,24 @@ std::expected RicfForwarding::findCase( int caseId ) return std::unexpected( QString( "Could not find case with ID %1" ).arg( caseId ) ); } +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +std::expected RicfForwarding::findCaseOrFirstEclipseResultCase( int caseId ) +{ + if ( caseId >= 0 ) return findCase( caseId ); + + RimProject* project = RimProject::current(); + if ( !project ) return std::unexpected( "No project is available." ); + + // Matches RiaProjectModifier::firstCaseId(): the first occurrence is the first Eclipse result case + for ( RimCase* rimCase : project->allGridCases() ) + { + if ( dynamic_cast( rimCase ) ) return rimCase; + } + return std::unexpected( "No Eclipse result cases found in project." ); +} + //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- @@ -95,11 +104,10 @@ std::expected RicfForwarding::findView( int viewId ) //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- -std::expected RicfForwarding::findCaseGroup( int groupId ) +namespace +{ +std::vector allCaseGroups( RimProject* project ) { - RimProject* project = RimProject::current(); - if ( !project ) return std::unexpected( "No project is available." ); - std::vector caseGroups; for ( RimOilField* oilField : project->oilFields() ) { @@ -110,14 +118,21 @@ std::expected RicfForwarding::findCaseGroup if ( caseGroup ) caseGroups.push_back( caseGroup ); } } + return caseGroups; +} +} // namespace - if ( groupId < 0 ) - { - if ( caseGroups.empty() ) return std::unexpected( "No grid case groups found in project." ); - return caseGroups.front(); - } +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +std::expected RicfForwarding::findCaseGroup( int groupId ) +{ + RimProject* project = RimProject::current(); + if ( !project ) return std::unexpected( "No project is available." ); - for ( RimIdenticalGridCaseGroup* caseGroup : caseGroups ) + if ( groupId < 0 ) return std::unexpected( "No grid case group id specified." ); + + for ( RimIdenticalGridCaseGroup* caseGroup : allCaseGroups( project ) ) { if ( caseGroup->groupId() == groupId ) return caseGroup; } @@ -125,6 +140,21 @@ std::expected RicfForwarding::findCaseGroup return std::unexpected( QString( "Could not find grid case group with ID %1" ).arg( groupId ) ); } +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +std::expected RicfForwarding::findCaseGroupOrFirst( int groupId ) +{ + if ( groupId >= 0 ) return findCaseGroup( groupId ); + + RimProject* project = RimProject::current(); + if ( !project ) return std::unexpected( "No project is available." ); + + auto caseGroups = allCaseGroups( project ); + if ( caseGroups.empty() ) return std::unexpected( "No grid case groups found in project." ); + return caseGroups.front(); +} + //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- diff --git a/ApplicationLibCode/CommandFileInterface/RicfCommandForwarding.h b/ApplicationLibCode/CommandFileInterface/RicfCommandForwarding.h index 97f08160f3..9c62bd509a 100644 --- a/ApplicationLibCode/CommandFileInterface/RicfCommandForwarding.h +++ b/ApplicationLibCode/CommandFileInterface/RicfCommandForwarding.h @@ -43,18 +43,26 @@ class PdmObjectHandle; //================================================================================================== namespace RicfForwarding { -/// Find a grid case by id. A negative id resolves to the first Eclipse result case in the project. +/// Find a grid case by id. A negative id is not accepted. std::expected findCase( int caseId ); +/// Find a grid case by id, or the first Eclipse result case in the project when the id is negative. +/// Only for legacy commands that documented this fallback (replaceCase). +std::expected findCaseOrFirstEclipseResultCase( int caseId ); + /// Find a view by id in the given case. A negative id is not accepted. std::expected findView( RimCase* rimCase, int viewId ); /// Find a 3D view by id anywhere in the project. A negative id is not accepted. std::expected findView( int viewId ); -/// Find a grid case group by id. A negative id resolves to the first case group in the project. +/// Find a grid case group by id. A negative id is not accepted. std::expected findCaseGroup( int groupId ); +/// Find a grid case group by id, or the first case group in the project when the id is negative. +/// Only for legacy commands that documented this fallback (replaceSourceCases). +std::expected findCaseGroupOrFirst( int groupId ); + /// Find a statistics case by case id in any grid case group. std::expected findStatisticsCase( int caseId ); diff --git a/ApplicationLibCode/CommandFileInterface/RicfReplaceCase.cpp b/ApplicationLibCode/CommandFileInterface/RicfReplaceCase.cpp index 60370ac261..b16feb5874 100644 --- a/ApplicationLibCode/CommandFileInterface/RicfReplaceCase.cpp +++ b/ApplicationLibCode/CommandFileInterface/RicfReplaceCase.cpp @@ -81,7 +81,7 @@ caf::PdmScriptResponse RicfSingleCaseReplace::execute() projectPath = lastProjectPath; } - auto rimCase = RicfForwarding::findCase( m_caseId() ); + auto rimCase = RicfForwarding::findCaseOrFirstEclipseResultCase( m_caseId() ); if ( !rimCase ) return RicfForwarding::errorResponse( rimCase.error(), commandName ); RimCase_replaceGrid method( rimCase.value() ); diff --git a/ApplicationLibCode/CommandFileInterface/RicfReplaceSourceCases.cpp b/ApplicationLibCode/CommandFileInterface/RicfReplaceSourceCases.cpp index dc0e4a0946..6b95dcade3 100644 --- a/ApplicationLibCode/CommandFileInterface/RicfReplaceSourceCases.cpp +++ b/ApplicationLibCode/CommandFileInterface/RicfReplaceSourceCases.cpp @@ -57,7 +57,7 @@ caf::PdmScriptResponse RicfReplaceSourceCases::execute() commandName ); } - auto caseGroup = RicfForwarding::findCaseGroup( m_caseGroupId() ); + auto caseGroup = RicfForwarding::findCaseGroupOrFirst( m_caseGroupId() ); if ( !caseGroup ) return RicfForwarding::errorResponse( caseGroup.error(), commandName ); RimIdenticalGridCaseGroup_replaceSourceCases method( caseGroup.value() );