11 def cutMesh(meshFileIn, meshFileOut = "", polyFile, offsetX=0, offsetY=0):
13 In a given MED mesh, remove faces and edges contained in a polygon (shapefile). Save the result in a new file.
15 meshFileIn: full path of input MED file. Coordinates should be without an origin offset of coordinates.
16 meshFileout: full path of output MED file (default="" : when "", output file is suffixed wit "_cut.med"
17 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.
18 offsetX: local X origin for cut operation and output
19 offsetY: local Y origin for cut operation and output
21 hydro_doc = HYDROData_Document.Document()
23 # --- coordonnées locales
25 hydro_doc.SetLocalCS( offsetX, offsetY )
27 # --- polygone decoupe
29 a = os.path.splitext(polyFile)
30 polyName = os.path.basename(a[0])
31 HYDROData_PolylineXY.ImportShapesFromFile(polyFile)
32 poly = hydro_doc.FindObjectByName(polyName + '_PolyXY_0')
36 zoneImmersible = hydro_doc.CreateObject( KIND_IMMERSIBLE_ZONE )
37 zoneImmersible.SetName( "zoneImmersible" )
38 zoneImmersible.SetPolyline( poly )
39 zoneImmersible.Update()
41 # --- cas de calcul = une face simple sur le polygone
43 casCalcul = hydro_doc.CreateObject( KIND_CALCULATION )
44 casCalcul.SetName( "casCalcul" )
45 casCalcul.SetAssignmentMode( HYDROData_CalculationCase.AUTOMATIC )
46 casCalcul.AddGeometryObject( zoneImmersible )
47 case_geom_group = zoneImmersible.GetGroup( 0 )
48 casCalcul.AddGeometryGroup( case_geom_group )
49 casCalcul.SetBoundaryPolyline( poly )
54 casCalcul_entry = casCalcul.Export()
55 print("casCalcul_entry", casCalcul_entry)
58 from salome.geom import geomBuilder
59 geompy = geomBuilder.New()
61 geomObj = salome.IDToObject(str(casCalcul_entry))
63 # --- decoupe maillage dans SMESH
65 import SMESH, SALOMEDS
66 from salome.smesh import smeshBuilder
68 # --- chargement maillage, changement de repère
70 smesh = smeshBuilder.New()
71 ([Mesh], status) = smesh.CreateMeshesFromMED(meshFileIn)
72 Mesh.TranslateObject( Mesh, [ -offsetX, -offsetY, 0 ], 0 )
74 # --- groupes des faces et des edges contenues dans le polygone de découpe
77 aCriterion = smesh.GetCriterion(SMESH.FACE,SMESH.FT_BelongToGeom,SMESH.FT_Undefined, geomObj)
78 aCriteria.append(aCriterion)
79 aFilter = smesh.GetFilterFromCriteria(aCriteria)
80 aFilter.SetMesh(Mesh.GetMesh())
81 decoupeFaces = Mesh.GroupOnFilter( SMESH.FACE, 'decoupeFaces', aFilter )
84 aCriterion = smesh.GetCriterion(SMESH.EDGE,SMESH.FT_BelongToGeom,SMESH.FT_Undefined, geomObj)
85 aCriteria.append(aCriterion)
86 aFilter = smesh.GetFilterFromCriteria(aCriteria)
87 aFilter.SetMesh(Mesh.GetMesh())
88 decoupeEdges = Mesh.GroupOnFilter( SMESH.EDGE, 'decoupeEdges', aFilter )
90 # --- suppression des groupes et de leurs mailles
92 Mesh.RemoveGroupWithContents( decoupeFaces )
93 Mesh.RemoveGroupWithContents( decoupeEdges )
95 # --- regeneration des edges de bord manquantes
97 nbAdded, Mesh, _NoneGroup = Mesh.MakeBoundaryElements( SMESH.BND_1DFROM2D, '', '', 0, [])
99 # --- enregistrement MED du maillage découpé
101 if meshFileOut == "":
102 a = os.path.splitext(meshFileIn)
103 smesh.SetName(Mesh, os.path.basename(a[0]))
104 meshFileOut = a[0] + '_cut' + a[1]
106 Mesh.ExportMED(meshFileOut,auto_groups=0,minor=40,overwrite=1,meshPart=None,autoDimension=1)
108 if salome.sg.hasDesktop():
109 salome.sg.updateObjBrowser()