From 12ff95909dc754fd7ad425bf4773aa2f35648478 Mon Sep 17 00:00:00 2001 From: Samuelopez-ansys Date: Wed, 6 Nov 2024 16:22:06 +0100 Subject: [PATCH 1/3] Update wireframe method --- src/ansys/aedt/core/modeler/cad/primitives.py | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/ansys/aedt/core/modeler/cad/primitives.py b/src/ansys/aedt/core/modeler/cad/primitives.py index 4da34256a9a..63a2d358f37 100644 --- a/src/ansys/aedt/core/modeler/cad/primitives.py +++ b/src/ansys/aedt/core/modeler/cad/primitives.py @@ -6280,6 +6280,27 @@ def update_object(self, assignment): o = self._create_object(name) return o + @pyaedt_function_handler() + def update_wireframe(self, assignment, value=True): + """Update wireframe property of assigned objects. + + Parameters + ---------- + assignment : str, or list + Object to be updated. + value : bool, optional + Display wireframe. The default is ``True``. + + Returns + ------- + bool + ``True`` when successful, ``False`` when failed. + + """ + assignment = self.convert_to_selections(assignment, True) + props = ["NAME:Display Wireframe", "Value:=", value] + return self._change_geometry_property(props, assignment) + @pyaedt_function_handler() def value_in_object_units(self, value): """Convert one or more strings for numerical lengths to floating point values. From 8a498a1ae9530bc1a84363c0abde44604bf3dde7 Mon Sep 17 00:00:00 2001 From: Samuelopez-ansys Date: Fri, 8 Nov 2024 12:21:36 +0100 Subject: [PATCH 2/3] Update a property of a set of objects --- src/ansys/aedt/core/modeler/cad/object_3d.py | 1 - src/ansys/aedt/core/modeler/cad/primitives.py | 68 +++++++++++++++++-- tests/system/general/test_08_Primitives3D.py | 36 ++++++++++ 3 files changed, 98 insertions(+), 7 deletions(-) diff --git a/src/ansys/aedt/core/modeler/cad/object_3d.py b/src/ansys/aedt/core/modeler/cad/object_3d.py index 0301c40392f..14f6f23f47f 100644 --- a/src/ansys/aedt/core/modeler/cad/object_3d.py +++ b/src/ansys/aedt/core/modeler/cad/object_3d.py @@ -1267,7 +1267,6 @@ def part_coordinate_system(self, sCS): pcs = ["NAME:Orientation", "Value:=", sCS] self._change_property(pcs) self._part_coordinate_system = sCS - return True @property def solve_inside(self): diff --git a/src/ansys/aedt/core/modeler/cad/primitives.py b/src/ansys/aedt/core/modeler/cad/primitives.py index 63a2d358f37..a6776995dba 100644 --- a/src/ansys/aedt/core/modeler/cad/primitives.py +++ b/src/ansys/aedt/core/modeler/cad/primitives.py @@ -43,6 +43,7 @@ from ansys.aedt.core.generic.data_handlers import json_to_dict from ansys.aedt.core.generic.general_methods import _dim_arg from ansys.aedt.core.generic.general_methods import _uname +from ansys.aedt.core.generic.general_methods import clamp from ansys.aedt.core.generic.general_methods import generate_unique_name from ansys.aedt.core.generic.general_methods import is_linux from ansys.aedt.core.generic.general_methods import is_number @@ -6281,15 +6282,19 @@ def update_object(self, assignment): return o @pyaedt_function_handler() - def update_wireframe(self, assignment, value=True): - """Update wireframe property of assigned objects. + def update_geometry_property(self, assignment, name=None, value=None): + """Update property of assigned geometry objects. Parameters ---------- assignment : str, or list - Object to be updated. - value : bool, optional - Display wireframe. The default is ``True``. + Object name or list of object names to be updated. + name : str, optional + Property name to change. The default is ``None``, in which case no property is updated. + Available options are: ``"display_wireframe"``, `"material"``, and `"solve_inside"``. + value : bool or str, optional + Property value. The default is ``None`` in which case + no value is assigned. Returns ------- @@ -6298,7 +6303,58 @@ def update_wireframe(self, assignment, value=True): """ assignment = self.convert_to_selections(assignment, True) - props = ["NAME:Display Wireframe", "Value:=", value] + + # Define property mapping + property_mapping = { + "display_wireframe": {"property_name": "Display Wireframe", "reset_attr": ["_wireframe"]}, + "material_name": {"property_name": "Material", "reset_attr": ["_material_name", "_model", "_solve_inside"]}, + "solve_inside": {"property_name": "Solve Inside", "reset_attr": ["_solve_inside"]}, + "color": {"property_name": "Color", "reset_attr": ["_color"]}, + "transparency": {"property_name": "Transparent", "reset_attr": ["_transparency"]}, + "part_coordinate_system": {"property_name": "Orientation", "reset_attr": ["_part_coordinate_system"]}, + } + + # Check if property name is valid + property_key = name.lower() + if property_key not in property_mapping: + self.logger.error("Invalid property name.") + return False + + # Retrieve property settings + property_name = property_mapping[property_key]["property_name"] + reset_attr = property_mapping[property_key]["reset_attr"] + + # Handle special cases for material + if property_key == "material_name" and isinstance(value, str): + matobj = self._materials.checkifmaterialexists(value) + if matobj: + value = f'"{matobj.name}"' + elif "[" in value or "(" in value: # pragma: no cover + value = value + else: + self.logger.error("Invalid material value.") + return False + + value_command = ["Value:=", value] + if property_key == "color": + if isinstance(value, tuple) or isinstance(value, list): + R = clamp(value[0], 0, 255) + G = clamp(value[1], 0, 255) + B = clamp(value[2], 0, 255) + value_command = ["R:=", str(R), "G:=", str(G), "B:=", str(B)] + else: + self.logger.error("Invalid color.") + return False + + # Reset property values + for obj_name in assignment: + obj = self.objects_by_name[obj_name] + for attr in reset_attr: + setattr(obj, attr, None) + + props = [f"NAME:{property_name}"] + props.extend(value_command) + return self._change_geometry_property(props, assignment) @pyaedt_function_handler() diff --git a/tests/system/general/test_08_Primitives3D.py b/tests/system/general/test_08_Primitives3D.py index a561b3a62ce..baff07f0c44 100644 --- a/tests/system/general/test_08_Primitives3D.py +++ b/tests/system/general/test_08_Primitives3D.py @@ -2035,3 +2035,39 @@ def test_94_create_equationbased_surface(self): x_uv="(sin(_v*2*pi)^2+1.2)*cos(_u*2*pi)", y_uv="(sin(_v*2*pi)^2+1.2)*sin(_u*2*pi)", z_uv="_v*2" ) assert surf.name in self.aedtapp.modeler.sheet_names + + def test_95_update_geometry_property(self): + self.aedtapp.insert_design("Update_properties") + box1 = self.aedtapp.modeler.create_box([0, 0, 0], [1, 2, 3]) + box2 = self.aedtapp.modeler.create_box([10, 10, 10], [1, 2, 3]) + box1.display_wireframe = False + box2.display_wireframe = False + + assert not self.aedtapp.modeler.update_geometry_property([box1.name], "wireframe", True) + assert not self.aedtapp.modeler.update_geometry_property([box1.name], "material_name", "invented") + assert not self.aedtapp.modeler.update_geometry_property([box1.name], "color", "red") + + self.aedtapp.modeler.update_geometry_property([box1.name], "display_wireframe", True) + assert box1.display_wireframe + + self.aedtapp.modeler.update_geometry_property([box1.name, box2.name], "display_wireframe", True) + assert box2.display_wireframe + + self.aedtapp.modeler.update_geometry_property([box1.name, box2.name], "material_name", "copper") + assert box2.material_name == "copper" + assert not box2.solve_inside + + self.aedtapp.modeler.update_geometry_property([box2.name], "solve_inside", True) + assert box2.solve_inside + assert not box1.solve_inside + + self.aedtapp.modeler.update_geometry_property([box1.name, box2.name], "color", (255, 255, 0)) + assert box2.color == box1.color == (255, 255, 0) + + self.aedtapp.modeler.update_geometry_property([box1.name, box2.name], "transparency", 0.75) + assert box2.transparency == 0.75 + + cs = self.aedtapp.modeler.create_coordinate_system() + self.aedtapp.modeler.update_geometry_property([box2.name], "part_coordinate_system", cs.name) + assert box2.part_coordinate_system == cs.name + assert box1.part_coordinate_system == "Global" From 8335d398337b6ae92e102560e02281f0ebc64ce7 Mon Sep 17 00:00:00 2001 From: Samuelopez-ansys Date: Mon, 11 Nov 2024 12:29:22 +0100 Subject: [PATCH 3/3] Deprecate checkifmaterialexists --- .../aedt/core/application/analysis_3d.py | 2 +- src/ansys/aedt/core/generic/configurations.py | 2 +- .../core/modeler/advanced_cad/stackup_3d.py | 2 +- src/ansys/aedt/core/modeler/cad/object_3d.py | 2 +- src/ansys/aedt/core/modeler/cad/primitives.py | 2 +- .../aedt/core/modeler/cad/primitives_3d.py | 4 +-- src/ansys/aedt/core/modules/material_lib.py | 35 +++++++++++++++++-- 7 files changed, 39 insertions(+), 10 deletions(-) diff --git a/src/ansys/aedt/core/application/analysis_3d.py b/src/ansys/aedt/core/application/analysis_3d.py index 71f71d031e5..be4a88b4503 100644 --- a/src/ansys/aedt/core/application/analysis_3d.py +++ b/src/ansys/aedt/core/application/analysis_3d.py @@ -952,7 +952,7 @@ def assignmaterial_from_sherlock_files(self, component_file, material_file): list_mat_obj += [rd for rd, md in zip(component_data["Ref Des"], component_data["Material"]) if md == mat] list_mat_obj = [mo for mo in list_mat_obj if mo in all_objs] if list_mat_obj: - newmat = self.materials.checkifmaterialexists(mat) + newmat = self.materials.exists_material(mat) if not newmat: newmat = self.materials.add_material(mat.lower()) if "Material Density" in material_data: diff --git a/src/ansys/aedt/core/generic/configurations.py b/src/ansys/aedt/core/generic/configurations.py index da649c10b74..5ccd1131b47 100644 --- a/src/ansys/aedt/core/generic/configurations.py +++ b/src/ansys/aedt/core/generic/configurations.py @@ -1148,7 +1148,7 @@ def import_config(self, config_file, *args): if self.options.import_materials and dict_in.get("materials", None): self.results.import_materials = True for el, val in dict_in["materials"].items(): - if self._app.materials.checkifmaterialexists(el): + if self._app.materials.exists_material(el): newname = generate_unique_name(el) self._app.logger.warning("Material %s already exists. Renaming to %s", el, newname) else: diff --git a/src/ansys/aedt/core/modeler/advanced_cad/stackup_3d.py b/src/ansys/aedt/core/modeler/advanced_cad/stackup_3d.py index cd7a7ba2845..fa6dc15ec9e 100644 --- a/src/ansys/aedt/core/modeler/advanced_cad/stackup_3d.py +++ b/src/ansys/aedt/core/modeler/advanced_cad/stackup_3d.py @@ -244,7 +244,7 @@ def __init__(self, application, material_name, cloned_material_name, list_of_pro self._magnetic_loss_tangent = None self._material = None self._material_name = None - if application.materials.checkifmaterialexists(material_name): + if application.materials.exists_material(material_name): if not list_of_properties: cloned_material = application.materials.duplicate_material(material_name, cloned_material_name) permittivity = cloned_material.permittivity.value diff --git a/src/ansys/aedt/core/modeler/cad/object_3d.py b/src/ansys/aedt/core/modeler/cad/object_3d.py index 14f6f23f47f..d5e53ff5d32 100644 --- a/src/ansys/aedt/core/modeler/cad/object_3d.py +++ b/src/ansys/aedt/core/modeler/cad/object_3d.py @@ -933,7 +933,7 @@ def material_name(self): @material_name.setter def material_name(self, mat): - matobj = self._primitives._materials.checkifmaterialexists(mat) + matobj = self._primitives._materials.exists_material(mat) mat_value = None if matobj: mat_value = chr(34) + matobj.name + chr(34) diff --git a/src/ansys/aedt/core/modeler/cad/primitives.py b/src/ansys/aedt/core/modeler/cad/primitives.py index 55ecf711ef9..a4198684c54 100644 --- a/src/ansys/aedt/core/modeler/cad/primitives.py +++ b/src/ansys/aedt/core/modeler/cad/primitives.py @@ -6326,7 +6326,7 @@ def update_geometry_property(self, assignment, name=None, value=None): # Handle special cases for material if property_key == "material_name" and isinstance(value, str): - matobj = self._materials.checkifmaterialexists(value) + matobj = self._materials.exists_material(value) if matobj: value = f'"{matobj.name}"' elif "[" in value or "(" in value: # pragma: no cover diff --git a/src/ansys/aedt/core/modeler/cad/primitives_3d.py b/src/ansys/aedt/core/modeler/cad/primitives_3d.py index f8397fb8540..adb0d5d72d1 100644 --- a/src/ansys/aedt/core/modeler/cad/primitives_3d.py +++ b/src/ansys/aedt/core/modeler/cad/primitives_3d.py @@ -3086,7 +3086,7 @@ def check_choke_values(self, input_dir, create_another_file=True): try: core_material = str(values["Core"]["Material"]) if len(core_material) > 0: - if self.materials.checkifmaterialexists(core_material): + if self.materials.exists_material(core_material): values["Core"]["Material"] = self.materials._get_aedt_case_name(core_material) else: self.logger.error( @@ -3109,7 +3109,7 @@ def check_choke_values(self, input_dir, create_another_file=True): try: winding_material = str(values["Outer Winding"]["Material"]) if len(winding_material) > 0: - if self.materials.checkifmaterialexists(winding_material): + if self.materials.exists_material(winding_material): values["Outer Winding"]["Material"] = self.materials._get_aedt_case_name(winding_material) else: self.logger.error( diff --git a/src/ansys/aedt/core/modules/material_lib.py b/src/ansys/aedt/core/modules/material_lib.py index d1e6aeb0323..1037e233f76 100644 --- a/src/ansys/aedt/core/modules/material_lib.py +++ b/src/ansys/aedt/core/modules/material_lib.py @@ -93,7 +93,7 @@ def __iter__(self): return iter(self.material_keys.values()) if sys.version_info.major > 2 else self.material_keys.itervalues() def __getitem__(self, item): - matobj = self.checkifmaterialexists(item) + matobj = self.exists_material(item) if matobj: return matobj elif item in list(self.surface_material_keys.keys()): @@ -243,6 +243,35 @@ def _get_surface_materials(self): def checkifmaterialexists(self, material): """Check if a material exists in AEDT or PyAEDT Definitions. + .. deprecated:: 0.11.4 + Use :func:`exists_material` method instead. + + Parameters + ---------- + material : str + Name of the material. If the material exists and is not in the materials database, + it is added to this database. + + Returns + ------- + :class:`ansys.aedt.core.modules.material.Material` + Material object if present, ``False`` when failed. + + References + ---------- + + >>> oDefinitionManager.GetProjectMaterialNames + >>> oMaterialManager.GetData + """ + warnings.warn( + "`checkifmaterialexists` is deprecated. Use `exists_material` method instead.", DeprecationWarning + ) + return self.exists_material(material=material) + + @pyaedt_function_handler() + def exists_material(self, material): + """Check if a material exists in AEDT or PyAEDT Definitions. + Parameters ---------- material : str @@ -293,7 +322,7 @@ def check_thermal_modifier(self, material): ``True`` when successful, ``False`` when failed. """ - omat = self.checkifmaterialexists(material) + omat = self.exists_material(material) if omat: for el in MatProperties.aedtname: if omat.__dict__["_" + el].thermalmodifier: @@ -451,7 +480,7 @@ def add_material_sweep(self, assignment, name): """ matsweep = [] for mat in assignment: - matobj = self.checkifmaterialexists(mat) + matobj = self.exists_material(mat) if matobj: matsweep.append(matobj)