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 <Locale_Convert.h>
30 #include <ModelAPI_AttributeDouble.h>
31 #include <ModelAPI_AttributeInteger.h>
32 #include <ModelAPI_AttributeRefList.h>
33 #include <ModelAPI_AttributeString.h>
34 #include <ModelAPI_AttributeValidator.h>
35 #include <ModelAPI_Document.h>
36 #include <ModelAPI_Events.h>
37 #include <ModelAPI_ResultParameter.h>
38 #include <ModelAPI_Session.h>
39 #include <ModelAPI_Tools.h>
41 #include <GeomDataAPI_Point.h>
42 #include <GeomDataAPI_Point2D.h>
48 //------------------------------------------------------------------------------
51 std::wstring toString(double theValue)
53 std::wostringstream sstream;
54 // write value in scientific format with 16 digits,
55 // thus, not check the dot position
56 sstream.precision(16);
57 sstream << std::scientific << theValue;
61 std::set<std::wstring> toSet(const std::list<std::wstring>& theContainer)
63 return std::set<std::wstring>(theContainer.begin(), theContainer.end());
66 //------------------------------------------------------------------------------
68 InitializationPlugin_EvalListener::InitializationPlugin_EvalListener()
70 Events_Loop* aLoop = Events_Loop::loop();
72 aLoop->registerListener(this, ModelAPI_AttributeEvalMessage::eventId(), NULL, true);
73 aLoop->registerListener(this, ModelAPI_ParameterEvalMessage::eventId(), NULL, true);
74 aLoop->registerListener(this, ModelAPI_ComputePositionsMessage::eventId(), NULL, true);
76 myInterp = std::shared_ptr<InitializationPlugin_PyInterp>(new InitializationPlugin_PyInterp());
77 myInterp->initialize();
80 InitializationPlugin_EvalListener::~InitializationPlugin_EvalListener()
84 void InitializationPlugin_EvalListener::processEvent(
85 const std::shared_ptr<Events_Message>& theMessage)
87 if (!theMessage.get())
90 if (theMessage->eventID() == ModelAPI_AttributeEvalMessage::eventId()) {
91 processEvaluationEvent(theMessage);
92 } else if (theMessage->eventID() == ModelAPI_ParameterEvalMessage::eventId()) {
93 std::shared_ptr<ModelAPI_ParameterEvalMessage> aMsg =
94 std::dynamic_pointer_cast<ModelAPI_ParameterEvalMessage>(theMessage);
95 FeaturePtr aParam = aMsg->parameter();
96 AttributeStringPtr anExprAttr = aParam->string(ParametersPlugin_Parameter::EXPRESSION_ID());
97 std::wstring anExp = anExprAttr->isUValue() ?
98 Locale::Convert::toWString(anExprAttr->valueU()) :
99 Locale::Convert::toWString(anExprAttr->value());
101 std::list<std::shared_ptr<ModelAPI_ResultParameter> > aParamsList;
102 double aResult = evaluate(aParam, anExp, anError, aParamsList, true);
103 aMsg->setResults(aParamsList, aResult, anError);
104 } else if (theMessage->eventID() == ModelAPI_ComputePositionsMessage::eventId()) {
105 std::shared_ptr<ModelAPI_ComputePositionsMessage> aMsg =
106 std::dynamic_pointer_cast<ModelAPI_ComputePositionsMessage>(theMessage);
107 aMsg->setPositions(myInterp->positions(aMsg->expression(), aMsg->parameter()));
111 double InitializationPlugin_EvalListener::evaluate(FeaturePtr theParameter,
112 const std::wstring& theExpression, std::string& theError,
113 std::list<std::shared_ptr<ModelAPI_ResultParameter> >& theParamsList,
114 const bool theIsParameter)
116 std::list<std::wstring> anExprParams = myInterp->compile(theExpression);
117 // find expression's params in the model
118 std::list<std::wstring> aContext;
119 std::list<std::wstring>::iterator it = anExprParams.begin();
120 for ( ; it != anExprParams.end(); it++) {
122 ResultParameterPtr aParamRes;
123 // If variable does not exist python interpreter will generate an error. It is OK.
124 // But due to the issue 1479 it should not check the history position of parameters relatively
125 // to feature that contains expression
126 if (!ModelAPI_Tools::findVariable(theIsParameter ? theParameter : FeaturePtr(),
127 *it, aValue, aParamRes, theParameter->document()))
131 theParamsList.push_back(aParamRes);
132 aContext.push_back(*it + L"=" + toString(aValue));
134 myInterp->extendLocalContext(aContext);
135 double result = myInterp->evaluate(theExpression, theError);
136 myInterp->clearLocalContext();
140 void InitializationPlugin_EvalListener::processEvaluationEvent(
141 const std::shared_ptr<Events_Message>& theMessage)
143 std::shared_ptr<ModelAPI_AttributeEvalMessage> aMessage =
144 std::dynamic_pointer_cast<ModelAPI_AttributeEvalMessage>(theMessage);
146 std::list<std::shared_ptr<ModelAPI_ResultParameter> > aParamsList;
147 FeaturePtr aParamFeature =
148 std::dynamic_pointer_cast<ModelAPI_Feature>(aMessage->attribute()->owner());
149 if (aMessage->attribute()->attributeType() == ModelAPI_AttributeInteger::typeId()) {
150 AttributeIntegerPtr anAttribute =
151 std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(aMessage->attribute());
153 int aValue = (int)evaluate(aParamFeature, anAttribute->text(), anError, aParamsList);
154 bool isValid = anError.empty();
156 anAttribute->setCalculatedValue(aValue);
157 anAttribute->setUsedParameters(isValid ?
158 toSet(myInterp->compile(anAttribute->text())) : std::set<std::wstring>());
159 anAttribute->setExpressionInvalid(!isValid);
160 anAttribute->setExpressionError(anAttribute->text().empty() ? "" : anError);
162 if (aMessage->attribute()->attributeType() == ModelAPI_AttributeDouble::typeId()) {
163 AttributeDoublePtr anAttribute =
164 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aMessage->attribute());
166 double aValue = evaluate(aParamFeature, anAttribute->text(), anError, aParamsList);
167 bool isValid = anError.empty();
169 anAttribute->setCalculatedValue(aValue);
170 anAttribute->setUsedParameters(isValid ?
171 toSet(myInterp->compile(anAttribute->text())) : std::set<std::wstring>());
172 anAttribute->setExpressionInvalid(!isValid);
173 anAttribute->setExpressionError(anAttribute->text().empty() ? "" : anError);
175 if (aMessage->attribute()->attributeType() == GeomDataAPI_Point::typeId()) {
176 AttributePointPtr anAttribute =
177 std::dynamic_pointer_cast<GeomDataAPI_Point>(aMessage->attribute());
178 std::wstring aText[] = {
179 anAttribute->textX(),
180 anAttribute->textY(),
183 double aCalculatedValue[] = {
188 for (int i = 0; i < 3; ++i) {
190 double aValue = evaluate(aParamFeature, aText[i], anError, aParamsList);
191 bool isValid = anError.empty();
192 if (isValid) aCalculatedValue[i] = aValue;
193 anAttribute->setUsedParameters(i,
194 isValid ? toSet(myInterp->compile(aText[i])) : std::set<std::wstring>());
195 anAttribute->setExpressionInvalid(i, !isValid);
196 anAttribute->setExpressionError(i, aText[i].empty() ? "" : anError);
198 anAttribute->setCalculatedValue(aCalculatedValue[0],
200 aCalculatedValue[2]);
202 if (aMessage->attribute()->attributeType() == GeomDataAPI_Point2D::typeId()) {
203 AttributePoint2DPtr anAttribute =
204 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aMessage->attribute());
205 std::wstring aText[] = {
206 anAttribute->textX(),
209 double aCalculatedValue[] = {
213 for (int i = 0; i < 2; ++i) {
215 double aValue = evaluate(aParamFeature, aText[i], anError, aParamsList);
216 bool isValid = anError.empty();
217 if (isValid) aCalculatedValue[i] = aValue;
218 anAttribute->setUsedParameters(i,
219 isValid ? toSet(myInterp->compile(aText[i])) : std::set<std::wstring>());
220 anAttribute->setExpressionInvalid(i, !isValid);
221 anAttribute->setExpressionError(i, aText[i].empty() ? "" : anError);
223 anAttribute->setCalculatedValue(aCalculatedValue[0],
224 aCalculatedValue[1]);
228 void InitializationPlugin_EvalListener::initDataModel()
230 myInterp->runString("import salome_iapp;salome_iapp.register_module_in_study(\"Shaper\")");