#14772 Python: Add View.clone as object method and forward cloneView to it

Rim3dView_clone copies an Eclipse or GeoMech view into the same case
and returns the new view. RicfCloneView forwards to it. Add a
project-wide RicfForwarding::findView(viewId) helper. The hand-written
Python View.clone() wrapper is removed since the generated method has
the same name and signature.
This commit is contained in:
Kristian Bendiksen committed 2026-09-22 14:19:23 +02:00
1 parent bdef98203b
commit e87e372c7b
7 files changed
+121 -59

No files matched your search

@@ -1,22 +1,12 @@
#include "RicfCloneView.h"
#include "RiaLogging.h"
#include "RicfCommandForwarding.h"
#include "RicfCreateView.h"
#include "Rim3dView.h"
#include "RimEclipseCase.h"
#include "RimEclipseView.h"
#include "RimGeoMechCase.h"
#include "RimGeoMechView.h"
#include "RimProject.h"
#include "Riu3DMainWindowTools.h"
#include "RimcGridView.h"
#include "cafPdmFieldScriptingCapability.h"
#include "cafSelectionManager.h"
#include <QAction>
CAF_PDM_SOURCE_INIT( RicfCloneView, "cloneView" );
@@ -33,46 +23,20 @@ RicfCloneView::RicfCloneView()
//--------------------------------------------------------------------------------------------------
caf::PdmScriptResponse RicfCloneView::execute()
{
RimProject* project = RimProject::current();
std::vector<Rim3dView*> allViews = project->descendantsIncludingThisOfType<Rim3dView>();
const QString commandName = classKeyword();
for ( Rim3dView* view : allViews )
{
if ( view->id() == m_viewId() )
{
const RimEclipseView* eclipseView = dynamic_cast<const RimEclipseView*>( view );
const RimGeoMechView* geoMechView = dynamic_cast<const RimGeoMechView*>( view );
auto view = RicfForwarding::findView( m_viewId() );
if ( !view ) return RicfForwarding::errorResponse( view.error(), commandName );
int newViewId = -1;
if ( eclipseView )
{
RimEclipseCase* eclipseCase = eclipseView->eclipseCase();
RimEclipseView* newEclipseView = eclipseCase->createCopyAndAddView( eclipseView );
newEclipseView->loadDataAndUpdate();
newViewId = newEclipseView->id();
eclipseCase->updateConnectedEditors();
Riu3DMainWindowTools::setExpanded( newEclipseView );
}
else if ( geoMechView )
{
RimGeoMechCase* geoMechCase = geoMechView->geoMechCase();
RimGeoMechView* newGeoMechView = geoMechCase->createCopyAndAddView( geoMechView );
newGeoMechView->loadDataAndUpdate();
newViewId = newGeoMechView->id();
geoMechCase->updateConnectedEditors();
Riu3DMainWindowTools::setExpanded( view );
}
Rim3dView_clone method( view.value() );
if ( newViewId >= 0 )
{
caf::PdmScriptResponse response;
response.setResult( new RicfCreateViewResult( newViewId ) );
return response;
}
}
}
auto result = method.execute();
if ( !result ) return RicfForwarding::errorResponse( result.error(), commandName );
QString error = QString( "cloneView: Could not clone view with id %1" ).arg( m_viewId() );
RiaLogging::error( error.toStdString() );
return caf::PdmScriptResponse( caf::PdmScriptResponse::COMMAND_ERROR, error );
auto* newView = dynamic_cast<Rim3dView*>( result.value() );
if ( !newView ) return RicfForwarding::errorResponse( "Cloned object is not a view", commandName );
caf::PdmScriptResponse response;
response.setResult( new RicfCreateViewResult( newView->id() ) );
return response;
}
@@ -69,6 +69,22 @@ std::expected<Rim3dView*, QString> RicfForwarding::findView( RimCase* rimCase, i
return std::unexpected( QString( "Could not find view with ID %1 in case %2" ).arg( viewId ).arg( rimCase->caseId() ) );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::expected<Rim3dView*, QString> RicfForwarding::findView( int viewId )
{
RimProject* project = RimProject::current();
if ( !project ) return std::unexpected( "No project is available." );
for ( Rim3dView* view : project->allViews() )
{
if ( view && view->id() == viewId ) return view;
}
return std::unexpected( QString( "Could not find view with ID %1" ).arg( viewId ) );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@@ -45,6 +45,9 @@ std::expected<RimCase*, QString> findCase( int caseId );
/// Find a view by id in the given case. A negative id is not accepted.
std::expected<Rim3dView*, QString> findView( RimCase* rimCase, int viewId );
/// Find a 3D view by id anywhere in the project. A negative id is not accepted.
std::expected<Rim3dView*, QString> findView( int viewId );
/// Convert the result of a Rimc method execution to a script response, logging errors with the command name.
caf::PdmScriptResponse toScriptResponse( const std::expected<caf::PdmObjectHandle*, QString>& result, const QString& commandName );
@@ -24,11 +24,16 @@
#include "Polygons/RimPolygon.h"
#include "Polygons/RimPolygonInViewCollection.h"
#include "Rim3dView.h"
#include "RimEclipseCase.h"
#include "RimEclipseView.h"
#include "RimGeoMechCase.h"
#include "RimGeoMechView.h"
#include "RimGridView.h"
#include "Surfaces/RimSurface.h"
#include "Surfaces/RimSurfaceInViewCollection.h"
#include "Riu3DMainWindowTools.h"
#include "cafPdmFieldScriptingCapability.h"
#include "cvfArray.h"
@@ -204,3 +209,58 @@ std::expected<caf::PdmObjectHandle*, QString> RimcGridView_setSurfaceProperty::e
gridView->scheduleCreateDisplayModelAndRedraw();
return nullptr;
}
CAF_PDM_OBJECT_METHOD_SOURCE_INIT( Rim3dView, Rim3dView_clone, "clone" );
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
Rim3dView_clone::Rim3dView_clone( caf::PdmObjectHandle* self )
: caf::PdmObjectCreationMethod( self )
{
CAF_PDM_InitObject( "Clone View", "", "", "Clone the view and add the copy to the same case" );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::expected<caf::PdmObjectHandle*, QString> Rim3dView_clone::execute()
{
auto* view = self<Rim3dView>();
if ( !view ) return std::unexpected( "No view is available." );
Rim3dView* newView = nullptr;
if ( auto* eclipseView = dynamic_cast<RimEclipseView*>( view ) )
{
RimEclipseCase* eclipseCase = eclipseView->eclipseCase();
if ( !eclipseCase ) return std::unexpected( "The view has no case." );
RimEclipseView* newEclipseView = eclipseCase->createCopyAndAddView( eclipseView );
newEclipseView->loadDataAndUpdate();
eclipseCase->updateConnectedEditors();
newView = newEclipseView;
}
else if ( auto* geoMechView = dynamic_cast<RimGeoMechView*>( view ) )
{
RimGeoMechCase* geoMechCase = geoMechView->geoMechCase();
if ( !geoMechCase ) return std::unexpected( "The view has no case." );
RimGeoMechView* newGeoMechView = geoMechCase->createCopyAndAddView( geoMechView );
newGeoMechView->loadDataAndUpdate();
geoMechCase->updateConnectedEditors();
newView = newGeoMechView;
}
if ( !newView ) return std::unexpected( QString( "Could not clone view '%1'" ).arg( view->name() ) );
Riu3DMainWindowTools::setExpanded( newView );
return newView;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString Rim3dView_clone::classKeywordReturnedType() const
{
return Rim3dView::classKeywordStatic();
}
@@ -95,3 +95,17 @@ private:
caf::PdmPtrField<RimSurface*> m_surface;
caf::PdmField<QString> m_propertyName;
};
//==================================================================================================
/// Clone the view and add the copy to the same case. Supports Eclipse and GeoMech views.
//==================================================================================================
class Rim3dView_clone : public caf::PdmObjectCreationMethod
{
CAF_PDM_HEADER_INIT;
public:
Rim3dView_clone( caf::PdmObjectHandle* self );
std::expected<caf::PdmObjectHandle*, QString> execute() override;
QString classKeywordReturnedType() const override;
};
@@ -111,6 +111,20 @@ def test_views_and_view_lookup(rips_instance, initialize_test):
assert project.view(999999) is None
def test_clone_view(rips_instance, initialize_test):
case_path = dataroot.PATH + "/TEST10K_FLT_LGR_NNC/TEST10K_FLT_LGR_NNC.EGRID"
case = rips_instance.project.load_case(case_path)
view = case.create_view()
project = rips_instance.project
view_count = len(project.views())
cloned = view.clone()
assert cloned is not None
assert cloned.id != view.id
assert cloned.case().id == case.id
assert len(project.views()) == view_count + 1
def test_link_and_unlink_views(rips_instance, initialize_test):
case_path = dataroot.PATH + "/TEST10K_FLT_LGR_NNC/TEST10K_FLT_LGR_NNC.EGRID"
case = rips_instance.project.load_case(case_path)
-9
View File
@@ -82,15 +82,6 @@ def apply_flow_diagnostics_cell_result(
cell_result.update()
@add_method(View)
def clone(self):
"""Clone the current view"""
view_id = self._execute_command(
cloneView=Cmd.CloneViewRequest(viewId=self.id)
).createViewResult.viewId
return self.case().view(view_id)
@add_method(View)
def set_time_step(self, time_step):
"""Set the time step for current view"""