]> SALOME platform Git repositories - modules/hydro.git/commitdiff
Salome HOME
fonctions Python conversion groupes en shapefiles
authorPaul RASCLE <paul.rascle@openfields.fr>
Wed, 8 Jul 2020 15:54:08 +0000 (17:54 +0200)
committerYOANN AUDOUIN <B61570@dsp0851742.postes.calibre.edf.fr>
Fri, 30 Oct 2020 16:06:22 +0000 (17:06 +0100)
src/HYDROTools/CMakeLists.txt
src/HYDROTools/shapesGroups.py [new file with mode: 0644]

index cceff83ce58712572fdeb517c94260a3a1eef39e..1074e2d9fbd0c506ca0c89f444e43d9c7fad5595 100644 (file)
@@ -24,6 +24,7 @@ SET(PYFILES
   controls.py
   interpolZ.py
   interpolS.py
+  shapesGroups.py
 )
 
 # --- rules ---
diff --git a/src/HYDROTools/shapesGroups.py b/src/HYDROTools/shapesGroups.py
new file mode 100644 (file)
index 0000000..14f6e8e
--- /dev/null
@@ -0,0 +1,163 @@
+
+
+# -----------------------------------------------------------------------------
+
+import salome
+salome.salome_init()
+
+import  SMESH, SALOMEDS
+from salome.smesh import smeshBuilder
+
+import numpy as np
+import MEDLoader as ml
+import medcoupling as mc
+
+import shapefile
+
+def freeBordersGroup(meshFile):
+    print(" === freeBordersGroup", meshFile)
+    smesh = smeshBuilder.New()
+    smesh.SetEnablePublish( False ) # Set to False to avoid publish in study if not needed
+    ([MESH], status) = smesh.CreateMeshesFromMED(meshFile)
+    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 )
+    smesh.SetName(MESH, 'MESH')
+    newMeshName = '/tmp/freeBorders.med'
+    try:
+        MESH.ExportMED(newMeshName,auto_groups=0,minor=41,overwrite=1,meshPart=None,autoDimension=1)
+        pass
+    except:
+        print('ExportMED() failed. Invalid file name?')
+    return newMeshName
+
+def explodeGroup(grp, grpName):
+    print(" === explodeGroup", grpName)
+    nbCells=grp.getNumberOfCells()
+
+    dicReverse = {} # id noeud --> id edges
+    for i in range(nbCells):
+        nodcell = grp.getNodeIdsOfCell(i)
+        for j in range(len(nodcell)):
+            if nodcell[j] in dicReverse:
+                dicReverse[nodcell[j]].append(i)
+            else:
+                dicReverse[nodcell[j]] = [i]
+
+    nodeChains = []
+    usedCells = [False] * nbCells
+    while False in usedCells:
+        icell = usedCells.index(False)
+        usedCells[icell] = True
+        nodcell = grp.getNodeIdsOfCell(icell)
+        closed = False
+        chain = [nodcell[0], nodcell[1]]
+        nextnode = nodcell[1]
+        prevnode = nodcell[0]
+        while nextnode in dicReverse:
+            nextcells = dicReverse[nextnode]
+            if len(nextcells) != 2:             # end of chain(1) or "edges connector"(>2): stop
+                closed = False
+                nextnode = -1                   # stop
+            else:
+                newcell =False
+                for i in range(len(nextcells)):
+                    ncell = nextcells[i]
+                    if not usedCells[ncell]:       # the chain of nodes grows
+                        usedCells[ncell] = True
+                        newcell = True
+                        nodcell = grp.getNodeIdsOfCell(ncell)
+                        if nodcell[0] == nextnode:
+                            nextnode = nodcell[1]  # forward edge
+                        else:
+                            nextnode = nodcell[0]  # reversed edge ?
+                        chain.append(nextnode)
+                if not newcell:                    # end of chain, closed
+                    closed =True
+                    nextnode = -1
+        while prevnode in dicReverse:
+            prevcells = dicReverse[prevnode]
+            if len(prevcells) != 2:             # end of chain(1) or "edges connector"(>2): stop
+                closed = False
+                prevnode = -1                   # stop
+            else:
+                newcell =False
+                for i in range(len(prevcells)):
+                    ncell = prevcells[i]
+                    if not usedCells[ncell]:       # the chain of nodes grows
+                        usedCells[ncell] = True
+                        newcell = True
+                        nodcell = grp.getNodeIdsOfCell(ncell)
+                        if nodcell[1] == prevnode:
+                            prevnode = nodcell[0]  # forward edge
+                        else:
+                            prevnode = nodcell[1]  # reversed edge ?
+                        chain.insert(0, prevnode)
+                if not newcell:                    # end of chain, closed
+                    closed =True
+                    prevnode = -1
+
+        chainDesc = (chain, grpName +"_%s" % len(nodeChains), closed)
+        nodeChains.append(chainDesc)
+        print(chainDesc[1:])
+    return nodeChains
+
+def writeShapeLines(mcMesh, grpName, nodeChains, offsetX=0., offsetY=0.):
+    print(" === writeShapeLines", grpName)
+    coords = mcMesh.getCoords()
+    w = shapefile.Writer(grpName)
+    w.shapeType = 3
+    w.field('name', 'C')
+    for (chain, chainName, closed) in nodeChains:
+        print("   --- ", chainName)
+        chaincoords = []
+        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.):
+    print(" === writeShapePoints", grpName)
+    coords = mcMesh.getCoords()
+    w = shapefile.Writer(grpName + '_pts')
+    #w.shapeType = 8
+    w.field('name', 'C')
+    for (chain, chainName, closed) in nodeChains:
+        print("   --- ", chainName)
+        chaincoords = []
+        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.):
+    print(" === exploreEdgeGroups", meshFile)
+    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
+
+    grp_names = mcMesh.getGroupsOnSpecifiedLev(d1) #names of edges groups
+    groups = [mcMesh.getGroup(d1, name) for name in grp_names]
+    for (grp, grpName) in zip(groups, grp_names):
+        nodeChains = explodeGroup(grp, grpName)
+        writeShapeLines(mcMesh, grpName, nodeChains, offsetX, offsetY)
+        writeShapePoints(mcMesh, grpName, nodeChains, offsetX, offsetY)
+
+# ---
+
+#meshFile = freeBordersGroup('/home/paul/projets/hydro95/V9_5_BR/tests/Maill_SB1610_ALB1501_barr_eff.med')
+#exploreEdgeGroups(meshFile, 1000000., 6000000.)