2 * ParametersPlugin_EvalListener.cpp
4 * Created on: Apr 28, 2015
10 #include <ParametersPlugin_EvalListener.h>
11 #include <ParametersPlugin_Parameter.h>
12 #include <ParametersPlugin_PyInterp.h>
14 #include <Events_Error.h>
16 #include <ModelAPI_AttributeString.h>
17 #include <ModelAPI_Document.h>
18 #include <ModelAPI_Events.h>
19 #include <ModelAPI_Session.h>
20 #include <ModelAPI_Tools.h>
22 #include <ModelAPI_AttributeDouble.h>
23 #include <GeomDataAPI_Point.h>
24 #include <GeomDataAPI_Point2D.h>
29 ParametersPlugin_EvalListener::ParametersPlugin_EvalListener()
31 Events_Loop* aLoop = Events_Loop::loop();
32 const Events_ID kEvaluationEvent = ModelAPI_AttributeEvalMessage::eventId();
33 aLoop->registerListener(this, kEvaluationEvent, NULL, true);
34 const Events_ID kObjectRenamedEvent = ModelAPI_ObjectRenamedMessage::eventId();
35 aLoop->registerListener(this, kObjectRenamedEvent, NULL, true);
37 myInterp = std::shared_ptr<ParametersPlugin_PyInterp>(new ParametersPlugin_PyInterp());
38 myInterp->initialize();
41 ParametersPlugin_EvalListener::~ParametersPlugin_EvalListener()
45 void ParametersPlugin_EvalListener::processEvent(
46 const std::shared_ptr<Events_Message>& theMessage)
48 if (!theMessage.get())
51 const Events_ID kEvaluationEvent = ModelAPI_AttributeEvalMessage::eventId();
52 const Events_ID kObjectRenamedEvent = ModelAPI_ObjectRenamedMessage::eventId();
53 if (theMessage->eventID() == kEvaluationEvent) {
54 processEvaluationEvent(theMessage);
55 } else if (theMessage->eventID() == kObjectRenamedEvent) {
56 processObjectRenamedEvent(theMessage);
58 Events_Error::send(std::string("ParametersPlugin python interpreter, unhandled message caught: ")
59 + theMessage->eventID().eventText());
63 double ParametersPlugin_EvalListener::evaluate(const std::string& theExpression,
64 std::string& theError)
66 std::list<std::string> anExprParams = myInterp->compile(theExpression);
67 // find expression's params in the model
68 std::list<std::string> aContext;
69 std::list<std::string>::iterator it = anExprParams.begin();
70 for ( ; it != anExprParams.end(); it++) {
72 ResultParameterPtr aParamRes;
73 if (!ModelAPI_Tools::findVariable(*it, aValue, aParamRes)) continue;
75 std::ostringstream sstream;
77 std::string aParamValue = sstream.str();
78 aContext.push_back(*it + "=" + aParamValue);
80 myInterp->extendLocalContext(aContext);
81 double result = myInterp->evaluate(theExpression, theError);
82 myInterp->clearLocalContext();
86 void ParametersPlugin_EvalListener::processEvaluationEvent(
87 const std::shared_ptr<Events_Message>& theMessage)
89 std::shared_ptr<ModelAPI_AttributeEvalMessage> aMessage =
90 std::dynamic_pointer_cast<ModelAPI_AttributeEvalMessage>(theMessage);
93 AttributeDoublePtr aDoubleAttribute =
94 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aMessage->attribute());
95 if (aDoubleAttribute.get()) {
97 double aValue = evaluate(aDoubleAttribute->text(), anError);
98 if (anError.empty()) {
99 aDoubleAttribute->setCalculatedValue(aValue);
100 aDoubleAttribute->setExpressionInvalid(false);
101 } else { // set feature as invalid-parameter arguments
102 aDoubleAttribute->setExpressionInvalid(true);
107 AttributePointPtr aPointAttribute =
108 std::dynamic_pointer_cast<GeomDataAPI_Point>(aMessage->attribute());
109 if (aPointAttribute.get()) {
110 std::string anError[3];
112 evaluate(aPointAttribute->textX(), anError[0]),
113 evaluate(aPointAttribute->textY(), anError[1]),
114 evaluate(aPointAttribute->textZ(), anError[2])
121 aPointAttribute->setExpressionInvalid(0, !isValid[0]);
122 aPointAttribute->setExpressionInvalid(1, !isValid[1]);
123 aPointAttribute->setExpressionInvalid(2, !isValid[2]);
125 aPointAttribute->setCalculatedValue(
126 isValid[0] ? aValue[0] : aPointAttribute->x(),
127 isValid[1] ? aValue[1] : aPointAttribute->y(),
128 isValid[2] ? aValue[2] : aPointAttribute->z()
133 AttributePoint2DPtr aPoint2DAttribute =
134 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aMessage->attribute());
135 if (aPoint2DAttribute.get()) {
136 std::string anError[2];
138 evaluate(aPoint2DAttribute->textX(), anError[0]),
139 evaluate(aPoint2DAttribute->textY(), anError[1])
145 aPoint2DAttribute->setExpressionInvalid(0, !isValid[0]);
146 aPoint2DAttribute->setExpressionInvalid(1, !isValid[1]);
148 aPoint2DAttribute->setCalculatedValue(
149 isValid[0] ? aValue[0] : aPoint2DAttribute->x(),
150 isValid[1] ? aValue[1] : aPoint2DAttribute->y()
155 std::string ParametersPlugin_EvalListener::renameInPythonExpression(
156 const std::string& theExpression,
157 const std::string& theOldName,
158 const std::string& theNewName)
160 std::string anExpressionString = theExpression;
162 std::list<std::pair<int, int> > aPositions =
163 myInterp->positions(anExpressionString, theOldName);
165 if (aPositions.empty())
166 return anExpressionString;
168 std::map<int, std::list<int> > aLines;
169 std::list<std::pair<int, int> >::const_iterator it = aPositions.begin();
170 for (; it != aPositions.end(); ++it)
171 aLines[it->first].push_back(it->second);
173 // Start renaming from the end to keep indexes if theNewName is longer then theOldName
174 std::map<int, std::list<int> >::const_reverse_iterator ritLine = aLines.rbegin();
175 for (; ritLine != aLines.rend(); ++ritLine) {
176 // Calculate the start of the line (find the aLineNo occurrence of "\n" )
177 int aLineNo = ritLine->first - 1;
178 size_t aLineStart = 0;
179 for (int i = 0; i < aLineNo; ++i)
180 aLineStart = anExpressionString.find("\n", aLineStart) + 1;
182 const std::list<int>& aColOffsets = ritLine->second;
183 std::list<int>::const_reverse_iterator ritOffset = aColOffsets.rbegin();
184 for (; ritOffset != aColOffsets.rend(); ++ritOffset) {
185 int anOffset = *ritOffset;
186 anExpressionString.replace(aLineStart + anOffset, theOldName.size(), theNewName);
190 return anExpressionString;
193 void ParametersPlugin_EvalListener::renameInParameter(
194 std::shared_ptr<ParametersPlugin_Parameter> theParameter,
195 const std::string& theOldName,
196 const std::string& theNewName)
198 std::shared_ptr<ModelAPI_AttributeString> anExpressionAttribute =
199 theParameter->string(ParametersPlugin_Parameter::EXPRESSION_ID());
201 std::string anExpressionString = anExpressionAttribute->value();
202 anExpressionString = renameInPythonExpression(anExpressionString,
205 // Issue #588. No need for reevaluating expression.
206 // Moreover, current history may not contain necessary parameters.
207 anExpressionAttribute->owner()->data()->blockSendAttributeUpdated(true);
208 anExpressionAttribute->setValue(anExpressionString);
209 anExpressionAttribute->owner()->data()->blockSendAttributeUpdated(false);
212 void ParametersPlugin_EvalListener::renameInAttribute(
213 std::shared_ptr<ModelAPI_Attribute> theAttribute,
214 const std::string& theOldName,
215 const std::string& theNewName)
218 AttributeDoublePtr aDoubleAttribute =
219 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
220 if (aDoubleAttribute.get()) {
221 std::string anExpressionString = aDoubleAttribute->text();
222 anExpressionString = renameInPythonExpression(anExpressionString,
225 aDoubleAttribute->setText(anExpressionString);
229 AttributePointPtr aPointAttribute =
230 std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttribute);
231 if (aPointAttribute.get()) {
232 std::string anExpressionString[3] = {
233 aPointAttribute->textX(),
234 aPointAttribute->textY(),
235 aPointAttribute->textZ()
237 for (int i = 0; i < 3; ++i)
238 anExpressionString[i] = renameInPythonExpression(anExpressionString[i],
241 aPointAttribute->setText(anExpressionString[0],
242 anExpressionString[1],
243 anExpressionString[2]);
247 AttributePoint2DPtr aPoint2DAttribute =
248 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
249 if (aPoint2DAttribute.get()) {
250 std::string anExpressionString[2] = {
251 aPoint2DAttribute->textX(),
252 aPoint2DAttribute->textY()
254 for (int i = 0; i < 2; ++i)
255 anExpressionString[i] = renameInPythonExpression(anExpressionString[i],
258 aPoint2DAttribute->setText(anExpressionString[0],
259 anExpressionString[1]);
263 void ParametersPlugin_EvalListener::processObjectRenamedEvent(
264 const std::shared_ptr<Events_Message>& theMessage)
266 std::shared_ptr<ModelAPI_ObjectRenamedMessage> aMessage =
267 std::dynamic_pointer_cast<ModelAPI_ObjectRenamedMessage>(theMessage);
269 if (!aMessage.get() || aMessage->oldName().empty() || aMessage->newName().empty())
272 // check that the renamed object is a result
273 std::shared_ptr<ModelAPI_ResultParameter> aResultParameter =
274 std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aMessage->object());
275 if (!aResultParameter.get())
278 // get parameter feature for the result
279 std::shared_ptr<ModelAPI_Feature> aFeature = aResultParameter->document()->feature(aResultParameter);
280 std::shared_ptr<ParametersPlugin_Parameter> aParameter =
281 std::dynamic_pointer_cast<ParametersPlugin_Parameter>(aFeature);
282 if (!aParameter.get())
285 // rename a parameter attributes
287 //aParameter->string(ParametersPlugin_Parameter::VARIABLE_ID())->setValue(aMessage->newName());
288 //aParameter->execute();
290 aParameter->data()->setName(aMessage->newName());
291 aParameter->string(ParametersPlugin_Parameter::VARIABLE_ID())->setValue(aMessage->newName());
293 // List of documents to process
294 std::list<DocumentPtr> aDocList;
295 SessionPtr aSession = ModelAPI_Session::get();
296 DocumentPtr aDocument = aSession->activeDocument();
297 DocumentPtr aRootDocument = aSession->moduleDocument();
298 aDocList.push_back(aDocument);
299 if (aDocument != aRootDocument) {
300 aDocList.push_back(aRootDocument);
304 for (std::list<DocumentPtr>::const_iterator aDicumentIt = aDocList.begin();
305 aDicumentIt != aDocList.end(); ++aDicumentIt) {
306 const DocumentPtr& aDocument = *aDicumentIt;
307 std::list<FeaturePtr> aFeatures = aDocument->allFeatures();
308 std::list<FeaturePtr>::iterator aFeatureIt = aFeatures.begin();
309 for (; aFeatureIt != aFeatures.end(); ++aFeatureIt) {
310 const FeaturePtr& aFeature = *aFeatureIt;
312 // If Parameter feature then rename its expression
313 std::shared_ptr<ParametersPlugin_Parameter> aParameter =
314 std::dynamic_pointer_cast<ParametersPlugin_Parameter>(aFeature);
315 if (aParameter.get()) {
317 renameInParameter(aParameter, aMessage->oldName(), aMessage->newName());
321 // Find all attributes
322 std::list<AttributePtr> anAttributes = aFeature->data()->attributes(std::string());
323 std::list<AttributePtr>::const_iterator anAttributeIt = anAttributes.begin();
324 for (; anAttributeIt != anAttributes.end(); ++anAttributeIt) {
325 const AttributePtr& anAttribute = *anAttributeIt;
328 renameInAttribute(anAttribute, aMessage->oldName(), aMessage->newName());