# Modifying Meshes

### Adding Nodes and Elements

#### Add Node

import SMESH_mechanic

mesh = SMESH_mechanic.mesh

# add node

new_id = mesh.AddNode(50, 10, 0)

print ""

if new_id == 0: print "KO node addition."

else: print "New Node has been added with ID ", new_id

#### Add Edge

import SMESH_mechanic

mesh = SMESH_mechanic.mesh

print ""

# add node

n1 = mesh.AddNode(50, 10, 0)

if n1 == 0: print "KO node addition."

# add edge

e1 = mesh.AddEdge([n1, 38])

if e1 == 0: print "KO edge addition."

else: print "New Edge has been added with ID ", e1

#### Add Triangle

import SMESH_mechanic

mesh = SMESH_mechanic.mesh

print ""

# add node

n1 = mesh.AddNode(50, 10, 0)

if n1 == 0: print "KO node addition."

# add triangle

t1 = mesh.AddFace([n1, 38, 39])

if t1 == 0: print "KO triangle addition."

else: print "New Triangle has been added with ID ", t1

#### Add Quadrangle

import SMESH_mechanic

mesh = SMESH_mechanic.mesh

print ""

# add node

n1 = mesh.AddNode(50, 10, 0)

if n1 == 0: print "KO node addition."

n2 = mesh.AddNode(40, 20, 0)

if n2 == 0: print "KO node addition."

# add quadrangle

q1 = mesh.AddFace([n2, n1, 38, 39])

if q1 == 0: print "KO quadrangle addition."

else: print "New Quadrangle has been added with ID ", q1

#### Add Tetrahedron

import SMESH_mechanic

mesh = SMESH_mechanic.mesh

print ""

# add node

n1 = mesh.AddNode(50, 10, 0)

if n1 == 0: print "KO node addition."

# add tetrahedron

t1 = mesh.AddVolume([n1, 38, 39, 246])

if t1 == 0: print "KO tetrahedron addition."

else: print "New Tetrahedron has been added with ID ", t1

#### Add Hexahedron

import SMESH_mechanic

mesh = SMESH_mechanic.mesh

print ""

# add nodes

nId1 = mesh.AddNode(50, 10, 0)

nId2 = mesh.AddNode(47, 12, 0)

nId3 = mesh.AddNode(50, 10, 10)

nId4 = mesh.AddNode(47, 12, 10)

if nId1 == 0 or nId2 == 0 or nId3 == 0 or nId4 == 0: print "KO node addition."

# add hexahedron

vId = mesh.AddVolume([nId2, nId1, 38, 39, nId4, nId3, 245, 246])

if vId == 0: print "KO Hexahedron addition."

else: print "New Hexahedron has been added with ID ", vId

#### Add Polygon

import math

import salome

import smesh

# create an empty mesh structure

mesh = smesh.Mesh()

# a method to build a polygonal mesh element with <nb_vert> angles:

def MakePolygon (a_mesh, x0, y0, z0, radius, nb_vert):

al = 2.0 * math.pi / nb_vert

node_ids = []

# Create nodes for a polygon

for ii in range(nb_vert):

nid = mesh.AddNode(x0 + radius * math.cos(ii*al),

y0 + radius * math.sin(ii*al),

z0)

node_ids.append(nid)

pass

# Create a polygon

return mesh.AddPolygonalFace(node_ids)

# Create three polygons

f1 = MakePolygon(mesh, 0, 0, 0, 30, 13)

f2 = MakePolygon(mesh, 0, 0, 10, 21, 9)

f3 = MakePolygon(mesh, 0, 0, 20, 13, 6)

salome.sg.updateObjBrowser(1)

#### Add Polyhedron

import salome

import math

# create an empty mesh structure

mesh = smesh.Mesh()

# Create nodes for 12-hedron with pentagonal faces

al = 2 * math.pi / 5.0

cosal = math.cos(al)

aa = 13

rr = aa / (2.0 * math.sin(al/2.0))

dr = 2.0 * rr * cosal

r1 = rr + dr

dh = rr * math.sqrt(2.0 * (1.0 - cosal * (1.0 + 2.0 * cosal)))

hh = 2.0 * dh - dr * (rr*(cosal - 1) + (rr + dr)*(math.cos(al/2) - 1)) / dh

dd = [] # top

cc = [] # below top

bb = [] # above bottom

aa = [] # bottom

for i in range(5):

cos_bot = math.cos(i*al)

sin_bot = math.sin(i*al)

cos_top = math.cos(i*al + al/2.0)

sin_top = math.sin(i*al + al/2.0)

nd = mesh.AddNode(rr * cos_top, rr * sin_top, hh ) # top

nc = mesh.AddNode(r1 * cos_top, r1 * sin_top, hh - dh) # below top

nb = mesh.AddNode(r1 * cos_bot, r1 * sin_bot, dh) # above bottom

na = mesh.AddNode(rr * cos_bot, rr * sin_bot, 0) # bottom

dd.append(nd) # top

cc.append(nc) # below top

bb.append(nb) # above bottom

aa.append(na) # bottom

pass

# Create a polyhedral volume (12-hedron with pentagonal faces)

MeshEditor.AddPolyhedralVolume([dd[0], dd[1], dd[2], dd[3], dd[4], # top

dd[0], cc[0], bb[1], cc[1], dd[1], # -

dd[1], cc[1], bb[2], cc[2], dd[2], # -

dd[2], cc[2], bb[3], cc[3], dd[3], # - below top

dd[3], cc[3], bb[4], cc[4], dd[4], # -

dd[4], cc[4], bb[0], cc[0], dd[0], # -

aa[4], bb[4], cc[4], bb[0], aa[0], # .

aa[3], bb[3], cc[3], bb[4], aa[4], # .

aa[2], bb[2], cc[2], bb[3], aa[3], # . above bottom

aa[1], bb[1], cc[1], bb[2], aa[2], # .

aa[0], bb[0], cc[0], bb[1], aa[1], # .

aa[0], aa[1], aa[2], aa[3], aa[4]], # bottom

[5,5,5,5,5,5,5,5,5,5,5,5])

salome.sg.updateObjBrowser(1)

### Removing Nodes and Elements

#### Removing Nodes

import SMESH_mechanic

mesh = SMESH_mechanic.mesh

# remove nodes #246 and #255

res = mesh.RemoveNodes([246, 255])

if res == 1: print "Nodes removing is OK!"

else: print "KO nodes removing."

#### Removing Elements

import SMESH_mechanic

mesh = SMESH_mechanic.mesh

# remove three elements: #850, #859 and #814

res = mesh.RemoveElements([850, 859, 814])

if res == 1: print "Elements removing is OK!"

else: print "KO Elements removing."

### Renumbering Nodes and Elements

import SMESH_mechanic

mesh = SMESH_mechanic.mesh

mesh.RenumberNodes()

### Moving Nodes

import SMESH_mechanic

mesh = SMESH_mechanic.mesh

# move node #38

mesh.MoveNode(38, 20., 10., 0.)

### Diagonal Inversion

import salome

import smesh

# create an empty mesh structure

mesh = smesh.Mesh()

# create the following mesh:

# .----.----.----.

# | /| /| /|

# | / | / | / |

# |/ |/ |/ |

# .----.----.----.

bb = [0, 0, 0, 0]

tt = [0, 0, 0, 0]

ff = [0, 0, 0, 0, 0, 0]

bb[0] = mesh.AddNode( 0., 0., 0.)

bb[1] = mesh.AddNode(10., 0., 0.)

bb[2] = mesh.AddNode(20., 0., 0.)

bb[3] = mesh.AddNode(30., 0., 0.)

tt[0] = mesh.AddNode( 0., 15., 0.)

tt[1] = mesh.AddNode(10., 15., 0.)

tt[2] = mesh.AddNode(20., 15., 0.)

tt[3] = mesh.AddNode(30., 15., 0.)

ff[0] = mesh.AddFace([bb[0], bb[1], tt[1]])

ff[1] = mesh.AddFace([bb[0], tt[1], tt[0]])

ff[2] = mesh.AddFace([bb[1], bb[2], tt[2]])

ff[3] = mesh.AddFace([bb[1], tt[2], tt[1]])

ff[4] = mesh.AddFace([bb[2], bb[3], tt[3]])

ff[5] = mesh.AddFace([bb[2], tt[3], tt[2]])

# inverse the diagonal bb[1] - tt[2]

print "\nDiagonal inversion ... ",

res = mesh.InverseDiag(bb[1], tt[2])

if not res: print "failed!"

else: print "done."

salome.sg.updateObjBrowser(1)

### Uniting two Triangles

import salome

import smesh

# create an empty mesh structure

mesh = smesh.Mesh()

# create the following mesh:

# .----.----.----.

# | /| /| /|

# | / | / | / |

# |/ |/ |/ |

# .----.----.----.

bb = [0, 0, 0, 0]

tt = [0, 0, 0, 0]

ff = [0, 0, 0, 0, 0, 0]

bb[0] = mesh.AddNode( 0., 0., 0.)

bb[1] = mesh.AddNode(10., 0., 0.)

bb[2] = mesh.AddNode(20., 0., 0.)

bb[3] = mesh.AddNode(30., 0., 0.)

tt[0] = mesh.AddNode( 0., 15., 0.)

tt[1] = mesh.AddNode(10., 15., 0.)

tt[2] = mesh.AddNode(20., 15., 0.)

tt[3] = mesh.AddNode(30., 15., 0.)

ff[0] = mesh.AddFace([bb[0], bb[1], tt[1]])

ff[1] = mesh.AddFace([bb[0], tt[1], tt[0]])

ff[2] = mesh.AddFace([bb[1], bb[2], tt[2]])

ff[3] = mesh.AddFace([bb[1], tt[2], tt[1]])

ff[4] = mesh.AddFace([bb[2], bb[3], tt[3]])

ff[5] = mesh.AddFace([bb[2], tt[3], tt[2]])

# delete the diagonal bb[1] - tt[2]

print "\nUnite two triangles ... ",

res = mesh.DeleteDiag(bb[1], tt[2])

if not res: print "failed!"

else: print "done."

salome.sg.updateObjBrowser(1)

### Uniting a Set of Triangles

import salome

import smesh

# create an empty mesh structure

mesh = smesh.Mesh()

# create the following mesh:

# .----.----.----.

# | /| /| /|

# | / | / | / |

# |/ |/ |/ |

# .----.----.----.

bb = [0, 0, 0, 0]

tt = [0, 0, 0, 0]

ff = [0, 0, 0, 0, 0, 0]

bb[0] = mesh.AddNode( 0., 0., 0.)

bb[1] = mesh.AddNode(10., 0., 0.)

bb[2] = mesh.AddNode(20., 0., 0.)

bb[3] = mesh.AddNode(30., 0., 0.)

tt[0] = mesh.AddNode( 0., 15., 0.)

tt[1] = mesh.AddNode(10., 15., 0.)

tt[2] = mesh.AddNode(20., 15., 0.)

tt[3] = mesh.AddNode(30., 15., 0.)

ff[0] = mesh.AddFace([bb[0], bb[1], tt[1]])

ff[1] = mesh.AddFace([bb[0], tt[1], tt[0]])

ff[2] = mesh.AddFace([bb[1], bb[2], tt[2]])

ff[3] = mesh.AddFace([bb[1], tt[2], tt[1]])

ff[4] = mesh.AddFace([bb[2], bb[3], tt[3]])

ff[5] = mesh.AddFace([bb[2], tt[3], tt[2]])

# unite a set of triangles

print "\nUnite a set of triangles ... ",

res = mesh.TriToQuad([ff[2], ff[3], ff[4], ff[5]], smesh.FT_MinimumAngle, 60.)

if not res: print "failed!"

else: print "done."

salome.sg.updateObjBrowser(1)

### Orientation

import salome

import smesh

# create an empty mesh structure

mesh = smesh.Mesh()

# build five quadrangles:

dx = 10

dy = 20

n1 = mesh.AddNode(0.0 * dx, 0, 0)

n2 = mesh.AddNode(1.0 * dx, 0, 0)

n3 = mesh.AddNode(2.0 * dx, 0, 0)

n4 = mesh.AddNode(3.0 * dx, 0, 0)

n5 = mesh.AddNode(4.0 * dx, 0, 0)

n6 = mesh.AddNode(5.0 * dx, 0, 0)

n7 = mesh.AddNode(0.0 * dx, dy, 0)

n8 = mesh.AddNode(1.0 * dx, dy, 0)

n9 = mesh.AddNode(2.0 * dx, dy, 0)

n10 = mesh.AddNode(3.0 * dx, dy, 0)

n11 = mesh.AddNode(4.0 * dx, dy, 0)

n12 = mesh.AddNode(5.0 * dx, dy, 0)

f1 = mesh.AddFace([n1, n2, n8 , n7 ])

f2 = mesh.AddFace([n2, n3, n9 , n8 ])

f3 = mesh.AddFace([n3, n4, n10, n9 ])

f4 = mesh.AddFace([n4, n5, n11, n10])

f5 = mesh.AddFace([n5, n6, n12, n11])

# Change the orientation of the second and the fourth faces.

mesh.Reorient([2, 4])

salome.sg.updateObjBrowser(1)

### Cutting Quadrangles

import SMESH_mechanic

smesh = SMESH_mechanic.smesh

mesh = SMESH_mechanic.mesh

# cut two quadrangles: 405 and 406

mesh.QuadToTri([405, 406], smesh.FT_MinimumAngle)

### Smoothing

import salome

import geompy

import SMESH_mechanic

smesh = SMESH_mechanic.smesh

mesh = SMESH_mechanic.mesh

# select the top face

faces = geompy.SubShapeAllSorted(SMESH_mechanic.shape_mesh, geompy.ShapeType["FACE"])

face = faces[3]

geompy.addToStudyInFather(SMESH_mechanic.shape_mesh, face, "face planar with hole")

# create a group of faces to be smoothed

GroupSmooth = mesh.GroupOnGeom(face, "Group of faces (smooth)", smesh.FACE)

# perform smoothing

# boolean SmoothObject(Object, IDsOfFixedNodes, MaxNbOfIterations, MaxAspectRatio, Method)

res = mesh.SmoothObject(GroupSmooth, [], 20, 2., smesh.CENTROIDAL_SMOOTH)

print "\nSmoothing ... ",

if not res: print "failed!"

else: print "done."

salome.sg.updateObjBrowser(1)

### Extrusion

import salome

import geompy

import SMESH_mechanic

smesh = SMESH_mechanic.smesh

mesh = SMESH_mechanic.mesh

# select the top face

faces = geompy.SubShapeAllSorted(SMESH_mechanic.shape_mesh, geompy.ShapeType["FACE"])

face = faces[7]

geompy.addToStudyInFather(SMESH_mechanic.shape_mesh, face, "face circular top")

# create a vector for extrusion

point = smesh.PointStruct(0., 0., 5.)

vector = smesh.DirStruct(point)

# create a group to be extruded

GroupTri = mesh.GroupOnGeom(face, "Group of faces (extrusion)", smesh.FACE)

# perform extrusion of the group

mesh.ExtrusionSweepObject(GroupTri, vector, 5)

salome.sg.updateObjBrowser(1)

### Extrusion along a Path

import math

import salome

# Geometry

import geompy

# 1. Create points

points = [[0, 0], [50, 30], [50, 110], [0, 150], [-80, 150], [-130, 70], [-130, -20]]

iv = 1

vertices = []

for point in points:

vert = geompy.MakeVertex(point[0], point[1], 0)

geompy.addToStudy(vert, "Vertex_" + `iv`)

vertices.append(vert)

iv += 1

pass

# 2. Create edges and wires

Edge_straight = geompy.MakeEdge(vertices[0], vertices[4])

Edge_bezierrr = geompy.MakeBezier(vertices)

Wire_polyline = geompy.MakePolyline(vertices)

Edge_Circle = geompy.MakeCircleThreePnt(vertices[0], vertices[1], vertices[2])

geompy.addToStudy(Edge_straight, "Edge_straight")

geompy.addToStudy(Edge_bezierrr, "Edge_bezierrr")

geompy.addToStudy(Wire_polyline, "Wire_polyline")

geompy.addToStudy(Edge_Circle , "Edge_Circle")

# 3. Explode wire on edges, as they will be used for mesh extrusion

Wire_polyline_edges = geompy.SubShapeAll(Wire_polyline, geompy.ShapeType["EDGE"])

for ii in range(len(Wire_polyline_edges)):

geompy.addToStudyInFather(Wire_polyline, Wire_polyline_edges[ii], "Edge_" + `ii + 1`)

pass

# Mesh

import smesh

# Mesh the given shape with the given 1d hypothesis

def Mesh1D(shape1d, nbSeg, name):

mesh1d_tool = smesh.Mesh(shape1d, name)

algo = mesh1d_tool.Segment()

hyp = algo.NumberOfSegments(nbSeg)

isDone = mesh1d_tool.Compute()

if not isDone: print 'Mesh ', name, ': computation failed'

return mesh1d_tool

# Create a mesh with six nodes, seven edges and two quadrangle faces

def MakeQuadMesh2(mesh_name):

quad_1 = smesh.Mesh(name = mesh_name)

# six nodes

n1 = quad_1.AddNode(0, 20, 10)

n2 = quad_1.AddNode(0, 40, 10)

n3 = quad_1.AddNode(0, 40, 30)

n4 = quad_1.AddNode(0, 20, 30)

n5 = quad_1.AddNode(0, 0, 30)

n6 = quad_1.AddNode(0, 0, 10)

# seven edges

quad_1.AddEdge([n1, n2]) # 1

quad_1.AddEdge([n2, n3]) # 2

quad_1.AddEdge([n3, n4]) # 3

quad_1.AddEdge([n4, n1]) # 4

quad_1.AddEdge([n4, n5]) # 5

quad_1.AddEdge([n5, n6]) # 6

quad_1.AddEdge([n6, n1]) # 7

# two quadrangle faces

quad_1.AddFace([n1, n2, n3, n4]) # 8

quad_1.AddFace([n1, n4, n5, n6]) # 9

return [quad_1, [1,2,3,4,5,6,7], [8,9]]

# Path meshes

Edge_straight_mesh = Mesh1D(Edge_straight, 7, "Edge_straight")

Edge_bezierrr_mesh = Mesh1D(Edge_bezierrr, 7, "Edge_bezierrr")

Wire_polyline_mesh = Mesh1D(Wire_polyline, 3, "Wire_polyline")

Edge_Circle_mesh = Mesh1D(Edge_Circle , 8, "Edge_Circle")

# Initial meshes (to be extruded)

[quad_1, ee_1, ff_1] = MakeQuadMesh2("quad_1")

[quad_2, ee_2, ff_2] = MakeQuadMesh2("quad_2")

[quad_3, ee_3, ff_3] = MakeQuadMesh2("quad_3")

[quad_4, ee_4, ff_4] = MakeQuadMesh2("quad_4")

[quad_5, ee_5, ff_5] = MakeQuadMesh2("quad_5")

[quad_6, ee_6, ff_6] = MakeQuadMesh2("quad_6")

[quad_7, ee_7, ff_7] = MakeQuadMesh2("quad_7")

# ExtrusionAlongPath

# IDsOfElements, PathMesh, PathShape, NodeStart,

# HasAngles, Angles, HasRefPoint, RefPoint

refPoint = smesh.PointStruct(0, 0, 0)

a10 = 10.0*math.pi/180.0

a45 = 45.0*math.pi/180.0

# 1. Extrusion of two mesh edges along a straight path

error = quad_1.ExtrusionAlongPath([1,2], Edge_straight_mesh, Edge_straight, 1,

0, [], 0, refPoint)

# 2. Extrusion of one mesh edge along a curved path

error = quad_2.ExtrusionAlongPath([2], Edge_bezierrr_mesh, Edge_bezierrr, 1,

0, [], 0, refPoint)

# 3. Extrusion of one mesh edge along a curved path with usage of angles

error = quad_3.ExtrusionAlongPath([2], Edge_bezierrr_mesh, Edge_bezierrr, 1,

1, [a45, a45, a45, 0, -a45, -a45, -a45], 0, refPoint)

# 4. Extrusion of one mesh edge along the path, which is a part of a meshed wire

error = quad_4.ExtrusionAlongPath([4], Wire_polyline_mesh, Wire_polyline_edges[0], 1,

1, [a10, a10, a10], 0, refPoint)

# 5. Extrusion of two mesh faces along the path, which is a part of a meshed wire

error = quad_5.ExtrusionAlongPath(ff_5 , Wire_polyline_mesh, Wire_polyline_edges[2], 4,

0, [], 0, refPoint)

# 6. Extrusion of two mesh faces along a closed path

error = quad_6.ExtrusionAlongPath(ff_6 , Edge_Circle_mesh, Edge_Circle, 1,

0, [], 0, refPoint)

# 7. Extrusion of two mesh faces along a closed path with usage of angles

error = quad_7.ExtrusionAlongPath(ff_7, Edge_Circle_mesh, Edge_Circle, 1,

1, [a45, -a45, a45, -a45, a45, -a45, a45, -a45], 0, refPoint)

salome.sg.updateObjBrowser(1)

### Revolution

import math

import SMESH_mechanic

mesh = SMESH_mechanic.mesh

smesh = SMESH_mechanic.smesh

# create a group of faces to be revolved

FacesRotate = [492, 493, 502, 503]

GroupRotate = mesh.CreateGroup(SMESH.FACE,"Group of faces (rotate)")

GroupRotate.Add(FacesRotate)

# define revolution angle and axis

angle45 = 45 * math.pi / 180

axisXYZ = SMESH.AxisStruct(-38.3128, -73.3658, -23.321, -13.3402, -13.3265, 6.66632)

# perform revolution of an object

mesh.RotationSweepObject(GroupRotate, axisXYZ, angle45, 4, 1e-5)

### Pattern Mapping

import geompy

import smesh

# define the geometry

Box_1 = geompy.MakeBoxDXDYDZ(200., 200., 200.)

geompy.addToStudy(Box_1, "Box_1")

faces = geompy.SubShapeAll(Box_1, geompy.ShapeType["FACE"])

Face_1 = faces[0]

Face_2 = faces[1]

geompy.addToStudyInFather(Box_1, Face_1, "Face_1")

geompy.addToStudyInFather(Box_1, Face_2, "Face_2")

# build a quadrangle mesh 3x3 on Face_1

Mesh_1 = smesh.Mesh(Face_1)

algo1D = Mesh_1.Segment()

algo1D.NumberOfSegments(3)

Mesh_1.Quadrangle()

isDone = Mesh_1.Compute()

if not isDone: print 'Mesh Mesh_1 : computation failed'

# build a triangle mesh on Face_2

Mesh_2 = smesh.Mesh(Face_2)

algo1D = Mesh_2.Segment()

algo1D.NumberOfSegments(1)

algo2D = Mesh_2.Triangle()

algo2D.MaxElementArea(240)

isDone = Mesh_2.Compute()

if not isDone: print 'Mesh Mesh_2 : computation failed'

# create a pattern

pattern = smesh.GetPattern()

isDone = pattern.LoadFromFace(Mesh_2.GetMesh(), Face_2, 0)

if (isDone != 1): print 'LoadFromFace :', pattern.GetErrorCode()

# apply the pattern to a face of the first mesh

pattern.ApplyToMeshFaces(Mesh_1.GetMesh(), [17], 0, 0)

isDone = pattern.MakeMesh(Mesh_1.GetMesh(), 0, 0)

if (isDone != 1): print 'MakeMesh :', pattern.GetErrorCode()