def test_mesh_sphere_quadrangle(self): aShape = BRepPrimAPI_MakeSphere(10.).Shape() # Create the Mesh aMeshGen = SMESH_Gen() aMesh = aMeshGen.CreateMesh(0, True) # 1D an1DHypothesis = StdMeshers_Arithmetic1D( 0, 0, aMeshGen) #discretization of the wire an1DHypothesis.SetLength( 0.1, False) #the smallest distance between 2 points an1DHypothesis.SetLength(0.5, True) # the longest distance between 2 points an1DAlgo = StdMeshers_Regular_1D(1, 0, aMeshGen) # interpolation # 2D a2dHypothseis = StdMeshers_TrianglePreference( 2, 0, aMeshGen) #define the boundary a2dAlgo = StdMeshers_Quadrangle_2D(3, 0, aMeshGen) # the 2D mesh #Calculate mesh aMesh.ShapeToMesh(aShape) #Assign hyptothesis to mesh aMesh.AddHypothesis(aShape, 0) aMesh.AddHypothesis(aShape, 1) aMesh.AddHypothesis(aShape, 2) aMesh.AddHypothesis(aShape, 3) #Compute the data aMeshGen.Compute(aMesh, aMesh.GetShapeToMesh())
def smesh_MEFISTO2D(event=None): # Create the Mesh aMeshGen = SMESH_Gen() aMesh = aMeshGen.CreateMesh(0, True) # 1D an1DHypothesis = StdMeshers_Arithmetic1D( 0, 0, aMeshGen) # discretization of the wire an1DHypothesis.SetLength(0.1, False) # the smallest distance between 2 points an1DHypothesis.SetLength(0.5, True) # the longest distance between 2 points an1DAlgo = StdMeshers_Regular_1D(1, 0, aMeshGen) # interpolation # 2D a2dHypothseis = StdMeshers_TrianglePreference( 2, 0, aMeshGen) # define the boundary a2dAlgo = StdMeshers_MEFISTO_2D(3, 0, aMeshGen) # alculate mesh aMesh.ShapeToMesh(aShape) # Assign hyptothesis to mesh aMesh.AddHypothesis(aShape, 0) aMesh.AddHypothesis(aShape, 1) aMesh.AddHypothesis(aShape, 2) aMesh.AddHypothesis(aShape, 3) # Compute the data aMeshGen.Compute(aMesh, aMesh.GetShapeToMesh()) # Display the data display_mesh(aMesh)
def generate_stl(self, min_length=None, max_length=None, outfile_stl=None): """ Generate and export the .STL surface mesh for the blade as a whole, including the upper face, lower face and tip. The method utilizes modules from OCC SMESH which is standalone mesh framework based on SALOME mesher project. Please refer to https://github.com/tpaviot and http://docs.salome-platform.org/7/gui/SMESH/index.html for further details. This method requires PythonOCC and SMESH to be installed. :param double min_length: smallest distance between two nodes. Default value is None :param double max_length: largest distance between two nodes. Default value is None :param string outfile_stl: if string is passed then the method exports the generated 2D surface mesh into .stl file holding the name <outfile_stl>.stl. Default value is None We note that since the current implementation performs triangulation based on a topological compound that combines the blade 3 generated shapes without "fusion", it may happen that the generated triangulation of the upper and lower blade faces do not share the same exact nodes on the joint edge/wire resulting from the faces intersection. The current implementation can be enough for visualization purpose. However if the generated mesh is intended for computational analysis then a manual mesh healing is recommended by the user (e.g. see "Repair > Sewing" in SALOME GUI) for a proper mesh closure. """ from OCC.Core.SMESH import SMESH_Gen from OCC.Core.StdMeshers import (StdMeshers_Arithmetic1D, StdMeshers_TrianglePreference, StdMeshers_Regular_1D, StdMeshers_MEFISTO_2D) from OCC.Core.BRep import BRep_Builder from OCC.Core.TopoDS import TopoDS_Shape, TopoDS_Compound if min_length <= 0 or max_length <= 0: raise ValueError('min_length and max_length must be positive.') if min_length >= max_length: raise ValueError('min_length can not be greater than max_length') # First we check that blade shapes are generated, otherwise we generate # them. After that we combine the generated_upper_face, # generated_lower_face, and generated_tip into a topological compound # that we use to compute the surface mesh if (self.generated_upper_face is None) or not isinstance( self.generated_upper_face, TopoDS_Shape): # Upper face is generated with a maximal U degree = 1 self._generate_upper_face(maxDeg=1) if (self.generated_lower_face is None) or not isinstance( self.generated_lower_face, TopoDS_Shape): # Upper face is generated with a maximal U degree = 1 self._generate_lower_face(maxDeg=1) if (self.generated_tip is None) or not isinstance( self.generated_tip, TopoDS_Shape): # Upper face is generated with a maximal U degree = 1 self._generate_tip(maxDeg=1) # Now we regroup all the shapes into a TopoDS_Compound aCompound = TopoDS_Compound() aBuilder = BRep_Builder() aBuilder.MakeCompound(aCompound) # Add shapes aBuilder.Add(aCompound, self.generated_upper_face) aBuilder.Add(aCompound, self.generated_lower_face) aBuilder.Add(aCompound, self.generated_tip) # In the following we build the surface mesh according to the given # hypotheses aMeshGen = SMESH_Gen() aMesh = aMeshGen.CreateMesh(0, True) # Adding 1D hypothesis and algorithms # Wire discretization. Nodes are distributed based on Arithmetic1D # hypothesis which allows to split edges into segments with a length # that changes in arithmetic progression (Lk = Lk-1 + d) beginning # from a given min length and up to a given max length. More about # 1D hypotheses can be viewed through: # http://docs.salome-platform.org/7/gui/SMESH/a1d_meshing_hypo_page.html an1DHypothesis = StdMeshers_Arithmetic1D(0, 0, aMeshGen) # Smallest distance between 2 points an1DHypothesis.SetLength(min_length, False) # Longest distance between 2 points an1DHypothesis.SetLength(max_length, True) # Regular Interpolation an1DAlgo = StdMeshers_Regular_1D(1, 0, aMeshGen) # Adding 2D hypothesis and algorithms # 2D surface mesh -- Triangulations a2dHypothseis = StdMeshers_TrianglePreference(2, 0, aMeshGen) a2dAlgo = StdMeshers_MEFISTO_2D(3, 0, aMeshGen) #Calculate mesh for the topological compound containing the 3 shapes aMesh.ShapeToMesh(aCompound) #Assign hyptothesis to mesh aMesh.AddHypothesis(aCompound, 0) aMesh.AddHypothesis(aCompound, 1) aMesh.AddHypothesis(aCompound, 2) aMesh.AddHypothesis(aCompound, 3) if outfile_stl is not None: if not isinstance(outfile_stl, str): raise ValueError('outfile_stl must be a valid string.') #Compute the data aMeshGen.Compute(aMesh, aMesh.GetShapeToMesh()) # Export STL aMesh.ExportSTL(outfile_stl + '.stl', False)
MeshVS_DMF_NodalColorDataPrs) from OCC.Core.SMDSAbs import SMDSAbs_Face from OCC.Display.SimpleGui import init_display display, start_display, add_menu, add_function_to_menu = init_display() # First create a 'complex' shape (actually a boolean op between a box and a cylinder) print('Creating geometry ...', end='') box = BRepPrimAPI_MakeBox(200, 30, 30).Shape() sphere = BRepPrimAPI_MakeSphere(gp_Pnt(150, 20, 20), 80).Shape() aShape = BRepAlgoAPI_Cut(box, sphere).Shape() print('Done.') # Create the Mesh print('Creating mesh ...', end='') aMeshGen = SMESH_Gen() aMesh = aMeshGen.CreateMesh(0, True) print('Done.') print('Adding hypothesis and algorithms ...', end='') # 1D an1DHypothesis = StdMeshers_Arithmetic1D( 0, 0, aMeshGen) # discretization of the wire an1DHypothesis.SetLength(5., False) # the smallest distance between 2 points an1DHypothesis.SetLength(10., True) # the longest distance between 2 points an1DAlgo = StdMeshers_Regular_1D(1, 0, aMeshGen) # interpolation # 2D a2dHypothseis = StdMeshers_QuadranglePreference( 2, 0, aMeshGen) # define the boundary a2dAlgo = StdMeshers_Quadrangle_2D(3, 0, aMeshGen) # the 2D mesh
# along with pythonOCC. If not, see <http://www.gnu.org/licenses/>. from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox from OCC.Core.SMESH import SMESH_Gen from OCC.Core.StdMeshers import (StdMeshers_Arithmetic1D, StdMeshers_Regular_1D, StdMeshers_TrianglePreference, StdMeshers_MEFISTO_2D, StdMeshers_QuadranglePreference, StdMeshers_Quadrangle_2D) # Create the shape to mesh aShape = BRepPrimAPI_MakeBox(10, 20, 40).Shape() aMeshGen = SMESH_Gen() aMesh = aMeshGen.CreateMesh(0, True) def ComputeMesh(MEFISTO2=False): an1DHypothesis = StdMeshers_Arithmetic1D(0, 0, aMeshGen) # print dir(an1DHypothesis) # print an1DHypothesis.SaveTo() an1DHypothesis.SetLength(1., False) an1DHypothesis.SetLength(2., True) an1DAlgo = StdMeshers_Regular_1D(1, 0, aMeshGen) if MEFISTO2: # 2D a2dHypothseis = StdMeshers_TrianglePreference(
def __init__(self): from OCC.Core.SMDSAbs import SMDSAbs_ElementType from OCC.Core.SMESH import SMESH_Gen, SMESH_subMesh self._gen = SMESH_Gen()
class MeshGen(object): """ This class is the primary meshing database for a given instance. """ def __init__(self): from OCC.Core.SMDSAbs import SMDSAbs_ElementType from OCC.Core.SMESH import SMESH_Gen, SMESH_subMesh self._gen = SMESH_Gen() @property def object(self): """ :return: The underlying mesh object. :rtype: OCC.Core.SMESH.SMESH_Gen """ return self._gen @classmethod def wrap(cls, gen): """ Create a new instance using an existing SMESH_Gen instance. :param OCC.Core.SMESH.SMESH_Gen gen: A SMESH_Gen instance. :return: The new instance. :rtype: afem.smesh.entities.MeshGen """ new_gen = cls.__new__(cls) new_gen._gen = gen return new_gen def new_id(self): """ Generate a new unique ID within this generator. :return: A new unique ID. :rtype: int """ return self._gen.GetANewId() def create_mesh(self, shape=None, is_embedded=False): """ Create a mesh. :param afem.topology.entities.Shape shape: The shape to mesh. :param bool is_embedded: Option for embedding mesh. :return: The mesh. :rtype: afem.smesh.entities.Mesh """ the_mesh = Mesh(self, is_embedded) if shape is not None: the_mesh.shape_to_mesh(shape) return the_mesh def check_algo_state(self, mesh, shape): """ Check if computation would fail because of some bad algorithm state. :param afem.smesh.entities.Mesh mesh: A mesh. :param afem.topology.entities.Shape shape: A shape. :return: *True* if ok, *False* if not. :rtype: bool """ return self._gen.CheckAlgoState(mesh.object, shape.object) def compute(self, mesh, shape=None): """ Compute a mesh on a shape. :param afem.smesh.entities.Mesh mesh: A mesh. :param afem.topology.entities.Shape shape: The shape to compute mesh on. If not provided then the shape associated to the mesh is used. :return: *True* if computed, *False* if not. :rtype: bool :raise ValueError: If no shape is available to apply the hypothesis to. """ if shape is None: if mesh.has_shape: shape = mesh.shape else: raise ValueError('No shape could be found.') return self._gen.Compute(mesh.object, shape.object)