1 // Copyright (C) 2014-2020 CEA/DEN, EDF R&D
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, or (at your option) any later version.
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 email : webmaster.salome@opencascade.com
22 #include <InitializationPlugin_EvalListener.h>
23 #include <ParametersPlugin_Parameter.h>
24 #include <InitializationPlugin_PyInterp.h>
26 #include <Events_InfoMessage.h>
28 #include <ModelAPI_AttributeDouble.h>
29 #include <ModelAPI_AttributeInteger.h>
30 #include <ModelAPI_AttributeRefList.h>
31 #include <ModelAPI_AttributeString.h>
32 #include <ModelAPI_AttributeValidator.h>
33 #include <ModelAPI_Document.h>
34 #include <ModelAPI_Events.h>
35 #include <ModelAPI_ResultParameter.h>
36 #include <ModelAPI_Session.h>
37 #include <ModelAPI_Tools.h>
39 #include <GeomDataAPI_Point.h>
40 #include <GeomDataAPI_Point2D.h>
46 //------------------------------------------------------------------------------
49 std::string toStdString(double theValue)
51 std::ostringstream sstream;
52 // write value in scientific format with 16 digits,
53 // thus, not check the dot position
54 sstream.precision(16);
55 sstream << std::scientific << theValue;
59 std::set<std::string> toSet(const std::list<std::string>& theContainer)
61 return std::set<std::string>(theContainer.begin(), theContainer.end());
64 //------------------------------------------------------------------------------
66 InitializationPlugin_EvalListener::InitializationPlugin_EvalListener()
68 Events_Loop* aLoop = Events_Loop::loop();
70 aLoop->registerListener(this, ModelAPI_AttributeEvalMessage::eventId(), NULL, true);
71 aLoop->registerListener(this, ModelAPI_ParameterEvalMessage::eventId(), NULL, true);
72 aLoop->registerListener(this, ModelAPI_ComputePositionsMessage::eventId(), NULL, true);
74 myInterp = std::shared_ptr<InitializationPlugin_PyInterp>(new InitializationPlugin_PyInterp());
75 myInterp->initialize();
78 InitializationPlugin_EvalListener::~InitializationPlugin_EvalListener()
82 void InitializationPlugin_EvalListener::processEvent(
83 const std::shared_ptr<Events_Message>& theMessage)
85 if (!theMessage.get())
88 if (theMessage->eventID() == ModelAPI_AttributeEvalMessage::eventId()) {
89 processEvaluationEvent(theMessage);
90 } else if (theMessage->eventID() == ModelAPI_ParameterEvalMessage::eventId()) {
91 std::shared_ptr<ModelAPI_ParameterEvalMessage> aMsg =
92 std::dynamic_pointer_cast<ModelAPI_ParameterEvalMessage>(theMessage);
93 FeaturePtr aParam = aMsg->parameter();
94 std::string anExp = aParam->string(ParametersPlugin_Parameter::EXPRESSION_ID())->value();
96 std::list<std::shared_ptr<ModelAPI_ResultParameter> > aParamsList;
97 double aResult = evaluate(aParam, anExp, anError, aParamsList, true);
98 aMsg->setResults(aParamsList, aResult, anError);
99 } else if (theMessage->eventID() == ModelAPI_ComputePositionsMessage::eventId()) {
100 std::shared_ptr<ModelAPI_ComputePositionsMessage> aMsg =
101 std::dynamic_pointer_cast<ModelAPI_ComputePositionsMessage>(theMessage);
102 aMsg->setPositions(myInterp->positions(aMsg->expression(), aMsg->parameter()));
106 double InitializationPlugin_EvalListener::evaluate(FeaturePtr theParameter,
107 const std::string& theExpression, std::string& theError,
108 std::list<std::shared_ptr<ModelAPI_ResultParameter> >& theParamsList,
109 const bool theIsParameter)
111 std::list<std::string> anExprParams = myInterp->compile(theExpression);
112 // find expression's params in the model
113 std::list<std::string> aContext;
114 std::list<std::string>::iterator it = anExprParams.begin();
115 for ( ; it != anExprParams.end(); it++) {
117 ResultParameterPtr aParamRes;
118 // If variable does not exist python interpreter will generate an error. It is OK.
119 // But due to the issue 1479 it should not check the history position of parameters relatively
120 // to feature that contains expression
121 if (!ModelAPI_Tools::findVariable(theIsParameter ? theParameter : FeaturePtr(),
122 *it, aValue, aParamRes, theParameter->document()))
126 theParamsList.push_back(aParamRes);
127 aContext.push_back(*it + "=" + toStdString(aValue));
129 myInterp->extendLocalContext(aContext);
130 double result = myInterp->evaluate(theExpression, theError);
131 myInterp->clearLocalContext();
135 void InitializationPlugin_EvalListener::processEvaluationEvent(
136 const std::shared_ptr<Events_Message>& theMessage)
138 std::shared_ptr<ModelAPI_AttributeEvalMessage> aMessage =
139 std::dynamic_pointer_cast<ModelAPI_AttributeEvalMessage>(theMessage);
141 std::list<std::shared_ptr<ModelAPI_ResultParameter> > aParamsList;
142 FeaturePtr aParamFeature =
143 std::dynamic_pointer_cast<ModelAPI_Feature>(aMessage->attribute()->owner());
144 if (aMessage->attribute()->attributeType() == ModelAPI_AttributeInteger::typeId()) {
145 AttributeIntegerPtr anAttribute =
146 std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(aMessage->attribute());
148 int aValue = (int)evaluate(aParamFeature, anAttribute->text(), anError, aParamsList);
149 bool isValid = anError.empty();
151 anAttribute->setCalculatedValue(aValue);
152 anAttribute->setUsedParameters(isValid ?
153 toSet(myInterp->compile(anAttribute->text())) : std::set<std::string>());
154 anAttribute->setExpressionInvalid(!isValid);
155 anAttribute->setExpressionError(anAttribute->text().empty() ? "" : anError);
157 if (aMessage->attribute()->attributeType() == ModelAPI_AttributeDouble::typeId()) {
158 AttributeDoublePtr anAttribute =
159 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aMessage->attribute());
161 double aValue = evaluate(aParamFeature, anAttribute->text(), anError, aParamsList);
162 bool isValid = anError.empty();
164 anAttribute->setCalculatedValue(aValue);
165 anAttribute->setUsedParameters(isValid ?
166 toSet(myInterp->compile(anAttribute->text())) : std::set<std::string>());
167 anAttribute->setExpressionInvalid(!isValid);
168 anAttribute->setExpressionError(anAttribute->text().empty() ? "" : anError);
170 if (aMessage->attribute()->attributeType() == GeomDataAPI_Point::typeId()) {
171 AttributePointPtr anAttribute =
172 std::dynamic_pointer_cast<GeomDataAPI_Point>(aMessage->attribute());
173 std::string aText[] = {
174 anAttribute->textX(),
175 anAttribute->textY(),
178 double aCalculatedValue[] = {
183 for (int i = 0; i < 3; ++i) {
185 double aValue = evaluate(aParamFeature, aText[i], anError, aParamsList);
186 bool isValid = anError.empty();
187 if (isValid) aCalculatedValue[i] = aValue;
188 anAttribute->setUsedParameters(i,
189 isValid ? toSet(myInterp->compile(aText[i])) : std::set<std::string>());
190 anAttribute->setExpressionInvalid(i, !isValid);
191 anAttribute->setExpressionError(i, aText[i].empty() ? "" : anError);
193 anAttribute->setCalculatedValue(aCalculatedValue[0],
195 aCalculatedValue[2]);
197 if (aMessage->attribute()->attributeType() == GeomDataAPI_Point2D::typeId()) {
198 AttributePoint2DPtr anAttribute =
199 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aMessage->attribute());
200 std::string aText[] = {
201 anAttribute->textX(),
204 double aCalculatedValue[] = {
208 for (int i = 0; i < 2; ++i) {
210 double aValue = evaluate(aParamFeature, aText[i], anError, aParamsList);
211 bool isValid = anError.empty();
212 if (isValid) aCalculatedValue[i] = aValue;
213 anAttribute->setUsedParameters(i,
214 isValid ? toSet(myInterp->compile(aText[i])) : std::set<std::string>());
215 anAttribute->setExpressionInvalid(i, !isValid);
216 anAttribute->setExpressionError(i, aText[i].empty() ? "" : anError);
218 anAttribute->setCalculatedValue(aCalculatedValue[0],
219 aCalculatedValue[1]);
223 void InitializationPlugin_EvalListener::initDataModel()
225 myInterp->runString("salome_iapp.register_module_in_study(\"Shaper\")");