Salome HOME
minor correction of the script
[modules/hydrosolver.git] / src / salome_hydro / interpolz_gui.py
index 91e4adcbca58530ba41c4b8e0609f7cdb73d2c96..16998413f874176c8740292707c847798928541b 100644 (file)
@@ -1,6 +1,8 @@
 
 import sys, os
-sys.path.append(os.path.join(os.environ['MEDFILE_ROOT_DIR'], 'lib\python2.7\site-packages\med'))
+sys.path.append(os.path.join(os.environ['MEDFILE_ROOT_DIR'], 'lib/python2.7/site-packages/med'))
+hydro_solver_root = os.path.join(os.environ['HYDROSOLVER_ROOT_DIR'], 'lib', 'python2.7', 'site-packages', 'salome', 'salome', 'hydro')
+sys.path.append(hydro_solver_root)
 
 import salome
 salome.salome_init()
@@ -8,15 +10,27 @@ salome.salome_init()
 import MEDLoader
 import HYDROPy
 
-from PyQt4 import QtCore, QtGui
-#from interpolzUI import Ui_InterpolZ
+from PyQt4 import QtCore, QtGui, uic
+
+import SalomePyQt
+import libSALOME_Swig
+salome_gui = libSALOME_Swig.SALOMEGUI_Swig()
+
+from generate_interpolz import generate
 
 def get_med_groups( file_path ):
-    meshes = MEDLoader.MEDLoader_GetMeshNames(file_path)
+    #print "get_med_groups", file_path
+    try:
+        meshes = MEDLoader.MEDLoader_GetMeshNames(file_path)
+    except:
+        return []
     if len(meshes)==0:
         return []
     mesh1 = meshes[0]
-    groups = list(MEDLoader.MEDLoader_GetMeshGroupsNames(file_path, mesh1))
+    try:
+        groups = list(MEDLoader.MEDLoader_GetMeshGroupsNames(file_path, mesh1))
+    except:
+        return []
     return groups
 
 def get_hydro_regions( calc_case_name ):
@@ -29,20 +43,154 @@ def get_hydro_regions( calc_case_name ):
       for r in regions:
         rname = r.GetName()
         regions_names.append( str(rname) )
+      
+      shape_groups = case.GetGeometryGroups()
+      for sg in shape_groups:
+          sgname = sg.GetName()
+          regions_names.append( sgname )
+      
       return regions_names
     else:
       return []
 
+def get_selected_calc_case():
+    ind = SalomePyQt.SalomePyQt.getObjectBrowser().selectionModel().selectedIndexes()
+    aStudyId = salome.myStudyId
+    doc = HYDROPy.HYDROData_Document.Document( aStudyId )
+    for i in ind:
+        if i.column()==0:
+            name = i.data().toString()
+            case = doc.FindObjectByName( name )
+            if isinstance(case, HYDROPy.HYDROData_CalculationCase):
+                return name
+    return None
+    
 class InterpolzDlg( QtGui.QDialog ):
-
     def __init__(self, parent = None):
         QtGui.QDialog.__init__( self, parent )
+        p = hydro_solver_root
+        uic.loadUi( p+'/interpolz.ui', self )
+        self.setWindowTitle( 'Generate interpolz script' )
+        self.connect( SalomePyQt.SalomePyQt.getObjectBrowser(), QtCore.SIGNAL("selectionChanged()"), self.onSelectionChanged )
+        self.connect( self.btnOutputPath, QtCore.SIGNAL( "clicked()" ), self.onOutputFile )
+        self.connect( self.btnMEDFile, QtCore.SIGNAL( "clicked()" ), self.onMEDFile )
+        self.connect( self.CalcCase, QtCore.SIGNAL( "textChanged( const QString& )" ), self.onCalcCaseChanged )
+        self.connect( self.MEDFile, QtCore.SIGNAL( "textChanged( const QString& )" ), self.onMEDChanged )
+        self.UndefZ.setRange( -100000, 100000 )
+        self.UndefZ.setValue( -9999 )
+        self.InterpMethod.addItem( "Interpolation at the nearest point" )
+        self.connect( self.ApplyClose, QtCore.SIGNAL( "clicked()" ), self.onApplyClose )
+        self.connect( self.Apply, QtCore.SIGNAL( "clicked()" ), self.onApply )
+        self.connect( self.Close, QtCore.SIGNAL( "clicked()" ), self.onClose )
+        self.connect( self.Help, QtCore.SIGNAL( "clicked()" ), self.onHelp )
+         
+    def onSelectionChanged( self ):
+        calc_case_name = get_selected_calc_case()
+        if calc_case_name is not None:
+          self.CalcCase.setText( calc_case_name )
+          
+    def onOutputFile( self ):
+      caption = "Python file"
+      mask = "*.py"
+      f = QtGui.QFileDialog.getSaveFileName( self, caption, ".", mask )
+      if f!=None and f!="":
+        self.OutputPath.setText( f )
+      
+    def onMEDFile( self ):
+      caption = "MED file"
+      mask = "*.med"
+      f = QtGui.QFileDialog.getOpenFileName( self, caption, ".", mask )
+      if f!=None and f!="":
+        self.MEDFile.setText( f )
+        
+    def onCalcCaseChanged( self ):
+      self.regions = get_hydro_regions( str(self.CalcCase.text()) )
+      self.onMEDChanged()
+        
+    def onMEDChanged( self ):
+      self.med_groups = get_med_groups( str(self.MEDFile.text()) )
+      #print self.med_groups
+      n = len( self.med_groups )
+      self.Groups.setRowCount( n )
+      for i in range( 0, n ):
+        if self.Groups.item( i, 0 ) is None:
+          self.Groups.setItem( i, 0, QtGui.QTableWidgetItem() )
+          self.Groups.setItem( i, 1, QtGui.QTableWidgetItem() )
+        self.Groups.item( i, 0 ).setText( self.med_groups[i] )
+        
+        cb = QtGui.QComboBox( self.Groups )
+        for r in self.regions:
+          cb.addItem( r )
+        self.Groups.setCellWidget( i, 1, cb )
+
+    def onApplyClose( self ):
+        self.onApply()
+        self.onClose()
+        
+    def onApply( self ):
+        path = str(self.OutputPath.text())
+        calc_case = str(self.CalcCase.text())
+        med_file = str(self.MEDFile.text())
+        med_groups_regions = {}
+        for i in range( 0, self.Groups.rowCount() ):
+            med_group = str( self.Groups.item( i, 0 ).text() )
+            hydro_reg = str( self.Groups.cellWidget( i, 1 ).currentText() )
+            if len(med_group)>0 and len(hydro_reg)>0:
+                med_groups_regions[med_group] = hydro_reg
+        z_undef = self.UndefZ.value()
+        interp = str(self.InterpMethod.currentText())
+        
+        msg = ""
+        if len(path)==0:
+            msg = "Please input the output path"
+        elif len(calc_case)==0:
+            msg = "Please choose the calculation case"
+        elif len(med_file)==0:
+            msg = "Please choose the MED file"
+        elif len(med_groups_regions)==0:
+            msg = "Please fill groups table"
+        elif len(interp)==0:
+            msg = "Please choose interpolation method"
+        
+        if len(msg)==0:
+            generate( path, calc_case, med_file, med_groups_regions, z_undef, interp )
+            msg = "InterpolZ script is successfully generated"
+            
+        QtGui.QMessageBox.information( self, "", msg )
+        
+    def onClose( self ):
+        self.close()     
+
+    """Shows help page"""
+    def onHelp( self ):
+        msg = """
+        <h2>Interpolz dialog</h2>
+
+        This dialog is a tool for automation the writing of the script <b>interpolz.py</b>.
+
+        <h3>Calculation case</h3>
+        The name of the calculation case. It can be filled automatically on the base of selection or can be input by user.
+
+        <h3>Output path</h3>
+        The path for the output, i.e. the path of the target script interpolz.
+
+        <h3>MED file</h3>
+        The path to MED file where MED groups are extracted.
+
+        <h3>Method</h3>
+        The interpolation method (interpolation at the nearest point and linear interpolation from a triangulation of the cloud of points).
+
+        <h3>Undefined Z</h3>
+        The Z value for nodes outside the regions.
+
+        <h3>Table</h3>
+        The table with MED groups and regions names.
 
+        When the data is input, the user clicks on "Apply" or "Apply and Close" button to perform the script generation.
+        """
+        QtGui.QMessageBox.about(self, self.tr("About boundary conditions dialog"), msg);
 
-#app = QtGui.QApplication( sys.argv )
-#dlg = InterpolzDlg()
-#dlg.show()
-#app.exec_()
 
-#print get_med_groups( "/dn26/HYDRO/channel.med" )
-print get_hydro_regions( "Case_1" )
+if __name__=='__main__':
+  dlg = InterpolzDlg()
+  dlg.show()