Salome HOME
2.17. Improved management of overconstraint situation: VPA correction in symbol prs...
[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                                                   SKETCH_OVERCONSTRAINT_COLOR);
43   theColor.push_back(aColor.Red()*255.);
44   theColor.push_back(aColor.Green()*255.);
45   theColor.push_back(aColor.Blue()*255.);
46 }
47
48 void PartSet_OverconstraintListener::processEvent(
49                                                  const std::shared_ptr<Events_Message>& theMessage)
50 {
51 #ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
52   bool isRepaired = theMessage->eventID() == Events_Loop::eventByName(EVENT_SOLVER_REPAIRED);
53   int aCount = 0;
54
55   std::shared_ptr<ModelAPI_SolverFailedMessage> anErrorMsg =
56                   std::dynamic_pointer_cast<ModelAPI_SolverFailedMessage>(theMessage);
57   QString anInfoStr;
58   if (anErrorMsg.get()) {
59     const std::set<ObjectPtr>& aConflictingObjects = anErrorMsg->objects();
60     aCount = aConflictingObjects.size();
61     anInfoStr = getObjectsInfo(aConflictingObjects);
62   }
63
64   QString aCurrentInfoStr = getObjectsInfo(myConflictingObjects);
65
66   qDebug(QString("PartSet_OverconstraintListener::processEvent: %1,\nobjects count = %2:%3\ncurrent objects count = %4:%5")
67                 .arg(isRepaired ? "REPAIRED" : "FAILED")
68                 .arg(aCount).arg(anInfoStr).arg(myConflictingObjects.size()).arg(aCurrentInfoStr).toStdString().c_str());
69 #endif
70
71   if (theMessage->eventID() == Events_Loop::eventByName(EVENT_SOLVER_FAILED)) {
72     std::shared_ptr<ModelAPI_SolverFailedMessage> anErrorMsg =
73                    std::dynamic_pointer_cast<ModelAPI_SolverFailedMessage>(theMessage);
74     bool anUpdated = false;
75     if (anErrorMsg.get()) {
76       const std::set<ObjectPtr>& aConflictingObjects = anErrorMsg->objects();
77       anUpdated = appendConflictingObjects(aConflictingObjects);
78     }
79     else {
80       // there is a crash in the solver. All objects are invalid
81       //anUpdated = appendConflictingObjects(std::set<ObjectPtr>());
82     }
83   }
84    if (theMessage->eventID() == Events_Loop::eventByName(EVENT_SOLVER_REPAIRED)) {
85     std::shared_ptr<ModelAPI_SolverFailedMessage> anErrorMsg =
86                    std::dynamic_pointer_cast<ModelAPI_SolverFailedMessage>(theMessage);
87     bool anUpdated = false;
88     if (anErrorMsg.get()) {
89       const std::set<ObjectPtr>& aConflictingObjects = anErrorMsg->objects();
90       anUpdated = repairConflictingObjects(aConflictingObjects);
91     }
92     else {
93       // there is no repaired objects, do nothing
94     }
95   }
96
97 #ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
98   aCurrentInfoStr = getObjectsInfo(myConflictingObjects);
99   qDebug(QString("RESULT: current objects count = %1:%2\n")
100                 .arg(myConflictingObjects.size()).arg(aCurrentInfoStr).toStdString().c_str());
101 #endif
102 }
103
104 bool PartSet_OverconstraintListener::appendConflictingObjects(
105                                                   const std::set<ObjectPtr>& theConflictingObjects)
106 {
107   std::set<ObjectPtr> aModifiedObjects;
108   std::vector<int> aColor;
109   getConflictingColor(aColor);
110
111   // set error state for new objects and append them in the internal map of objects
112   std::set<ObjectPtr>::const_iterator anIt = theConflictingObjects.begin(), aLast = theConflictingObjects.end();
113   for (; anIt != aLast; anIt++) {
114     ObjectPtr anObject = *anIt;
115     if (myConflictingObjects.find(anObject) == myConflictingObjects.end()) { // it is not found
116       aModifiedObjects.insert(anObject);
117       myConflictingObjects.insert(anObject);
118     }
119   }
120   bool isUpdated = !aModifiedObjects.empty();
121   if (isUpdated)
122     redisplayObjects(aModifiedObjects);
123
124   return isUpdated;
125 }
126
127 bool PartSet_OverconstraintListener::repairConflictingObjects(
128                                                   const std::set<ObjectPtr>& theConflictingObjects)
129 {
130   std::set<ObjectPtr> aModifiedObjects;
131   // erase error state of absent current objects in the parameter list
132   std::set<ObjectPtr>::const_iterator anIt, aLast;
133   for (anIt = theConflictingObjects.begin(), aLast = theConflictingObjects.end() ; anIt != aLast; anIt++) {
134     ObjectPtr anObject = *anIt;
135     if (theConflictingObjects.find(anObject) != theConflictingObjects.end()) { // it is found
136       myConflictingObjects.erase(anObject);
137
138       aModifiedObjects.insert(anObject);
139     }
140   }
141   bool isUpdated = !aModifiedObjects.empty();
142   if (isUpdated)
143     redisplayObjects(aModifiedObjects);
144
145   return isUpdated;
146 }
147
148 void PartSet_OverconstraintListener::redisplayObjects(
149                                               const std::set<ObjectPtr>& theObjects)
150 {
151 /*static Events_Loop* aLoop = Events_Loop::loop();
152   static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_UPDATED);
153
154   static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
155
156   std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
157   for (; anIt != aLast; anIt++) {
158     aECreator->sendUpdated(*anIt, EVENT_DISP);
159
160     //#ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
161     //  QString anInfoStr = ModuleBase_Tools::objectInfo(*anIt);
162     //  qDebug(QString("PartSet_OverconstraintListener::SEND UPDATED: %1").arg(anInfoStr).toStdString().c_str());
163     //#endif
164   }
165   aLoop->flush(EVENT_DISP);*/
166
167   XGUI_Displayer* aDisplayer = workshop()->displayer();
168   bool aHidden;
169   std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
170   for (; anIt != aLast; anIt++) {
171     ObjectPtr anObject = *anIt;
172     aHidden = !anObject->data() || !anObject->data()->isValid() || 
173                anObject->isDisabled() || (!anObject->isDisplayed());
174     if (!aHidden)
175       aDisplayer->redisplay(anObject, false);
176   }
177   aDisplayer->updateViewer();
178 }
179
180 XGUI_Workshop* PartSet_OverconstraintListener::workshop() const
181 {
182   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
183   return aConnector->workshop();
184 }
185
186 QString PartSet_OverconstraintListener::getObjectsInfo(const std::set<ObjectPtr>& theObjects)
187 {
188   std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(),
189                                       aLast = theObjects.end();
190   QStringList anInfo;
191   for (; anIt != aLast; ++anIt)
192     anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
193
194   return anInfo.join(";\n");
195 }