Salome HOME
Issue #19036: Can't use a parameter with 1e-5 value in the sketcher
[modules/shaper.git] / src / InitializationPlugin / InitializationPlugin_EvalListener.cpp
1 // Copyright (C) 2014-2020  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <pyconfig.h>
21
22 #include <InitializationPlugin_EvalListener.h>
23 #include <ParametersPlugin_Parameter.h>
24 #include <InitializationPlugin_PyInterp.h>
25
26 #include <Events_InfoMessage.h>
27
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>
38
39 #include <GeomDataAPI_Point.h>
40 #include <GeomDataAPI_Point2D.h>
41
42 #include <string>
43 #include <set>
44 #include <sstream>
45
46 //------------------------------------------------------------------------------
47 // Tools
48
49 std::string toStdString(double theValue)
50 {
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;
56   return sstream.str();
57 }
58
59 std::set<std::string> toSet(const std::list<std::string>& theContainer)
60 {
61   return std::set<std::string>(theContainer.begin(), theContainer.end());
62 }
63
64 //------------------------------------------------------------------------------
65
66 InitializationPlugin_EvalListener::InitializationPlugin_EvalListener()
67 {
68   Events_Loop* aLoop = Events_Loop::loop();
69
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);
73
74   myInterp = std::shared_ptr<InitializationPlugin_PyInterp>(new InitializationPlugin_PyInterp());
75   myInterp->initialize();
76 }
77
78 InitializationPlugin_EvalListener::~InitializationPlugin_EvalListener()
79 {
80 }
81
82 void InitializationPlugin_EvalListener::processEvent(
83     const std::shared_ptr<Events_Message>& theMessage)
84 {
85   if (!theMessage.get())
86     return;
87
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();
95     std::string anError;
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()));
103   }
104 }
105
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)
110 {
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++) {
116     double aValue;
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()))
123       continue;
124
125     if (theIsParameter)
126       theParamsList.push_back(aParamRes);
127     aContext.push_back(*it + "=" + toStdString(aValue));
128   }
129   myInterp->extendLocalContext(aContext);
130   double result = myInterp->evaluate(theExpression, theError);
131   myInterp->clearLocalContext();
132   return result;
133 }
134
135 void InitializationPlugin_EvalListener::processEvaluationEvent(
136     const std::shared_ptr<Events_Message>& theMessage)
137 {
138   std::shared_ptr<ModelAPI_AttributeEvalMessage> aMessage =
139       std::dynamic_pointer_cast<ModelAPI_AttributeEvalMessage>(theMessage);
140
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());
147     std::string anError;
148     int aValue = (int)evaluate(aParamFeature, anAttribute->text(), anError, aParamsList);
149     bool isValid = anError.empty();
150     if (isValid)
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);
156   } else
157   if (aMessage->attribute()->attributeType() == ModelAPI_AttributeDouble::typeId()) {
158     AttributeDoublePtr anAttribute =
159         std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aMessage->attribute());
160     std::string anError;
161     double aValue = evaluate(aParamFeature, anAttribute->text(), anError, aParamsList);
162     bool isValid = anError.empty();
163     if (isValid)
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);
169   } else
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(),
176       anAttribute->textZ()
177     };
178     double aCalculatedValue[] = {
179       anAttribute->x(),
180       anAttribute->y(),
181       anAttribute->z()
182     };
183     for (int i = 0; i < 3; ++i) {
184       std::string anError;
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);
192     }
193     anAttribute->setCalculatedValue(aCalculatedValue[0],
194                                     aCalculatedValue[1],
195                                     aCalculatedValue[2]);
196   } else
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(),
202       anAttribute->textY()
203     };
204     double aCalculatedValue[] = {
205       anAttribute->x(),
206       anAttribute->y()
207     };
208     for (int i = 0; i < 2; ++i) {
209       std::string anError;
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);
217     }
218     anAttribute->setCalculatedValue(aCalculatedValue[0],
219                                     aCalculatedValue[1]);
220   }
221 }