import sys import salome import geompy import smesh, SMESH import math import NETGENPlugin import StdMeshers def isin(test,alist): return bool([x for x in alist if test in x]) salome.salome_init() # I'm using BREP files for geometry, but Salome understands other formats too demo_brep_1 = geompy.Import("your geometry file.brep", "BREP") geompy.addToStudy( demo_brep_1, "demo.brep_1" ) # get a list of all solids from the BREP file solids = geompy.SubShapeAll(demo_brep_1, geompy.ShapeType["SOLID"]) # add each one to the geometry for idx in range(len(solids)): sld = solids[idx] sld_name = "sld_" + str(idx) id_sld = geompy.addToStudyInFather(demo_brep_1, sld, sld_name) # print "Adding solid {0} {1}".format(sld_name,id_sld) # get a list of all faces faces = geompy.SubShapeAll(demo_brep_1, geompy.ShapeType["FACE"]) # add each face to the geometry for idx in range(len(faces)) : face = faces[idx] face_name = "face_" + str(idx) id_face = geompy.addToStudyInFather(demo_brep_1, face, face_name) print "Adding face {0} {1}".format(face_name,id_face) # it is assumed that the BREP file is ready for meshing already (all # coincidental faces are shared) # we have some special software to do that when the model is built # you can use the Partition operation in Salome to take care of that print "Meshing..." # Perform meshing # meshing here is with Netgen 1D-2D-3D algorithms, with a coarse mesh # change this for your favorite meshing algorithm Mesh_1 = smesh.Mesh(demo_brep_1) Netgen_1D_2D_3D = Mesh_1.Tetrahedron(algo=smesh.FULL_NETGEN) NETGEN_3D_Parameters = Netgen_1D_2D_3D.Parameters() NETGEN_3D_Parameters.SetMaxSize( 216.004 ) NETGEN_3D_Parameters.SetSecondOrder( 0 ) NETGEN_3D_Parameters.SetOptimize( 1 ) # should be 1 NETGEN_3D_Parameters.SetFineness( 2 ) isDone = Mesh_1.Compute() print "Done" # dict_solids is a dictionary that will hold all tet ids for each volume dict_solids = { } # create groups for idx in range(len(solids)): sld = solids[idx] sld_g = Mesh_1.Group(sld) # create a group for the solid sld_elems = sld_g.GetIDs() # get the list of all id's in the volume #sld_elems = sld_g.GetListOfId() dict_solids[idx+1] = sld_elems # save the list of tet id's in the dictionary # dict_faces is a dictionary that will hold all triangle id's for each face dict_faces = { } # create groups for idx in range(len(faces)) : face = faces[idx] face_g = Mesh_1.Group(face) # create the group for the face face_elems = face_g.GetListOfID() # get the list of triangle id's on the face dict_faces[idx+1] = face_elems # save the list of triangle id's in the dict salome.sg.updateObjBrowser(1) # write the .msh file print "Writing mesh" fmsh = open( 'your msh file.msh', 'w') fmsh.write( '$MeshFormat\n' ) fmsh.write( '2.2 0 8\n' ) fmsh.write( '$EndMeshFormat\n' ) # write the list of nodes in the mesh fmsh.write( '$Nodes\n' ) nodeIDs = Mesh_1.GetNodesId() fmsh.write( str(len(nodeIDs)) + '\n') for j in nodeIDs: xyz = Mesh_1.GetNodeXYZ(j) fmsh.write ( "{0} {1} {2} {3}\n".format(j,xyz[0],xyz[1],xyz[2]) ) #print "Node %d: (%f,%f,%f)" % (j, xyz[0], xyz[1], xyz[2]) fmsh.write ('$EndNodes\n') # write the list of elements in the mesh # triangles and tets are saved in this section fmsh.write ('$Elements\n') elemIDs = Mesh_1.GetElementsId() elemTriangles = Mesh_1.GetElementsByType(smesh.FACE) # get all triangles elemTetras = Mesh_1.GetElementsByType(smesh.VOLUME) # get all tets fmsh.write (str(len(elemTriangles) + len(elemTetras)) + '\n') # elements will be re-index dynamically (i.e. the index of the elements in # the .msh file may be different from the index of elements in Salome) # it's much faster to do it this way, and there does not seem to be any # side effect. # if you want to preserve the Salome internal indexes, use the commented # fragments below crt_idx = 1 # crt_idx will hold the current index of the element for key in sorted(dict_faces.iterkeys()): for t in dict_faces[key]: node1,node2,node3 = Mesh_1.GetElemNodes(t) fmsh.write( "{0} 2 2 {1} {2} {3} {4} {5}\n".format(crt_idx,key,key,node1,node2,node3)) crt_idx = crt_idx+1 ########################################################################### # uncomment this section and comment the one above to preserve the indexes # from Salome (much slower method) #for t in elemTriangles: # node1,node2,node3 = Mesh_1.GetElemNodes(t) # face = Mesh_1.GetShapeIDForElem(t) # faceId = Mesh_1.GetShapeID(face) # for i in dict_faces.keys(): # if (t in dict_faces[i]): # faceId = i # break # fmsh.write( "{0} 2 2 {1} {2} {3} {4} {5}\n".format(crt_idx,faceId,faceId,node1,node2,node3)) # crt_idx = crt_idx+1 ########################################################################### #write the tets for key in sorted(dict_solids.iterkeys()): for t in dict_solids[key]: node1,node2,node3,node4 = Mesh_1.GetElemNodes(t) fmsh.write( "{0} 4 2 {1} {2} {3} {4} {5} {6}\n".format(crt_idx,key,key,node1,node2,node3,node4)) crt_idx = crt_idx+1 ########################################################################### # uncomment this section and comment the one above to preserve the indexes # from Salome (much slower method) #for t in elemTetras: # node1,node2,node3,node4 = Mesh_1.GetElemNodes(t) # solid = Mesh_1.GetShapeIDForElem(t) # solidId = Mesh_1.GetShapeID(solid) # for i in dict_solids.keys(): # if (t in dict_solids[i]): # solidId = i # break # fmsh.write( "{0} 4 2 {1} {2} {3} {4} {5} {6}\n".format(crt_idx,solidId,solidId,node1,node2,node3,node4)) # crt_idx = crt_idx+1 ########################################################################### fmsh.write ('$EndElements\n') fmsh.close() print "Done"