]> SALOME platform Git repositories - modules/parametric.git/commitdiff
Salome HOME
First part of new HDF5-based persistence (Save is fully implemented, not Load)
authorRenaud Barate <renaud.barate@edf.fr>
Wed, 20 Feb 2013 17:03:46 +0000 (18:03 +0100)
committerRenaud Barate <renaud.barate@edf.fr>
Wed, 20 Feb 2013 17:03:46 +0000 (18:03 +0100)
src/PARAMETRIC/PARAMETRIC.py
src/PARAMETRICGUI/PARAMETRICGUI.py
src/salome/parametric/persistence.py [new file with mode: 0644]

index 91e7499b3bd3c8631bdf90ba4e19d7a92171f744..5b6f3e181da6af9452090a4689ee8463dcf0e675 100644 (file)
@@ -21,6 +21,8 @@ import inspect
 import traceback
 import copy
 import cPickle
+import tempfile
+import os
 
 import salome
 import SALOME
@@ -38,6 +40,7 @@ logger = Logger("PARAMETRIC", color = termcolor.BLUE)
 logger.setLevel(logging.DEBUG)
 
 from salome.parametric import PARAM_STUDY_TYPE_ID, ParametricStudy, parse_entry
+from salome.parametric.persistence import load_param_study_dict, save_param_study_dict
 
 # module constants
 MODULE_NAME = "PARAMETRIC"
@@ -305,3 +308,42 @@ class PARAMETRIC(PARAMETRIC_ORB__POA.PARAMETRIC_Gen, SALOME_ComponentPy_i, SALOM
       self.endService("PARAMETRIC.RunStudy")
     except:
       self._raiseSalomeError()
+
+  def Save(self, theComponent, theURL, isMultiFile):
+    try:
+      # Select parametric studies to save
+      salomeStudyID = theComponent.GetStudy()._get_StudyId()
+      componentEntry = theComponent.GetID()
+      dict_to_save = {}
+      if salomeStudyID in self.param_study_dict:
+        for (entry, param_study) in self.param_study_dict[salomeStudyID].iteritems():
+          if entry.startswith(componentEntry):
+            dict_to_save[entry] = param_study
+      
+      if len(dict_to_save) > 0:
+        # Save parametric studies in temporary file
+        (fd, filename) = tempfile.mkstemp(prefix = "PARAMETRIC_", suffix = ".hdf")
+        os.close(fd)
+        save_param_study_dict(dict_to_save, filename)
+        
+        # Return the content of the temporary file as a byte sequence
+        with open(filename) as f:
+          buf = f.read()
+        
+        # Delete the temporary file
+        #os.remove(filename)
+        
+        return buf
+      else:
+        return ""
+    except:
+      logger.exception("Error while trying to save study")
+      return ""
+
+  def Load(self, theComponent, theStream, theURL, isMultiFile):
+    try:
+      print "PARAMETRIC load"
+      return 1
+    except:
+      logger.exception("Error while trying to load study")
+      return 0
index 51b164487ceff4d221513cb50902a8e190a630a3..934d9c0eb3bbec56a67067372974b66c22ffd0be 100644 (file)
@@ -185,10 +185,15 @@ def new_study():
   panel.new_study()
 
 def edit_study():
-  entry = salome.sg.getSelected(0)
-  ed = ParametricStudyEditor()
-  panel = MainPanel()
-  panel.edit_study(ed.get_parametric_study(entry))
+  try:
+    entry = salome.sg.getSelected(0)
+    ed = ParametricStudyEditor()
+    panel = MainPanel()
+    panel.edit_study(ed.get_parametric_study(entry))
+  except Exception, exc:
+    logger.exception("Error while trying to edit parametric study")
+    qapp = QtGui.QApplication
+    QtGui.QMessageBox.critical(sgPyQt.getDesktop(), qapp.translate("edit_study", "Error"), str(exc))
 
 def run_study():
   qapp = QtGui.QApplication
diff --git a/src/salome/parametric/persistence.py b/src/salome/parametric/persistence.py
new file mode 100644 (file)
index 0000000..17b033d
--- /dev/null
@@ -0,0 +1,83 @@
+# Copyright (C) 2012 EDF
+#
+# This file is part of SALOME PARAMETRIC module.
+#
+# SALOME PARAMETRIC module is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# SALOME PARAMETRIC module is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with SALOME PARAMETRIC module.  If not, see <http://www.gnu.org/licenses/>.
+
+import h5py
+
+from study import ParametricStudy
+
+def save_param_study_dict(param_study_dict, filename):
+  f = h5py.File(filename)
+  f.attrs["filetype"] = "salome_parametric" # simple tag used to identify PARAMETRIC files
+  f.attrs["version"] = "7.2" # version tag used to ensure backwards compatibility
+  for entry, param_study in param_study_dict.iteritems():
+    param_study_group = f.create_group(entry)
+    if param_study.name is not None:
+      param_study_group.attrs["name"] = param_study.name
+    
+    # Store input and output variables definitions
+    ordered_vars = []
+    input_vars_group = param_study_group.create_group("input_vars")
+    for input_var in param_study.input_vars:
+      ordered_vars.append(input_var.name)
+      var_group = input_vars_group.create_group(input_var.name)
+      var_group.attrs["min"] = input_var.min
+      var_group.attrs["max"] = input_var.max
+      var_group.attrs["step"] = input_var.step
+    output_vars_group = param_study_group.create_group("output_vars")
+    for output_varname in param_study.output_vars:
+      ordered_vars.append(output_varname)
+      var_group = output_vars_group.create_group(output_varname)
+    
+    # Store solver definition
+    solver_group = param_study_group.create_group("solver")
+    if param_study.solver_code_type == ParametricStudy.SALOME_COMPONENT:
+      solver_group.attrs["solver_code_type"] = "salome_component"
+      if param_study.salome_component_name is not None:
+        solver_group.attrs["salome_component_name"] = param_study.salome_component_name
+      if param_study.solver_case_entry is not None:
+        solver_group.attrs["solver_case_entry"] = param_study.solver_case_entry
+    else:
+      solver_group.attrs["solver_code_type"] = "python_script"
+      if param_study.python_script is not None:
+        solver_group.attrs["python_script"] = param_study.python_script
+    # Execution parameters
+    execution_group = param_study_group.create_group("execution")
+    execution_group.attrs["nb_parallel_computations"] = param_study.nb_parallel_computations
+    
+    # Data
+    if param_study.data is not None:
+      dset = param_study_group.create_dataset("data", (param_study.datasize, len(ordered_vars)))
+      dset.attrs["ordered_vars"] = ordered_vars
+      for idx_var, var in enumerate(ordered_vars):
+        if var in param_study.data and len(param_study.data[var]) > 0:
+          for idx_value, value in enumerate(param_study.data[var]):
+            if value is not None:
+              dset[idx_value, idx_var] = value
+      
+      # Error messages
+      if "__ERROR_MESSAGE__" in param_study.data and len(param_study.data["__ERROR_MESSAGE__"]) > 0:
+        dt = h5py.special_dtype(vlen=str)
+        error_set = param_study_group.create_dataset("error_message", (param_study.datasize,), dt)
+        for idx_value, value in enumerate(param_study.data["__ERROR_MESSAGE__"]):
+          if value is not None:
+            error_set[idx_value] = value
+
+  f.close()
+
+def load_param_study_dict(filename):
+  pass