]> SALOME platform Git repositories - modules/parametric.git/commitdiff
Salome HOME
Use PARAMETRIC engine to store the parametric studies in SALOME study (no persistence...
authorRenaud Barate <renaud.barate@edf.fr>
Tue, 19 Feb 2013 09:52:10 +0000 (10:52 +0100)
committerRenaud Barate <renaud.barate@edf.fr>
Tue, 19 Feb 2013 09:52:10 +0000 (10:52 +0100)
idl/PARAMETRIC_Gen.idl
src/PARAMETRIC/PARAMETRIC.py
src/PARAMETRICGUI/PARAMETRICGUI.py
src/salome/parametric/__init__.py
src/salome/parametric/gui/wizard.py
src/salome/parametric/study.py

index e5a57ec02dfa8c17bb3205ce88b8652a48423340..0888f8311833a996054cf8c969acc420c42b2c17 100644 (file)
@@ -26,7 +26,21 @@ module PARAMETRIC_ORB
 
 interface PARAMETRIC_Gen : Engines::EngineComponent, SALOMEDS::Driver
 {
-    void RunStudy(in long studyID, in SALOMEDS::ID caseEntry)
+    void AddParametricStudy(in Engines::fileBlock parametricStudy,
+                            in long salomeStudyID)
+      raises (SALOME::SALOME_Exception);
+
+    void SetParametricStudy(in Engines::fileBlock parametricStudy,
+                            in long salomeStudyID,
+                            in SALOMEDS::ID entry)
+      raises (SALOME::SALOME_Exception);
+
+    Engines::fileBlock GetParametricStudy(in long salomeStudyID,
+                                          in SALOMEDS::ID entry)
+      raises (SALOME::SALOME_Exception);
+
+    void RunStudy(in long salomeStudyID,
+                  in SALOMEDS::ID entry)
       raises (SALOME::SALOME_Exception);
 };
 
index 631a49d7aa0fd39d2ac5080fc82c71ebe7364f56..91e7499b3bd3c8631bdf90ba4e19d7a92171f744 100644 (file)
@@ -20,6 +20,7 @@ import threading
 import inspect
 import traceback
 import copy
+import cPickle
 
 import salome
 import SALOME
@@ -30,12 +31,21 @@ from SALOME_DriverPy import SALOME_DriverPy_i
 import SALOMERuntime
 import pilot
 
+from salome.kernel.studyedit import getStudyEditor
 from salome.kernel.logger import Logger
 from salome.kernel import termcolor
 logger = Logger("PARAMETRIC", color = termcolor.BLUE)
 logger.setLevel(logging.DEBUG)
 
-from salome.parametric import ParametricStudyEditor, ParametricStudy, parse_entry
+from salome.parametric import PARAM_STUDY_TYPE_ID, ParametricStudy, parse_entry
+
+# module constants
+MODULE_NAME = "PARAMETRIC"
+
+COMPONENT_NAME = "Parametric"
+COMPONENT_ICON = "PARAMETRIC_small.png"
+
+PARAM_STUDY_ICON = "param_study.png"
 
 start_script = """
 from salome.kernel.parametric.pyscript_utils import \
@@ -71,6 +81,8 @@ class PARAMETRIC(PARAMETRIC_ORB__POA.PARAMETRIC_Gen, SALOME_ComponentPy_i, SALOM
   def __init__(self, orb, poa, contID, containerName, instanceName, interfaceName):
     SALOME_ComponentPy_i.__init__(self, orb, poa, contID, containerName, instanceName, interfaceName, 0)
     SALOME_DriverPy_i.__init__(self, interfaceName)
+    self.param_comp = {}
+    self.param_study_dict = {}
     self.salome_runtime = None
     self.session_catalog = None
 
@@ -82,6 +94,66 @@ class PARAMETRIC(PARAMETRIC_ORB__POA.PARAMETRIC_Gen, SALOME_ComponentPy_i, SALOM
                                  inspect.stack()[1][1], inspect.stack()[1][2])
     raise SALOME.SALOME_Exception(exc)
 
+  def _find_or_create_param_component(self, salomeStudyID):
+    """
+    Find the component "Parametric" or create it if none is found
+    :return: the SComponent found or created.
+    """
+    if salomeStudyID not in self.param_comp:
+      ed = getStudyEditor(salomeStudyID)
+      self.param_comp[salomeStudyID] = ed.findOrCreateComponent(MODULE_NAME, COMPONENT_NAME, COMPONENT_ICON)
+    return self.param_comp[salomeStudyID]
+
+  def _set_param_study_sobj(self, parametric_study, salomeStudyID, sobj):
+    getStudyEditor(salomeStudyID).setItem(sobj,
+                                          name = parametric_study.name,
+                                          icon = PARAM_STUDY_ICON,
+                                          typeId = PARAM_STUDY_TYPE_ID)
+    if salomeStudyID not in self.param_study_dict:
+      self.param_study_dict[salomeStudyID] = {}
+    entry = sobj.GetID()
+    self.param_study_dict[salomeStudyID][entry] = parametric_study
+
+  def AddParametricStudy(self, parametricStudy, salomeStudyID):
+    try:
+      self.beginService("PARAMETRIC.AddParametricStudy")
+      param_study = cPickle.loads(parametricStudy)
+      param_comp = self._find_or_create_param_component(salomeStudyID)
+      sobj = getStudyEditor(salomeStudyID).createItem(param_comp, "__NEW_STUDY__")
+      self._set_param_study_sobj(param_study, salomeStudyID, sobj)
+      self.endService("PARAMETRIC.AddParametricStudy")
+    except:
+      self._raiseSalomeError()
+
+  def _set_parametric_study(self, param_study, salomeStudyID, entry):
+    sobj = getStudyEditor(salomeStudyID).study.FindObjectID(entry)
+    self._set_param_study_sobj(param_study, salomeStudyID, sobj)
+
+  def SetParametricStudy(self, parametricStudy, salomeStudyID, entry):
+    try:
+      self.beginService("PARAMETRIC.SetParametricStudy")
+      param_study = cPickle.loads(parametricStudy)
+      self._set_parametric_study(param_study, salomeStudyID, entry)
+      self.endService("PARAMETRIC.SetParametricStudy")
+    except:
+      self._raiseSalomeError()
+
+  def _get_parametric_study(self, salomeStudyID, entry):
+    if salomeStudyID not in self.param_study_dict or entry not in self.param_study_dict[salomeStudyID]:
+      raise Exception("No valid parametric study at entry %s" % entry)
+    param_study = self.param_study_dict[salomeStudyID][entry]
+    param_study.entry = entry
+    return param_study
+
+  def GetParametricStudy(self, salomeStudyID, entry):
+    try:
+      self.beginService("PARAMETRIC.GetParametricStudy")
+      param_study = self._get_parametric_study(salomeStudyID, entry)
+      return cPickle.dumps(param_study)
+      self.endService("PARAMETRIC.GetParametricStudy")
+    except:
+      self._raiseSalomeError()
+
   def _get_salome_runtime(self):
     if self.salome_runtime is None:
       # Initialize runtime for YACS
@@ -95,7 +167,7 @@ class PARAMETRIC(PARAMETRIC_ORB__POA.PARAMETRIC_Gen, SALOME_ComponentPy_i, SALOM
       self.salome_runtime.addCatalog(self.session_catalog)
     return self.salome_runtime
 
-  def RunStudy(self, studyId, caseEntry):
+  def RunStudy(self, salomeStudyID, entry):
     try:
       self.beginService("PARAMETRIC.RunStudy")
 
@@ -104,8 +176,7 @@ class PARAMETRIC(PARAMETRIC_ORB__POA.PARAMETRIC_Gen, SALOME_ComponentPy_i, SALOM
       PARAMETRIC.lock.release()
       
       # Get parametric study from the case in Salome study
-      ed = ParametricStudyEditor(studyId)
-      param_study = ed.get_parametric_study(caseEntry)
+      param_study = self._get_parametric_study(salomeStudyID, entry)
 
       # Generate YACS schema
       runtime = self._get_salome_runtime()
@@ -131,9 +202,9 @@ class PARAMETRIC(PARAMETRIC_ORB__POA.PARAMETRIC_Gen, SALOME_ComponentPy_i, SALOM
   
         init_solver = solver_compo_def._serviceMap["Init"].clone(None)
         init_solver.setComponent(solver_compo_inst)
-        init_solver.getInputPort("studyID").edInit(studyId)
-        entry = parse_entry(param_study.solver_case_entry)
-        init_solver.getInputPort("detCaseEntry").edInit(entry)
+        init_solver.getInputPort("studyID").edInit(salomeStudyID)
+        solver_case_entry = parse_entry(param_study.solver_case_entry)
+        init_solver.getInputPort("detCaseEntry").edInit(solver_case_entry)
         foreach.edSetInitNode(init_solver)
   
         exec_solver = solver_compo_def._serviceMap["Exec"].clone(None)
@@ -229,7 +300,7 @@ class PARAMETRIC(PARAMETRIC_ORB__POA.PARAMETRIC_Gen, SALOME_ComponentPy_i, SALOM
           param_study.data["__ERROR_MESSAGE__"].append(result["errorMessage"])
 
       # Save results in Salome study
-      ed.set_parametric_study_at_entry(param_study, caseEntry)
+      self._set_parametric_study(param_study, salomeStudyID, entry)
 
       self.endService("PARAMETRIC.RunStudy")
     except:
index eea5a25cd278e8e35f71bc42f9d4b9daa3389725..51b164487ceff4d221513cb50902a8e190a630a3 100644 (file)
@@ -194,10 +194,10 @@ def run_study():
   qapp = QtGui.QApplication
   try:
     qapp.setOverrideCursor(QtGui.QCursor(QtCore.Qt.WaitCursor))
-    study_id = sgPyQt.getStudyId()
+    ed = ParametricStudyEditor()
     entry = salome.sg.getSelected(0)
-    engine = salome.lcc.FindOrLoadComponent("ParametricContainer", "PARAMETRIC")
-    engine.RunStudy(study_id, entry)
+    engine = ed.find_or_create_engine()
+    engine.RunStudy(ed.study_id, entry)
     qapp.restoreOverrideCursor()
   except SALOME.SALOME_Exception, exc:
     qapp.restoreOverrideCursor()
index 28860dea1560d88f2b6c69774fe4eced835d0182..b7f0b55e8cd8d4dc986fd92e9c9bd3f0036dae0a 100644 (file)
@@ -18,6 +18,7 @@
 import study
 import genjob
 
+PARAM_STUDY_TYPE_ID = study.PARAM_STUDY_TYPE_ID
 ParametricStudy = study.ParametricStudy
 ParametricVariable = study.ParametricVariable
 ParametricStudyEditor = study.ParametricStudyEditor
index 6f6e47c782a6333b91a929adeb4197e56cc05cf9..0fa04d325fcb134bdbea56a352f620dfd8b57c1c 100644 (file)
@@ -94,31 +94,34 @@ class Wizard(QtGui.QWidget, Ui_Wizard):
     self.exec_params_frame.set_pyscript_label_from_vars(exchange_vars)
 
   def validate(self):
-    if not self.step_frames[self.curstep].check_values():
-      return
-    param_study = ParametricStudy()
-    # Input variables
-    for (name, range_widget) in self.define_values_frame.varwidgets.iteritems():
-      minval = range_widget.fromSpinBox.value()
-      maxval = range_widget.toSpinBox.value()
-      step = range_widget.stepSpinBox.value()
-      var = ParametricVariable(name, minval, maxval, step)
-      param_study.add_input_variable(var)
-    # Output variables
-    exch_vars = self.select_vars_frame.getSelectedExchangeVariables()
-    for outvar in exch_vars.outputVarList:
-      param_study.add_output_variable(outvar.name)
-    # Execution parameters
-    self.exec_params_frame.gui_to_study(param_study)
-
-    # Save to Salome study
-    ed = ParametricStudyEditor()
-    if self.entry is not None:
-      ed.set_parametric_study_at_entry(param_study, self.entry)
-    else:
-      ed.add_parametric_study(param_study)
-    salome.sg.updateObjBrowser(0)
-    self.close()
+    try:
+      if not self.step_frames[self.curstep].check_values():
+        return
+      param_study = ParametricStudy()
+      # Input variables
+      for (name, range_widget) in self.define_values_frame.varwidgets.iteritems():
+        minval = range_widget.fromSpinBox.value()
+        maxval = range_widget.toSpinBox.value()
+        step = range_widget.stepSpinBox.value()
+        var = ParametricVariable(name, minval, maxval, step)
+        param_study.add_input_variable(var)
+      # Output variables
+      exch_vars = self.select_vars_frame.getSelectedExchangeVariables()
+      for outvar in exch_vars.outputVarList:
+        param_study.add_output_variable(outvar.name)
+      # Execution parameters
+      self.exec_params_frame.gui_to_study(param_study)
+  
+      # Save to Salome study
+      ed = ParametricStudyEditor()
+      if self.entry is not None:
+        ed.set_parametric_study_at_entry(param_study, self.entry)
+      else:
+        ed.add_parametric_study(param_study)
+      salome.sg.updateObjBrowser(0)
+      self.close()
+    except Exception, exc:
+      QtGui.QMessageBox.critical(self, self.tr("Error"), str(exc))
 
   def set_study(self, param_study):
     self.entry = param_study.entry
index cdd3656192865734c1370540d3f09790bf7b7170..6c13dbd6244e7a13bb547c9eb9f068cf67a7cdec 100644 (file)
 import cPickle
 import numpy
 
-from salome.kernel.studyedit import getStudyEditor
+import salome
+import SALOME
+from salome.kernel.studyedit import getActiveStudyId
+import PARAMETRIC
 
-# module constants
-MODULE_NAME = "PARAMETRIC"
-
-COMPONENT_NAME = "Parametric"
-COMPONENT_ICON = "PARAMETRIC_small.png"
-
-PARAM_STUDY_ICON = "param_study.png"
 PARAM_STUDY_TYPE_ID = 1
 
 class ParametricStudyEditor:
@@ -36,44 +32,43 @@ class ParametricStudyEditor:
   it is :const:`None`, the edited study will be the current study.
   """
   def __init__(self, study_id = None):
-    self.editor = getStudyEditor(study_id)
-    self.param_comp = None
+    self.study_id = study_id
+    if self.study_id is None:
+      self.study_id = getActiveStudyId()
+    self.engine = None
 
-  def find_or_create_param_component(self):
+  def find_or_create_engine(self):
     """
-    Find the component "Parametric" or create it if none is found
-    :return: the SComponent found or created.
+    Find the engine "PARAMETRIC" or create it if none is found
+    :return: the PARAMETRIC engine
     """
-    if self.param_comp is None:
-      self.param_comp = self.editor.findOrCreateComponent(MODULE_NAME, COMPONENT_NAME, COMPONENT_ICON)
-    return self.param_comp
+    if self.engine is None:
+      self.engine = salome.lcc.FindOrLoadComponent("FactoryServer", "PARAMETRIC")
+    return self.engine
 
   def add_parametric_study(self, parametric_study):
-    self.find_or_create_param_component()
-    sobj = self.editor.createItem(self.param_comp, "__NEW_STUDY__")
-    self._set_sobj(parametric_study, sobj)
+    engine = self.find_or_create_engine()
+    pickled_param_study = cPickle.dumps(parametric_study)
+    try:
+      engine.AddParametricStudy(pickled_param_study, self.study_id)
+    except SALOME.SALOME_Exception, exc:
+      raise Exception(exc.details.text)
 
   def set_parametric_study_at_entry(self, parametric_study, entry):
-    sobj = self.editor.study.FindObjectID(entry)
-    self._set_sobj(parametric_study, sobj)
-
-  def _set_sobj(self, parametric_study, sobj):
-    self.editor.setItem(sobj,
-                        name = parametric_study.name,
-                        icon = PARAM_STUDY_ICON,
-                        typeId = PARAM_STUDY_TYPE_ID)
-    attr = self.editor.builder.FindOrCreateAttribute(sobj, "AttributeParameter")
-    attr.SetString("study", cPickle.dumps(parametric_study))
+    engine = self.find_or_create_engine()
+    pickled_param_study = cPickle.dumps(parametric_study)
+    try:
+      engine.SetParametricStudy(pickled_param_study, self.study_id, entry)
+    except SALOME.SALOME_Exception, exc:
+      raise Exception(exc.details.text)
 
   def get_parametric_study(self, entry):
-    sobj = self.editor.study.FindObjectID(entry)
-    if sobj is None or self.editor.getTypeId(sobj) != PARAM_STUDY_TYPE_ID:
-      raise Exception("No valid parametric study at entry %s" % entry)
-    found, attr = self.editor.builder.FindAttribute(sobj, "AttributeParameter")
-    if not found:
-      raise Exception("No valid parametric study at entry %s" % entry)
-    param_study = cPickle.loads(attr.GetString("study"))
-    param_study.entry = entry
+    engine = self.find_or_create_engine()
+    try:
+      pickled_param_study = engine.GetParametricStudy(self.study_id, entry)
+    except SALOME.SALOME_Exception, exc:
+      raise Exception(exc.details.text)
+    param_study = cPickle.loads(pickled_param_study)
     return param_study