Salome HOME
Merge branch 'master' into cgt/devCEA
[modules/shaper.git] / src / PartSet / PartSet_OverconstraintListener.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        SketcherPrs_Angle.cpp
4 // Created:     20 August 2015
5 // Author:      Vitaly SMETANNIKOV
6
7 #include "PartSet_OverconstraintListener.h"
8
9 #include "XGUI_ModuleConnector.h"
10 #include "XGUI_Workshop.h"
11 #include "XGUI_Displayer.h"
12
13 #include "SketcherPrs_SymbolPrs.h"
14 #include "SketchPlugin_SketchEntity.h"
15
16 #include "Events_Loop.h"
17
18 #include <GeomAPI_IPresentable.h>
19 #include <ModelAPI_Events.h>
20 #include <ModuleBase_Tools.h>
21
22 #include <QString>
23
24 //#define DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
25
26 PartSet_OverconstraintListener::PartSet_OverconstraintListener(ModuleBase_IWorkshop* theWorkshop)
27 : myWorkshop(theWorkshop)
28 {
29   Events_Loop* aLoop = Events_Loop::loop();
30   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SOLVER_FAILED));
31   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SOLVER_REPAIRED));
32 }
33
34 bool PartSet_OverconstraintListener::isConflictingObject(const ObjectPtr& theObject)
35 {
36   return myConflictingObjects.find(theObject) != myConflictingObjects.end();
37 }
38
39 void PartSet_OverconstraintListener::getConflictingColor(std::vector<int>& theColor)
40 {
41   Quantity_Color aColor = ModuleBase_Tools::color("Visualization", "sketch_overconstraint_color");
42   theColor.push_back(aColor.Red()*255.);
43   theColor.push_back(aColor.Green()*255.);
44   theColor.push_back(aColor.Blue()*255.);
45 }
46
47 void PartSet_OverconstraintListener::processEvent(
48                                                  const std::shared_ptr<Events_Message>& theMessage)
49 {
50 #ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
51   bool isRepaired = theMessage->eventID() == Events_Loop::eventByName(EVENT_SOLVER_REPAIRED);
52   int aCount = 0;
53
54   std::shared_ptr<ModelAPI_SolverFailedMessage> anErrorMsg =
55                   std::dynamic_pointer_cast<ModelAPI_SolverFailedMessage>(theMessage);
56   QString anInfoStr;
57   if (anErrorMsg.get()) {
58     const std::set<ObjectPtr>& aConflictingObjects = anErrorMsg->objects();
59     aCount = aConflictingObjects.size();
60     anInfoStr = getObjectsInfo(aConflictingObjects);
61   }
62
63   QString aCurrentInfoStr = getObjectsInfo(myConflictingObjects);
64
65   QString aMsg("PartSet_OverconstraintListener::processEvent: %1,\nobjects "
66                "count = %2:%3\ncurrent objects count = %4:%5");
67   qDebug(aMsg.arg(isRepaired ? "REPAIRED" : "FAILED")
68              .arg(aCount).arg(anInfoStr).arg(myConflictingObjects.size())
69              .arg(aCurrentInfoStr).toStdString().c_str());
70 #endif
71
72   if (theMessage->eventID() == Events_Loop::eventByName(EVENT_SOLVER_FAILED)) {
73     std::shared_ptr<ModelAPI_SolverFailedMessage> anErrorMsg =
74                    std::dynamic_pointer_cast<ModelAPI_SolverFailedMessage>(theMessage);
75     bool anUpdated = false;
76     if (anErrorMsg.get()) {
77       const std::set<ObjectPtr>& aConflictingObjects = anErrorMsg->objects();
78       anUpdated = appendConflictingObjects(aConflictingObjects);
79     }
80     else {
81       // there is a crash in the solver. All objects are invalid
82       //anUpdated = appendConflictingObjects(std::set<ObjectPtr>());
83     }
84   }
85    if (theMessage->eventID() == Events_Loop::eventByName(EVENT_SOLVER_REPAIRED)) {
86     std::shared_ptr<ModelAPI_SolverFailedMessage> anErrorMsg =
87                    std::dynamic_pointer_cast<ModelAPI_SolverFailedMessage>(theMessage);
88     bool anUpdated = false;
89     if (anErrorMsg.get()) {
90       const std::set<ObjectPtr>& aConflictingObjects = anErrorMsg->objects();
91       anUpdated = repairConflictingObjects(aConflictingObjects);
92     }
93     else {
94       // there is no repaired objects, do nothing
95     }
96   }
97
98 #ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
99   aCurrentInfoStr = getObjectsInfo(myConflictingObjects);
100   qDebug(QString("RESULT: current objects count = %1:%2\n")
101                 .arg(myConflictingObjects.size()).arg(aCurrentInfoStr).toStdString().c_str());
102 #endif
103 }
104
105 bool PartSet_OverconstraintListener::appendConflictingObjects(
106                                                const std::set<ObjectPtr>& theConflictingObjects)
107 {
108   std::set<ObjectPtr> aModifiedObjects;
109   std::vector<int> aColor;
110   getConflictingColor(aColor);
111
112   // set error state for new objects and append them in the internal map of objects
113   std::set<ObjectPtr>::const_iterator
114     anIt = theConflictingObjects.begin(), aLast = theConflictingObjects.end();
115   for (; anIt != aLast; anIt++) {
116     ObjectPtr anObject = *anIt;
117     if (myConflictingObjects.find(anObject) == myConflictingObjects.end()) { // it is not found
118       aModifiedObjects.insert(anObject);
119       myConflictingObjects.insert(anObject);
120     }
121   }
122   bool isUpdated = !aModifiedObjects.empty();
123   if (isUpdated)
124     redisplayObjects(aModifiedObjects);
125
126   return isUpdated;
127 }
128
129 bool PartSet_OverconstraintListener::repairConflictingObjects(
130                                               const std::set<ObjectPtr>& theConflictingObjects)
131 {
132   std::set<ObjectPtr> aModifiedObjects;
133   // erase error state of absent current objects in the parameter list
134   std::set<ObjectPtr>::const_iterator anIt, aLast;
135   for (anIt = theConflictingObjects.begin(), aLast = theConflictingObjects.end();
136        anIt != aLast; anIt++) {
137     ObjectPtr anObject = *anIt;
138     if (theConflictingObjects.find(anObject) != theConflictingObjects.end()) { // it is found
139       myConflictingObjects.erase(anObject);
140
141       aModifiedObjects.insert(anObject);
142     }
143   }
144   bool isUpdated = !aModifiedObjects.empty();
145   if (isUpdated)
146     redisplayObjects(aModifiedObjects);
147
148   return isUpdated;
149 }
150
151 void PartSet_OverconstraintListener::redisplayObjects(
152                                               const std::set<ObjectPtr>& theObjects)
153 {
154   static Events_Loop* aLoop = Events_Loop::loop();
155
156   static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
157   static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
158
159   std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
160   for (; anIt != aLast; anIt++)
161     aECreator->sendUpdated(*anIt, EVENT_DISP);
162
163   aLoop->flush(EVENT_DISP);
164 }
165
166 XGUI_Workshop* PartSet_OverconstraintListener::workshop() const
167 {
168   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
169   return aConnector->workshop();
170 }
171
172 #ifdef _DEBUG
173 QString PartSet_OverconstraintListener::getObjectsInfo(const std::set<ObjectPtr>& theObjects)
174 {
175   std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(),
176                                       aLast = theObjects.end();
177   QStringList anInfo;
178   for (; anIt != aLast; ++anIt)
179     anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
180
181   return anInfo.join(";\n");
182 }
183 #endif