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->setValue(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->setValue(
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->setValue(
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 anExpressionAttribute->setValue(anExpressionString);
208 void ParametersPlugin_EvalListener::renameInAttribute(
209 std::shared_ptr<ModelAPI_Attribute> theAttribute,
210 const std::string& theOldName,
211 const std::string& theNewName)
214 AttributeDoublePtr aDoubleAttribute =
215 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
216 if (aDoubleAttribute.get()) {
217 std::string anExpressionString = aDoubleAttribute->text();
218 anExpressionString = renameInPythonExpression(anExpressionString,
221 aDoubleAttribute->setText(anExpressionString);
225 AttributePointPtr aPointAttribute =
226 std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttribute);
227 if (aPointAttribute.get()) {
228 std::string anExpressionString[3] = {
229 aPointAttribute->textX(),
230 aPointAttribute->textY(),
231 aPointAttribute->textZ()
233 for (int i = 0; i < 3; ++i)
234 anExpressionString[i] = renameInPythonExpression(anExpressionString[i],
237 aPointAttribute->setText(anExpressionString[0],
238 anExpressionString[1],
239 anExpressionString[2]);
243 AttributePoint2DPtr aPoint2DAttribute =
244 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
245 if (aPoint2DAttribute.get()) {
246 std::string anExpressionString[2] = {
247 aPoint2DAttribute->textX(),
248 aPoint2DAttribute->textY()
250 for (int i = 0; i < 2; ++i)
251 anExpressionString[i] = renameInPythonExpression(anExpressionString[i],
254 aPoint2DAttribute->setText(anExpressionString[0],
255 anExpressionString[1]);
259 void ParametersPlugin_EvalListener::processObjectRenamedEvent(
260 const std::shared_ptr<Events_Message>& theMessage)
262 std::shared_ptr<ModelAPI_ObjectRenamedMessage> aMessage =
263 std::dynamic_pointer_cast<ModelAPI_ObjectRenamedMessage>(theMessage);
265 if (!aMessage.get() || aMessage->oldName().empty() || aMessage->newName().empty())
268 // check that the renamed object is a result
269 std::shared_ptr<ModelAPI_ResultParameter> aResultParameter =
270 std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aMessage->object());
271 if (!aResultParameter.get())
274 // get parameter feature for the result
275 std::shared_ptr<ModelAPI_Feature> aFeature = aResultParameter->document()->feature(aResultParameter);
276 std::shared_ptr<ParametersPlugin_Parameter> aParameter =
277 std::dynamic_pointer_cast<ParametersPlugin_Parameter>(aFeature);
278 if (!aParameter.get())
281 // rename a parameter attributes
283 //aParameter->string(ParametersPlugin_Parameter::VARIABLE_ID())->setValue(aMessage->newName());
284 //aParameter->execute();
286 aParameter->data()->setName(aMessage->newName());
287 aParameter->string(ParametersPlugin_Parameter::VARIABLE_ID())->setValue(aMessage->newName());
289 // List of documents to process
290 std::list<DocumentPtr> aDocList;
291 SessionPtr aSession = ModelAPI_Session::get();
292 DocumentPtr aDocument = aSession->activeDocument();
293 DocumentPtr aRootDocument = aSession->moduleDocument();
294 aDocList.push_back(aDocument);
295 if (aDocument != aRootDocument) {
296 aDocList.push_back(aRootDocument);
300 for (std::list<DocumentPtr>::const_iterator aDicumentIt = aDocList.begin();
301 aDicumentIt != aDocList.end(); ++aDicumentIt) {
302 const DocumentPtr& aDocument = *aDicumentIt;
303 std::list<FeaturePtr> aFeatures = aDocument->allFeatures();
304 std::list<FeaturePtr>::iterator aFeatureIt = aFeatures.begin();
305 for (; aFeatureIt != aFeatures.end(); ++aFeatureIt) {
306 const FeaturePtr& aFeature = *aFeatureIt;
308 // If Parameter feature then rename its expression
309 std::shared_ptr<ParametersPlugin_Parameter> aParameter =
310 std::dynamic_pointer_cast<ParametersPlugin_Parameter>(aFeature);
311 if (aParameter.get()) {
313 renameInParameter(aParameter, aMessage->oldName(), aMessage->newName());
317 // Find all attributes
318 std::list<AttributePtr> anAttributes = aFeature->data()->attributes(std::string());
319 std::list<AttributePtr>::const_iterator anAttributeIt = anAttributes.begin();
320 for (; anAttributeIt != anAttributes.end(); ++anAttributeIt) {
321 const AttributePtr& anAttribute = *anAttributeIt;
324 renameInAttribute(anAttribute, aMessage->oldName(), aMessage->newName());