diff --git a/src/ansys/dpf/core/field_definition.py b/src/ansys/dpf/core/field_definition.py index 185048b6ba..a10f92f6d2 100644 --- a/src/ansys/dpf/core/field_definition.py +++ b/src/ansys/dpf/core/field_definition.py @@ -153,6 +153,61 @@ 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 + + Returns + ------- + quantity_type : Quantity Type + Quantity type of the elementary data at a given index. + """ + if index < 0: + raise ValueError("Index must be greater than or equal to 0") + + quantity_type = self._api.csfield_definition_get_quantity_type(self, index) + return str(quantity_type) + + def set_quantity_type(self, quantity_type): + """Setter for Quantity Type + + Parameters + ---------- + quantity_type: Quantity Type + Quantity type to set + """ + self._api.csfield_definition_set_quantity_type(self, quantity_type) + + def get_num_available_quantity_types(self): + """Getter for the number of available quantity types + + Returns + ------- + num_quantity_types : int + Number of quantity types + """ + num_quantity_types = self._api.csfield_definition_get_num_available_quantity_types(self) + return num_quantity_types + + def is_of_quantity_type(self, quantity_type): + """Check if the field definition is of a given quantity type + + Parameters + ---------- + quantity_type: Quantity Type + Quantity type to check + + Returns + ------- + is_of_quantity_type : bool + True if the field definition is of the given quantity type + """ + is_of_quantity_type = self._api.csfield_definition_is_of_quantity_type(self, quantity_type) + return is_of_quantity_type + @unit.setter def unit(self, value): self._api.csfield_definition_set_unit(self, value, None, 0, 0, 0) diff --git a/src/ansys/dpf/gate/generated/field_definition_capi.py b/src/ansys/dpf/gate/generated/field_definition_capi.py index 97ef4bae29..29d808f360 100644 --- a/src/ansys/dpf/gate/generated/field_definition_capi.py +++ b/src/ansys/dpf/gate/generated/field_definition_capi.py @@ -184,6 +184,35 @@ 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): @@ -241,6 +270,15 @@ 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) diff --git a/tests/test_field.py b/tests/test_field.py index 640ab83bfc..14febba7c3 100644 --- a/tests/test_field.py +++ b/tests/test_field.py @@ -517,6 +517,37 @@ def test_create_and_update_field_definition(server_type): assert fieldDef.location == locations.nodal +def test_field_definition_quantity_type(server_type): + fieldDef = FieldDefinition(server=server_type) + + # Testing the setter + qt = "my_quantity_type" + fieldDef.set_quantity_type(qt) + + # Testing the getter + assert fieldDef.get_quantity_type(0) == qt + + # Adding a second quantity type + qt2 = "another_quantity_type" + fieldDef.set_quantity_type(qt2) + + # Testing the getter again + assert fieldDef.get_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) + + # Getting the number of available quantity types + assert fieldDef.get_num_available_quantity_types() == 2 + + # Checking if the field definition is of a given quantity type + assert fieldDef.is_of_quantity_type(qt) + + @conftest.raises_for_servers_version_under("4.0") def test_create_and_set_get_name_field_definition(server_type): fieldDef = FieldDefinition(server=server_type)