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