From 8fa62bba38f8b71f85dba7af9c3bdf1434d53900 Mon Sep 17 00:00:00 2001 From: L-Nafaryus Date: Fri, 14 Jan 2022 00:12:08 +0500 Subject: [PATCH] Mod: mesh and shape io Mod: mesh patches --- anisotropy/meshing/mesh.py | 68 +++++++++++++++++++++++++++---------- anisotropy/shaping/shape.py | 45 ++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 18 deletions(-) diff --git a/anisotropy/meshing/mesh.py b/anisotropy/meshing/mesh.py index 310ced5..11cc720 100644 --- a/anisotropy/meshing/mesh.py +++ b/anisotropy/meshing/mesh.py @@ -7,9 +7,19 @@ from netgen import meshing from numpy import array import os + +class NoGeometrySpecified(Exception): + def __init__(self, msg): + super().__init__(self, msg) + + +class NotSupportedMeshFormat(Exception): + def __init__(self, msg): + super().__init__(self, msg) + class Mesh(object): - def __init__(self, shape): - self.geometry = OCCGeometry(shape) + def __init__(self, shape: OCCGeometry = None): + self.geometry = OCCGeometry(shape) if shape else None self.mesh = None # Parameters @@ -43,15 +53,44 @@ class Mesh(object): ) def build(self): - self.mesh = self.geometry.GenerateMesh(self.parameters) + if self.geometry: + self.mesh = self.geometry.GenerateMesh(self.parameters) - def export(self, filename: str): - """Export a shape. + else: + raise NoGeometrySpecified("Specify a geometry to build a mesh") + + formats = { + "vol": "Netgen Format", + "mesh": "Neutral Format", + "msh": "Gmsh2 Format" + } + + def load(self, filename: str): + """Import a mesh. - Supported formats: vol, mesh. + Use `Mesh.formats` to see supported formats. :param filename: - Name of the file to store the given shape in. + Name of the file to store the given mesh in. + """ + ext = os.path.splitext(filename)[1][1: ] + + if ext in self.formats.keys(): + self.mesh = meshing.Mesh() + self.mesh.Load(filename) + + else: + raise NotSupportedMeshFormat(f"Mesh format '{ ext }' is not supported") + + return self + + def export(self, filename: str): + """Export a mesh. + + Use `Mesh.formats` to see supported formats. + + :param filename: + Name of the file to store the given mesh in. :return: Output, error messages and returncode @@ -63,20 +102,13 @@ class Mesh(object): if ext == "vol": self.mesh.Save(filename) - elif ext == "mesh": - self.mesh.Export(filename, "Neutral Format") - - elif ext == "msh": - self.mesh.Export(filename, "Gmsh2 Format") + elif ext in self.formats.keys(): + self.mesh.Export(filename, self.formats[ext]) else: - raise NotImplementedError(f"Mesh format '{ ext }' is not supported") + raise NotSupportedMeshFormat(f"Mesh format '{ ext }' is not supported") - except NotImplementedError as e: - err = e - returncode = 1 - - except Exception as e: + except (NotSupportedMeshFormat, Exception) as e: err = e returncode = 1 diff --git a/anisotropy/shaping/shape.py b/anisotropy/shaping/shape.py index 0c0a2d2..1707c52 100644 --- a/anisotropy/shaping/shape.py +++ b/anisotropy/shaping/shape.py @@ -56,6 +56,51 @@ class Shape(object): else: raise NotImplementedError(f"Shape format '{ext}' is not supported") + + return self + + def patches(self, group: bool = False, shiftIndex: bool = False, prefix: str = None): + """Get patches indices with their names. + + :param group: + Group indices together with the same patches names. + + :param shiftIndex: + Start numerating with one instead of zero. + + :param prefix: + Add string prefix to the index. + + :return: + List if group = False else dictionary. + """ + if group: + patches_ = {} + + for idn, face in enumerate(self.shape.faces): + if shiftIndex: + idn += 1 + + item = idn if not prefix else prefix + str(idn) + + if patches_.get(face.name): + patches_[face.name].append(item) + + else: + patches_[face.name] = [ item ] + + else: + patches_ = [] + + for idn, face in enumerate(self.shape.faces): + if shiftIndex: + idn += 1 + + item = idn if not prefix else prefix + str(idn) + + patches_.append((item, face.name)) + + return patches_ def normal(self, face: FACE) -> numpy.array: """