1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: Model_Update.cxx
4 // Created: 25 Jun 2014
5 // Author: Mikhail PONIKAROV
7 #include <Model_Update.h>
8 #include <Model_Document.h>
9 #include <Model_Data.h>
10 #include <Model_Objects.h>
11 #include <ModelAPI_Feature.h>
12 #include <ModelAPI_Data.h>
13 #include <ModelAPI_Document.h>
14 #include <ModelAPI_Events.h>
15 #include <ModelAPI_AttributeReference.h>
16 #include <ModelAPI_AttributeRefList.h>
17 #include <ModelAPI_AttributeRefAttr.h>
18 #include <ModelAPI_AttributeSelection.h>
19 #include <ModelAPI_AttributeSelectionList.h>
20 #include <ModelAPI_Result.h>
21 #include <ModelAPI_ResultPart.h>
22 #include <ModelAPI_Validator.h>
23 #include <ModelAPI_CompositeFeature.h>
24 #include <ModelAPI_Session.h>
25 #include <ModelAPI_Tools.h>
26 #include <GeomDataAPI_Point.h>
27 #include <GeomDataAPI_Point2D.h>
28 #include <Events_Loop.h>
29 #include <Events_LongOp.h>
30 #include <Events_Error.h>
31 #include <Config_PropManager.h>
35 Model_Update MY_UPDATER_INSTANCE; /// the only one instance initialized on load of the library
37 Model_Update::Model_Update()
39 Events_Loop* aLoop = Events_Loop::loop();
40 static const Events_ID kChangedEvent = aLoop->eventByName("PreferenceChanged");
41 aLoop->registerListener(this, kChangedEvent);
42 static const Events_ID kRebuildEvent = aLoop->eventByName("Rebuild");
43 aLoop->registerListener(this, kRebuildEvent);
44 static const Events_ID kCreatedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED);
45 aLoop->registerListener(this, kCreatedEvent);
46 static const Events_ID kUpdatedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED);
47 aLoop->registerListener(this, kUpdatedEvent);
48 static const Events_ID kMovedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_MOVED);
49 aLoop->registerListener(this, kMovedEvent);
50 static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
51 aLoop->registerListener(this, kOpFinishEvent);
52 static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
53 aLoop->registerListener(this, kOpAbortEvent);
54 static const Events_ID kOpStartEvent = aLoop->eventByName("StartOperation");
55 aLoop->registerListener(this, kOpStartEvent);
57 Config_PropManager::registerProp("Model update", "automatic_rebuild", "Rebuild immediately",
58 Config_Prop::Boolean, "false");
60 Config_PropManager::findProp("Model update", "automatic_rebuild")->value() == "true";
61 myIsParamUpdated = false;
64 void Model_Update::processEvent(const std::shared_ptr<Events_Message>& theMessage)
66 static Events_Loop* aLoop = Events_Loop::loop();
67 static const Events_ID kChangedEvent = aLoop->eventByName("PreferenceChanged");
68 static const Events_ID kRebuildEvent = aLoop->eventByName("Rebuild");
69 static const Events_ID kCreatedEvent = aLoop->eventByName(EVENT_OBJECT_CREATED);
70 static const Events_ID kUpdatedEvent = aLoop->eventByName(EVENT_OBJECT_UPDATED);
71 static const Events_ID kMovedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_MOVED);
72 static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
73 static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
74 static const Events_ID kOpStartEvent = aLoop->eventByName("StartOperation");
75 bool isOperationChanged = false;
76 if (theMessage->eventID() == kChangedEvent) { // automatic and manual rebuild flag is changed
78 Config_PropManager::findProp("Model update", "automatic_rebuild")->value() == "true";
79 if (aPropVal != myIsAutomatic) { // something is changed
80 myIsAutomatic = aPropVal;
81 if (myIsAutomatic) // higher level of automatization => to rebuild
82 processOperation(false);
84 } else if (theMessage->eventID() == kRebuildEvent) { // the rebuild command
85 processOperation(true);
86 } else if (theMessage->eventID() == kCreatedEvent || theMessage->eventID() == kUpdatedEvent ||
87 theMessage->eventID() == kMovedEvent) {
88 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
89 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
90 const std::set<ObjectPtr>& anObjs = aMsg->objects();
91 std::set<ObjectPtr>::const_iterator anObjIter = anObjs.cbegin();
92 bool isOnlyResults = true; // check that only results were changed: only redisplay is needed
93 for(; anObjIter != anObjs.cend(); anObjIter++) {
94 if (!std::dynamic_pointer_cast<ModelAPI_Result>(*anObjIter).get()) {
95 isOnlyResults = false;
97 if ((*anObjIter)->groupName() == ModelAPI_ResultParameter::group()) {
98 myIsParamUpdated = true;
100 // created objects are always must be up to date (python box feature)
101 // and updated not in internal uptation chain
102 if (theMessage->eventID() == kCreatedEvent) {
103 myJustCreated.insert(*anObjIter);
104 } else if (myJustCreated.find(*anObjIter) == myJustCreated.end()) { // moved and updated
105 myJustUpdated.insert(*anObjIter);
108 // this event is for solver update, not here, do not react immideately
109 if (!isOnlyResults && !(theMessage->eventID() == kMovedEvent))
110 processOperation(false);
111 } else if (theMessage->eventID() == kOpStartEvent) {
112 // we don't need the update only on operation start (caused problems in PartSet_Listener::processEvent)
113 isOperationChanged = true;
114 } else if (theMessage->eventID() == kOpFinishEvent || theMessage->eventID() == kOpAbortEvent) {
115 processOperation(true, theMessage->eventID() == kOpFinishEvent);
116 isOperationChanged = true;
118 if (isOperationChanged) {
119 // remove all macros before clearing all created and execute all not-previewed
120 std::set<ObjectPtr>::iterator aCreatedIter = myJustCreated.begin();
121 std::set<ObjectPtr>::iterator anUpdatedIter = myJustUpdated.begin();
122 for(; aCreatedIter != myJustCreated.end() || anUpdatedIter != myJustUpdated.end();
123 aCreatedIter == myJustCreated.end() ? anUpdatedIter++ : aCreatedIter++) {
124 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*
125 (aCreatedIter == myJustCreated.end() ? anUpdatedIter : aCreatedIter));
126 if (aFeature.get()) {
127 // execute not-previewed feature on "apply"
128 if (!aFeature->isPreviewNeeded() && (myJustCreated.find(aFeature) != myJustCreated.end() ||
129 myJustUpdated.find(aFeature) != myJustUpdated.end())) {
130 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
131 if (aFactory->validate(aFeature)) {
132 executeFeature(aFeature);
135 // remove macro on apply
136 if (aFeature->isMacro()) {
137 aFeature->document()->removeFeature(aFeature);
141 myJustCreated.clear();
142 myJustUpdated.clear();
143 myIsParamUpdated = false;
147 void Model_Update::processOperation(const bool theTotalUpdate, const bool theFinish)
150 // the hardcode (DBC asked): hide the sketch referenced by extrusion on apply
151 std::set<std::shared_ptr<ModelAPI_Object> >::iterator aFIter;
152 for(aFIter = myJustCreated.begin(); aFIter != myJustCreated.end(); aFIter++)
154 FeaturePtr aF = std::dynamic_pointer_cast<ModelAPI_Feature>(*aFIter);
155 if (aF && aF->data()->isValid() && aF->getKind() == "Extrusion") {
156 AttributeSelectionListPtr aBase = aF->selectionList("base");
158 for(int a = aBase->size() - 1; a >= 0; a--) {
159 ResultPtr aSketchRes = aBase->value(a)->context();
161 aSketchRes->setDisplayed(false);
168 // perform update of everything if needed
172 bool isAutomaticChanged = false;
174 if (theTotalUpdate && !myIsAutomatic) { // Apply button now works as "Rebuild"
175 isAutomaticChanged = true;
176 myIsAutomatic = true;
179 updateInDoc(ModelAPI_Session::get()->moduleDocument());
181 if (isAutomaticChanged) myIsAutomatic = false;
182 myIsExecuted = false;
184 // flush to update display
185 static Events_Loop* aLoop = Events_Loop::loop();
186 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
187 aLoop->flush(EVENT_DISP);
191 void Model_Update::updateInDoc(std::shared_ptr<ModelAPI_Document> theDoc)
193 std::set<FeaturePtr> alreadyProcessed; // features that are processed before others
194 // all features one by one
195 Model_Objects* anObjs = std::dynamic_pointer_cast<Model_Document>(theDoc)->objects();
197 FeaturePtr aFeatureIter = anObjs->firstFeature();
198 for (; aFeatureIter.get(); aFeatureIter = anObjs->nextFeature(aFeatureIter)) {
199 if (!aFeatureIter->data()->isValid()) // this may be on close of the document
201 if (aFeatureIter && alreadyProcessed.find(aFeatureIter) == alreadyProcessed.end()) {
202 // update selection and parameters attributes first, before sub-features analysis (sketch plane)
203 updateArguments(aFeatureIter);
204 // composite feature must be executed after sub-features execution
205 CompositeFeaturePtr aComposite =
206 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeatureIter);
208 // number of subs can be changed in execution: like fillet
209 for(int a = 0; a < aComposite->numberOfSubs(); a++) {
210 FeaturePtr aSub = aComposite->subFeature(a);
211 updateArguments(aSub);
213 alreadyProcessed.insert(aSub);
217 updateFeature(aFeatureIter);
218 // update the document results recursively
219 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeatureIter->results();
220 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
221 for (; aRIter != aResults.cend(); aRIter++) {
222 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aRIter);
224 if (!aPart->isDisabled() && aPart->partDoc().get()) {
225 updateInDoc(aPart->partDoc());
233 void Model_Update::redisplayWithResults(FeaturePtr theFeature, const ModelAPI_ExecState theState)
235 // make updated and redisplay all results
236 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
237 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
238 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
239 for (; aRIter != aResults.cend(); aRIter++) {
240 std::shared_ptr<ModelAPI_Result> aRes = *aRIter;
241 aRes->data()->execState(theState);
242 myJustUpdated.insert(aRes);
243 ModelAPI_EventCreator::get()->sendUpdated(aRes, EVENT_DISP);
245 // to redisplay "presentable" feature (for ex. distance constraint)
246 ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
247 theFeature->data()->execState(theState);
250 /// Updates the state by the referenced object: if something bad with it, set state for this one
251 ModelAPI_ExecState stateByReference(ObjectPtr theTarget, const ModelAPI_ExecState theCurrent)
254 ModelAPI_ExecState aRefState = theTarget->data()->execState();
255 if (aRefState == ModelAPI_StateMustBeUpdated) {
256 return ModelAPI_StateMustBeUpdated;
257 } else if (aRefState != ModelAPI_StateDone) {
258 return ModelAPI_StateInvalidArgument;
264 void Model_Update::updateArguments(FeaturePtr theFeature) {
265 // perform this method also for disabled features: to make "not done" state for
266 // featuers referenced to the active and modified features
268 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
270 bool aJustUpdated = false;
271 ModelAPI_ExecState aState = theFeature->data()->execState();
272 if (aState == ModelAPI_StateInvalidArgument) // a chance to be corrected
273 aState = ModelAPI_StateMustBeUpdated;
274 // check the parameters state
276 std::list<AttributePtr> aDoubles =
277 theFeature->data()->attributes(ModelAPI_AttributeDouble::typeId());
278 std::list<AttributePtr>::iterator aDoubleIter = aDoubles.begin();
279 for(; aDoubleIter != aDoubles.end(); aDoubleIter++) {
280 AttributeDoublePtr aDouble =
281 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(*aDoubleIter);
282 if (aDouble.get() && !aDouble->text().empty()) {
283 if (myIsParamUpdated) {
284 ModelAPI_AttributeEvalMessage::send(aDouble, this);
286 if (aDouble->expressionInvalid()) {
287 aState = ModelAPI_StateInvalidArgument;
293 std::list<AttributePtr> anAttributes =
294 theFeature->data()->attributes(GeomDataAPI_Point::typeId());
295 std::list<AttributePtr>::iterator anIter = anAttributes.begin();
296 for(; anIter != anAttributes.end(); anIter++) {
297 AttributePointPtr aPointAttribute =
298 std::dynamic_pointer_cast<GeomDataAPI_Point>(*anIter);
299 if (aPointAttribute.get()) {
300 if (myIsParamUpdated) {
301 ModelAPI_AttributeEvalMessage::send(aPointAttribute, this);
303 if ((!aPointAttribute->textX().empty() && aPointAttribute->expressionInvalid(0)) ||
304 (!aPointAttribute->textY().empty() && aPointAttribute->expressionInvalid(1)) ||
305 (!aPointAttribute->textZ().empty() && aPointAttribute->expressionInvalid(2)))
306 aState = ModelAPI_StateInvalidArgument;
312 std::list<AttributePtr> anAttributes =
313 theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
314 std::list<AttributePtr>::iterator anIter = anAttributes.begin();
315 for(; anIter != anAttributes.end(); anIter++) {
316 AttributePoint2DPtr aPoint2DAttribute =
317 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIter);
318 if (aPoint2DAttribute.get()) {
319 if (myIsParamUpdated) {
320 ModelAPI_AttributeEvalMessage::send(aPoint2DAttribute, this);
322 if ((!aPoint2DAttribute->textX().empty() && aPoint2DAttribute->expressionInvalid(0)) ||
323 (!aPoint2DAttribute->textY().empty() && aPoint2DAttribute->expressionInvalid(1)))
324 aState = ModelAPI_StateInvalidArgument;
329 //if (aState == ModelAPI_StateDone) {// all referenced objects are ready to be used
330 //std::cout<<"Execute feature "<<theFeature->getKind()<<std::endl;
331 // before execution update the selection attributes if any
332 list<AttributePtr> aRefs =
333 theFeature->data()->attributes(ModelAPI_AttributeSelection::typeId());
334 list<AttributePtr>::iterator aRefsIter = aRefs.begin();
335 for (; aRefsIter != aRefs.end(); aRefsIter++) {
336 std::shared_ptr<ModelAPI_AttributeSelection> aSel =
337 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*aRefsIter);
338 ObjectPtr aContext = aSel->context();
339 // update argument onlt if the referenced object is changed
340 if (aContext.get() && isUpdated(aContext) && !aContext->isDisabled()) {
341 if (aState == ModelAPI_StateDone)
342 aState = ModelAPI_StateMustBeUpdated;
343 if (!aSel->update()) { // this must be done on execution since it may be long operation
344 if (!aFactory->isNotObligatory(theFeature->getKind(), theFeature->data()->id(aSel)) &&
345 aFactory->isCase(theFeature, theFeature->data()->id(aSel)))
346 aState = ModelAPI_StateInvalidArgument;
350 aRefs = theFeature->data()->attributes(ModelAPI_AttributeSelectionList::typeId());
351 for (aRefsIter = aRefs.begin(); aRefsIter != aRefs.end(); aRefsIter++) {
352 std::shared_ptr<ModelAPI_AttributeSelectionList> aSel =
353 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(*aRefsIter);
354 for(int a = aSel->size() - 1; a >= 0; a--) {
355 std::shared_ptr<ModelAPI_AttributeSelection> aSelAttr =
356 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(aSel->value(a));
358 ObjectPtr aContext = aSelAttr->context();
359 // update argument onlt if the referenced object is changed
360 if (aContext.get() && isUpdated(aContext) && !aContext->isDisabled()) {
361 if (aState == ModelAPI_StateDone)
362 aState = ModelAPI_StateMustBeUpdated;
363 if (!aSelAttr->update()) {
364 if (!aFactory->isNotObligatory(
365 theFeature->getKind(), theFeature->data()->id(aSel)) &&
366 aFactory->isCase(theFeature, theFeature->data()->id(aSel)))
367 aState = ModelAPI_StateInvalidArgument;
374 if (aJustUpdated && myJustCreated.find(theFeature) == myJustCreated.end())
375 myJustUpdated.insert(theFeature);
376 if (aState != ModelAPI_StateDone)
377 theFeature->data()->execState(aState);
380 void Model_Update::updateFeature(FeaturePtr theFeature)
382 // check all features this feature depended on (recursive call of updateFeature)
383 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
385 if (theFeature->data()->execState() == ModelAPI_StateInvalidArgument)
387 bool aJustUpdated = false;
390 if (myIsAutomatic && theFeature->data()->execState() == ModelAPI_StateMustBeUpdated)
393 ModelAPI_ExecState aState = ModelAPI_StateDone;
395 // check all references: if referenced objects are updated, this object also must be updated
396 // also check state of referenced objects: if they are not ready, inherit corresponding state
397 std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
398 std::shared_ptr<Model_Data> aData =
399 std::dynamic_pointer_cast<Model_Data>(theFeature->data());
400 aData->referencesToObjects(aRefs);
401 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRef = aRefs.begin();
402 for(; aRef != aRefs.end(); aRef++) {
403 std::list<ObjectPtr>::iterator aRefObj = aRef->second.begin();
404 for(; aRefObj != aRef->second.end(); aRefObj++) {
405 // if reference is null, it may mean that this reference is to other document
406 // the does not supported by RefList: peremeters may be recomputed
407 if (!aRefObj->get() && theFeature->firstResult().get() &&
408 theFeature->firstResult()->groupName() == ModelAPI_ResultParameter::group()) {
410 } else if (myJustCreated.find(*aRefObj) != myJustCreated.end() ||
411 myJustUpdated.find(*aRefObj) != myJustUpdated.end()) {
414 aState = stateByReference(*aRefObj, aState);
418 // some arguments were changed, so, this feature must be updated
419 if (myJustCreated.find(theFeature) != myJustCreated.end()) {
423 if (myJustUpdated.find(theFeature) == myJustUpdated.end())
424 myJustUpdated.insert(theFeature);
426 aJustUpdated = myJustUpdated.find(theFeature) != myJustUpdated.end();
429 //std::cout<<"Update feature "<<theFeature->getKind()<<" must be updated = "<<aMustbeUpdated<<std::endl;
430 // execute feature if it must be updated
432 if (theFeature->isDisabled()) { // do not execute the disabled feature
433 // the disabled features must be updated after enabling anyway if their arguments were changed
434 if (theFeature->data()->execState() == ModelAPI_StateDone &&
435 aState == ModelAPI_StateMustBeUpdated)
436 theFeature->data()->execState(ModelAPI_StateMustBeUpdated);
438 if (theFeature->isPreviewNeeded()) {
439 if (std::dynamic_pointer_cast<Model_Document>(theFeature->document())->executeFeatures() ||
440 !theFeature->isPersistentResult()) {
441 if (aFactory->validate(theFeature)) {
442 FeaturePtr aCurrent = theFeature->document()->currentFeature(false);
443 if (myIsAutomatic || !theFeature->isPersistentResult() /* execute quick, not persistent results */
444 || (isUpdated(theFeature) &&
445 (theFeature == aCurrent ||
446 (aCurrent.get() && aCurrent->isMacro())))) // currently edited
448 if (aState == ModelAPI_StateDone || aState == ModelAPI_StateMustBeUpdated) {
449 executeFeature(theFeature);
451 } else { // must be updatet, but not updated yet
452 theFeature->data()->execState(ModelAPI_StateMustBeUpdated);
453 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
454 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
455 for (; aRIter != aResults.cend(); aRIter++) {
456 std::shared_ptr<ModelAPI_Result> aRes = *aRIter;
457 aRes->data()->execState(ModelAPI_StateMustBeUpdated);
461 theFeature->eraseResults();
462 redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
464 } else { // for automatically updated features (on abort, etc) it is necessary to redisplay anyway
465 redisplayWithResults(theFeature, ModelAPI_StateNothing);
467 } else { // preview is not needed => make state Done
468 if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated)
469 theFeature->data()->execState(ModelAPI_StateDone);
476 void Model_Update::executeFeature(FeaturePtr theFeature)
478 // execute in try-catch to avoid internal problems of the feature
479 ModelAPI_ExecState aState = ModelAPI_StateDone;
480 theFeature->data()->execState(ModelAPI_StateDone);
482 theFeature->execute();
483 if (theFeature->data()->execState() != ModelAPI_StateDone) {
484 aState = ModelAPI_StateExecFailed;
486 aState = ModelAPI_StateDone;
489 aState = ModelAPI_StateExecFailed;
491 "Feature " + theFeature->getKind() + " has failed during the execution");
493 if (aState != ModelAPI_StateDone) {
494 theFeature->eraseResults();
496 redisplayWithResults(theFeature, aState);
499 bool Model_Update::isUpdated(const ObjectPtr& theObj)
501 return myJustCreated.find(theObj) != myJustCreated.end() ||
502 myJustUpdated.find(theObj) != myJustUpdated.end();