Salome HOME
minor correction of the script
[modules/hydrosolver.git] / src / salome_hydro / interpolz_gui.py
1
2 import sys, os
3 sys.path.append(os.path.join(os.environ['MEDFILE_ROOT_DIR'], 'lib/python2.7/site-packages/med'))
4 hydro_solver_root = os.path.join(os.environ['HYDROSOLVER_ROOT_DIR'], 'lib', 'python2.7', 'site-packages', 'salome', 'salome', 'hydro')
5 sys.path.append(hydro_solver_root)
6
7 import salome
8 salome.salome_init()
9
10 import MEDLoader
11 import HYDROPy
12
13 from PyQt4 import QtCore, QtGui, uic
14
15 import SalomePyQt
16 import libSALOME_Swig
17 salome_gui = libSALOME_Swig.SALOMEGUI_Swig()
18
19 from generate_interpolz import generate
20
21 def get_med_groups( file_path ):
22     #print "get_med_groups", file_path
23     try:
24         meshes = MEDLoader.MEDLoader_GetMeshNames(file_path)
25     except:
26         return []
27     if len(meshes)==0:
28         return []
29     mesh1 = meshes[0]
30     try:
31         groups = list(MEDLoader.MEDLoader_GetMeshGroupsNames(file_path, mesh1))
32     except:
33         return []
34     return groups
35
36 def get_hydro_regions( calc_case_name ):
37     aStudyId = salome.myStudyId
38     doc = HYDROPy.HYDROData_Document.Document( aStudyId )
39     case = doc.FindObjectByName( calc_case_name )
40     if isinstance(case, HYDROPy.HYDROData_CalculationCase):
41       regions = case.GetRegions()
42       regions_names = []
43       for r in regions:
44         rname = r.GetName()
45         regions_names.append( str(rname) )
46       
47       shape_groups = case.GetGeometryGroups()
48       for sg in shape_groups:
49           sgname = sg.GetName()
50           regions_names.append( sgname )
51       
52       return regions_names
53     else:
54       return []
55
56 def get_selected_calc_case():
57     ind = SalomePyQt.SalomePyQt.getObjectBrowser().selectionModel().selectedIndexes()
58     aStudyId = salome.myStudyId
59     doc = HYDROPy.HYDROData_Document.Document( aStudyId )
60     for i in ind:
61         if i.column()==0:
62             name = i.data().toString()
63             case = doc.FindObjectByName( name )
64             if isinstance(case, HYDROPy.HYDROData_CalculationCase):
65                 return name
66     return None
67     
68 class InterpolzDlg( QtGui.QDialog ):
69     def __init__(self, parent = None):
70         QtGui.QDialog.__init__( self, parent )
71         p = hydro_solver_root
72         uic.loadUi( p+'/interpolz.ui', self )
73         self.setWindowTitle( 'Generate interpolz script' )
74         self.connect( SalomePyQt.SalomePyQt.getObjectBrowser(), QtCore.SIGNAL("selectionChanged()"), self.onSelectionChanged )
75         self.connect( self.btnOutputPath, QtCore.SIGNAL( "clicked()" ), self.onOutputFile )
76         self.connect( self.btnMEDFile, QtCore.SIGNAL( "clicked()" ), self.onMEDFile )
77         self.connect( self.CalcCase, QtCore.SIGNAL( "textChanged( const QString& )" ), self.onCalcCaseChanged )
78         self.connect( self.MEDFile, QtCore.SIGNAL( "textChanged( const QString& )" ), self.onMEDChanged )
79         self.UndefZ.setRange( -100000, 100000 )
80         self.UndefZ.setValue( -9999 )
81         self.InterpMethod.addItem( "Interpolation at the nearest point" )
82         self.connect( self.ApplyClose, QtCore.SIGNAL( "clicked()" ), self.onApplyClose )
83         self.connect( self.Apply, QtCore.SIGNAL( "clicked()" ), self.onApply )
84         self.connect( self.Close, QtCore.SIGNAL( "clicked()" ), self.onClose )
85         self.connect( self.Help, QtCore.SIGNAL( "clicked()" ), self.onHelp )
86          
87     def onSelectionChanged( self ):
88         calc_case_name = get_selected_calc_case()
89         if calc_case_name is not None:
90           self.CalcCase.setText( calc_case_name )
91           
92     def onOutputFile( self ):
93       caption = "Python file"
94       mask = "*.py"
95       f = QtGui.QFileDialog.getSaveFileName( self, caption, ".", mask )
96       if f!=None and f!="":
97         self.OutputPath.setText( f )
98       
99     def onMEDFile( self ):
100       caption = "MED file"
101       mask = "*.med"
102       f = QtGui.QFileDialog.getOpenFileName( self, caption, ".", mask )
103       if f!=None and f!="":
104         self.MEDFile.setText( f )
105         
106     def onCalcCaseChanged( self ):
107       self.regions = get_hydro_regions( str(self.CalcCase.text()) )
108       self.onMEDChanged()
109         
110     def onMEDChanged( self ):
111       self.med_groups = get_med_groups( str(self.MEDFile.text()) )
112       #print self.med_groups
113       n = len( self.med_groups )
114       self.Groups.setRowCount( n )
115       for i in range( 0, n ):
116         if self.Groups.item( i, 0 ) is None:
117           self.Groups.setItem( i, 0, QtGui.QTableWidgetItem() )
118           self.Groups.setItem( i, 1, QtGui.QTableWidgetItem() )
119         self.Groups.item( i, 0 ).setText( self.med_groups[i] )
120         
121         cb = QtGui.QComboBox( self.Groups )
122         for r in self.regions:
123           cb.addItem( r )
124         self.Groups.setCellWidget( i, 1, cb )
125
126     def onApplyClose( self ):
127         self.onApply()
128         self.onClose()
129         
130     def onApply( self ):
131         path = str(self.OutputPath.text())
132         calc_case = str(self.CalcCase.text())
133         med_file = str(self.MEDFile.text())
134         med_groups_regions = {}
135         for i in range( 0, self.Groups.rowCount() ):
136             med_group = str( self.Groups.item( i, 0 ).text() )
137             hydro_reg = str( self.Groups.cellWidget( i, 1 ).currentText() )
138             if len(med_group)>0 and len(hydro_reg)>0:
139                 med_groups_regions[med_group] = hydro_reg
140         z_undef = self.UndefZ.value()
141         interp = str(self.InterpMethod.currentText())
142         
143         msg = ""
144         if len(path)==0:
145             msg = "Please input the output path"
146         elif len(calc_case)==0:
147             msg = "Please choose the calculation case"
148         elif len(med_file)==0:
149             msg = "Please choose the MED file"
150         elif len(med_groups_regions)==0:
151             msg = "Please fill groups table"
152         elif len(interp)==0:
153             msg = "Please choose interpolation method"
154         
155         if len(msg)==0:
156             generate( path, calc_case, med_file, med_groups_regions, z_undef, interp )
157             msg = "InterpolZ script is successfully generated"
158             
159         QtGui.QMessageBox.information( self, "", msg )
160         
161     def onClose( self ):
162         self.close()     
163
164     """Shows help page"""
165     def onHelp( self ):
166         msg = """
167         <h2>Interpolz dialog</h2>
168
169         This dialog is a tool for automation the writing of the script <b>interpolz.py</b>.
170
171         <h3>Calculation case</h3>
172         The name of the calculation case. It can be filled automatically on the base of selection or can be input by user.
173
174         <h3>Output path</h3>
175         The path for the output, i.e. the path of the target script interpolz.
176
177         <h3>MED file</h3>
178         The path to MED file where MED groups are extracted.
179
180         <h3>Method</h3>
181         The interpolation method (interpolation at the nearest point and linear interpolation from a triangulation of the cloud of points).
182
183         <h3>Undefined Z</h3>
184         The Z value for nodes outside the regions.
185
186         <h3>Table</h3>
187         The table with MED groups and regions names.
188
189         When the data is input, the user clicks on "Apply" or "Apply and Close" button to perform the script generation.
190         """
191         QtGui.QMessageBox.about(self, self.tr("About boundary conditions dialog"), msg);
192
193
194 if __name__=='__main__':
195   dlg = InterpolzDlg()
196   dlg.show()