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 <ModelAPI_Tools.h>
8 #include <ModelAPI_AttributeString.h>
9
10 #include "PartSet_OverconstraintListener.h"
11 #include <PartSet_Module.h>
12 #include <PartSet_SketcherMgr.h>
13 #include <PartSet_SketcherReentrantMgr.h>
14
15 #include "XGUI_ModuleConnector.h"
16 #include "XGUI_Workshop.h"
17 #include "XGUI_Displayer.h"
18 #include "XGUI_CustomPrs.h"
19
20 #include "SketcherPrs_SymbolPrs.h"
21 #include "SketchPlugin_SketchEntity.h"
22 #include "SketchPlugin_MacroArcReentrantMessage.h"
23 #include "SketchPlugin_Sketch.h"
24
25 #include "Events_Loop.h"
26
27 #include <GeomAPI_IPresentable.h>
28 #include <ModelAPI_Events.h>
29 #include <ModelAPI_EventReentrantMessage.h>
30 #include <ModuleBase_Tools.h>
31
32 #include <QString>
33
34 //#define DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
35
36 PartSet_OverconstraintListener::PartSet_OverconstraintListener(ModuleBase_IWorkshop* theWorkshop)
37 : myWorkshop(theWorkshop), myIsActive(false), myIsFullyConstrained(false)
38 {
39   Events_Loop* aLoop = Events_Loop::loop();
40   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SOLVER_FAILED));
41   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SOLVER_REPAIRED));
42
43   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SKETCH_UNDER_CONSTRAINED));
44   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SKETCH_FULLY_CONSTRAINED));
45
46   aLoop->registerListener(this, ModelAPI_EventReentrantMessage::eventId());
47   aLoop->registerListener(this, SketchPlugin_MacroArcReentrantMessage::eventId());
48 }
49
50 void PartSet_OverconstraintListener::setActive(const bool& theActive)
51 {
52   if (myIsActive == theActive)
53     return;
54
55   myIsActive = theActive;
56   myIsFullyConstrained = false; /// returned to default state, no custom color for it
57
58   if (myIsActive) {
59     PartSet_Module* aModule = module();
60     CompositeFeaturePtr aSketch = aModule->sketchMgr()->activeSketch();
61     if (aSketch.get()) {
62       std::string aDOFMessage = aSketch->string(SketchPlugin_Sketch::SOLVER_DOF())->value();
63       myIsFullyConstrained = QString(aDOFMessage.c_str()).contains("DoF = 0");
64     }
65   }
66 }
67
68 void PartSet_OverconstraintListener::getCustomColor(const ObjectPtr& theObject,
69                                                     std::vector<int>& theColor)
70 {
71   if (!myIsActive)
72     return;
73
74   if (myConflictingObjects.find(theObject) != myConflictingObjects.end()) {
75     theColor = Config_PropManager::color("Visualization", "sketch_overconstraint_color");
76   }
77   if (myIsFullyConstrained) {
78     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
79     // only entity features has custom color when sketch is fully constrained
80     if (aFeature.get() && PartSet_SketcherMgr::isEntity(aFeature->getKind())) {
81       PartSet_Module* aModule = module();
82       CompositeFeaturePtr aSketch = aModule->sketchMgr()->activeSketch();
83       // the given object is sub feature of the current sketch(created or edited)
84       if (ModelAPI_Tools::compositeOwner(aFeature) == aSketch)
85         theColor = Config_PropManager::color("Visualization", "sketch_fully_constrained_color");
86     }
87   }
88 }
89
90 void PartSet_OverconstraintListener::processEvent(
91                                                  const std::shared_ptr<Events_Message>& theMessage)
92 {
93   if (!myIsActive)
94     return;
95
96 #ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
97   bool isRepaired = theMessage->eventID() == Events_Loop::eventByName(EVENT_SOLVER_REPAIRED);
98   int aCount = 0;
99
100   std::shared_ptr<ModelAPI_SolverFailedMessage> anErrorMsg =
101                   std::dynamic_pointer_cast<ModelAPI_SolverFailedMessage>(theMessage);
102   QString anInfoStr;
103   if (anErrorMsg.get()) {
104     const std::set<ObjectPtr>& aConflictingObjects = anErrorMsg->objects();
105     aCount = aConflictingObjects.size();
106     anInfoStr = getObjectsInfo(aConflictingObjects);
107   }
108
109   QString aCurrentInfoStr = getObjectsInfo(myConflictingObjects);
110
111   QString aMsg("PartSet_OverconstraintListener::processEvent: %1,\nobjects "
112                "count = %2:%3\ncurrent objects count = %4:%5");
113   qDebug(aMsg.arg(isRepaired ? "REPAIRED" : "FAILED")
114              .arg(aCount).arg(anInfoStr).arg(myConflictingObjects.size())
115              .arg(aCurrentInfoStr).toStdString().c_str());
116 #endif
117
118   Events_ID anEventID = theMessage->eventID();
119   if (anEventID == Events_Loop::eventByName(EVENT_SOLVER_FAILED) ||
120       anEventID == Events_Loop::eventByName(EVENT_SOLVER_REPAIRED)) {
121     std::shared_ptr<ModelAPI_SolverFailedMessage> anErrorMsg =
122                    std::dynamic_pointer_cast<ModelAPI_SolverFailedMessage>(theMessage);
123     bool anUpdated = false;
124     if (anErrorMsg.get()) {
125       const std::set<ObjectPtr>& aConflictingObjects = anErrorMsg->objects();
126       if (anEventID == Events_Loop::eventByName(EVENT_SOLVER_FAILED))
127         anUpdated = appendConflictingObjects(aConflictingObjects);
128       else
129         anUpdated = repairConflictingObjects(aConflictingObjects);
130     }
131   }
132   else if (anEventID == Events_Loop::eventByName(EVENT_SKETCH_UNDER_CONSTRAINED) ||
133            anEventID == Events_Loop::eventByName(EVENT_SKETCH_FULLY_CONSTRAINED)) {
134     bool aPrevFullyConstrained = myIsFullyConstrained;
135     myIsFullyConstrained = anEventID == Events_Loop::eventByName(EVENT_SKETCH_FULLY_CONSTRAINED);
136
137     if (aPrevFullyConstrained != myIsFullyConstrained) {
138       std::set<ObjectPtr> aModifiedObjects;
139       PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
140       CompositeFeaturePtr aSketch = aModule->sketchMgr()->activeSketch();
141       if (aSketch.get()) {
142         for (int i = 0; i < aSketch->numberOfSubs(); i++) {
143           FeaturePtr aFeature = aSketch->subFeature(i);
144           aModifiedObjects.insert(aFeature); // is necessary to redisplay presentations
145           std::list<ResultPtr> aResults = aFeature->results();
146           for (std::list<ResultPtr>::const_iterator aIt = aResults.begin();
147                aIt != aResults.end(); ++aIt) {
148             aModifiedObjects.insert(*aIt);
149           }
150         }
151         redisplayObjects(aModifiedObjects);
152       }
153     }
154   }
155   else if (anEventID == ModelAPI_EventReentrantMessage::eventId() ||
156            anEventID == SketchPlugin_MacroArcReentrantMessage::eventId()) {
157     PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
158     PartSet_SketcherReentrantMgr* aReentrantMgr = aModule->sketchReentranceMgr();
159     aReentrantMgr->setReentrantMessage(theMessage);
160   }
161
162 #ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
163   aCurrentInfoStr = getObjectsInfo(myConflictingObjects);
164   qDebug(QString("RESULT: current objects count = %1:%2\n")
165                 .arg(myConflictingObjects.size()).arg(aCurrentInfoStr).toStdString().c_str());
166 #endif
167 }
168
169 bool PartSet_OverconstraintListener::appendConflictingObjects(
170                                                const std::set<ObjectPtr>& theConflictingObjects)
171 {
172   std::set<ObjectPtr> aModifiedObjects;
173
174   // set error state for new objects and append them in the internal map of objects
175   std::set<ObjectPtr>::const_iterator
176     anIt = theConflictingObjects.begin(), aLast = theConflictingObjects.end();
177   for (; anIt != aLast; anIt++) {
178     ObjectPtr anObject = *anIt;
179     if (myConflictingObjects.find(anObject) == myConflictingObjects.end()) { // it is not found
180       aModifiedObjects.insert(anObject);
181       myConflictingObjects.insert(anObject);
182     }
183   }
184   bool isUpdated = !aModifiedObjects.empty();
185   if (isUpdated)
186     redisplayObjects(aModifiedObjects);
187
188   return isUpdated;
189 }
190
191 bool PartSet_OverconstraintListener::repairConflictingObjects(
192                                               const std::set<ObjectPtr>& theConflictingObjects)
193 {
194   std::set<ObjectPtr> aModifiedObjects;
195   // erase error state of absent current objects in the parameter list
196   std::set<ObjectPtr>::const_iterator anIt, aLast;
197   for (anIt = theConflictingObjects.begin(), aLast = theConflictingObjects.end();
198        anIt != aLast; anIt++) {
199     ObjectPtr anObject = *anIt;
200     if (theConflictingObjects.find(anObject) != theConflictingObjects.end()) { // it is found
201       myConflictingObjects.erase(anObject);
202
203       aModifiedObjects.insert(anObject);
204     }
205   }
206   bool isUpdated = !aModifiedObjects.empty();
207   if (isUpdated)
208     redisplayObjects(aModifiedObjects);
209
210   return isUpdated;
211 }
212
213 void PartSet_OverconstraintListener::redisplayObjects(
214                                               const std::set<ObjectPtr>& theObjects)
215 {
216   static Events_Loop* aLoop = Events_Loop::loop();
217
218   static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
219   static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
220
221   std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
222   for (; anIt != aLast; anIt++)
223     aECreator->sendUpdated(*anIt, EVENT_DISP);
224
225   aLoop->flush(EVENT_DISP);
226 }
227
228 PartSet_Module* PartSet_OverconstraintListener::module() const
229 {
230   return dynamic_cast<PartSet_Module*>(myWorkshop->module());
231 }
232
233 #ifdef _DEBUG
234 QString PartSet_OverconstraintListener::getObjectsInfo(const std::set<ObjectPtr>& theObjects)
235 {
236   std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(),
237                                       aLast = theObjects.end();
238   QStringList anInfo;
239   for (; anIt != aLast; ++anIt)
240     anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
241
242   return anInfo.join(";\n");
243 }
244 #endif