smesh/doc/examples/use_existing_faces.py

117 lines
4.3 KiB
Python
Raw Permalink Normal View History

# Usage of "Use Faces to be Created Manually" algorithm
2013-02-12 20:37:44 +06:00
import salome
salome.salome_init_without_session()
2022-04-11 18:28:01 +05:00
from salome.geom import geomBuilder
from salome.smesh import smeshBuilder
2022-04-11 18:28:01 +05:00
geom_builder = geomBuilder.New()
smesh_builder = smeshBuilder.New()
2013-02-12 20:37:44 +06:00
# define my 2D algorithm
2022-04-11 18:28:01 +05:00
def my2DMeshing(geomFace, mesh):
import numpy as np
2013-02-12 20:37:44 +06:00
# find gravity center of geomFace
gcXYZ = geom_builder.PointCoordinates( geom_builder.MakeCDG( geomFace ))
2013-02-12 20:37:44 +06:00
# define order and orientation of edges
sortedEdges = []
geomEdges = geom_builder.SubShapeAll( geomFace, geom_builder.ShapeType["EDGE"])
2013-02-12 20:37:44 +06:00
sortedEdges.append(( geomEdges.pop(0), True ))
while geomEdges:
prevEdge_rev = sortedEdges[ -1 ]
prevVV = geom_builder.SubShapeAll( prevEdge_rev[0], geom_builder.ShapeType["VERTEX"])
2013-02-12 20:37:44 +06:00
prevV2 = prevVV[ prevEdge_rev[1] ]
found = False
for iE in range( len( geomEdges )):
v1,v2 = geom_builder.SubShapeAll( geomEdges[ iE ], geom_builder.ShapeType["VERTEX"])
same1,same2 = [( geom_builder.MinDistance( prevV2, v ) < 1e-7 ) for v in [v1,v2] ]
2013-02-12 20:37:44 +06:00
if not same1 and not same2: continue
sortedEdges.append(( geomEdges.pop( iE ), same1 ))
found = True
break
assert found
sortedEdges.reverse()
# put nodes on edges in a right order
nodes = []
for edge, isForward in sortedEdges:
v1,v2 = geom_builder.SubShapeAll( edge, geom_builder.ShapeType["VERTEX"])
2022-04-11 18:28:01 +05:00
edgeNodes = mesh.GetSubMeshNodesId( v2, all=False ) + \
mesh.GetSubMeshNodesId( edge, all=False ) + \
mesh.GetSubMeshNodesId( v1, all=False )
2013-02-12 20:37:44 +06:00
if not isForward: edgeNodes.reverse()
nodes.extend( edgeNodes[:-1] )
# create nodes inside the geomFace
r1 = 0.6
r2 = 1 - r1
nodesInside = []
for n in nodes:
2022-04-11 18:28:01 +05:00
nXYZ = mesh.GetNodeXYZ( n )
2013-02-12 20:37:44 +06:00
newXYZ = np.add( np.multiply( r1, gcXYZ ), np.multiply( r2, nXYZ ))
2022-04-11 18:28:01 +05:00
nodesInside.append( mesh.AddNode( newXYZ[0], newXYZ[1], newXYZ[2] ))
mesh.SetNodeOnFace( nodesInside[-1], geomFace, 0, 0 )
2013-02-12 20:37:44 +06:00
# find out orientation of faces to create
# geomFace normal
faceNorm = geom_builder.GetNormal( geomFace )
v1,v2 = [ geom_builder.PointCoordinates( v ) \
for v in geom_builder.SubShapeAll( faceNorm, geom_builder.ShapeType["VERTEX"]) ]
2013-02-12 20:37:44 +06:00
faceNormXYZ = np.subtract( v2, v1 )
outDirXYZ = np.subtract( v1, [ 50, 50, 50 ] )
if np.dot( faceNormXYZ, outDirXYZ ) < 0: # reversed face
faceNormXYZ = np.multiply( -1., faceNormXYZ )
# mesh face normal
2022-04-11 18:28:01 +05:00
e1 = np.subtract( mesh.GetNodeXYZ( nodes[0] ), mesh.GetNodeXYZ( nodes[1] ))
e2 = np.subtract( mesh.GetNodeXYZ( nodes[0] ), mesh.GetNodeXYZ( nodesInside[0] ))
2013-02-12 20:37:44 +06:00
meshNorm = np.cross( e1, e2 )
# faces orientation
reverse = ( np.dot( faceNormXYZ, meshNorm ) < 0 )
# create mesh faces
iN = len( nodes )
while iN:
n1, n2, n3, n4 = nodes[iN-1], nodes[iN-2], nodesInside[iN-2], nodesInside[iN-1]
iN -= 1
if reverse:
2022-04-11 18:28:01 +05:00
f = mesh.AddFace( [n1, n2, n3, n4] )
2013-02-12 20:37:44 +06:00
else:
2022-04-11 18:28:01 +05:00
f = mesh.AddFace( [n4, n3, n2, n1] )
2013-02-12 20:37:44 +06:00
# new faces must be assigned to geometry to allow 3D algorithm finding them
2022-04-11 18:28:01 +05:00
mesh.SetMeshElementOnShape( f, geomFace )
2013-02-12 20:37:44 +06:00
if reverse:
nodesInside.reverse()
2022-04-11 18:28:01 +05:00
polygon = mesh.AddPolygonalFace( nodesInside )
mesh.SetMeshElementOnShape( polygon, geomFace )
2013-02-12 20:37:44 +06:00
return
# create geometry and get faces to mesh with my2DMeshing()
2022-04-11 18:28:01 +05:00
box = geom_builder.MakeBoxDXDYDZ( 100, 100, 100 )
f1 = geom_builder.SubShapeAll( box, geom_builder.ShapeType["FACE"])[0]
f2 = geom_builder.GetOppositeFace( box, f1 )
geom_builder.addToStudy( box, "box" )
geom_builder.addToStudy( f1, "f1" )
geom_builder.addToStudy( f2, "f2" )
2013-02-12 20:37:44 +06:00
# compute 1D mesh
2022-04-11 18:28:01 +05:00
mesh = smesh_builder.Mesh( box )
2013-02-12 20:37:44 +06:00
mesh.Segment().NumberOfSegments( 5 )
if not mesh.Compute(): raise Exception("Error when computing Mesh")
2013-02-12 20:37:44 +06:00
# compute 2D mesh
mesh.Quadrangle()
mesh.UseExistingFaces(f1) # UseExistingFaces() allows using my2DMeshing();
mesh.UseExistingFaces(f2) # assign UseExistingFaces() BEFORE calling my2DMeshing()!
2022-04-11 18:28:01 +05:00
my2DMeshing(f1, mesh)
my2DMeshing(f2, mesh)
if not mesh.Compute(): raise Exception("Error when computing Mesh")
2013-02-12 20:37:44 +06:00
# compute 3D mesh
mesh.Prism()
if not mesh.Compute(): raise Exception("Error when computing Mesh")