From 9a99ff24c281c1e225c3653e6502c96554731e4d Mon Sep 17 00:00:00 2001 From: Paul Profizi <100710998+PProfizi@users.noreply.github.com> Date: Wed, 10 Apr 2024 14:17:56 +0200 Subject: [PATCH] Add PropertyField.name (#1469) * Add the FieldDefinition to the PropertyField and get/set the name through it. Signed-off-by: paul.profizi * Add testing Signed-off-by: paul.profizi * Fix retro-compatibility Signed-off-by: paul.profizi * Fix retro-compatibility Signed-off-by: paul.profizi * Add typehinting Signed-off-by: paul.profizi * Add @version_requires decorators to new properties Signed-off-by: paul.profizi --------- Signed-off-by: paul.profizi --- src/ansys/dpf/core/field.py | 4 +-- src/ansys/dpf/core/fields_container.py | 2 +- src/ansys/dpf/core/property_field.py | 43 ++++++++++++++++++++++++++ tests/conftest.py | 4 ++- tests/test_propertyfield.py | 8 +++++ 5 files changed, 57 insertions(+), 4 deletions(-) diff --git a/src/ansys/dpf/core/field.py b/src/ansys/dpf/core/field.py index d60f66c4d2..a32086a189 100644 --- a/src/ansys/dpf/core/field.py +++ b/src/ansys/dpf/core/field.py @@ -338,7 +338,7 @@ def shell_layers(self, value): fielddef.shell_layers = value self.field_definition = fielddef - def get_entity_data(self, index): + def get_entity_data(self, index: int) -> dpf_array.DPFArray: try: vec = dpf_vector.DPFVectorDouble(client=self._server.client) self._api.csfield_get_entity_data_for_dpf_vector( @@ -353,7 +353,7 @@ def get_entity_data(self, index): data.shape = (data.size // n_comp, n_comp) return data - def get_entity_data_by_id(self, id): + def get_entity_data_by_id(self, id: int) -> dpf_array.DPFArray: try: vec = dpf_vector.DPFVectorDouble(client=self._server.client) self._api.csfield_get_entity_data_by_id_for_dpf_vector( diff --git a/src/ansys/dpf/core/fields_container.py b/src/ansys/dpf/core/fields_container.py index 5a21b8c287..c949f79407 100644 --- a/src/ansys/dpf/core/fields_container.py +++ b/src/ansys/dpf/core/fields_container.py @@ -297,7 +297,7 @@ def get_imaginary_field(self, timeid=None): return super()._get_entry(label_space) - def __getitem__(self, key): + def __getitem__(self, key) -> field.Field: """Retrieve the field at a requested index. Parameters diff --git a/src/ansys/dpf/core/property_field.py b/src/ansys/dpf/core/property_field.py index a57fbc9e6c..3059f43316 100644 --- a/src/ansys/dpf/core/property_field.py +++ b/src/ansys/dpf/core/property_field.py @@ -4,9 +4,12 @@ """ import numpy as np +from ansys.dpf.core.check_version import version_requires from ansys.dpf.core.common import natures, locations, _get_size_of_list from ansys.dpf.core import scoping, dimensionality from ansys.dpf.core.field_base import _FieldBase, _LocalFieldBase +from ansys.dpf.core.check_version import meets_version +from ansys.dpf.core.field_definition import FieldDefinition from ansys.dpf.gate import ( property_field_abstract_api, property_field_capi, @@ -69,6 +72,9 @@ def __init__( field=property_field, server=server, ) + self._field_definition = None + if meets_version(self._server.version, "8.1"): + self._field_definition = self._load_field_definition() @property def _api(self) -> property_field_abstract_api.PropertyFieldAbstractAPI: @@ -101,6 +107,14 @@ def _field_create_internal_obj( else: return api.csproperty_field_new(nentities, nentities * dim.component_count) + + @version_requires("8.1") + def _load_field_definition(self): + """Attempt to load the field definition for this field.""" + # try: + out = self._api.csproperty_field_get_shared_field_definition(self) + return FieldDefinition(out, self._server) + @property def location(self): """Location of the property field. @@ -299,6 +313,35 @@ def as_local_field(self): """ return _LocalPropertyField(self) + @property + @version_requires("8.1") + def name(self): + """Name of the property field. + + ..note: + Available starting with DPF 2024.2.pre1. + """ + if self._field_definition: + return self._field_definition.name + + @name.setter + @version_requires("8.1") + def name(self, value): + """Change the name of the property field + + Parameters + ---------- + value : str + Name of the property field. + + ..note: + Available starting with DPF 2024.2.pre1. + """ + if self._field_definition: + self._field_definition._api.csfield_definition_set_name( + self._field_definition, name=value + ) + class _LocalPropertyField(_LocalFieldBase, PropertyField): """Caches the internal data of a field so that it can be modified locally. diff --git a/tests/conftest.py b/tests/conftest.py index bd4afc54d6..bd63851974 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -304,7 +304,9 @@ def return_ds(server=None): return return_ds - +SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_8_1 = meets_version( + get_server_version(core._global_server()), "8.1" +) SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_8_0 = meets_version( get_server_version(core._global_server()), "8.0" ) diff --git a/tests/test_propertyfield.py b/tests/test_propertyfield.py index 83270f8618..d9c9b6d1f1 100644 --- a/tests/test_propertyfield.py +++ b/tests/test_propertyfield.py @@ -41,6 +41,14 @@ def test_set_get_data_property_field(server_type): assert np.allclose(field.data, data) +@pytest.mark.skipif(not conftest.SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_8_1, + reason="Available starting with DPF 8.1") +def test_set_get_name_property_field(server_type): + field = dpf.core.PropertyField(server=server_type) + field.name = "test" + assert field.name == "test" + + def test_create_property_field_push_back(server_type): f_vec = core.PropertyField(1, core.natures.vector, core.locations.nodal, server=server_type) f_vec.append([1, 2, 4], 1)