From 0c06eabb3d9968ce993b8c0f689808fe8e13859f Mon Sep 17 00:00:00 2001 From: aboutheo Date: Fri, 13 Dec 2024 10:31:02 +0100 Subject: [PATCH] feat: Update after PR Review --- src/ansys/dpf/core/field_definition.py | 39 +++++++++--------- .../gate/generated/field_definition_capi.py | 41 +------------------ tests/test_field.py | 15 +++---- 3 files changed, 26 insertions(+), 69 deletions(-) diff --git a/src/ansys/dpf/core/field_definition.py b/src/ansys/dpf/core/field_definition.py index a10f92f6d2..a979bee973 100644 --- a/src/ansys/dpf/core/field_definition.py +++ b/src/ansys/dpf/core/field_definition.py @@ -153,36 +153,35 @@ def dimensionality(self): self._api.csfield_definition_fill_dimensionality(self, dim, nature, dim.internal_size) return Dimensionality(dim.tolist(), natures(int(nature))) - def get_quantity_type(self, index=0): - """Getter for Quantity Type - - Parameters - ---------- - index: Index of the quantity type to return + @property + def quantity_type(self): + """Getter for Quantity Types Returns ------- - quantity_type : Quantity Type - Quantity type of the elementary data at a given index. + str + All quantity types of the elementary data for this FieldDefinition. """ - if index < 0: - raise ValueError("Index must be greater than or equal to 0") + quantity_types = [] + for i in range(self.num_quantity_types()): + qt = self._api.csfield_definition_get_quantity_type(self, i) + print(qt) + quantity_types.append(str(qt)) - quantity_type = self._api.csfield_definition_get_quantity_type(self, index) - return str(quantity_type) + return quantity_types - def set_quantity_type(self, quantity_type): - """Setter for Quantity Type + def add_quantity_type(self, quantity_type_to_add): + """Add a new Quantity Type Parameters ---------- - quantity_type: Quantity Type - Quantity type to set + quantity_type_to_add: str + Quantity type to add """ - self._api.csfield_definition_set_quantity_type(self, quantity_type) + self._api.csfield_definition_set_quantity_type(self, quantity_type_to_add) - def get_num_available_quantity_types(self): - """Getter for the number of available quantity types + def num_quantity_types(self): + """Number of available quantity types Returns ------- @@ -197,7 +196,7 @@ def is_of_quantity_type(self, quantity_type): Parameters ---------- - quantity_type: Quantity Type + quantity_type: str Quantity type to check Returns diff --git a/src/ansys/dpf/gate/generated/field_definition_capi.py b/src/ansys/dpf/gate/generated/field_definition_capi.py index 29d808f360..7a4a8a66da 100644 --- a/src/ansys/dpf/gate/generated/field_definition_capi.py +++ b/src/ansys/dpf/gate/generated/field_definition_capi.py @@ -184,35 +184,6 @@ def csfield_definition_get_shell_layers(res): if errorSize.value != 0: raise errors.DPFServerException(sError.value) return res - - @staticmethod - def csfield_definition_get_quantity_type(res, index): - errorSize = ctypes.c_int(0) - sError = ctypes.c_wchar_p() - res = capi.dll.CSFieldDefinition_GetQuantityType(res._internal_obj if res is not None else None, index, ctypes.byref(utils.to_int32(errorSize)), ctypes.byref(sError)) - if errorSize.value != 0: - raise errors.DPFServerException(sError.value) - newres = ctypes.cast(res, ctypes.c_char_p).value.decode("utf-8") if res else None - capi.dll.DataProcessing_String_post_event(res, ctypes.byref(errorSize), ctypes.byref(sError)) - return newres - - @staticmethod - def csfield_definition_get_num_available_quantity_types(fieldDef): - errorSize = ctypes.c_int(0) - sError = ctypes.c_wchar_p() - res = capi.dll.CSFieldDefinition_GetNumAvailableQuantityTypes(fieldDef._internal_obj if fieldDef is not None else None, ctypes.byref(utils.to_int32(errorSize)), ctypes.byref(sError)) - if errorSize.value != 0: - raise errors.DPFServerException(sError.value) - return res - - @staticmethod - def csfield_definition_is_of_quantity_type(fieldDef, quantityType): - errorSize = ctypes.c_int(0) - sError = ctypes.c_wchar_p() - res = capi.dll.CSFieldDefinition_GetNumAvailableQuantityTypes(fieldDef._internal_obj if fieldDef is not None else None, utils.to_char_ptr(quantityType), ctypes.byref(utils.to_int32(errorSize)), ctypes.byref(sError)) - if errorSize.value != 0: - raise errors.DPFServerException(sError.value) - return res @staticmethod def csfield_definition_get_location(res): @@ -270,15 +241,6 @@ def csfield_definition_set_shell_layers(fieldDef, shellLayers): raise errors.DPFServerException(sError.value) return res - @staticmethod - def csfield_definition_set_quantity_type(fieldDef, quantityType): - errorSize = ctypes.c_int(0) - sError = ctypes.c_wchar_p() - res = capi.dll.CSFieldDefinition_SetQuantityType(fieldDef._internal_obj if fieldDef is not None else None, utils.to_char_ptr(quantityType), ctypes.byref(utils.to_int32(errorSize)), ctypes.byref(sError)) - if errorSize.value != 0: - raise errors.DPFServerException(sError.value) - return res - @staticmethod def csfield_definition_set_location(fieldDef, location): errorSize = ctypes.c_int(0) @@ -389,5 +351,4 @@ def dimensionality_get_num_comp_for_object(api_to_use, nature, size, vsize): res = capi.dll.Dimensionality_GetNumComp_for_object(api_to_use._internal_obj if api_to_use is not None else None, utils.to_int32(nature), utils.to_int32_ptr(size), utils.to_int32(vsize), ctypes.byref(utils.to_int32(errorSize)), ctypes.byref(sError)) if errorSize.value != 0: raise errors.DPFServerException(sError.value) - return res - + return res \ No newline at end of file diff --git a/tests/test_field.py b/tests/test_field.py index 14febba7c3..2b69f9f7f7 100644 --- a/tests/test_field.py +++ b/tests/test_field.py @@ -522,27 +522,24 @@ def test_field_definition_quantity_type(server_type): # Testing the setter qt = "my_quantity_type" - fieldDef.set_quantity_type(qt) + fieldDef.add_quantity_type(qt) # Testing the getter - assert fieldDef.get_quantity_type(0) == qt + assert fieldDef.quantity_type[0] == qt # Adding a second quantity type qt2 = "another_quantity_type" - fieldDef.set_quantity_type(qt2) + fieldDef.add_quantity_type(qt2) # Testing the getter again - assert fieldDef.get_quantity_type(1) == qt2 + assert fieldDef.quantity_type[1] == qt2 # Testing the getter with an index out of range - assert fieldDef.get_quantity_type(2) == None - - # Testing the getter with a negative index with pytest.raises(Exception): - fieldDef.get_quantity_type(-1) + fieldDef.quantity_type[2] # Getting the number of available quantity types - assert fieldDef.get_num_available_quantity_types() == 2 + assert fieldDef.num_quantity_types() == 2 # Checking if the field definition is of a given quantity type assert fieldDef.is_of_quantity_type(qt)