Skip to content

Commit b2c2aa0

Browse files
committed
#13841 Python: Add available_properties API for regular surfaces
1 parent 0aee903 commit b2c2aa0

File tree

7 files changed

+107
-1
lines changed

7 files changed

+107
-1
lines changed

ApplicationLibCode/ProjectDataModel/Surfaces/RimRegularSurface.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,19 @@ std::vector<float> RimRegularSurface::getProperty( const QString& key ) const
415415
return {};
416416
}
417417

418+
//--------------------------------------------------------------------------------------------------
419+
///
420+
//--------------------------------------------------------------------------------------------------
421+
std::vector<QString> RimRegularSurface::propertyNames() const
422+
{
423+
std::vector<QString> names;
424+
for ( const auto& [key, value] : m_properties )
425+
{
426+
names.push_back( key );
427+
}
428+
return names;
429+
}
430+
418431
//--------------------------------------------------------------------------------------------------
419432
///
420433
//--------------------------------------------------------------------------------------------------

ApplicationLibCode/ProjectDataModel/Surfaces/RimRegularSurface.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ class RimRegularSurface : public RimSurface
4242
void setProperty( const QString& key, const std::vector<float>& values );
4343
void setPropertyAsDepth( const QString& key );
4444

45-
std::vector<float> getProperty( const QString& key ) const;
45+
std::vector<float> getProperty( const QString& key ) const;
46+
std::vector<QString> propertyNames() const;
4647

4748
int nx() const;
4849
int ny() const;

ApplicationLibCode/ProjectDataModelCommands/RimcRegularSurface.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "KeyValueStore/RiaKeyValueStoreUtil.h"
2121
#include "RiaApplication.h"
2222

23+
#include "RimcDataContainerString.h"
2324
#include "RimRegularSurface.h"
2425
#include "RimSurfaceCollection.h"
2526

@@ -138,3 +139,36 @@ std::expected<caf::PdmObjectHandle*, QString> RimcRegularSurface_setPropertyAsDe
138139

139140
return nullptr;
140141
}
142+
143+
CAF_PDM_OBJECT_METHOD_SOURCE_INIT( RimRegularSurface, RimcRegularSurface_propertyNames, "PropertyNames" );
144+
145+
//--------------------------------------------------------------------------------------------------
146+
///
147+
//--------------------------------------------------------------------------------------------------
148+
RimcRegularSurface_propertyNames::RimcRegularSurface_propertyNames( caf::PdmObjectHandle* self )
149+
: PdmObjectMethod( self, PdmObjectMethod::NullPointerType::NULL_IS_INVALID, PdmObjectMethod::ResultType::PERSISTENT_FALSE )
150+
{
151+
CAF_PDM_InitObject( "Property Names", "", "", "Property Names." );
152+
}
153+
154+
//--------------------------------------------------------------------------------------------------
155+
///
156+
//--------------------------------------------------------------------------------------------------
157+
std::expected<caf::PdmObjectHandle*, QString> RimcRegularSurface_propertyNames::execute()
158+
{
159+
RimRegularSurface* surface = self<RimRegularSurface>();
160+
if ( !surface ) return std::unexpected( "No surface found" );
161+
162+
auto dataObject = new RimcDataContainerString();
163+
dataObject->m_stringValues = surface->propertyNames();
164+
165+
return dataObject;
166+
}
167+
168+
//--------------------------------------------------------------------------------------------------
169+
///
170+
//--------------------------------------------------------------------------------------------------
171+
QString RimcRegularSurface_propertyNames::classKeywordReturnedType() const
172+
{
173+
return RimcDataContainerString::classKeywordStatic();
174+
}

ApplicationLibCode/ProjectDataModelCommands/RimcRegularSurface.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,17 @@ class RimcRegularSurface_setPropertyAsDepth : public caf::PdmVoidObjectMethod
7575
private:
7676
caf::PdmField<QString> m_name;
7777
};
78+
79+
//==================================================================================================
80+
///
81+
//==================================================================================================
82+
class RimcRegularSurface_propertyNames : public caf::PdmObjectMethod
83+
{
84+
CAF_PDM_HEADER_INIT;
85+
86+
public:
87+
RimcRegularSurface_propertyNames( caf::PdmObjectHandle* self );
88+
89+
std::expected<caf::PdmObjectHandle*, QString> execute() override;
90+
QString classKeywordReturnedType() const override;
91+
};

GrpcInterface/Python/rips/PythonExamples/surfaces_and_visualization/create_regular_surface.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,9 @@ def create_wave_surface(nx, ny):
8383
f"Retrieved {len(wave_values)} wave property values, min={min(wave_values):.2f}, max={max(wave_values):.2f}"
8484
)
8585

86+
# List available properties
87+
props = s.available_properties()
88+
print(f"Available properties: {props}")
89+
8690
# Use the wave as depth for the surface
8791
s.set_property_as_depth("wave")

GrpcInterface/Python/rips/surface.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@ def set_property(self: RegularSurface, name: str, values: List[float]) -> None:
2525
self.set_property_from_key(name=name, value_key=key)
2626

2727

28+
@add_method(RegularSurface)
29+
def available_properties(self: RegularSurface) -> List[str]:
30+
"""Gets the list of available property names on a regular surface.
31+
32+
Returns:
33+
List[str]: Names of all properties set on this surface.
34+
"""
35+
result = self.property_names()
36+
return result.values
37+
38+
2839
@add_method(RegularSurface)
2940
def get_property(self: RegularSurface, name: str) -> List[float]:
3041
"""Gets a property from a regular surface.

GrpcInterface/Python/rips/tests/test_surfaces.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,35 @@ def test_get_property(rips_instance, initialize_test):
106106
s.get_property("non_existent_property")
107107

108108

109+
def test_available_properties(rips_instance, initialize_test):
110+
case_path = dataroot.PATH + "/Case_with_10_timesteps/Real0/BRUGGE_0000.EGRID"
111+
c = rips_instance.project.load_case(path=case_path)
112+
assert len(c.grids()) == 1
113+
114+
surface_collection = rips_instance.project.descendants(rips.SurfaceCollection)[0]
115+
116+
nx = 5
117+
ny = 4
118+
s = surface_collection.new_regular_surface(nx=nx, ny=ny)
119+
120+
# Empty property list initially
121+
props = s.available_properties()
122+
assert props == []
123+
124+
# Property names after setting one property
125+
s.set_property("prop_a", [float(i) for i in range(nx * ny)])
126+
props = s.available_properties()
127+
assert "prop_a" in props
128+
assert len(props) == 1
129+
130+
# Property names persist after setting multiple properties
131+
s.set_property("prop_b", [float(i) for i in range(nx * ny)])
132+
props = s.available_properties()
133+
assert len(props) == 2
134+
assert "prop_a" in props
135+
assert "prop_b" in props
136+
137+
109138
def test_create_regular_surface_invalid_values(rips_instance, initialize_test):
110139
case_path = dataroot.PATH + "/Case_with_10_timesteps/Real0/BRUGGE_0000.EGRID"
111140
c = rips_instance.project.load_case(path=case_path)

0 commit comments

Comments
 (0)