From bcae2ccbcbe600fcbb9c35ea5573ef1f3842a1d1 Mon Sep 17 00:00:00 2001 From: Stefan Crooijmans Date: Tue, 3 Nov 2026 06:09:55 +0100 Subject: [PATCH] #13861 Python: Add surfaces() and surface_by_name() to the rips API Project.surfaces(surface_type) returns every surface in the project, SurfaceCollection.surfaces(surface_type, recursive) the surfaces of one folder, and Project.surface_by_name(name, surface_type, folder) looks a surface up by its user description. Subclasses of the requested type are included; ensemble surfaces are skipped unless include_ensembles is set. Project.surface_folder(name) now returns None when the folder does not exist, as PythonExamples/surfaces_and_visualization/surface_import.py already expects, instead of failing with a gRPC error. --- .../ProjectDataModelCommands/RimcProject.cpp | 2 +- .../ProjectDataModelCommands/RimcProject.h | 2 +- GrpcInterface/Python/rips/surface.py | 120 +++++++++++++++++- .../Python/rips/tests/test_surfaces.py | 84 ++++++++++++ 4 files changed, 204 insertions(+), 4 deletions(-) diff --git a/ApplicationLibCode/ProjectDataModelCommands/RimcProject.cpp b/ApplicationLibCode/ProjectDataModelCommands/RimcProject.cpp index d5cdee4fdd..ea8baf3c7c 100644 --- a/ApplicationLibCode/ProjectDataModelCommands/RimcProject.cpp +++ b/ApplicationLibCode/ProjectDataModelCommands/RimcProject.cpp @@ -245,7 +245,7 @@ CAF_PDM_OBJECT_METHOD_SOURCE_INIT( RimProject, RimProject_surfaceFolder, "surfac /// //-------------------------------------------------------------------------------------------------- RimProject_surfaceFolder::RimProject_surfaceFolder( caf::PdmObjectHandle* self ) - : PdmObjectCreationMethod( self ) + : PdmObjectMethod( self, PdmObjectMethod::NullPointerType::NULL_IS_VALID, PdmObjectMethod::ResultType::PERSISTENT_TRUE ) { CAF_PDM_InitObject( "Get Surface Folder", "", "", "Get Surface Folder" ); diff --git a/ApplicationLibCode/ProjectDataModelCommands/RimcProject.h b/ApplicationLibCode/ProjectDataModelCommands/RimcProject.h index da736b8a92..f025d42f9b 100644 --- a/ApplicationLibCode/ProjectDataModelCommands/RimcProject.h +++ b/ApplicationLibCode/ProjectDataModelCommands/RimcProject.h @@ -90,7 +90,7 @@ private: //================================================================================================== /// //================================================================================================== -class RimProject_surfaceFolder : public caf::PdmObjectCreationMethod +class RimProject_surfaceFolder : public caf::PdmObjectMethod { CAF_PDM_HEADER_INIT; diff --git a/GrpcInterface/Python/rips/surface.py b/GrpcInterface/Python/rips/surface.py index b3584d1836..d83b175529 100644 --- a/GrpcInterface/Python/rips/surface.py +++ b/GrpcInterface/Python/rips/surface.py @@ -1,10 +1,18 @@ +import inspect import uuid from .exception import RipsError from .pdmobject import add_method from .project import Project -from .resinsight_classes import RegularSurface -from typing import List +from .resinsight_classes import ( + EnsembleSurface, + RegularSurface, + SurfaceCollection, + SurfaceInterface, +) +from typing import List, Optional, Type, TypeVar + +SurfaceT = TypeVar("SurfaceT", bound=SurfaceInterface) @add_method(RegularSurface) @@ -61,3 +69,111 @@ def get_property(self: RegularSurface, name: str) -> List[float]: project.remove_key_values(key) except Exception: pass + + +def _check_surface_type(surface_type: Type[SurfaceT]) -> None: + if not ( + inspect.isclass(surface_type) and issubclass(surface_type, SurfaceInterface) + ): + raise RipsError( + "surface_type must be a surface class such as rips.SurfaceInterface, " + "rips.Surface or rips.RegularSurface, got {}".format(surface_type) + ) + + +@add_method(SurfaceCollection) +def surfaces( + self: SurfaceCollection, + surface_type: Type[SurfaceT] = SurfaceInterface, + recursive: bool = False, + include_ensembles: bool = False, +) -> List[SurfaceT]: + """Get the surfaces in this folder + + Arguments: + surface_type (class): Only return surfaces of this class, subclasses included. + rips.SurfaceInterface (the default) returns every surface, rips.Surface + returns file surfaces only, and rips.RegularSurface also returns + rips.RegularFileSurface. + recursive (bool): Also return the surfaces in sub folders. + include_ensembles (bool): When recursive, also descend into ensemble surfaces + (rips.EnsembleSurface), which hold six statistics surfaces and one surface + per realization. Off by default, since that can be a long list. + + Returns: + List of surfaces of the requested class, this folder first, then its sub folders + """ + _check_surface_type(surface_type) + + result: List[SurfaceT] = [ + s for s in self.surfaces_field() if isinstance(s, surface_type) + ] + if recursive: + for folder in self.sub_collections(): + if isinstance(folder, EnsembleSurface) and not include_ensembles: + continue + result.extend( + folder.surfaces( + surface_type, recursive=True, include_ensembles=include_ensembles + ) + ) + return result + + +@add_method(Project) # type: ignore[no-redef] +def surfaces( # noqa: F811 + self: Project, + surface_type: Type[SurfaceT] = SurfaceInterface, + include_ensembles: bool = False, +) -> List[SurfaceT]: + """Get all surfaces in the project, from the top surface folder and its sub folders + + Arguments: + surface_type (class): Only return surfaces of this class, subclasses included. + rips.SurfaceInterface (the default) returns every surface, rips.Surface + returns file surfaces only, and rips.RegularSurface also returns + rips.RegularFileSurface. + include_ensembles (bool): Also return the statistics and realization surfaces + of ensemble surfaces (rips.EnsembleSurface). Off by default. + + Returns: + List of surfaces of the requested class + """ + folder = self.surface_folder() + if folder is None: + return [] + return folder.surfaces( + surface_type, recursive=True, include_ensembles=include_ensembles + ) + + +@add_method(Project) +def surface_by_name( + self: Project, + name: str, + surface_type: Type[SurfaceT] = SurfaceInterface, # type: ignore[assignment] + folder: Optional[SurfaceCollection] = None, +) -> Optional[SurfaceT]: + """Get a surface by name + + Names are only unique within one folder. Without a folder the first match is + returned, searching the top surface folder first and then its sub folders, ensemble + surfaces excluded. The name is matched against surface_user_description, so a depth + offset shown in the tree does not affect the match. + + Arguments: + name (str): The surface name (surface_user_description) + surface_type (class): Only consider surfaces of this class, subclasses included + folder (SurfaceCollection): Only look in this folder, sub folders excluded + + Returns: + The surface, or None when no surface has that name + """ + if folder is not None: + candidates = folder.surfaces(surface_type) + else: + candidates = self.surfaces(surface_type) + for surface in candidates: + if surface.surface_user_description == name: + return surface + return None diff --git a/GrpcInterface/Python/rips/tests/test_surfaces.py b/GrpcInterface/Python/rips/tests/test_surfaces.py index 32836f340b..e1f0c37535 100644 --- a/GrpcInterface/Python/rips/tests/test_surfaces.py +++ b/GrpcInterface/Python/rips/tests/test_surfaces.py @@ -283,3 +283,87 @@ def test_import_surface_errors(rips_instance, initialize_test): on_name_conflict=rips.NameConflictPolicy.AUTO_RENAME, ) assert renamed.surface_user_description == "mysurface.ts_1" + + +def test_list_surfaces(rips_instance, initialize_test): + case_path = dataroot.PATH + "/Case_with_10_timesteps/Real0/BRUGGE_0000.EGRID" + case = rips_instance.project.load_case(path=case_path) + + root = rips_instance.project.surface_folder() + assert root is not None + assert len(rips_instance.project.surfaces()) == 0 + + root.new_regular_surface(name="Top") + grid_surface = root.new_surface(case, 5) + folder = root.add_folder(folder_name="Folder A") + folder.new_regular_surface(name="Top") + + with tempfile.TemporaryDirectory(prefix="rips") as tmpdirname: + path = Path(tmpdirname, "k_layer.ts") + grid_surface.export_to_file(path.as_posix()) + file_surface = folder.import_surface(path.as_posix()) + assert file_surface is not None + + # Non-recursive on a folder, recursive from the project + assert len(root.surfaces()) == 2 + assert len(root.surfaces(recursive=True)) == 4 + assert len(folder.surfaces()) == 2 + assert len(rips_instance.project.surfaces()) == 4 + + # Filter by class; rips.Surface is the file surface class + assert len(rips_instance.project.surfaces(rips.RegularSurface)) == 2 + assert len(rips_instance.project.surfaces(rips.GridCaseSurface)) == 1 + assert len(rips_instance.project.surfaces(rips.Surface)) == 1 + assert len(root.surfaces(rips.Surface)) == 0 + assert len(folder.surfaces(rips.Surface)) == 1 + assert all( + isinstance(s, rips.RegularSurface) + for s in rips_instance.project.surfaces(rips.RegularSurface) + ) + + with pytest.raises(rips.RipsError, match="surface class"): + rips_instance.project.surfaces(rips.SurfaceCollection) + + +def test_surface_by_name(rips_instance, initialize_test): + case_path = dataroot.PATH + "/Case_with_10_timesteps/Real0/BRUGGE_0000.EGRID" + rips_instance.project.load_case(path=case_path) + + root = rips_instance.project.surface_folder() + folder = root.add_folder(folder_name="Folder A") + root_top = root.new_regular_surface(name="Top") + folder_top = folder.new_regular_surface(name="Top") + folder.new_regular_surface(name="Base") + + # First match is the one in the top folder; a folder argument restricts the search + assert rips_instance.project.surface_by_name("Top").address() == root_top.address() + assert ( + rips_instance.project.surface_by_name("Top", folder=folder).address() + == folder_top.address() + ) + assert rips_instance.project.surface_by_name("Base", folder=root) is None + assert rips_instance.project.surface_by_name("Base") is not None + assert rips_instance.project.surface_by_name("Missing") is None + assert rips_instance.project.surface_by_name("Top", rips.GridCaseSurface) is None + + # The displayed name gets the depth offset appended, the match does not + root_top.depth_offset = 10.0 + root_top.update() + assert rips_instance.project.surface_by_name("Top").address() == root_top.address() + + +def test_surface_folder_lookup(rips_instance, initialize_test): + case_path = dataroot.PATH + "/Case_with_10_timesteps/Real0/BRUGGE_0000.EGRID" + rips_instance.project.load_case(path=case_path) + + root = rips_instance.project.surface_folder() + assert ( + root.address() + == rips_instance.project.descendants(rips.SurfaceCollection)[0].address() + ) + + folder = root.add_folder(folder_name="Folder A") + assert ( + rips_instance.project.surface_folder("Folder A").address() == folder.address() + ) + assert rips_instance.project.surface_folder("Missing") is None