1 # Copyright (C) 2011 CEA/DEN, EDF R&D, OPEN CASCADE
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License.
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # Lesser General Public License for more details.
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 # See http://www.salome-platform.org/ or
18 # email : webmaster.salome@opencascade.com
22 This module provides classes and functions to handle "Exchange Variables",
23 i.e. description of variables to be exchanged between a supervision code and a
24 computation code. These Exchange Variables can be stored in a SObject in
28 from studyedit import getStudyEditor
30 DEFAULT_NAME = "Variables"
31 INPUT_VAR_NAMES = "ExchangeVariables.InputVarNames"
32 OUTPUT_VAR_NAMES = "ExchangeVariables.OutputVarNames"
33 REF_ENTRY = "ExchangeVariables.RefEntry"
37 This class describes a single variable. For now, it is only described by
38 its name. Other attributes are reserved for future use.
41 def __init__(self, name, dimension = [], minValue = None, maxValue = None,
45 # Reserved for future use
46 self.dimension = dimension
47 self.minValue = minValue
48 self.maxValue = maxValue
49 self.initialValue = initialValue
52 class ExchangeVariables:
54 This class describes "Exchange Variables", i.e. a structure containing all
55 the necessary information to exchange variables between a supervision code
56 and a computation code.
58 .. attribute:: inputVarList
60 This instance attribute is a list of :class:`Variable` objects,
61 describing the input variables for the computation code.
63 .. attribute:: outputVarList
65 This instance attribute is a list of :class:`Variable` objects,
66 describing the output variables for the computation code.
68 .. attribute:: refEntry
70 This instance attribute is optional and can be used to store a
71 reference to a Salome entry containing an "Exchange Variable" SObject
72 that was used to build this one (when the current object has been built
73 by selecting variables of interest in a list of potential variables).
77 def __init__(self, inputVarList = [], outputVarList = [],
79 self.inputVarList = inputVarList
80 self.outputVarList = outputVarList
81 self.refEntry = refEntry
84 def createSObjectForExchangeVariables(fatherSobj, exchangeVariables,
86 icon = None, typeId = None):
88 Create a SObject to store an :class:`ExchangeVariables` instance.
90 :type fatherSobj: SObject
91 :param fatherSobj: parent of the SObject to create.
93 :type exchangeVariables: :class:`ExchangeVariables`
94 :param exchangeVariables: :class:`ExchangeVariables` instance to store in
98 :param name: name of the SObject to create.
101 :param icon: icon of the SObject to create.
103 :type typeId: integer
104 :param typeId: type of the SObject to create.
106 :return: the newly created SObject.
109 studyId = fatherSobj.GetStudy()._get_StudyId()
110 editor = getStudyEditor(studyId)
111 sobj = editor.createItem(fatherSobj,
115 _setSObjectForExchangeVariables(editor, sobj, exchangeVariables)
117 def updateSObjectForExchangeVariables(sobj, exchangeVariables,
119 icon = None, typeId = None):
121 Update an existing SObject storing an :class:`ExchangeVariables` instance.
124 :param sobj: the SObject to update.
126 See :func:`createSObjectForExchangeVariables` for the description of the
130 studyId = sobj.GetStudy()._get_StudyId()
131 editor = getStudyEditor(studyId)
132 editor.setItem(sobj, name = name, icon = icon, typeId = typeId)
133 editor.builder.RemoveAttribute(sobj, "AttributeParameter")
134 _setSObjectForExchangeVariables(editor, sobj, exchangeVariables)
136 def _setSObjectForExchangeVariables(editor, sobj, exchangeVariables):
137 attr = editor.builder.FindOrCreateAttribute(sobj, "AttributeParameter")
138 attr.SetStrArray(INPUT_VAR_NAMES,
139 [x.name for x in exchangeVariables.inputVarList])
140 attr.SetStrArray(OUTPUT_VAR_NAMES,
141 [x.name for x in exchangeVariables.outputVarList])
142 if exchangeVariables.refEntry is not None:
143 attr.SetString(REF_ENTRY, exchangeVariables.refEntry)
145 def getExchangeVariablesFromSObject(sobj):
147 Get an :class:`ExchangeVariables` instance from a SObject that stores it.
150 :param sobj: the SObject from which to read the :class:`ExchangeVariables`
153 :return: the newly created :class:`ExchangeVariables` instance.
156 (found, attr) = sobj.FindAttribute("AttributeParameter")
160 if attr.IsSet(REF_ENTRY, 3):
161 refEntry = attr.GetString(REF_ENTRY)
162 return ExchangeVariables(
163 [Variable(name) for name in attr.GetStrArray(INPUT_VAR_NAMES)],
164 [Variable(name) for name in attr.GetStrArray(OUTPUT_VAR_NAMES)],