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