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