From 4267f4e710315e782c1e905fb1f546ffbe53a0ef Mon Sep 17 00:00:00 2001 From: Massimo Capodiferro <77293250+maxcapodi78@users.noreply.github.com> Date: Tue, 25 Jul 2023 19:51:14 +0200 Subject: [PATCH] Fix coordinate systems get props (#3306) * props updated was missed * props updated was missed * update duplicate cs + tests * Improved method generate_fluent_mesh --------- Co-authored-by: maxcapodi78 Co-authored-by: Giulia Malinverno --- _unittest/test_02_3D_modeler.py | 18 +++ _unittest/test_98_Icepak.py | 82 ++++++++-- pyaedt/modeler/cad/Modeler.py | 259 ++++++++++++++++++++++++-------- 3 files changed, 284 insertions(+), 75 deletions(-) diff --git a/_unittest/test_02_3D_modeler.py b/_unittest/test_02_3D_modeler.py index e62abee9d4b..b6f8e34ab70 100644 --- a/_unittest/test_02_3D_modeler.py +++ b/_unittest/test_02_3D_modeler.py @@ -737,6 +737,24 @@ def test_57_duplicate_coordinate_system_to_global(self): assert all(abs(o[i] - s) < tol for i, s in enumerate([1.82842712474619, 2.20832611206852, 9.0])) assert all(abs(q[i] - s) < tol for i, s in enumerate([-0.0, -0.09853761796664, 0.99513332666807, 0.0])) assert self.aedtapp.modeler.reference_cs_to_global(cs4) + box = self.aedtapp.modeler.create_box([0, 0, 0], [2, 2, 2]) + face = box.faces[0] + fcs = self.aedtapp.modeler.create_face_coordinate_system(face, face.edges[0], face.edges[1]) + new_fcs = self.aedtapp.modeler.duplicate_coordinate_system_to_global(fcs) + assert new_fcs + assert new_fcs.props == fcs.props + fcs = self.aedtapp.modeler.create_face_coordinate_system(face, face, face.edges[1]) + new_fcs = self.aedtapp.modeler.duplicate_coordinate_system_to_global(fcs) + assert new_fcs + assert new_fcs.props == fcs.props + fcs = self.aedtapp.modeler.create_face_coordinate_system(face, face, face.edges[1].vertices[0]) + new_fcs = self.aedtapp.modeler.duplicate_coordinate_system_to_global(fcs) + assert new_fcs + assert new_fcs.props == fcs.props + fcs = self.aedtapp.modeler.create_face_coordinate_system(face, face.edges[1].vertices[0], face.edges[1]) + new_fcs = self.aedtapp.modeler.duplicate_coordinate_system_to_global(fcs) + assert new_fcs + assert new_fcs.props == fcs.props def test_58_invert_cs(self): self.aedtapp.modeler.create_coordinate_system( diff --git a/_unittest/test_98_Icepak.py b/_unittest/test_98_Icepak.py index ac7e5f37782..bb0ed65360e 100644 --- a/_unittest/test_98_Icepak.py +++ b/_unittest/test_98_Icepak.py @@ -822,13 +822,84 @@ def test_50_advanced3dcomp_export(self): fan_obj_3d.delete() def test_51_advanced3dcomp_import(self): + self.aedtapp.insert_design("test_3d_comp") + surf1 = self.aedtapp.modeler.create_rectangle(self.aedtapp.PLANE.XY, [0, 0, 0], [10, 20], name="surf1") + box1 = self.aedtapp.modeler.create_box([20, 20, 2], [10, 10, 3], "box1", "copper") + fan = self.aedtapp.create_fan("Fan", cross_section="YZ", radius="1mm", hub_radius="0mm") + cs0 = self.aedtapp.modeler.create_coordinate_system(name="CS0") + cs0.props["OriginX"] = 10 + cs0.props["OriginY"] = 10 + cs0.props["OriginZ"] = 10 + cs1 = self.aedtapp.modeler.create_coordinate_system(name="CS1", reference_cs="CS0") + cs1.props["OriginX"] = 10 + cs1.props["OriginY"] = 10 + cs1.props["OriginZ"] = 10 + fan2_prop = dict(fan.props).copy() + fan2_prop["TargetCS"] = "CS1" + fan2 = NativeComponentObject(self.aedtapp, "Fan", "Fan2", fan2_prop) + fan2.create() + pcb = self.aedtapp.create_ipk_3dcomponent_pcb( + "Board", link_data, solution_freq, resolution, custom_x_resolution=400, custom_y_resolution=500 + ) + self.aedtapp.monitor.assign_face_monitor(box1.faces[0].id, "Temperature", "FaceMonitor") + self.aedtapp.monitor.assign_point_monitor_in_object(box1.name, "Temperature", "BoxMonitor") + self.aedtapp.monitor.assign_surface_monitor(surf1.name, "Temperature", "SurfaceMonitor") + self.aedtapp.create_dataset( + "test_dataset", + [1, 2, 3, 4], + [1, 2, 3, 4], + zlist=None, + vlist=None, + is_project_dataset=False, + xunit="cel", + yunit="W", + zunit="", + vunit="", + ) + file_path = self.local_scratch.path + file_name = "Advanced3DComp_T51.a3dcomp" + fan_obj = self.aedtapp.create_fan(is_2d=True) + self.aedtapp.monitor.assign_surface_monitor( + list(self.aedtapp.modeler.user_defined_components[fan_obj.name].parts.values())[0].name + ) + self.aedtapp.monitor.assign_face_monitor( + list(self.aedtapp.modeler.user_defined_components[fan_obj.name].parts.values())[0].faces[0].id + ) + fan_obj_3d = self.aedtapp.create_fan(is_2d=False) + self.aedtapp.monitor.assign_point_monitor_in_object( + list(self.aedtapp.modeler.user_defined_components[fan_obj_3d.name].parts.values())[0].name + ) + self.aedtapp.create_dataset( + "test_ignore", + [1, 2, 3, 4], + [1, 2, 3, 4], + zlist=None, + vlist=None, + is_project_dataset=False, + xunit="cel", + yunit="W", + zunit="", + vunit="", + ) + mon_list = list(self.aedtapp.monitor.all_monitors.keys()) + self.aedtapp.monitor.assign_point_monitor([0, 0, 0]) + cs_list = [cs.name for cs in self.aedtapp.modeler.coordinate_systems if cs.name != "CS0"] + self.aedtapp.modeler.create_coordinate_system() + assert self.aedtapp.modeler.create_3dcomponent( + os.path.join(file_path, file_name), + component_name="board_assembly", + included_cs=cs_list, + auxiliary_dict=True, + reference_cs="CS1", + monitor_objects=mon_list, + datasets=["test_dataset"], + ) self.aedtapp.insert_design("test_51_1") cs2 = self.aedtapp.modeler.create_coordinate_system(name="CS2") cs2.props["OriginX"] = 20 cs2.props["OriginY"] = 20 cs2.props["OriginZ"] = 20 file_path = self.local_scratch.path - file_name = "Advanced3DComp.a3dcomp" self.aedtapp.modeler.insert_3d_component( comp_file=os.path.join(file_path, file_name), targetCS="CS2", auxiliary_dict=True ) @@ -850,15 +921,6 @@ def test_51_advanced3dcomp_import(self): self.aedtapp.modeler.insert_3d_component( comp_file=os.path.join(file_path, file_name), targetCS="Global", auxiliary_dict=False, name="test" ) - file_name = "Advanced3DComp1.a3dcomp" - self.aedtapp.insert_design("test_51_3") - cs2 = self.aedtapp.modeler.create_coordinate_system(name="CS2") - cs2.props["OriginX"] = 20 - cs2.props["OriginY"] = 20 - cs2.props["OriginZ"] = 20 - self.aedtapp.modeler.insert_3d_component( - comp_file=os.path.join(file_path, file_name), targetCS="CS2", auxiliary_dict=True, name="test" - ) def test_52_flatten_3d_components(self): self.aedtapp.insert_design("test_52") diff --git a/pyaedt/modeler/cad/Modeler.py b/pyaedt/modeler/cad/Modeler.py index 2f8a1089975..acc40dd3a5a 100644 --- a/pyaedt/modeler/cad/Modeler.py +++ b/pyaedt/modeler/cad/Modeler.py @@ -15,10 +15,10 @@ import os import warnings +# from pyaedt.generic.general_methods import property +from pyaedt.application.Variables import decompose_variable_value from pyaedt.generic.DataHandlers import _dict2arg from pyaedt.generic.constants import AEDT_UNITS - -# from pyaedt.generic.general_methods import property from pyaedt.generic.general_methods import PropsManager from pyaedt.generic.general_methods import generate_unique_name from pyaedt.generic.general_methods import pyaedt_function_handler @@ -98,6 +98,125 @@ def __init__(self, modeler, name=None): self.model_units = self._modeler.model_units self.name = name + def _get_coordinates_data(self): + self._props = {} + id2name = {1: "Global"} + name2refid = {} + if self._modeler._app.design_properties and "ModelSetup" in self._modeler._app.design_properties: + cs = self._modeler._app.design_properties["ModelSetup"]["GeometryCore"]["GeometryOperations"][ + "CoordinateSystems" + ] + for ds in cs: + try: + if isinstance(cs[ds], (OrderedDict, dict)): + if cs[ds]["OperationType"] == "CreateRelativeCoordinateSystem": + props = cs[ds]["RelativeCSParameters"] + name = cs[ds]["Attributes"]["Name"] + if name != self.name: + continue + cs_id = cs[ds]["ID"] + id2name[cs_id] = name + name2refid[name] = cs[ds]["ReferenceCoordSystemID"] + self._props = CsProps(self, props) + if "ZXZ" in props["Mode"]: + self.mode = "zxz" + elif "ZYZ" in props["Mode"]: + self.mode = "zyz" + else: + self.mode = "axis" + elif cs[ds]["OperationType"] == "CreateFaceCoordinateSystem": + name = cs[ds]["Attributes"]["Name"] + if name != self.name: + continue + cs_id = cs[ds]["ID"] + id2name[cs_id] = name + op_id = cs[ds]["PlaceHolderOperationID"] + geometry_part = self._modeler._app.design_properties["ModelSetup"]["GeometryCore"][ + "GeometryOperations" + ]["ToplevelParts"]["GeometryPart"] + if isinstance(geometry_part, (OrderedDict, dict)): + op = geometry_part["Operations"]["FaceCSHolderOperation"] + if isinstance(op, (OrderedDict, dict)): + if op["ID"] == op_id: + props = op["FaceCSParameters"] + self._props = CsProps(self, props) + elif isinstance(op, list): + for iop in op: + if iop["ID"] == op_id: + props = iop["FaceCSParameters"] + self._props = CsProps(self, props) + break + elif isinstance(geometry_part, list): + for gp in geometry_part: + op = gp["Operations"]["FaceCSHolderOperation"] + if isinstance(op, (OrderedDict, dict)): + if op["ID"] == op_id: + props = op["FaceCSParameters"] + self._props = CsProps(self, props) + elif isinstance(op, list): + for iop in op: + if iop["ID"] == op_id: + props = iop["FaceCSParameters"] + self._props = CsProps(self, props) + break + elif isinstance(cs[ds], list): + for el in cs[ds]: + if el["OperationType"] == "CreateRelativeCoordinateSystem": + props = el["RelativeCSParameters"] + name = el["Attributes"]["Name"] + if name != self.name: + continue + cs_id = el["ID"] + id2name[cs_id] = name + name2refid[name] = el["ReferenceCoordSystemID"] + self._props = CsProps(self, props) + if "ZXZ" in props["Mode"]: + self.mode = "zxz" + elif "ZYZ" in props["Mode"]: + self.mode = "zyz" + else: + self.mode = "axis" + elif el["OperationType"] == "CreateFaceCoordinateSystem": + name = el["Attributes"]["Name"] + if name != self.name: + continue + cs_id = el["ID"] + id2name[cs_id] = name + op_id = el["PlaceHolderOperationID"] + geometry_part = self._modeler._app.design_properties["ModelSetup"]["GeometryCore"][ + "GeometryOperations" + ]["ToplevelParts"]["GeometryPart"] + if isinstance(geometry_part, (OrderedDict, dict)): + op = geometry_part["Operations"]["FaceCSHolderOperation"] + if isinstance(op, (OrderedDict, dict)): + if op["ID"] == op_id: + props = op["FaceCSParameters"] + self._props = CsProps(self, props) + elif isinstance(op, list): + for iop in op: + if iop["ID"] == op_id: + props = iop["FaceCSParameters"] + self._props = CsProps(self, props) + break + elif isinstance(geometry_part, list): + for gp in geometry_part: + try: + op = gp["Operations"]["FaceCSHolderOperation"] + except KeyError: + continue + if isinstance(op, (OrderedDict, dict)): + if op["ID"] == op_id: + props = op["FaceCSParameters"] + self._props = CsProps(self, props) + elif isinstance(op, list): + for iop in op: + if iop["ID"] == op_id: + props = iop["FaceCSParameters"] + self._props = CsProps(self, props) + break + except: + pass + @pyaedt_function_handler() def _dim_arg(self, value, units=None): """Dimension argument. @@ -249,21 +368,7 @@ def props(self): """ if self._props or settings.aedt_version <= "2022.2" or self.name is None: return self._props - obj1 = self._modeler.oeditor.GetChildObject(self.name) - props = {} - origin = obj1.GetPropValue("Origin") - props["Origin"] = origin - move_cs_to_end = obj1.GetPropValue("Always Move CS to End") - props["MoveToEnd"] = move_cs_to_end - move_cs_to_end = obj1.GetPropValue("Axis") - props["WhichAxis"] = move_cs_to_end - move_cs_to_end = obj1.GetPropValue("Z Rotation Angle") - props["ZRotationAngle"] = move_cs_to_end - move_cs_to_end = obj1.GetPropValue("Position Offset XY/X") - props["XOffset"] = move_cs_to_end - move_cs_to_end = obj1.GetPropValue("Position Offset XY/Y") - props["YOffset"] = move_cs_to_end - self._props = CsProps(self, props) + self._get_coordinates_data() return self._props @property @@ -532,12 +637,15 @@ def mode(self): """Coordinate System mode.""" if self._mode: return self._mode - if "Axis" in self.props.get("Mode", ""): - self._mode = "axis" - elif "ZXZ" in self.props.get("Mode", ""): - self._mode = "zxz" - elif "ZYZ" in self.props.get("Mode", ""): - self._mode == "zyz" + try: + if "Axis" in self.props["Mode"]: + self._mode = "axis" + elif "ZXZ" in self.props["Mode"]: + self._mode = "zxz" + elif "ZYZ" in self.props["Mode"]: + self._mode = "zyz" + except: + pass return self._mode @mode.setter @@ -554,29 +662,7 @@ def props(self): """ if self._props or settings.aedt_version <= "2022.2" or self.name is None: return self._props - obj1 = self._modeler.oeditor.GetChildObject(self.name) - props = {} - origin = obj1.GetPropValue("Origin") - props["OriginX"] = origin[1] - props["OriginY"] = origin[3] - props["OriginZ"] = origin[5] - props["Mode"] = obj1.GetPropValue("Mode") - if "X Axis" in obj1.GetPropNames(): - axisvec = obj1.GetPropValue("X Axis") - props["XAxisXvec"] = axisvec[1] - props["XAxisYvec"] = axisvec[3] - props["XAxisZvec"] = axisvec[5] - ypoint = obj1.GetPropValue("Y Point") - - props["YAxisXvec"] = ypoint[1] - props["YAxisYvec"] = ypoint[3] - props["YAxisZvec"] = ypoint[5] - else: - props["Phi"] = obj1.GetPropValue("Phi") - props["Theta"] = obj1.GetPropValue("Theta") - props["Psi"] = obj1.GetPropValue("Psi") - self._props = CsProps(self, props) - return self._props + self._get_coordinates_data() @property def ref_cs(self): @@ -1360,7 +1446,7 @@ def materials(self): """ return self._app.materials - def _get_coordinates_data(self): + def _get_coordinates_data(self): # pragma: no cover coord = [] id2name = {1: "Global"} name2refid = {} @@ -2108,24 +2194,67 @@ def duplicate_coordinate_system_to_global(self, coordinate_system): cs = self.coordinate_systems[cs_names.index(coordinate_system)] else: # pragma: no cover raise AttributeError("coordinate_system must either be a string or a CoordinateSystem object.") - o, q = self.reference_cs_to_global(coordinate_system) - x, y, _ = GeometryOperators.quaternion_to_axis(q) - reference_cs = "Global" - name = cs.name + "_RefToGlobal" - if name in cs_names: - name = cs.name + generate_unique_name("_RefToGlobal") - cs = CoordinateSystem(self) - if cs: - result = cs.create( - origin=o, - reference_cs=reference_cs, - name=name, - mode="axis", - x_pointing=x, - y_pointing=y, - ) - if result: - return cs + if isinstance(cs, CoordinateSystem): + o, q = self.reference_cs_to_global(coordinate_system) + x, y, _ = GeometryOperators.quaternion_to_axis(q) + reference_cs = "Global" + name = cs.name + "_RefToGlobal" + if name in cs_names: + name = cs.name + generate_unique_name("_RefToGlobal") + cs = CoordinateSystem(self) + if cs: + result = cs.create( + origin=o, + reference_cs=reference_cs, + name=name, + mode="axis", + x_pointing=x, + y_pointing=y, + ) + if result: + return cs + elif isinstance(cs, FaceCoordinateSystem): + name = cs.name + "_RefToGlobal" + if name in cs_names: + name = cs.name + generate_unique_name("_RefToGlobal") + face_cs = FaceCoordinateSystem(self, props=cs.props, name=name, face_id=cs.face_id) + obj = [obj for obj in self._app.modeler.object_list for face in obj.faces if face.id == face_cs.face_id][0] + face = [face for face in obj.faces if face.id == face_cs.face_id][0] + if face_cs.props["Origin"]["PositionType"] == "FaceCenter": + origin = face + elif face_cs.props["Origin"]["PositionType"] == "EdgeCenter": + origin = [edge for edge in face.edges if edge.id == face_cs.props["Origin"]["EntityID"]][0] + elif face_cs.props["Origin"]["PositionType"] == "OnVertex": + edge = [ + edge + for edge in face.edges + for vertex in edge.vertices + if vertex.id == face_cs.props["Origin"]["EntityID"] + ][0] + origin = [v for v in edge.vertices if v.id == face_cs.props["Origin"]["EntityID"]][0] + if face_cs.props["AxisPosn"]["PositionType"] == "EdgeCenter": + axis_position = [edge for edge in face.edges if edge.id == face_cs.props["AxisPosn"]["EntityID"]][0] + elif face_cs.props["AxisPosn"]["PositionType"] == "OnVertex": + edge = [ + edge + for edge in face.edges + for vertex in edge.vertices + if vertex.id == face_cs.props["AxisPosn"]["EntityID"] + ][0] + axis_position = [v for v in edge.vertices if v.id == face_cs.props["AxisPosn"]["EntityID"]][0] + if face_cs: + result = face_cs.create( + face=face, + origin=origin, + axis_position=axis_position, + axis=face_cs.props["WhichAxis"], + name=name, + offset=[face_cs["XOffset"], face_cs["YOffset"]], + rotation=decompose_variable_value(face_cs["ZRotationAngle"])[0], + always_move_to_end=face_cs["MoveToEnd"], + ) + if result: + return cs return False @pyaedt_function_handler()