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