From 2eddc353d873f25a86ade26f78d93d4acc4f7b8d Mon Sep 17 00:00:00 2001 From: Renaud Barate Date: Tue, 19 Feb 2013 10:52:10 +0100 Subject: [PATCH] Use PARAMETRIC engine to store the parametric studies in SALOME study (no persistence yet) --- idl/PARAMETRIC_Gen.idl | 16 +++++- src/PARAMETRIC/PARAMETRIC.py | 87 ++++++++++++++++++++++++++--- src/PARAMETRICGUI/PARAMETRICGUI.py | 6 +- src/salome/parametric/__init__.py | 1 + src/salome/parametric/gui/wizard.py | 53 +++++++++--------- src/salome/parametric/study.py | 69 +++++++++++------------ 6 files changed, 158 insertions(+), 74 deletions(-) diff --git a/idl/PARAMETRIC_Gen.idl b/idl/PARAMETRIC_Gen.idl index e5a57ec..0888f83 100644 --- a/idl/PARAMETRIC_Gen.idl +++ b/idl/PARAMETRIC_Gen.idl @@ -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); }; diff --git a/src/PARAMETRIC/PARAMETRIC.py b/src/PARAMETRIC/PARAMETRIC.py index 631a49d..91e7499 100644 --- a/src/PARAMETRIC/PARAMETRIC.py +++ b/src/PARAMETRIC/PARAMETRIC.py @@ -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: diff --git a/src/PARAMETRICGUI/PARAMETRICGUI.py b/src/PARAMETRICGUI/PARAMETRICGUI.py index eea5a25..51b1644 100644 --- a/src/PARAMETRICGUI/PARAMETRICGUI.py +++ b/src/PARAMETRICGUI/PARAMETRICGUI.py @@ -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() diff --git a/src/salome/parametric/__init__.py b/src/salome/parametric/__init__.py index 28860de..b7f0b55 100644 --- a/src/salome/parametric/__init__.py +++ b/src/salome/parametric/__init__.py @@ -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 diff --git a/src/salome/parametric/gui/wizard.py b/src/salome/parametric/gui/wizard.py index 6f6e47c..0fa04d3 100644 --- a/src/salome/parametric/gui/wizard.py +++ b/src/salome/parametric/gui/wizard.py @@ -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 diff --git a/src/salome/parametric/study.py b/src/salome/parametric/study.py index cdd3656..6c13dbd 100644 --- a/src/salome/parametric/study.py +++ b/src/salome/parametric/study.py @@ -18,15 +18,11 @@ 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 -- 2.39.2