Version: 5.1.6

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

import geompy
import smesh

# 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(smesh.NETGEN)
algo3D.MaxElementVolume(900.)

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


Construction of a Submesh

from geompy import *
import smesh

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

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

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

# create a regular 1D algorithm for the faces
algo1D = quadra.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
quadra.Quadrangle()

# construct a submesh on the edge with a local hypothesis
algo_local = quadra.Segment(EdgeX)

# define "Arithmetic1D" hypothesis to cut the edge in several segments with increasing arithmetic length
algo_local.Arithmetic1D(1, 4)

# define "Propagation" hypothesis that propagates all other hypotheses
# on all edges of the opposite side in case of quadrangular faces
algo_local.Propagation()

# compute the mesh
quadra.Compute()


Change priority of submeshes in Mesh

import salome
import geompy
import smesh
import SMESH

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
Regular_1D = Mesh_1.Segment()
Nb_Segments_1 = Regular_1D.NumberOfSegments(20)
Nb_Segments_1.SetDistrType( 0 )
MEFISTO_2D = Mesh_1.Triangle()
Max_Element_Area_1 = MEFISTO_2D.MaxElementArea(1200)
Tetrahedron_Netgen = Mesh_1.Tetrahedron(algo=smesh.NETGEN)
Max_Element_Volume_1 = Tetrahedron_Netgen.MaxElementVolume(40000)

# create submesh and assign algorithms on Face_1
Netgen_1D_2D = Mesh_1.Triangle(algo=smesh.NETGEN,geom=Face_1)
SubMesh_1 = Netgen_1D_2D.GetSubMesh()
NETGEN_2D_Simple_Parameters_1 = Netgen_1D_2D.Parameters(which=smesh.SIMPLE)
NETGEN_2D_Simple_Parameters_1.SetNumberOfSegments( 4 )
NETGEN_2D_Simple_Parameters_1.LengthFromEdges()

# create submesh and assign algorithms on Face_2
Netgen_1D_2D_1 = Mesh_1.Triangle(algo=smesh.NETGEN,geom=Face_2)
SubMesh_2 = Netgen_1D_2D_1.GetSubMesh()
NETGEN_2D_Simple_Parameters_2 = Netgen_1D_2D_1.Parameters(which=smesh.SIMPLE)
NETGEN_2D_Simple_Parameters_2.SetNumberOfSegments( 8 )
NETGEN_2D_Simple_Parameters_2.LengthFromEdges()
smeshObj_1 = smesh.CreateHypothesis('NETGEN_SimpleParameters_2D',
'NETGENEngine')

# create submesh and assign algorithms on Face_3
Netgen_1D_2D_2 = Mesh_1.Triangle(algo=smesh.NETGEN,geom=Face_3)
SubMesh_3 = Netgen_1D_2D_2.GetSubMesh()
NETGEN_2D_Simple_Parameters_3 = Netgen_1D_2D_2.Parameters(which=smesh.SIMPLE)
NETGEN_2D_Simple_Parameters_3.SetNumberOfSegments( 12 )
NETGEN_2D_Simple_Parameters_3.LengthFromEdges()

# check exisiting submesh priority order
[ [ SubMesh_1, SubMesh_3, SubMesh_2 ] ] = Mesh_1.GetMeshOrder()
# set new submesh order
isDone = Mesh_1.SetMeshOrder( [ [ SubMesh_1, SubMesh_2, SubMesh_3 ] ])
# compute mesh
isDone = Mesh_1.Compute()

# clear mesh result and compute with other submesh order
Mesh_1.Clear()
isDone = Mesh_1.SetMeshOrder( [ [ SubMesh_2, SubMesh_1, SubMesh_3 ] ])
isDone = Mesh_1.Compute()


Editing of a mesh

import geompy
import smesh

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
mesh = tria.GetMesh()
mesh.RemoveHypothesis(edge, hyp4)

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

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

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


Export of a Mesh

import geompy
import smesh

# 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(smesh.NETGEN)
algo3D.MaxElementVolume(900.)

# compute the mesh
tetra.Compute()

# export the mesh in a MED file
tetra.ExportMED("/tmp/meshMED.med", 0)


How to mesh a cylinder with hexahedrons?

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

#  -*- coding: iso-8859-1 -*-
#  Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
#
#  This library is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Lesser General Public
#  License as published by the Free Software Foundation; either
#  version 2.1 of the License.
#
#  This library is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  Lesser General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public
#  License along with this library; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
#
#  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
#

# ==================================
#
import math

import geompy
import smesh
import salome
geo = geompy

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

radius =  50
height = 200

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

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

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

geo.addToStudy(cylinder, "cylinder")

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

size = radius/2.0

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

hole = geo.MakeCut(cylinder, box)

plane_trim = 2000

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

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

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

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

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

    geo.addToStudy(g, name)
    g.SetName(name)

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

    return g

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

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

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

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

smesh.SetCurrentStudy(salome.myStudy)

def discretize(x, y, z,  n, s=blocks):
    p = geo.MakeVertex(x, y, z)
    e = geo.GetEdgeNearPoint(s, p)
    a = hexa.Segment(e)
    a.NumberOfSegments(n)
    a.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)
mesh_cylinder_hexa.png


Building a compound of meshes

import geompy
import smesh

## 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 groups with the same names and
# merging of elements with the given tolerance
Compound1 = smesh.smesh.Concatenate([Mesh_inf.GetMesh(), Mesh_sup.GetMesh()], 0, 1, 1e-05)
smesh.SetName(Compound1, 'Compound_with_RenamedGrps_and_MergeElems')
# create a compound of two meshes with uniting groups with the same names and
# creating groups of all elements
Compound2 = smesh.smesh.Concatenate([Mesh_inf.GetMesh(), Mesh_sup.GetMesh()], 1, 0, 1e-05, True)
smesh.SetName(Compound2, 'Compound_with_UniteGrps_and_GrpsOfAllElems')
#end