Skip to content

Commit

Permalink
Enhance docstring+fix backward compatiblity manipulation (#4585)
Browse files Browse the repository at this point in the history
Co-authored-by: Kathy Pippert <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Apr 26, 2024
1 parent 87739a9 commit 61f1bf2
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 20 deletions.
8 changes: 6 additions & 2 deletions _unittest/test_09_Primitives2D.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,14 @@ def test_06_create_region(self):
if self.aedtapp.modeler["Region"]:
self.aedtapp.modeler.delete("Region")
assert "Region" not in self.aedtapp.modeler.object_names
assert self.aedtapp.modeler.create_region([20, "50", "100mm", 20], False)
assert self.aedtapp.modeler.create_region([20, "50", "100mm", 20], "Absolute Offset")
self.aedtapp.modeler["Region"].delete()
region = self.aedtapp.modeler.create_region("100", True)
region = self.aedtapp.modeler.create_region("100", "Percentage Offset")
region.delete()
# test backward compatibility
region = self.aedtapp.modeler.create_region(pad_percent=[100, 10, 5, 2], pad_type=True)
region.delete()
#
region = self.aedtapp.modeler.create_region([100, 100, 100, 100])
assert region.solve_inside
assert region.model
Expand Down
4 changes: 2 additions & 2 deletions pyaedt/generic/configurations.py
Original file line number Diff line number Diff line change
Expand Up @@ -1673,7 +1673,7 @@ def _export_objects_properties(self, dict_out):
def _export_mesh_operations(self, dict_out):
dict_out["mesh"] = {}
args = ["NAME:Settings"]
args += self._app.mesh.global_mesh_region.settings.parse_settings()
args += self._app.mesh.global_mesh_region.settings.parse_settings_as_args()
mop = OrderedDict({})
_arg2dict(args, mop)
dict_out["mesh"]["Settings"] = mop["Settings"]
Expand All @@ -1683,7 +1683,7 @@ def _export_mesh_operations(self, dict_out):
args = ["NAME:Settings"]
else:
args = ["NAME:" + mesh.name, "Enable:=", mesh.Enable]
args += mesh.settings.parse_settings()
args += mesh.settings.parse_settings_as_args()
args += getattr(mesh, "_parse_assignment_value")()
args += ["UserSpecifiedSettings:=", not mesh.manual_settings]
mop = OrderedDict({})
Expand Down
15 changes: 9 additions & 6 deletions pyaedt/modeler/cad/Primitives2D.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,8 @@ def create_region(self, pad_value=300, pad_type="Percentage Offset", name="Regio
Padding values to apply. If a list is not provided, the same
value is applied to all padding directions. If a list of floats
or strings is provided, the values are
interpreted as padding for ``["+X", "-X", "+Y", "-Y", "+Z", "-Z"]``.
interpreted as padding for ``["+X", "-X", "+Y", "-Y"]`` for XY geometry mode,
and ``["+R", "+Z", "-Z"]`` for RZ geometry mode.
pad_type : str, optional
Padding definition. The default is ``"Percentage Offset"``.
Options are ``"Absolute Offset"``,
Expand Down Expand Up @@ -318,24 +319,26 @@ def create_region(self, pad_value=300, pad_type="Percentage Offset", name="Regio
if kwarg.get("pad_percent", False):
pad_percent = kwarg["pad_percent"]
pad_value = pad_percent
if isinstance(pad_value, list) and len(pad_value) < 6:
pad_value = [pad_value[i // 2 + 3 * (i % 2)] for i in range(6)]
if isinstance(pad_value, list) and len(pad_value) == 4:
pad_value = [pad_value[i // 2 + 2 * (i % 2)] for i in range(4)]
pad_type = ["Absolute Offset", "Percentage Offset"][int(is_percentage)]

if isinstance(pad_type, bool):
pad_type = ["Absolute Offset", "Percentage Offset"][int(pad_type)]

if not isinstance(pad_value, list):
pad_value = [pad_value] * 4
if self._app.design_type == "2D Extractor" or (
self._app.design_type == "Maxwell 2D" and self._app.odesign.GetGeometryMode() == "XY"
):
if not isinstance(pad_value, list):
pad_value = [pad_value] * 4
if len(pad_value) != 4:
self.logger.error("Wrong padding list provided. Four values have to be provided.")
return False
pad_value = [pad_value[0], pad_value[2], pad_value[1], pad_value[3], 0, 0]
else:
if len(pad_value) < 3:
if not isinstance(pad_value, list):
pad_value = [pad_value] * 3
if len(pad_value) != 3:
self.logger.error("Wrong padding list provided. Three values have to be provided for RZ geometry.")
return False
pad_value = [pad_value[0], 0, 0, 0, pad_value[1], pad_value[2]]
Expand Down
60 changes: 50 additions & 10 deletions pyaedt/modules/MeshIcepak.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,8 @@ def name(self):
@name.setter
def name(self, value):
try:
self._app.modeler.objects_by_name[self._name].name = value
if self._app.modeler.objects_by_name[self._name].name != value:
self._app.modeler.objects_by_name[self._name].name = value
except KeyError:
if self._app.modeler.objects_by_name[value].history().command == "CreateSubRegion":
self._name = value
Expand Down Expand Up @@ -408,6 +409,13 @@ def parts(self, parts):


class MeshSettings(object):
"""
Class for managing mesh settings.
It can be used like a dictionary. Available keys change according
to the type of settings chosen (manual or automatic).
"""

_automatic_mesh_settings = {"MeshRegionResolution": 3} # min: 1, max: 5
_common_mesh_settings = {
"ProximitySizeFunction": True,
Expand Down Expand Up @@ -464,14 +472,14 @@ def _dim_arg(self, value):
else:
return _dim_arg(value, getattr(self._mesh_class, "_model_units"))

def parse_settings(self):
def parse_settings_as_args(self):
"""
Parse mesh region settings.
Returns
-------
dict
List of strings containing all the parts that must be included in the subregion.
List
Arguments to pass to native APIs.
"""
out = []
for k, v in self._instance_settings.items():
Expand All @@ -481,6 +489,22 @@ def parse_settings(self):
out.append(v)
return out

def parse_settings_as_dictionary(self):
"""
Parse mesh region settings.
Returns
-------
dict
Settings of the subregion.
"""
out = {}
for k, v in self._instance_settings.items():
if k in ["MaxElementSizeX", "MaxElementSizeY", "MaxElementSizeZ", "MinGapX", "MinGapY", "MinGapZ"]:
v = self._dim_arg(v)
out[k] = v
return out

def _key_in_dict(self, key):
if self._mesh_class.manual_settings:
ref_dict = self._manual_mesh_settings
Expand All @@ -489,13 +513,29 @@ def _key_in_dict(self, key):
return key in ref_dict or key in self._common_mesh_settings

def keys(self):
return self.parse_settings().keys()
"""
Get mesh region settings keys.
Returns
-------
dict_keys
Available settings keys.
"""
return self.parse_settings_as_dictionary().keys()

def values(self):
return self.parse_settings().values()
"""
Get mesh region settings values.
Returns
-------
dict_values
Settings values.
"""
return self.parse_settings_as_dictionary().values()

def __repr__(self):
return repr(self.parse_settings())
return repr(self.parse_settings_as_dictionary())

def __getitem__(self, key):
if key == "Level":
Expand Down Expand Up @@ -628,7 +668,7 @@ def update(self):
>>> oModule.EditGlobalMeshRegion
"""
args = ["NAME:Settings"]
args += self.settings.parse_settings()
args += self.settings.parse_settings_as_args()
args += ["UserSpecifiedSettings:=", self.manual_settings]
try:
self._app.omeshmodule.EditGlobalMeshRegion(args)
Expand Down Expand Up @@ -754,7 +794,7 @@ def update(self):
>>> oModule.EditMeshRegion
"""
args = ["NAME:" + self.name, "Enable:=", self.enable]
args += self.settings.parse_settings()
args += self.settings.parse_settings_as_args()
args += self._parse_assignment_value()
args += ["UserSpecifiedSettings:=", self.manual_settings]
try:
Expand Down Expand Up @@ -854,7 +894,7 @@ def create(self):
self._app.logger.error("Cannot create a new mesh region with this Name")
return False
args = ["NAME:" + self.name, "Enable:=", self.enable]
args += self.settings.parse_settings()
args += self.settings.parse_settings_as_args()
args += ["UserSpecifiedSettings:=", not self.manual_settings]
args += self._parse_assignment_value()
self._app.omeshmodule.AssignMeshRegion(args)
Expand Down

0 comments on commit 61f1bf2

Please sign in to comment.