Skip to content

Commit

Permalink
FIX: Fixes to get_object_material_properties method (#5626)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
ansys-satyajeet and pre-commit-ci[bot] authored Jan 15, 2025
1 parent 00c3a82 commit 11e2fdd
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/ansys/aedt/core/application/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1643,7 +1643,7 @@ def get_object_material_properties(self, assignment=None, prop_names=None):

dict = {}
for entry in assignment:
mat_name = self.modeler[entry].material_name
mat_name = self.modeler[entry].material_name.casefold()
mat_props = self.materials.material_keys[mat_name]
if prop_names is None:
dict[entry] = mat_props._props
Expand Down
88 changes: 46 additions & 42 deletions src/ansys/aedt/core/modules/material_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def _mat_names_aedt(self):
@property
def _mat_names_aedt_lower(self):
if len(self._mats_lower) < len(self._mat_names_aedt):
self._mats_lower = [i.lower() for i in self._mat_names_aedt]
self._mats_lower = [i.casefold() for i in self._mat_names_aedt]
return self._mats_lower

@property
Expand Down Expand Up @@ -216,18 +216,18 @@ def get_mat_list(file_name):

@pyaedt_function_handler()
def _get_aedt_case_name(self, material_name):
if material_name.lower() in self.material_keys:
return self.material_keys[material_name.lower()].name
if material_name.lower() in self.mat_names_aedt_lower:
return self._mat_names_aedt[self.mat_names_aedt_lower.index(material_name.lower())]
if material_name.casefold() in self.material_keys:
return self.material_keys[material_name.casefold()].name
if material_name.casefold() in self.mat_names_aedt_lower:
return self._mat_names_aedt[self.mat_names_aedt_lower.index(material_name.casefold())]
return False

@pyaedt_function_handler()
def _get_surface_materials(self):
mats = {}
try:
for ds in self._app.project_properties["AnsoftProject"]["Definitions"]["SurfaceMaterials"]:
mats[ds.lower()] = SurfaceMaterial(
mats[ds.casefold()] = SurfaceMaterial(
self,
ds,
self._app.project_properties["AnsoftProject"]["Definitions"]["SurfaceMaterials"][ds],
Expand Down Expand Up @@ -286,17 +286,17 @@ def exists_material(self, material):
>>> oMaterialManager.GetData
"""
if isinstance(material, Material):
if material.name.lower() in self.material_keys:
if material.name.casefold() in self.material_keys:
return material
else:
return False
if material.lower() in self.material_keys:
if material.lower() in self.mat_names_aedt_lower:
return self.material_keys[material.lower()]
if material.lower() not in list(self.odefinition_manager.GetProjectMaterialNames()):
self.material_keys[material.lower()].update()
return self.material_keys[material.lower()]
elif material.lower() in self.mat_names_aedt_lower:
if material.casefold() in self.material_keys:
if material.casefold() in self.mat_names_aedt_lower:
return self.material_keys[material.casefold()]
if material.casefold() not in list(self.odefinition_manager.GetProjectMaterialNames()):
self.material_keys[material.casefold()].update()
return self.material_keys[material.casefold()]
elif material.casefold() in self.mat_names_aedt_lower:
return self._aedmattolibrary(material)
elif settings.remote_api or settings.remote_rpc_session:
return self._aedmattolibrary(material)
Expand Down Expand Up @@ -360,9 +360,9 @@ def add_material(self, name, properties=None):
"""
self.logger.info("Adding new material to the Project Library: " + name)
if name.lower() in self.material_keys:
if name.casefold() in self.material_keys:
self.logger.warning("Warning. The material is already in the database. Change or edit the name.")
return self.material_keys[name.lower()]
return self.material_keys[name.casefold()]
elif self._get_aedt_case_name(name):
return self._aedmattolibrary(self._get_aedt_case_name(name))
else:
Expand All @@ -371,9 +371,9 @@ def add_material(self, name, properties=None):
material._material_update = True
if material:
self.logger.info("Material has been added in Desktop.")
self.material_keys[name.lower()] = material
self.material_keys[name.casefold()] = material
self._mats.append(name)
return self.material_keys[name.lower()]
return self.material_keys[name.casefold()]
return False

@pyaedt_function_handler(material_name="name")
Expand Down Expand Up @@ -408,18 +408,18 @@ def add_surface_material(self, name, emissivity=None):
"""
self.logger.info("Adding a surface material to the project library: " + name)
if name.lower() in self.surface_material_keys:
if name.casefold() in self.surface_material_keys:
self.logger.warning("Warning. The material is already in the database. Change the name or edit it.")
return self.surface_material_keys[name.lower()]
return self.surface_material_keys[name.casefold()]
else:
material = SurfaceMaterial(self._app, name, material_update=False)
if emissivity:
material.emissivity.value = emissivity
material.update()
material._material_update = True
self.logger.info("Material has been added.")
self.surface_material_keys[name.lower()] = material
return self.surface_material_keys[name.lower()]
self.surface_material_keys[name.casefold()] = material
return self.surface_material_keys[name.casefold()]

@pyaedt_function_handler()
def _create_mat_project_vars(self, matlist):
Expand Down Expand Up @@ -490,7 +490,7 @@ def add_material_sweep(self, assignment, name):
newmat._update_props(el, array_var_name + "[" + index + "]", False)

newmat.update()
self.material_keys[name.lower()] = newmat
self.material_keys[name.casefold()] = newmat
return index

@pyaedt_function_handler(material_name="material", new_name="name", props="properties")
Expand Down Expand Up @@ -544,15 +544,15 @@ def duplicate_material(self, material, name=None, properties=None):
)

# Get the material definition.
material_in_aedt = material.lower() in list(self.mat_names_aedt_lower)
material_in_project = material.lower() in list(self.material_keys.keys())
material_in_aedt = material.casefold() in list(self.mat_names_aedt_lower)
material_in_project = material.casefold() in list(self.material_keys.keys())
if not (material_in_aedt or material_in_project): # Check for material definition
self.logger.error(f"Material {material} is not present")
return False
if not material_in_project:
material = self._aedmattolibrary(material)
else:
material = self.material_keys[material.lower()]
material = self.material_keys[material.casefold()]

if not name:
name = material + "_clone"
Expand All @@ -575,11 +575,11 @@ def duplicate_material(self, material, name=None, properties=None):
new_material.update()
new_material._material_update = True
self._mats.append(name)
self.material_keys[name.lower()] = new_material
self.material_keys[name.casefold()] = new_material
return new_material

@pyaedt_function_handler(new_name="name")
def duplicate_surface_material(self, material, name):
def duplicate_surface_material(self, material, name=None):
"""Duplicate a surface material.
Parameters
Expand All @@ -605,13 +605,15 @@ def duplicate_surface_material(self, material, name):
>>> hfss.materials.add_surface_material("MyMaterial")
>>> hfss.materials.duplicate_surface_material("MyMaterial","MyMaterial2")
"""
if not material.lower() in list(self.surface_material_keys.keys()):
if material.casefold() not in list(self.surface_material_keys.keys()):
self.logger.error(f"Material {material} is not present")
return False
if not name:
name = material + "_clone"
newmat = SurfaceMaterial(
self, name.lower(), self.surface_material_keys[material.lower()]._props, material_update=True
self, name, self.surface_material_keys[material.casefold()]._props, material_update=True
)
self.surface_material_keys[name.lower()] = newmat
self.surface_material_keys[name.casefold()] = newmat
return newmat

@pyaedt_function_handler()
Expand Down Expand Up @@ -643,7 +645,7 @@ def remove_material(self, material, library="Project"):
>>> hfss.materials.remove_material("MyMaterial")
"""
mat = material.lower()
mat = material.casefold()
if mat not in list(self.material_keys.keys()):
self.logger.error(f"Material {mat} is not present")
return False
Expand Down Expand Up @@ -718,8 +720,8 @@ def _aedmattolibrary(self, matname):
first_value = next(value_iterator)
newmat = Material(self, matname, first_value, material_update=False)
newmat._material_update = True
self.material_keys[matname.lower()] = newmat
return self.material_keys[matname.lower()]
self.material_keys[matname.casefold()] = newmat
return self.material_keys[matname.casefold()]

@pyaedt_function_handler(full_json_path="output_file")
def export_materials_to_file(self, output_file):
Expand Down Expand Up @@ -758,7 +760,9 @@ def find_datasets(d, out_list):
# Data to be written
output_dict = {}
for el, val in self.material_keys.items():
output_dict[el] = copy.deepcopy(val._props)
matobj = self.material_keys[el]
matname = matobj.name
output_dict[matname] = copy.deepcopy(val._props)
out_list = []
find_datasets(output_dict, out_list)
datasets = {}
Expand Down Expand Up @@ -812,11 +816,11 @@ def import_materials_from_file(self, input_file=None, **kwargs):
_, file_extension = os.path.splitext(input_file)
json_flag = True
datasets = {}
if file_extension.lower() == ".json":
if file_extension.casefold() == ".json":
data = read_json(input_file)
if "datasets" in list(data.keys()):
datasets = data["datasets"]
elif file_extension.lower() == ".amat":
elif file_extension.casefold() == ".amat":
data = load_entire_aedt_file(input_file)
json_flag = False
new_data = {}
Expand Down Expand Up @@ -860,10 +864,10 @@ def import_materials_from_file(self, input_file=None, **kwargs):
if numcol > 2:
zunit = val["Coordinates"]["DimUnits"][2]
zval = [sublist[2] for sublist in new_list]
self._app.create_dataset(el[1:], x=xval, y=yval, z=zval, x_unit=xunit, y_unit=yunit)
self._app.create_dataset(el[1:], x=xval, y=yval, z=zval, x_unit=xunit, y_unit=yunit, z_unit=zunit)
if json_flag:
for el, val in data["materials"].items():
if el.lower() in list(self.material_keys.keys()):
if el.casefold() in list(self.material_keys.keys()):
newname = generate_unique_name(el)
self.logger.warning("Material %s already exists. Renaming to %s", el, newname)
else:
Expand All @@ -882,7 +886,7 @@ def import_materials_from_file(self, input_file=None, **kwargs):
invalid_names = ["$base_index$", "$index$"]
if mat_name in invalid_names:
continue
if mat_name.lower() in list(self.material_keys.keys()):
if mat_name.casefold() in list(self.material_keys.keys()):
newname = generate_unique_name(mat_name)
self.logger.warning("Material %s already exists. Renaming to %s", mat_name, newname)
else:
Expand Down Expand Up @@ -920,11 +924,11 @@ def import_materials_from_excel(self, input_file):
else:
self.logger.error("Only csv and xlsx are supported.")
return False
keys = [i.lower() for i in list(df.keys())]
keys = [i.casefold() for i in list(df.keys())]
for el, val in df[::-1].iterrows():
if isinstance(el, float):
break
if el.lower() in list(self.material_keys.keys()):
if el.casefold() in list(self.material_keys.keys()):
newname = generate_unique_name(el)
self.logger.warning("Material %s already exists. Renaming to %s", el, newname)
else:
Expand Down
1 change: 1 addition & 0 deletions tests/system/general/test_03_Materials.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ def test_06_surface_material(self, add_app):
assert mat2.surface_roughness.value == SurfMatProperties.get_defaultvalue(aedtname="surface_roughness")
assert ipk.materials.duplicate_surface_material("Steel", "Steel2")
assert not ipk.materials.duplicate_surface_material("Steel4", "Steel2")
assert ipk.materials.duplicate_surface_material("Steel")

def test_07_export_materials(self):
assert self.aedtapp.materials.export_materials_to_file(os.path.join(self.local_scratch.path, "materials.json"))
Expand Down
12 changes: 12 additions & 0 deletions tests/system/general/test_98_Icepak.py
Original file line number Diff line number Diff line change
Expand Up @@ -2023,3 +2023,15 @@ def test_83_MultipleMeshRegions(self):
m1 = mesh_class.assign_mesh_region([c1.name])
m2 = mesh_class.assign_mesh_region([c2.name])
assert m1.assignment.name != m2.assignment.name

def test_84_get_object_material_properties(self):
self.aedtapp.modeler.create_box(
origin=[0, 0, 0],
sizes=[10, 10, 10],
name="myBox",
material="Al-Extruded",
)
obj_mat_prop = self.aedtapp.get_object_material_properties(
assignment=["myBox"], prop_names="thermal_conductivity"
)
assert obj_mat_prop["myBox"]["thermal_conductivity"] == "205"

0 comments on commit 11e2fdd

Please sign in to comment.