Salome HOME
dialog initial field
[modules/hydrosolver.git] / src / salome_hydro / changeCoordsDialog.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
21 from PyQt5.QtCore import *
22 from PyQt5.QtGui import *
23 from PyQt5.QtWidgets import *
24 from PyQt5 import uic
25
26 import pyproj
27 from pyproj import CRS
28
29 from salome.hydrotools.changeCoords import changeCoords
30
31 import sysconfig
32 pythonVersion = 'python' + sysconfig.get_python_version()
33 hydro_solver_root = os.path.join(os.environ['HYDROSOLVER_ROOT_DIR'], 'lib', pythonVersion, 'site-packages', 'salome', 'salome', 'hydro')
34
35 class changeCoordsDialog(QDialog):
36     """
37     """
38
39     def __init__(self, parent = None, modal = 0):
40         QDialog.__init__(self, parent)
41         uic.loadUi(os.path.join(hydro_solver_root, 'changeCoords.ui'), self )
42
43         # Connections
44         self.pb_medFileIn.clicked.connect(self.on_med_file_in_browse)
45         self.pb_medFileOut.clicked.connect(self.on_med_file_out_browse)
46         self.pb_help.clicked.connect(self.on_help)
47         self.pb_ok.accepted.connect(self.on_accept)
48         self.pb_ok.rejected.connect(self.on_reject)
49         self.medFileIn = None
50         self.medFileOut = None
51         EPSG_list = (2154, 27561, 27562, 27563, 27564, 27571, 27572, 27573, 27574 )
52         for epsg in EPSG_list:
53             crs_code = CRS.from_epsg(epsg)
54             codeName = str(epsg) + ' ' + crs_code.name
55             self.cmb_systemIn.addItem(codeName)
56             self.cmb_systemOut.addItem(codeName)
57         self.cb_changeCoordSystem.setChecked(True)
58         self.cb_changeCoordSystem.setChecked(False)
59         
60     def on_med_file_in_browse(self):
61         """
62         Select input MED file
63         """
64         print("on_med_file_in_browse")
65         self.medFileIn, filt = QFileDialog.getOpenFileName(self, self.tr("Input MED file"), "", self.tr("MED files (*.med)"))
66         print(self.medFileIn)
67         if not self.medFileIn:
68             return
69         self.le_medFileIn.setText(self.medFileIn)
70         a = os.path.splitext(self.medFileIn)
71         self.medFileOut = a[0] + '_coo' + a[1]
72         self.le_medFileOut.setText(self.medFileOut)
73         
74     def on_med_file_out_browse(self):
75         """
76         Select output MED file
77         """
78         print("on_med_file_out_browse")
79         self.medFileOut, filt = QFileDialog.getSaveFileName(self, self.tr("Output MED file"), "", self.tr("MED files (*.med)"))
80         print(self.medFileOut)
81         if not self.medFileOut:
82             return
83         self.le_medFileOut.setText(self.medFileOut)
84         
85     def on_help(self):
86         """
87         display a help message
88         """
89         msg = """
90         <h2>Change mesh coordinates dialog</h2>
91
92         This dialog is used to change the nodes coordinates in a mesh. It is used either for a simple translation, a change of coordinates system
93         (for instance from Lambert II to Lambert-93), or a combination.
94         <br><br>        
95         The modified mesh is saved in a new file.
96         <br> 
97         If there is a change of coordinates system, the input mesh is first translated, when it uses a local origin.
98         After the change of coordinates system, the output mesh can be translated (new local origin).
99         <br>     
100         When there is only a simple translation, the operation is: Xout = Xin + offsetXin -offsetXout, Yout = Yin + offsetYin -offsetYout.
101         <br><br>         
102         Below is the description of the dialog controls.
103
104         <h3>Change coordinates system</h3>
105         If this checkbox is checked, the output mesh will be removed after calculation of the shapes.
106
107         <h3>Input MED file</h3>
108         This field allows the selection of a med file (via the standard file open dialog).
109         The filling of this field is mandatory.
110         
111         <h3>Input offsetX, offsetY</h3>
112         These fields are used to set the Origin of the local coordinates system of the input mesh, if any. 
113
114         <h3>Input Coord System</h3>
115         This combo box is use to select the coordinate system of the input mesh. 
116
117         <h3>Output MED file</h3>
118         This field allows the definition of a med file (via the standard file save dialog).
119         The filling of this field is mandatory.
120         
121         <h3>Output offsetX, offsetY</h3>
122         These fields are used to set the Origin of the local coordinates system of the output mesh, if any. 
123
124         <h3>Output Coord System</h3>
125         This combo box is use to select the coordinate system of the output mesh. 
126         """
127         QMessageBox.about(self, self.tr("About mesh edges to shapes dialog"), msg);
128        
129    
130     def on_accept(self):
131         print("accept")
132         #TODO check medfile in and out not empty
133         #TODO preset for medFileOut
134         medFileIn = self.le_medFileIn.text()
135         medFileOut = self.le_medFileOut.text()
136         offsetXin = self.dsb_offsetXin.value()
137         offsetYin = self.dsb_offsetYin.value()
138         offsetXout = self.dsb_offsetXout.value()
139         offsetYout = self.dsb_offsetYout.value()
140         isChangeCoordSystem = self.cb_changeCoordSystem.isChecked()
141         epsgIn = 2154
142         epsgOut = 2154
143         if isChangeCoordSystem:
144             epsgIn = int(self.cmb_systemIn.currentText().split()[0])
145             epsgOut = int(self.cmb_systemOut.currentText().split()[0])
146         print(medFileIn)
147         print(medFileOut)
148         print(isChangeCoordSystem)
149         print(offsetXin, offsetYin)
150         print(offsetXout, offsetYout)
151         print(epsgIn, epsgOut)
152         if not os.path.isfile(medFileIn):
153             msgBox = QMessageBox()
154             msgBox.setText( "Input MED file does not exist" )
155             msgBox.exec_()
156             return
157         dir = os.path.dirname(medFileOut)
158         base = os.path.basename(medFileOut)
159         ext = os.path.splitext(medFileOut)[1]
160         if not os.path.isdir(dir) or (base == "") or (ext.lower() != ".med"):
161             msgBox = QMessageBox()
162             msgBox.setText( "Output MED file is not a valid path" )
163             msgBox.exec_()
164             return
165         self.accept()
166         changeCoords(medFileIn, medFileOut, epsgIn, epsgOut, offsetXin, offsetYin, offsetXout, offsetYout)
167
168     def on_reject(self):
169         print("reject")
170         self.reject()
171         
172
173 def execDialog(context):
174     print("execDialog")
175   # get context study, salomeGui
176     study = context.study
177     sg = context.sg
178     dlg = changeCoordsDialog()
179     dlg.exec_()