Creating Meshes

First of all see Example of 3d mesh generation: which is an example of good python script style for Mesh module.

Construction of a mesh

creating_meshes_ex01.py

# Construction of a Mesh

import salome
salome.salome_init()
import GEOM
from salome.geom import geomBuilder
geompy = geomBuilder.New(salome.myStudy)

import SMESH, SALOMEDS
from salome.smesh import smeshBuilder
smesh =  smeshBuilder.New(salome.myStudy)

# create a box
box = geompy.MakeBox(0., 0., 0., 100., 200., 300.)
idbox = geompy.addToStudy(box, "box")

# create a mesh
tetra = smesh.Mesh(box, "MeshBox")

algo1D = tetra.Segment()
algo1D.NumberOfSegments(7)

algo2D = tetra.Triangle()
algo2D.MaxElementArea(800.)

algo3D = tetra.Tetrahedron()
algo3D.MaxElementVolume(900.)

# compute the mesh
ret = tetra.Compute()
if ret == 0:
    print "problem when computing the mesh"
else:
    print "mesh computed"
    pass

../../../examples/creating_meshes_ex01.py

Construction of a sub-mesh

creating_meshes_ex02.py

# Construction of a Sub-mesh

import salome
salome.salome_init()
import GEOM
from salome.geom import geomBuilder
geompy = geomBuilder.New(salome.myStudy)

import SMESH, SALOMEDS
from salome.smesh import smeshBuilder
smesh =  smeshBuilder.New(salome.myStudy)

# create a box
box = geompy.MakeBoxDXDYDZ(10., 10., 10.)
geompy.addToStudy(box, "Box")

# select one edge of the box for definition of a local hypothesis
p5 = geompy.MakeVertex(5., 0., 0.)
EdgeX = geompy.GetEdgeNearPoint(box, p5)
geompy.addToStudyInFather(box, EdgeX, "Edge [0,0,0 - 10,0,0]")

# create a hexahedral mesh on the box
mesh = smesh.Mesh(box, "Box : hexahedral 3D mesh")

# create a Regular_1D algorithm for discretization of edges
algo1D = mesh.Segment()

# define "NumberOfSegments" hypothesis to cut
# all the edges in a fixed number of segments
algo1D.NumberOfSegments(4)

# create a quadrangle 2D algorithm for the faces
mesh.Quadrangle()

# construct a sub-mesh on the edge with a local Regular_1D algorithm
algo_local = mesh.Segment(EdgeX)

# define "Arithmetic1D" hypothesis to cut EdgeX in several segments with length arithmetically
# increasing from 1.0 to 4.0
algo_local.Arithmetic1D(1, 4)

# define "Propagation" hypothesis that propagates algo_local and "Arithmetic1D" hypothesis
# from EdgeX to all parallel edges
algo_local.Propagation()

# assign a hexahedral algorithm
mesh.Hexahedron()


# any sub-shape can be meshed individually --
# compute mesh on two surfaces using different methods

# get surfaces
surfaces = geompy.SubShapeAll(box, geompy.ShapeType["FACE"])

# method 1: no sub-mesh is created
mesh.Compute( surfaces[0] )

# method 2: a sub-mesh is created
submesh = mesh.GetSubMesh( surfaces[2], "submesh 2" )
submesh.Compute()



# compute the whole mesh
mesh.Compute()

../../../examples/creating_meshes_ex02.py

Change priority of sub-meshes in mesh

creating_meshes_ex03.py

# Change priority of sub-meshes in Mesh

import salome
salome.salome_init()
import GEOM
from salome.geom import geomBuilder
geompy = geomBuilder.New(salome.myStudy)

import SMESH, SALOMEDS
from salome.smesh import smeshBuilder
smesh =  smeshBuilder.New(salome.myStudy)

Box_1 = geompy.MakeBoxDXDYDZ(200, 200, 200)
[Face_1,Face_2,Face_3,Face_4,Face_5,Face_6] = geompy.SubShapeAllSorted(Box_1, geompy.ShapeType["FACE"])

# create Mesh object on Box shape
Mesh_1 = smesh.Mesh(Box_1)

# assign mesh algorithms and hypotheses
Regular_1D = Mesh_1.Segment()
Nb_Segments_1 = Regular_1D.NumberOfSegments(20)
MEFISTO_2D = Mesh_1.Triangle()
Max_Element_Area_1 = MEFISTO_2D.MaxElementArea(1200)
Tetrahedron = Mesh_1.Tetrahedron()
Max_Element_Volume_1 = Tetrahedron.MaxElementVolume(40000)

# create sub-mesh and assign algorithms on Face_1
Regular_1D_1 = Mesh_1.Segment(geom=Face_1)
Nb_Segments_2 = Regular_1D_1.NumberOfSegments(4)
MEFISTO_2D_1 = Mesh_1.Triangle(algo=smeshBuilder.MEFISTO,geom=Face_1)
SubMesh_1 = MEFISTO_2D_1.GetSubMesh()

# create sub-mesh and assign algorithms on Face_2
Regular_1D_2 = Mesh_1.Segment(geom=Face_2)
Nb_Segments_3 = Regular_1D_2.NumberOfSegments(8)
MEFISTO_2D_2 = Mesh_1.Triangle(algo=smeshBuilder.MEFISTO,geom=Face_2)
SubMesh_2 = MEFISTO_2D_2.GetSubMesh()

# create sub-mesh and assign algorithms on Face_3
Regular_1D_3 = Mesh_1.Segment(geom=Face_3)
Nb_Segments_4 = Regular_1D_3.NumberOfSegments(12)
MEFISTO_2D_3 = Mesh_1.Triangle(algo=smeshBuilder.MEFISTO,geom=Face_3)
SubMesh_3 = MEFISTO_2D_3.GetSubMesh()

# check existing sub-mesh priority order
[ [ SubMesh_1, SubMesh_3, SubMesh_2 ] ] = Mesh_1.GetMeshOrder()
isDone = Mesh_1.Compute()
print "Nb elements at initial order of sub-meshes:", Mesh_1.NbElements()

# set new sub-mesh order
isDone = Mesh_1.SetMeshOrder( [ [ SubMesh_1, SubMesh_2, SubMesh_3 ] ])
# compute mesh
isDone = Mesh_1.Compute()
print "Nb elements at new order of sub-meshes:", Mesh_1.NbElements()

# compute with other sub-mesh order
isDone = Mesh_1.SetMeshOrder( [ [ SubMesh_2, SubMesh_1, SubMesh_3 ] ])
isDone = Mesh_1.Compute()
print "Nb elements at another order of sub-meshes:", Mesh_1.NbElements()

../../../examples/creating_meshes_ex03.py

Intermediate edition while meshing

a3DmeshOnModified2Dmesh.py

import salome
salome.salome_init()

from salome.geom import geomBuilder
geompy = geomBuilder.New(salome.myStudy)

# This script demonstrates generation of 3D mesh basing on a modified 2D mesh
#
# Purpose is to get a tetrahedral mesh in a sphere cut by a cube.
# The requirement is to have a surface mesh on the cube comprised of
# triangles of exactly the same size arranged in a grid pattern.
#
# To fulfill this requirement we mesh the box using Quadrangle: Mapping
# meshing algorithm, split quadrangles into triangles and then generate
# tetrahedrons.


# Make the geometry

Box_1    = geompy.MakeBox(-100,-100,-100, 100, 100, 100)
Sphere_1 = geompy.MakeSphereR( 300 )
Cut_1    = geompy.MakeCut(Sphere_1, Box_1, theName="Cut_1")
# get a spherical face
Sph_Face = geompy.ExtractShapes( Sphere_1, geompy.ShapeType["FACE"] )[0]

# get the shape Sph_Face turned into during MakeCut()
Sph_Face = geompy.GetInPlace(Cut_1, Sph_Face, isNewImplementation=True, theName="Sphere_1")


# 1) Define a mesh with 1D and 2D meshers

import  SMESH
from salome.smesh import smeshBuilder
smesh = smeshBuilder.New(salome.myStudy)

Mesh_1 = smesh.Mesh(Cut_1)

# "global" meshers (assigned to Cut_1) that will be used for the box
Regular_1D = Mesh_1.Segment()
Local_Length_1 = Regular_1D.LocalLength(20)
Quadrangle_2D = Mesh_1.Quadrangle()

# a "local" mesher (assigned to a sub-mesh on Sphere_1) to mesh the sphere
algo_2D = Mesh_1.Triangle( smeshBuilder.NETGEN_1D2D, Sph_Face )
algo_2D.SetMaxSize( 70. )
algo_2D.SetFineness( smeshBuilder.Moderate )
algo_2D.SetMinSize( 7. )

# 2) Compute 2D mesh
isDone = Mesh_1.Compute()

# 3) Split quadrangles into triangles
isDone = Mesh_1.SplitQuadObject( Mesh_1, Diag13=True )

# 4) Define a 3D mesher
Mesh_1.Tetrahedron()

# 5) Compute 3D mesh
Mesh_1.Compute()

if salome.sg.hasDesktop():
  salome.sg.updateObjBrowser(True)

../../../examples/a3DmeshOnModified2Dmesh.py

Editing a mesh (i.e. changing hypotheses)

creating_meshes_ex04.py

# Editing of a mesh

import salome
salome.salome_init()
import GEOM
from salome.geom import geomBuilder
geompy = geomBuilder.New(salome.myStudy)

import SMESH, SALOMEDS
from salome.smesh import smeshBuilder
smesh =  smeshBuilder.New(salome.myStudy)

def PrintMeshInfo(theMesh):
    aMesh = theMesh.GetMesh()
    print "Information about mesh:"
    print "Number of nodes       : ", aMesh.NbNodes()
    print "Number of edges       : ", aMesh.NbEdges()
    print "Number of faces       : ", aMesh.NbFaces()
    print "Number of volumes     : ", aMesh.NbVolumes()
    pass

# create a box
box = geompy.MakeBox(0., 0., 0., 20., 20., 20.)
geompy.addToStudy(box, "box")

# select one edge of the box for definition of a local hypothesis
subShapeList = geompy.SubShapeAll(box, geompy.ShapeType["EDGE"])
edge = subShapeList[0]
name = geompy.SubShapeName(edge, box)
geompy.addToStudyInFather(box, edge, name)

# create a mesh
tria = smesh.Mesh(box, "Mesh 2D")
algo1D = tria.Segment()
hyp1 = algo1D.NumberOfSegments(3)
algo2D = tria.Triangle()
hyp2 = algo2D.MaxElementArea(10.)

# create a sub-mesh
algo_local = tria.Segment(edge)
hyp3 = algo_local.Arithmetic1D(1, 6)
hyp4 = algo_local.Propagation()

# compute the mesh
tria.Compute()
PrintMeshInfo(tria)

# remove a local hypothesis
tria.RemoveHypothesis(hyp4, edge)

# compute the mesh
tria.Compute()
PrintMeshInfo(tria)

# change the value of the 2D hypothesis
hyp2.SetMaxElementArea(2.)

# compute the mesh
tria.Compute()
PrintMeshInfo(tria)

../../../examples/creating_meshes_ex04.py

Export of a Mesh

creating_meshes_ex05.py

# Export of a Mesh

import salome
salome.salome_init()
import GEOM
from salome.geom import geomBuilder
geompy = geomBuilder.New(salome.myStudy)

import SMESH, SALOMEDS
from salome.smesh import smeshBuilder
smesh =  smeshBuilder.New(salome.myStudy)

# create a box
box = geompy.MakeBox(0., 0., 0., 100., 200., 300.)
idbox = geompy.addToStudy(box, "box")

# create a mesh
tetra = smesh.Mesh(box, "MeshBox")
tetra.Segment().NumberOfSegments(7)
tetra.Triangle()
tetra.Tetrahedron()

# compute the mesh
tetra.Compute()

# export the mesh in a MED file
import tempfile
medFile = tempfile.NamedTemporaryFile(suffix=".med").name
tetra.ExportMED( medFile, 0 )

# export a group in a MED file
face = geompy.SubShapeAll( box, geompy.ShapeType["FACE"])[0] # a box side
group = tetra.GroupOnGeom( face, "face group" ) # group of 2D elements on the <face>
tetra.ExportMED( medFile, meshPart=group )

# ========================
# autoDimension parameter
# ========================

face = geompy.MakeFaceHW( 10, 10, 1, "rectangle" )
mesh2D = smesh.Mesh( face, "mesh2D" )
mesh2D.AutomaticHexahedralization(0)

import MEDLoader, os

# exported mesh is in 2D space because it is a planar mesh lying
# on XOY plane, and autoDimension=True by default
mesh2D.ExportMED( medFile )
medMesh = MEDLoader.MEDLoader.ReadUMeshFromFile(medFile,mesh2D.GetName(),0)
print "autoDimension==True, exported mesh is in %sD"%medMesh.getSpaceDimension()

# exported mesh is in 3D space, same as in Mesh module,
# thanks to autoDimension=False
mesh2D.ExportMED( medFile, autoDimension=False )
medMesh = MEDLoader.MEDLoader.ReadUMeshFromFile(medFile,mesh2D.GetName(),0)
print "autoDimension==False, exported mesh is in %sD"%medMesh.getSpaceDimension()

os.remove( medFile )

../../../examples/creating_meshes_ex05.py

How to mesh a cylinder with hexahedrons?

Here you can see an example of python script, creating a hexahedral mesh on a cylinder. A picture below the source code of the script demonstrates the resulting mesh.

creating_meshes_ex06.py

# Creating a hexahedral mesh on a cylinder.
#
# This example uses Partition to divide the cylinder into blocks, which is
# a general approach. But for the case of cylinder there is a dedicated
# command creating a blocked cylinder: geompy.MakeDividedCylinder()

import salome
salome.salome_init()
import GEOM
from salome.geom import geomBuilder
geompy = geomBuilder.New(salome.myStudy)

import SMESH, SALOMEDS
from salome.smesh import smeshBuilder
smesh =  smeshBuilder.New(salome.myStudy)

import math

# Parameters
# ----------

radius =  50
height = 200

# Build a cylinder
# ----------------

base      = geompy.MakeVertex(0, 0, 0)
direction = geompy.MakeVectorDXDYDZ(0, 0, 1)

cylinder = geompy.MakeCylinder(base, direction, radius, height)

geompy.addToStudy(cylinder, "cylinder")

# Build blocks
# ------------

size = radius/2.0

box_rot  = geompy.MakeBox(-size, -size, 0,  +size, +size, height)
box_axis = geompy.MakeLine(base, direction)
box      = geompy.MakeRotation(box_rot, box_axis, math.pi/4)

hole = geompy.MakeCut(cylinder, box)

plane_trim = 2000

plane_a = geompy.MakePlane(base, geompy.MakeVectorDXDYDZ(1, 0, 0), plane_trim)
plane_b = geompy.MakePlane(base, geompy.MakeVectorDXDYDZ(0, 1, 0), plane_trim)

blocks_part = geompy.MakePartition([hole], [plane_a, plane_b], [], [], geompy.ShapeType["SOLID"])
blocks_list = [box] + geompy.SubShapeAll(blocks_part, geompy.ShapeType["SOLID"])
blocks_all  = geompy.MakeCompound(blocks_list)
blocks      = geompy.MakeGlueFaces(blocks_all, 0.0001)

geompy.addToStudy(blocks, "cylinder:blocks")

# Build geometric groups
# ----------------------

def group(name, shape, type, base=None, direction=None):
    t = geompy.ShapeType[type]
    g = geompy.CreateGroup(shape, t)

    geompy.addToStudyInFather(shape, g, name)

    if base!=None:
        l = geompy.GetShapesOnPlaneWithLocationIDs(shape, t, direction, base, GEOM.ST_ON)
        geompy.UnionIDs(g, l)

    return g

group_a = group("baseA", blocks, "FACE", base, direction)

base_b  = geompy.MakeVertex(0, 0, height)
group_b = group("baseB", blocks, "FACE", base_b, direction)

group_1     = group("limit", blocks, "SOLID")
group_1_all = geompy.SubShapeAllIDs(blocks, geompy.ShapeType["SOLID"])
geompy.UnionIDs(group_1, group_1_all)
group_1_box = geompy.GetBlockNearPoint(blocks, base)
geompy.DifferenceList(group_1, [group_1_box])

# Mesh the blocks with hexahedral
# -------------------------------

smesh.SetCurrentStudy(salome.myStudy)

def discretize(x, y, z,  nbSeg, shape=blocks):
    vert = geompy.MakeVertex( x, y, z )
    edge = geompy.GetEdgeNearPoint( shape, vert )
    algo = hexa.Segment( edge )
    algo.NumberOfSegments( nbSeg )
    algo.Propagation()

hexa = smesh.Mesh(blocks)

hexa_1d = hexa.Segment()
hexa_1d.NumberOfSegments(1)

discretize(+radius        , +radius,        0,   5)
discretize(-radius        , +radius,        0,   8)
discretize((radius+size)/2,       0,        0,  10)
discretize(        +radius,       0, height/2,  20)

hexa.Quadrangle()
hexa.Hexahedron()

hexa.Compute()

hexa.Group(group_a)
hexa.Group(group_b)
hexa.Group(group_1)

../../../examples/creating_meshes_ex06.py

_images/mesh_cylinder_hexa.png

Building a compound of meshes

creating_meshes_ex07.py

# Building a compound of meshes

import salome
salome.salome_init()
import GEOM
from salome.geom import geomBuilder
geompy = geomBuilder.New(salome.myStudy)

import SMESH, SALOMEDS
from salome.smesh import smeshBuilder
smesh =  smeshBuilder.New(salome.myStudy)

## create a bottom box
Box_inf = geompy.MakeBox(0., 0., 0., 200., 200., 50.)

# get a top face
Psup1=geompy.MakeVertex(100., 100., 50.)
Fsup1=geompy.GetFaceNearPoint(Box_inf, Psup1)
# get a bottom face
Pinf1=geompy.MakeVertex(100., 100., 0.)
Finf1=geompy.GetFaceNearPoint(Box_inf, Pinf1)

## create a top box
Box_sup = geompy.MakeBox(100., 100., 50., 200., 200., 100.)

# get a top face
Psup2=geompy.MakeVertex(150., 150., 100.)
Fsup2=geompy.GetFaceNearPoint(Box_sup, Psup2)
# get a bottom face
Pinf2=geompy.MakeVertex(150., 150., 50.)
Finf2=geompy.GetFaceNearPoint(Box_sup, Pinf2)

## Publish in the study
geompy.addToStudy(Box_inf, "Box_inf")
geompy.addToStudyInFather(Box_inf, Fsup1, "Fsup")
geompy.addToStudyInFather(Box_inf, Finf1, "Finf")

geompy.addToStudy(Box_sup, "Box_sup")
geompy.addToStudyInFather(Box_sup, Fsup2, "Fsup")
geompy.addToStudyInFather(Box_sup, Finf2, "Finf")

smesh.SetCurrentStudy(salome.myStudy)

## create a bottom mesh
Mesh_inf = smesh.Mesh(Box_inf, "Mesh_inf")
algo1D_1=Mesh_inf.Segment()
algo1D_1.NumberOfSegments(10)
algo2D_1=Mesh_inf.Quadrangle()
algo3D_1=Mesh_inf.Hexahedron()
Mesh_inf.Compute()

# create a group on the top face
Gsup1=Mesh_inf.Group(Fsup1, "Sup")
# create a group on the bottom face
Ginf1=Mesh_inf.Group(Finf1, "Inf")

## create a top mesh
Mesh_sup = smesh.Mesh(Box_sup, "Mesh_sup")
algo1D_2=Mesh_sup.Segment()
algo1D_2.NumberOfSegments(5)
algo2D_2=Mesh_sup.Quadrangle()
algo3D_2=Mesh_sup.Hexahedron()
Mesh_sup.Compute()

# create a group on the top face
Gsup2=Mesh_sup.Group(Fsup2, "Sup")
# create a group on the bottom face
Ginf2=Mesh_sup.Group(Finf2, "Inf")

## create compounds
# create a compound of two meshes with renaming namesake groups and
# merging elements with the given tolerance
Compound1 = smesh.Concatenate([Mesh_inf, Mesh_sup], 0, 1, 1e-05,
                              name='Compound with RenamedGrps and MergeElems')
# create a compound of two meshes with uniting namesake groups and
# creating groups of all elements
Compound2 = smesh.Concatenate([Mesh_inf, Mesh_sup], 1, 0, 1e-05, True,
                              name='Compound with UniteGrps and GrpsOfAllElems')

if salome.sg.hasDesktop():
    salome.sg.updateObjBrowser(True)

../../../examples/creating_meshes_ex07.py

Mesh Copying

creating_meshes_ex08.py

# Mesh Copying

import salome
salome.salome_init()
import GEOM
from salome.geom import geomBuilder
geompy = geomBuilder.New(salome.myStudy)

import SMESH, SALOMEDS
from salome.smesh import smeshBuilder
smesh =  smeshBuilder.New(salome.myStudy)

# make geometry of a box
box = geompy.MakeBoxDXDYDZ(100,100,100)
face = geompy.SubShapeAllSorted(box, geompy.ShapeType["FACE"])[0]

# generate a prismatic 3D mesh
mesh = smesh.Mesh(box)
localAlgo = mesh.Triangle(face)
mesh.AutomaticHexahedralization()

# objects to copy
fGroup = mesh.GroupOnGeom( face, "2D on face")
nGroup = mesh.GroupOnGeom( face, "nodes on face", SMESH.NODE)
subMesh = localAlgo.GetSubMesh()

# make a new mesh by copying different parts of the mesh

# 1. copy the whole mesh
newMesh = smesh.CopyMesh( mesh, "whole mesh copy")

# 2. copy a group of 2D elements along with groups
newMesh = smesh.CopyMesh( fGroup,  "face group copy with groups",toCopyGroups=True)

# 3. copy a group of nodes
newMesh = smesh.CopyMesh( nGroup, "node group copy")

# 4. copy some faces
faceIds = fGroup.GetIDs()[-10:]
newMesh = smesh.CopyMesh( mesh.GetIDSource( faceIds, SMESH.FACE ), "some faces copy")

# 5. copy some nodes
nodeIds = nGroup.GetIDs()[-10:]
newMesh = smesh.CopyMesh( mesh.GetIDSource( nodeIds, SMESH.NODE), "some nodes copy")

# 6. copy a sub-mesh
newMesh = smesh.CopyMesh( subMesh, "sub-mesh copy" )

../../../examples/creating_meshes_ex08.py

Table Of Contents

This Page