From e0470a8aef26fef18fcc1a5ed29c59b6bd3333a6 Mon Sep 17 00:00:00 2001 From: maxcapodi78 Date: Mon, 6 May 2024 16:05:18 +0200 Subject: [PATCH 1/3] improve nastran method --- pyaedt/modeler/modeler3d.py | 68 +++++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 30 deletions(-) diff --git a/pyaedt/modeler/modeler3d.py b/pyaedt/modeler/modeler3d.py index b2f19ef0ec0..c32d7927292 100644 --- a/pyaedt/modeler/modeler3d.py +++ b/pyaedt/modeler/modeler3d.py @@ -6,6 +6,8 @@ import os.path import warnings +from line_profiler_pycharm import profile + from pyaedt.application.Variables import generate_validation_errors from pyaedt.generic.general_methods import GrpcApiError from pyaedt.generic.general_methods import generate_unique_name @@ -877,7 +879,8 @@ def objects_in_bounding_box(self, bounding_box, check_solids=True, check_lines=T return objects @pyaedt_function_handler() - def import_nastran(self, file_path, import_lines=True, lines_thickness=0, **kwargs): + @profile + def import_nastran(self, file_path, import_lines=True, lines_thickness=0, import_as_light_weight=False, **kwargs): """Import Nastran file into 3D Modeler by converting the faces to stl and reading it. The solids are translated directly to AEDT format. @@ -890,6 +893,8 @@ def import_nastran(self, file_path, import_lines=True, lines_thickness=0, **kwar lines_thickness : float, optional Whether to thicken lines after creation and it's default value. Every line will be parametrized with a design variable called ``xsection_linename``. + import_as_light_weight : bool, optional + Import the stl generatated as light weight. It works only on SBR+ and HFSS Regions. Default is ``False``. Returns ------- @@ -940,8 +945,8 @@ def _write_solid_stl(triangle, nas_to_dict): continue elif line_type in ["GRID", "CTRIA3"]: grid_id = int(line[8:16]) - if line_type == "CTRIA3": - tria_id = int(line[16:24]) + # if line_type == "CTRIA3": + # tria_id = int(line[16:24]) n1 = line[24:32].strip() if "-" in n1[1:]: n1 = n1[0] + n1[1:].replace("-", "e-") @@ -957,14 +962,12 @@ def _write_solid_stl(triangle, nas_to_dict): id += 1 else: tri = [int(n1), int(n2), int(n3)] - tri.sort() - if tri not in nas_to_dict["Triangles"]: - nas_to_dict["Triangles"].append(tri) + nas_to_dict["Triangles"].append(tri) elif line_type in ["GRID*", "CTRIA3*"]: grid_id = int(line[8:24]) - if line_type == "CTRIA3*": - tria_id = int(line[24:40]) + # if line_type == "CTRIA3*": + # tria_id = int(line[24:40]) n1 = line[40:56].strip() if "-" in n1[1:]: n1 = n1[0] + n1[1:].replace("-", "e-") @@ -983,16 +986,16 @@ def _write_solid_stl(triangle, nas_to_dict): nas_to_dict["PointsId"][grid_id] = id id += 1 else: - tri = [int(n1), int(n2), int(n3)] - tri.sort() - if tri not in nas_to_dict["Triangles"]: - nas_to_dict["Triangles"].append(tri) + tri = (int(n1), int(n2), int(n3)) + nas_to_dict["Triangles"].append(tri) elif line_type in ["CPENTA", "CHEXA", "CTETRA"]: - obj_id = line[16:24].strip() + # obj_id = line[16:24].strip() n = [] el_id = line[24:32].strip() - # n = [int(line[24:32])] + if el_id not in nas_to_dict["Solids"]: + nas_to_dict["Solids"][el_id] = [] + n.append(int(line[32:40])) n.append(int(line[40:48])) n.append(int(line[48:56])) @@ -1008,17 +1011,18 @@ def _write_solid_stl(triangle, nas_to_dict): n.append(int(lines[lk][16:24].strip())) from itertools import combinations - tris = [] for k in list(combinations(n, 3)): tri = [int(k[0]), int(k[1]), int(k[2])] - tris.append(tri) - nas_to_dict["Solids"]["{}_{}".format(el_id, obj_id)] = tris - if el_id not in el_ids: - el_ids.append(el_id) + tri.sort() + tri = tuple(tri) + nas_to_dict["Solids"][el_id].append(tri) + elif line_type in ["CTETRA*"]: - obj_id = line[8:24].strip() + # obj_id = line[8:24].strip() n = [] el_id = line[24:40].strip() + if el_id not in nas_to_dict["Solids"]: + nas_to_dict["Solids"][el_id] = [] # n.append(line[24:40].strip()) n.append(line[40:56].strip()) @@ -1028,14 +1032,11 @@ def _write_solid_stl(triangle, nas_to_dict): from itertools import combinations - tris = [] for k in list(combinations(n, 3)): tri = [int(k[0]), int(k[1]), int(k[2])] - tris.append(tri) - - nas_to_dict["Solids"]["{}_{}".format(el_id, obj_id)] = tris - if el_id not in el_ids: - el_ids.append(el_id) + tri.sort() + tri = tuple(tri) + nas_to_dict["Solids"][el_id].append(tri) elif line_type in ["CROD", "CBEAM"]: obj_id = int(line[16:24]) @@ -1059,14 +1060,21 @@ def _write_solid_stl(triangle, nas_to_dict): f.write("endsolid\n") for solidid, solid_triangles in nas_to_dict["Solids"].items(): f.write("solid Solid_{}\n".format(solidid)) - for triangle in solid_triangles: + import pandas as pd + + df = pd.Series(solid_triangles) + undulicated_values = df.drop_duplicates(keep=False).to_list() + for triangle in undulicated_values: _write_solid_stl(triangle, nas_to_dict) f.write("endsolid\n") f.close() self.logger.info("STL file created") - self.import_3d_cad(os.path.join(self._app.working_directory, self._app.design_name + "_test.stl")) - for el in el_ids: - obj_names = [i for i in self.solid_names if i.startswith("Solid_{}_".format(el))] + self.import_3d_cad( + os.path.join(self._app.working_directory, self._app.design_name + "_test.stl"), + create_lightweigth_part=import_as_light_weight, + ) + for el in nas_to_dict["Solids"].keys(): + obj_names = [i for i in self.solid_names if i.startswith("Solid_{}".format(el))] self.create_group(obj_names, group_name=el) self.logger.info_timer("Faces imported") From e49b657c45f49ba8827fd0b455ab9a6b8fc05f45 Mon Sep 17 00:00:00 2001 From: maxcapodi78 Date: Mon, 6 May 2024 16:27:21 +0200 Subject: [PATCH 2/3] improve nastran method --- pyaedt/modeler/modeler3d.py | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/pyaedt/modeler/modeler3d.py b/pyaedt/modeler/modeler3d.py index c32d7927292..faabee5894b 100644 --- a/pyaedt/modeler/modeler3d.py +++ b/pyaedt/modeler/modeler3d.py @@ -930,7 +930,7 @@ def _write_solid_stl(triangle, nas_to_dict): f.write(" endloop\n") f.write(" endfacet\n") - nas_to_dict = {"Points": {}, "PointsId": {}, "Triangles": [], "Lines": {}, "Solids": {}} + nas_to_dict = {"Points": {}, "PointsId": {}, "Triangles": {}, "Lines": {}, "Solids": {}} self.logger.reset_timer() self.logger.info("Loading file") @@ -945,8 +945,10 @@ def _write_solid_stl(triangle, nas_to_dict): continue elif line_type in ["GRID", "CTRIA3"]: grid_id = int(line[8:16]) - # if line_type == "CTRIA3": - # tria_id = int(line[16:24]) + if line_type == "CTRIA3": + tria_id = int(line[16:24]) + if tria_id not in nas_to_dict["Triangles"]: + nas_to_dict["Triangles"][tria_id] = [] n1 = line[24:32].strip() if "-" in n1[1:]: n1 = n1[0] + n1[1:].replace("-", "e-") @@ -962,12 +964,14 @@ def _write_solid_stl(triangle, nas_to_dict): id += 1 else: tri = [int(n1), int(n2), int(n3)] - nas_to_dict["Triangles"].append(tri) + nas_to_dict["Triangles"][tria_id].append(tri) elif line_type in ["GRID*", "CTRIA3*"]: grid_id = int(line[8:24]) - # if line_type == "CTRIA3*": - # tria_id = int(line[24:40]) + if line_type == "CTRIA3*": + tria_id = int(line[24:40]) + if tria_id not in nas_to_dict["Triangles"]: + nas_to_dict["Triangles"][tria_id] = [] n1 = line[40:56].strip() if "-" in n1[1:]: n1 = n1[0] + n1[1:].replace("-", "e-") @@ -987,7 +991,7 @@ def _write_solid_stl(triangle, nas_to_dict): id += 1 else: tri = (int(n1), int(n2), int(n3)) - nas_to_dict["Triangles"].append(tri) + nas_to_dict["Triangles"][tria_id].append(tri) elif line_type in ["CPENTA", "CHEXA", "CTETRA"]: # obj_id = line[16:24].strip() @@ -1053,11 +1057,11 @@ def _write_solid_stl(triangle, nas_to_dict): self.logger.reset_timer() self.logger.info("Creating STL file with detected faces") f = open(os.path.join(self._app.working_directory, self._app.design_name + "_test.stl"), "w") - f.write("solid PyaedtStl\n") - for triangle in nas_to_dict["Triangles"]: - _write_solid_stl(triangle, nas_to_dict) - - f.write("endsolid\n") + for tri_id, triangles in nas_to_dict["Triangles"].items(): + f.write("solid Sheet_{}\n".format(tri_id)) + for triangle in triangles: + _write_solid_stl(triangle, nas_to_dict) + f.write("endsolid\n") for solidid, solid_triangles in nas_to_dict["Solids"].items(): f.write("solid Solid_{}\n".format(solidid)) import pandas as pd @@ -1074,8 +1078,11 @@ def _write_solid_stl(triangle, nas_to_dict): create_lightweigth_part=import_as_light_weight, ) for el in nas_to_dict["Solids"].keys(): - obj_names = [i for i in self.solid_names if i.startswith("Solid_{}".format(el))] - self.create_group(obj_names, group_name=el) + obj_names = [i for i in self.object_names if i.startswith("Solid_{}".format(el))] + self.create_group(obj_names, group_name=str(el)) + for el in nas_to_dict["Triangles"].keys(): + obj_names = [i for i in self.object_names if i.startswith("Sheet_{}".format(el))] + self.create_group(obj_names, group_name=str(el)) self.logger.info_timer("Faces imported") if import_lines: From 84648f3545e2c53be7a2a237593577aead38935c Mon Sep 17 00:00:00 2001 From: maxcapodi78 Date: Mon, 6 May 2024 16:33:20 +0200 Subject: [PATCH 3/3] improve nastran method --- pyaedt/modeler/modeler3d.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/pyaedt/modeler/modeler3d.py b/pyaedt/modeler/modeler3d.py index faabee5894b..db3394affb6 100644 --- a/pyaedt/modeler/modeler3d.py +++ b/pyaedt/modeler/modeler3d.py @@ -6,8 +6,6 @@ import os.path import warnings -from line_profiler_pycharm import profile - from pyaedt.application.Variables import generate_validation_errors from pyaedt.generic.general_methods import GrpcApiError from pyaedt.generic.general_methods import generate_unique_name @@ -879,7 +877,6 @@ def objects_in_bounding_box(self, bounding_box, check_solids=True, check_lines=T return objects @pyaedt_function_handler() - @profile def import_nastran(self, file_path, import_lines=True, lines_thickness=0, import_as_light_weight=False, **kwargs): """Import Nastran file into 3D Modeler by converting the faces to stl and reading it. The solids are translated directly to AEDT format.