Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEAT: Update wireframe method #5386

Merged
merged 5 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ansys/aedt/core/application/analysis_3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -952,7 +952,7 @@
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)

Check warning on line 955 in src/ansys/aedt/core/application/analysis_3d.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/aedt/core/application/analysis_3d.py#L955

Added line #L955 was not covered by tests
if not newmat:
newmat = self.materials.add_material(mat.lower())
if "Material Density" in material_data:
Expand Down
2 changes: 1 addition & 1 deletion src/ansys/aedt/core/generic/configurations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/ansys/aedt/core/modeler/advanced_cad/stackup_3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@
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):

Check warning on line 247 in src/ansys/aedt/core/modeler/advanced_cad/stackup_3d.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/aedt/core/modeler/advanced_cad/stackup_3d.py#L247

Added line #L247 was not covered by tests
if not list_of_properties:
cloned_material = application.materials.duplicate_material(material_name, cloned_material_name)
permittivity = cloned_material.permittivity.value
Expand Down
3 changes: 1 addition & 2 deletions src/ansys/aedt/core/modeler/cad/object_3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down
77 changes: 77 additions & 0 deletions src/ansys/aedt/core/modeler/cad/primitives.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -6280,6 +6281,82 @@ def update_object(self, assignment):
o = self._create_object(name)
return o

@pyaedt_function_handler()
def update_geometry_property(self, assignment, name=None, value=None):
"""Update property of assigned geometry objects.

Parameters
----------
assignment : str, or list
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
-------
bool
``True`` when successful, ``False`` when failed.

"""
assignment = self.convert_to_selections(assignment, True)

# 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.exists_material(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
Samuelopez-ansys marked this conversation as resolved.
Show resolved Hide resolved
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()
def value_in_object_units(self, value):
"""Convert one or more strings for numerical lengths to floating point values.
Expand Down
4 changes: 2 additions & 2 deletions src/ansys/aedt/core/modeler/cad/primitives_3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand Down
35 changes: 32 additions & 3 deletions src/ansys/aedt/core/modules/material_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
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()):
Expand Down Expand Up @@ -243,6 +243,35 @@
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(

Check warning on line 266 in src/ansys/aedt/core/modules/material_lib.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/aedt/core/modules/material_lib.py#L266

Added line #L266 was not covered by tests
"`checkifmaterialexists` is deprecated. Use `exists_material` method instead.", DeprecationWarning
)
return self.exists_material(material=material)

Check warning on line 269 in src/ansys/aedt/core/modules/material_lib.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/aedt/core/modules/material_lib.py#L269

Added line #L269 was not covered by tests

@pyaedt_function_handler()
def exists_material(self, material):
"""Check if a material exists in AEDT or PyAEDT Definitions.

Parameters
----------
material : str
Expand Down Expand Up @@ -293,7 +322,7 @@
``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:
Expand Down Expand Up @@ -451,7 +480,7 @@
"""
matsweep = []
for mat in assignment:
matobj = self.checkifmaterialexists(mat)
matobj = self.exists_material(mat)
if matobj:
matsweep.append(matobj)

Expand Down
36 changes: 36 additions & 0 deletions tests/system/general/test_08_Primitives3D.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Samuelopez-ansys marked this conversation as resolved.
Show resolved Hide resolved
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"
Loading