import os
-def freeBordersGroup(meshFile):
- print(" === freeBordersGroup", meshFile)
+def freeBordersGroup(meshFileIn, meshFileOut=""):
+ """
+ In a mesh, create a group of Edges for the borders: domain, internal holes (isles)
+ parameters:
+ meshFileIn: full path of the input mesh file, format MED
+ meshFileOut: full path of the output mesh file, format MED (default="" : when "", output file is suffixed with "_brd.med"
+ return full path of the output mesh file
+ """
smesh = smeshBuilder.New()
smesh.SetEnablePublish( False ) # Set to False to avoid publish in study if not needed
- ([MESH], status) = smesh.CreateMeshesFromMED(meshFile)
+ ([MESH], status) = smesh.CreateMeshesFromMED(meshFileIn)
+
nbAdded, MESH, addedBnd = MESH.MakeBoundaryElements( SMESH.BND_1DFROM2D, '', '', 0, [])
- groups = MESH.GetGroups()
+
aCriteria = []
aCriterion = smesh.GetCriterion(SMESH.EDGE,SMESH.FT_FreeBorders,SMESH.FT_Undefined,0)
aCriteria.append(aCriterion)
aFilter = smesh.GetFilterFromCriteria(aCriteria)
aFilter.SetMesh(MESH.GetMesh())
FreeBorders = MESH.GroupOnFilter( SMESH.EDGE, 'FreeBorders', aFilter )
- a = os.path.splitext(meshFile)
+
+ a = os.path.splitext(meshFileIn)
smesh.SetName(MESH, os.path.basename(a[0]))
- newMeshName = a[0] + '_brd' + a[1]
- print(newMeshName)
+
+ if meshFileOut == "":
+ a = os.path.splitext(meshFileIn)
+ smesh.SetName(MESH, os.path.basename(a[0]))
+ meshFileOut = a[0] + '_brd' + a[1]
+
try:
- MESH.ExportMED(newMeshName,auto_groups=0,minor=40,overwrite=1,meshPart=None,autoDimension=1)
+ MESH.ExportMED(meshFileOut,auto_groups=0,minor=40,overwrite=1,meshPart=None,autoDimension=1)
pass
except:
print('ExportMED() failed. Invalid file name?')
- return newMeshName
+ return meshFileOut
def explodeGroup(grp, grpName):
+ """
+ from a group of edges loaded with MEDCoupling, create ordered lists of nodes, one list for each set of connected edges.
+ parameters:
+ grp: MEDCoupling object for the group of edges
+ grpName: name of the group
+ return:
+ List of descriptors [(ordered list of nodeIds, name of the list, closed status)]
+ """
print(" === explodeGroup", grpName)
nbCells=grp.getNumberOfCells()
- dicReverse = {} # id noeud --> id edges
+ dicReverse = {} # id node --> id edges
for i in range(nbCells):
nodcell = grp.getNodeIdsOfCell(i)
for j in range(len(nodcell)):
return nodeChains
-def writeShapeLines(mcMesh, grpName, nodeChains, offsetX=0., offsetY=0.):
+def writeShapeLines(mcMesh, grpName, nodeChains, outputDirectory, offsetX=0., offsetY=0.):
+ """
+ from a mesh loaded in memory with MEDLoader, and a list of list of connected nodes associated to a group of edges, write a shapefile of type line, with one record per list of connected nodes.
+ parameters:
+ mcMesh: mesh loaded in memory with MEDLoader
+ grpName: name associated to the group of edges
+ nodeChains: List of descriptors corresponding to the group [(ordered list of nodeIds, name of the list, closed status)]
+ outputDirectory: directory for writing the shapefile
+ offsetX : offset of origin X in the mesh (default 0). The shapefile is always written without local origin to be ready for a direct load in Qgis.
+ offsetY : offset of origin Y in the mesh (default 0). The shapefile is always written without local origin to be ready for a direct load in Qgis.
+ """
print(" === writeShapeLines", grpName)
coords = mcMesh.getCoords()
- w = shapefile.Writer(grpName)
+ shapeFileName = os.path.join(outputDirectory, grpName)
+ w = shapefile.Writer(shapeFileName)
w.shapeType = 3
w.field('name', 'C')
for (chain, chainName, closed) in nodeChains:
for node in chain:
coord = coords[node].getValues()
coordLb93=[coord[0] + offsetX, coord[1] + offsetY]
- #print(" ", coordLb93)
chaincoords.append(coordLb93)
w.line([chaincoords])
w.record(chainName)
w.close()
-def writeShapePoints(mcMesh, grpName, nodeChains, offsetX=0., offsetY=0.):
+def writeShapePoints(mcMesh, grpName, nodeChains, outputDirectory, offsetX=0., offsetY=0.):
+ """
+ from a mesh loaded in memory with MEDLoader, and a list of list of connected nodes associated to a group of edges, write a shapefile of type multi points, with one record per list of connected nodes.
+ parameters:
+ mcMesh: mesh loaded in memory with MEDLoader
+ grpName: name associated to the group of edges
+ nodeChains: List of descriptors corresponding to the group [(ordered list of nodeIds, name of the list, closed status)]
+ outputDirectory: directory for writing the shapefile
+ offsetX : offset of origin X in the mesh (default 0). The shapefile is always written without local origin to be ready for a direct load in Qgis.
+ offsetY : offset of origin Y in the mesh (default 0). The shapefile is always written without local origin to be ready for a direct load in Qgis.
+ """
print(" === writeShapePoints", grpName)
coords = mcMesh.getCoords()
- w = shapefile.Writer(grpName + '_pts')
+ shapeFileName = os.path.join(outputDirectory, grpName)
+ w = shapefile.Writer(shapeFileName + '_pts')
#w.shapeType = 8
w.field('name', 'C')
for (chain, chainName, closed) in nodeChains:
for node in chain:
coord = coords[node].getValues()
coordLb93=[coord[0] + offsetX, coord[1] + offsetY]
- #print(" ", coordLb93)
chaincoords.append(coordLb93)
w.multipoint(chaincoords)
w.record(chainName)
w.close()
-def exploreEdgeGroups(meshFile, offsetX=0., offsetY=0.):
+def exploreEdgeGroups(meshFile, outputDirectory="", offsetX=0., offsetY=0.):
+ """
+ Find all the groups of edges in a mesh and, for each group, create one shapefile of lines and one of points. The shapefiles are created in the same system of coordinates as the mesh (For instance Lambert 93), but without origin offset (to be ready for a direct load in Qgis)
+ parameters:
+ meshFile: full path of the input mesh file, format MED
+ outputDirectory: directory in which the shapefiles are written (default "", if "" use the directory containing the mesh
+ offsetX: local X origin of the mesh
+ offsetY: local Y origin of the mesh
+ """
print(" === exploreEdgeGroups", meshFile)
+ if outputDirectory == "":
+ outputDirectory = os.path.dirname(meshFile)
+
+ a = os.path.splitext(meshFile)
+ prefix = os.path.basename(a[0]) # prefix = file name without extension
+
mcMesh = ml.MEDFileMesh.New(meshFile)
dim = mcMesh.getSpaceDimension()
d1=-1 # when dimension 2, edges are dim -1
if dim == 3: # when dimension 3, edges are dim -2
d1=-2
- a = os.path.splitext(meshFile)
- prefix = os.path.basename(a[0])
grp_names = mcMesh.getGroupsOnSpecifiedLev(d1) #names of edges groups
- groups = [mcMesh.getGroup(d1, name) for name in grp_names]
+
+ groups = [mcMesh.getGroup(d1, name) for name in grp_names] # list of groups in their name order
+
for (grp, grpName) in zip(groups, grp_names):
fullGrpName = prefix + '_' + grpName
nodeChains = explodeGroup(grp, fullGrpName)
- writeShapeLines(mcMesh, fullGrpName, nodeChains, offsetX, offsetY)
- writeShapePoints(mcMesh, fullGrpName, nodeChains, offsetX, offsetY)
+ writeShapeLines(mcMesh, fullGrpName, nodeChains, outputDirectory, offsetX, offsetY)
+ writeShapePoints(mcMesh, fullGrpName, nodeChains, outputDirectory, offsetX, offsetY)
def fitShapePointsToMesh(freeBorderShapefile, shapefileToAdjust):