Salome HOME
Merge branch 'BR_H2018_1' into TRUNK
[modules/hydrosolver.git] / src / salome_hydro / assignStrickler_gui.py
1 # -*- coding: utf-8 -*-
2
3 import sys, os
4 import string
5
6 hydro_solver_root = os.path.join(os.environ['HYDROSOLVER_ROOT_DIR'], 'lib', 'python2.7', 'site-packages', 'salome', 'salome', 'hydro')
7
8 import salome
9 salome.salome_init()
10
11 import HYDROPy
12 from salome.hydrotools.interpolS import assignStrickler
13
14 from generate_interpolz import replace
15
16 from PyQt5.QtWidgets import QDialog, QFileDialog, QTableWidgetItem, QComboBox, QMessageBox
17 from PyQt5 import uic
18
19 import SalomePyQt
20 import libSALOME_Swig
21 salome_gui = libSALOME_Swig.SALOMEGUI_Swig()
22
23
24 def get_selected_calc_case():
25     ind = SalomePyQt.SalomePyQt.getObjectBrowser().selectionModel().selectedIndexes()
26     aStudyId = salome.myStudyId
27     doc = HYDROPy.HYDROData_Document.Document( aStudyId )
28     for i in ind:
29         if i.column()==0:
30             name = str(i.data())
31             case = doc.FindObjectByName( name )
32             if isinstance(case, HYDROPy.HYDROData_CalculationCase):
33                 return name
34     return None
35
36 class assignStricklerDlg( QDialog ):
37     def __init__(self, parent = None):
38         QDialog.__init__( self, parent )
39         p = hydro_solver_root
40         uic.loadUi( p+'/assignStrickler.ui', self )
41         self.setWindowTitle( 'Generate assignStrickler script' )
42         SalomePyQt.SalomePyQt.getObjectBrowser().selectionChanged.connect(self.onSelectionChanged)
43         self.btnOutputPath.clicked.connect(self.onOutputFile)
44         self.btnMEDFile.clicked.connect(self.onMEDFile)
45         self.CalcCase.textChanged.connect(self.onCalcCaseChanged)
46         self.MEDFile.textChanged.connect(self.onMEDChanged)
47         self.ApplyClose.clicked.connect(self.onApplyClose)
48         self.Apply.clicked.connect(self.onApply)
49         self.Close.clicked.connect(self.onClose)
50         self.Help.clicked.connect(self.onHelp)
51         self.onSelectionChanged()
52
53     def onSelectionChanged( self ):
54         calc_case_name = get_selected_calc_case()
55         if calc_case_name is not None:
56           self.CalcCase.setText( calc_case_name )
57
58     def onOutputFile( self ):
59       caption = "Python file"
60       mask = "*.py"
61       fname, filt = QFileDialog.getSaveFileName( self, caption, ".", mask )
62       if fname!=None and fname!="":
63         if string.split(fname, '.')[-1] != 'py':
64           fname += '.py'
65         self.OutputPath.setText( fname )
66
67     def onMEDFile( self ):
68       caption = "MED file"
69       mask = "*.med"
70       fname, filt = QFileDialog.getOpenFileName( self, caption, ".", mask )
71       if fname!=None and fname!="":
72         self.MEDFile.setText( fname )
73
74     def onCalcCaseChanged( self ):
75       self.onMEDChanged()
76
77     def onMEDChanged( self ):
78       pass
79                  
80     def onApplyClose( self ):
81         if self.onApply():
82             self.onClose()
83
84     def generateScript( self, path, calc_case, med_file ):
85       f = open( path, "w" )
86       tf = open( hydro_solver_root+"/assignStrickler.template", "r" )
87       templ = tf.readlines()
88       replace( templ, "<case_name_from_dlg>", calc_case )
89       replace( templ, "<MED_file_path_from_dlg>", med_file )
90       for line in templ:
91         f.write( line )
92       f.close()
93
94     def onApply( self ):
95         path = str(self.OutputPath.text())
96         calc_case = str(self.CalcCase.text())
97         med_file = str(self.MEDFile.text())
98         isScriptExec = self.cb_scriptExec.isChecked()
99         msg = ""
100         if len(path)==0:
101             msg = "Please input the output path"
102         elif len(calc_case)==0:
103             msg = "Please choose the calculation case"
104         elif len(med_file)==0:
105             msg = "Please choose the MED file"
106
107         result = False
108         
109         if len(msg)==0:
110             self.generateScript( path, calc_case, med_file )
111             msg = "Strickler script is successfully generated"
112             if isScriptExec:
113               assignStrickler(calc_case, med_file) #, output_file_name=fichierMaillage_out, verbose=True)
114               msg = "Strickler script is successfully generated and executed"
115             result = True
116
117         QMessageBox.information( self, "", msg )
118         return result
119
120     def onClose( self ):
121         self.close()
122
123     """Shows help page"""
124     def onHelp( self ):
125         msg = """
126         <h2>assignStrickler dialog</h2>
127
128         This dialog is a tool for automation the writing of the script <b>assignStrickler.py</b>.
129
130         <h3>Calculation case</h3>
131         The name of the calculation case. It can be filled automatically on the base of selection or can be input by user.
132
133         <h3>Output path</h3>
134         The path for the output, i.e. the path of the target script assignStrickler.
135
136         <h3>MED file</h3>
137         The path to MED file where MED groups are extracted.
138
139         <h3>Method</h3>
140         The interpolation method (interpolation at the nearest point and linear interpolation from a triangulation of the cloud of points).
141
142         <h3>Undefined Z</h3>
143         The Z value for nodes outside the regions.
144
145         <h3>Table</h3>
146         The table with MED groups and regions names.
147
148         When the data is input, the user clicks on "Apply" or "Apply and Close" button to perform the script generation.
149         """
150         QMessageBox.about(self, self.tr("About boundary conditions dialog"), msg);
151
152
153 if __name__=='__main__':
154   dlg = assignStricklerDlg()
155   dlg.show()