Salome HOME
Correction for hydro_test
[modules/hydro.git] / src / HYDROTools / plugins / meshEdgesToShapesDialog.py
1 #  Copyright (C) 2012-2013 EDF
2 #
3 #  This file is part of SALOME HYDRO module.
4 #
5 #  SALOME HYDRO module is free software: you can redistribute it and/or modify
6 #  it under the terms of the GNU General Public License as published by
7 #  the Free Software Foundation, either version 3 of the License, or
8 #  (at your option) any later version.
9 #
10 #  SALOME HYDRO module is distributed in the hope that it will be useful,
11 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #  GNU General Public License for more details.
14 #
15 #  You should have received a copy of the GNU General Public License
16 #  along with SALOME HYDRO module.  If not, see <http://www.gnu.org/licenses/>.
17
18 import os
19 import sys
20 import salome
21
22 salome.salome_init()
23
24 import SalomePyQt
25 sgPyQt = SalomePyQt.SalomePyQt()
26 import libSALOME_Swig
27 salome_gui = libSALOME_Swig.SALOMEGUI_Swig()
28
29 from PyQt5.QtCore import *
30 from PyQt5.QtGui import *
31 from PyQt5.QtWidgets import *
32 from PyQt5 import uic
33 from HYDROPy import *
34 import json
35
36 from salome.hydrotools.shapesGroups import freeBordersGroup, exploreEdgeGroups
37 from salome.hydrotools.hydroGeoMeshUtils import importPolylines
38
39 import  SMESH, SALOMEDS
40 from salome.smesh import smeshBuilder
41
42 hydro_root = os.path.join(os.environ['HYDRO_ROOT_DIR'], 'share', 'salome', 'plugins', 'hydro', 'plugins')
43 hydro_resources = os.path.join(os.environ['HYDRO_ROOT_DIR'], 'share', 'salome', 'resources', 'hydro')
44
45 class meshEdgesToShapesDialog(QDialog):
46     """
47     This dialog is used to extract all groups of edges from a mesh, plus all the border (free) edges,
48     and write the groups as shapefiles.
49     """
50
51     def __init__(self, parent = None, modal = 0):
52         QDialog.__init__(self, parent)
53         uic.loadUi(os.path.join(hydro_root, 'meshEdgesToShapes.ui'), self )
54
55         # Connections
56         self.pb_medFile.clicked.connect(self.on_med_file_browse)
57         self.tb_medFile.setIcon(QIcon(QPixmap(os.path.join(hydro_resources, "icon_select.png"))))
58         self.tb_medFile.clicked.connect(self.on_select_med_file)
59         self.pb_outDir.clicked.connect(self.on_outputDir_browse)
60         self.pb_help.clicked.connect(self.on_help)
61         self.pb_ok.accepted.connect(self.on_accept)
62         self.pb_ok.rejected.connect(self.on_reject)
63         self.cb_keepOutMed.setChecked(True)
64         self.medFile = None
65         self.outDir = None
66         
67     def get_selected_mesh(self):
68         """
69         Select a mesh in the object browser and return associated filename
70         """
71         nbsel = salome.sg.SelectedCount()
72         if nbsel != 1:
73             return ""
74         sel=salome.sg.getSelected(0)
75         so=salome.myStudy.FindObjectID(sel)
76         if so is None:
77             return ""
78         obj=so.GetObject()
79         smesh = smeshBuilder.New()
80         smesh.SetEnablePublish( False )
81         mesh1 = smesh.Mesh()
82         smesh.SetEnablePublish( True )
83         if not isinstance(obj, type(mesh1.GetMesh())):
84             return ""
85         filename = obj.GetMesh().GetMEDFileInfo().fileName
86         return filename
87     
88     def on_select_med_file(self):
89         """
90         set selected mesh filename on dialog
91         """
92         filename = self.get_selected_mesh()
93         print("selected mesh: %s"%filename)
94         if filename != "":
95             self.medFile = filename
96             self.le_medFile.setText(self.medFile)
97         
98     def on_med_file_browse(self):
99         """
100         Select MED file in file system
101         """
102         print("on_med_file_browse")
103         self.medFile, filt = QFileDialog.getOpenFileName(self, self.tr("Input MED file"), "", self.tr("MED files (*.med)"))
104         print(self.medFile)
105         if not self.medFile:
106             return
107         self.le_medFile.setText(self.medFile)
108         
109     def on_outputDir_browse(self):
110         """
111         Select OutputDirectory
112         """
113         print("on_ouptutDir_browse")
114         self.outDir = QFileDialog.getExistingDirectory(self, self.tr("Output Directory"), "")
115         print(self.outDir)
116         if not self.outDir:
117             return
118         self.le_outDir.setText(self.outDir)
119         
120     def on_help(self):
121         """
122         display a help message
123         """
124         msg = """
125         <h2>Mesh Edges to Shapes Dialog</h2>
126
127         This dialog is used to extract all groups of edges from a mesh, plus all the border (free) edges,
128         and write the groups as shapefiles.
129         <br><br>
130         The free edges regroup the external border of the mesh, and all the internal borders (isles).
131         A group containing the free edges is added to the mesh.
132         <br>        
133         The mesh is saved in a new file, in an ouput directory used also to store the shapefiles.
134         <br>        
135         A shapefile of edges and a shapefile of points are generated for each group of edges.
136         The shapefiles are intended to be loaded in a SIG tool (Qgis) and should preferaby be set in a correct coordinates system.
137         <br>
138         If the mesh uses a local coordinate system with an origin offset, the coordinates of this origin should be set in the dialog.
139         <br><br>         
140         Below is the description of the dialog controls.
141
142         <h3>MED file</h3>
143         This field allows selection of a med file (via the standard file open dialog).
144         The filling of this field is mandatory.
145         
146         <h3>offsetX, offsetY</h3>
147         These fields are used to set the Origin of the local coordinates system of the mesh, if any. 
148
149         <h3>Output directory</h3>
150         This field allows selection of a directory to store the shapes and the outputMesh.
151
152         <h3>Keep output MED file</h3>
153         If this checkbox is unchecked, the output mesh will be removed after calculation of the shapes.
154         """
155         QMessageBox.about(self, self.tr("About mesh edges to shapes dialog"), msg);
156        
157    
158     def on_accept(self):
159         print("accept")
160         medFile = self.le_medFile.text()
161         outDir = self.le_outDir.text()
162         offsetX = self.dsb_offsetX.value()
163         offsetY = self.dsb_offsetY.value()
164         isOutMedKept = self.cb_keepOutMed.isChecked()
165         isLoadFreeBorders = self.cb_loadFreeBorders.isChecked()
166         isLoadOthers = self.cb_loadOthers.isChecked()
167         self.accept()
168         print(medFile)
169         print(outDir)
170         print(isOutMedKept)
171         print(offsetX, offsetY)
172         a = os.path.splitext(medFile)
173         medFileOut = os.path.join(outDir, os.path.basename(a[0]) + '_brd' + a[1])
174         print(medFileOut)
175         smesh = smeshBuilder.New()
176         smesh.SetEnablePublish( False )
177         medFileOut = freeBordersGroup(medFile, medFileOut)
178         exploreEdgeGroups(medFileOut, outDir, offsetX, offsetY)
179         smesh.SetEnablePublish( True )
180         if not isOutMedKept:
181             print("remove", medFileOut)
182             os.remove(medFileOut)
183         shapesListFile = os.path.join(outDir, "shapesList.json")
184         fileShapes = []
185         with open(shapesListFile, 'r') as f:
186             fileShapes = json.load(f)
187         print(fileShapes)
188         hydro_doc = HYDROData_Document.Document()
189         l = []
190         if isLoadFreeBorders:
191             l = l + [ a for a in fileShapes if "FreeBorders.shp" in a]
192         if isLoadOthers:
193             l = l + [ a for a in fileShapes if "FreeBorders.shp" not in a]
194         for fileShape in l:
195             a = os.path.splitext(os.path.basename(fileShape))            
196             shapeName = a[0]
197             shapes = importPolylines(hydro_doc, fileShape, 0, 0)
198         if salome.sg.hasDesktop():
199             salome.sg.updateObjBrowser()
200             
201
202     def on_reject(self):
203         print("reject")
204         self.reject()
205         
206
207 def execDialog(context):
208     print("execDialog")
209     desktop = sgPyQt.getDesktop()
210     dlg = meshEdgesToShapesDialog(desktop)
211     dlg.show()