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