From efb8aa34fedc1585ea10e57dd22a7f207eb18398 Mon Sep 17 00:00:00 2001 From: Paul RASCLE Date: Sun, 19 Jul 2020 20:23:11 +0200 Subject: [PATCH] documentation on cutMesh and shapesGroups --- src/HYDROTools/cutMesh.py | 12 +++-- src/HYDROTools/shapesGroups.py | 98 ++++++++++++++++++++++++++-------- 2 files changed, 84 insertions(+), 26 deletions(-) diff --git a/src/HYDROTools/cutMesh.py b/src/HYDROTools/cutMesh.py index 56c18d15..17ec40d4 100644 --- a/src/HYDROTools/cutMesh.py +++ b/src/HYDROTools/cutMesh.py @@ -8,16 +8,18 @@ salome.salome_init() from HYDROPy import * -def cutMesh(meshFileIn, meshFileOut = "", polyFile, offsetX=0, offsetY=0): +def cutMesh(meshFileIn, polyFile, meshFileOut = "", offsetX=0, offsetY=0): """ In a given MED mesh, remove faces and edges contained in a polygon (shapefile). Save the result in a new file. parameters: meshFileIn: full path of input MED file. Coordinates should be without an origin offset of coordinates. - meshFileout: full path of output MED file (default="" : when "", output file is suffixed wit "_cut.med" polyFile: a shapefile giving a single polygon for cut (should be in the same coordinate system as the mesh, without an origin offset of coordinates. + meshFileout: full path of output MED file (default="" : when "", output file is suffixed with "_cut.med" offsetX: local X origin for cut operation and output offsetY: local Y origin for cut operation and output - """ + return: + meshFileout + """ hydro_doc = HYDROData_Document.Document() # --- coordonnées locales @@ -106,4 +108,6 @@ def cutMesh(meshFileIn, meshFileOut = "", polyFile, offsetX=0, offsetY=0): Mesh.ExportMED(meshFileOut,auto_groups=0,minor=40,overwrite=1,meshPart=None,autoDimension=1) if salome.sg.hasDesktop(): - salome.sg.updateObjBrowser() + salome.sg.updateObjBrowser() + + return meshFileOut diff --git a/src/HYDROTools/shapesGroups.py b/src/HYDROTools/shapesGroups.py index aeb10c61..1f3da15a 100644 --- a/src/HYDROTools/shapesGroups.py +++ b/src/HYDROTools/shapesGroups.py @@ -14,36 +14,56 @@ import math 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)): @@ -111,10 +131,21 @@ def explodeGroup(grp, grpName): 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: @@ -123,17 +154,27 @@ def writeShapeLines(mcMesh, grpName, nodeChains, offsetX=0., offsetY=0.): 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: @@ -142,30 +183,43 @@ def writeShapePoints(mcMesh, grpName, nodeChains, offsetX=0., offsetY=0.): 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): -- 2.30.2