Salome HOME
Issue #2027 Sketcher Trim Feature: preselection in reentrant operation
[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)
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     theColor = Config_PropManager::color("Visualization", "sketch_overconstraint_color");
50   }
51   if (myIsFullyConstrained) {
52     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
53     // only entity features has custom color when sketch is fully constrained
54     if (aFeature.get() && PartSet_SketcherMgr::isEntity(aFeature->getKind())) {
55       PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
56       CompositeFeaturePtr aCompositeFeature = aModule->sketchMgr()->activeSketch();
57       // the given object is sub feature of the current sketch(created or edited)
58       if (ModelAPI_Tools::compositeOwner(aFeature) == aCompositeFeature)
59         theColor = Config_PropManager::color("Visualization", "sketch_fully_constrained_color");
60     }
61   }
62 }
63
64 void PartSet_OverconstraintListener::processEvent(
65                                                  const std::shared_ptr<Events_Message>& theMessage)
66 {
67 #ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
68   bool isRepaired = theMessage->eventID() == Events_Loop::eventByName(EVENT_SOLVER_REPAIRED);
69   int aCount = 0;
70
71   std::shared_ptr<ModelAPI_SolverFailedMessage> anErrorMsg =
72                   std::dynamic_pointer_cast<ModelAPI_SolverFailedMessage>(theMessage);
73   QString anInfoStr;
74   if (anErrorMsg.get()) {
75     const std::set<ObjectPtr>& aConflictingObjects = anErrorMsg->objects();
76     aCount = aConflictingObjects.size();
77     anInfoStr = getObjectsInfo(aConflictingObjects);
78   }
79
80   QString aCurrentInfoStr = getObjectsInfo(myConflictingObjects);
81
82   QString aMsg("PartSet_OverconstraintListener::processEvent: %1,\nobjects "
83                "count = %2:%3\ncurrent objects count = %4:%5");
84   qDebug(aMsg.arg(isRepaired ? "REPAIRED" : "FAILED")
85              .arg(aCount).arg(anInfoStr).arg(myConflictingObjects.size())
86              .arg(aCurrentInfoStr).toStdString().c_str());
87 #endif
88
89   Events_ID anEventID = theMessage->eventID();
90   if (anEventID == Events_Loop::eventByName(EVENT_SOLVER_FAILED) ||
91       anEventID == Events_Loop::eventByName(EVENT_SOLVER_REPAIRED)) {
92     std::shared_ptr<ModelAPI_SolverFailedMessage> anErrorMsg =
93                    std::dynamic_pointer_cast<ModelAPI_SolverFailedMessage>(theMessage);
94     bool anUpdated = false;
95     if (anErrorMsg.get()) {
96       const std::set<ObjectPtr>& aConflictingObjects = anErrorMsg->objects();
97       if (anEventID == Events_Loop::eventByName(EVENT_SOLVER_FAILED))
98         anUpdated = appendConflictingObjects(aConflictingObjects);
99       else
100         anUpdated = repairConflictingObjects(aConflictingObjects);
101     }
102   }
103   else if (anEventID == Events_Loop::eventByName(EVENT_SKETCH_UNDER_CONSTRAINED) ||
104            anEventID == Events_Loop::eventByName(EVENT_SKETCH_FULLY_CONSTRAINED)) {
105     bool aPrevFullyConstrained = myIsFullyConstrained;
106     myIsFullyConstrained = anEventID == Events_Loop::eventByName(EVENT_SKETCH_FULLY_CONSTRAINED);
107
108     if (aPrevFullyConstrained != myIsFullyConstrained) {
109       std::set<ObjectPtr> aModifiedObjects;
110       PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
111       CompositeFeaturePtr aSketch = aModule->sketchMgr()->activeSketch();
112       if (aSketch.get()) {
113         for (int i = 0; i < aSketch->numberOfSubs(); i++) {
114           FeaturePtr aFeature = aSketch->subFeature(i);
115           aModifiedObjects.insert(aFeature); // is necessary to redisplay presentations
116           std::list<ResultPtr> aResults = aFeature->results();
117           for (std::list<ResultPtr>::const_iterator aIt = aResults.begin();
118                aIt != aResults.end(); ++aIt) {
119             aModifiedObjects.insert(*aIt);
120           }
121         }
122         redisplayObjects(aModifiedObjects);
123       }
124     }
125   }
126   else if (anEventID == Events_Loop::eventByName(EVENT_OBJECT_CREATED)) {
127     PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
128     PartSet_SketcherReentrantMgr* aReentrantMgr = aModule->sketchReentranceMgr();
129     if (aReentrantMgr->isInternalEditActive()) {
130       std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
131             std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
132       std::set<ObjectPtr> anObjects = aUpdMsg->objects();
133       aReentrantMgr->appendCreatedObjects(anObjects);
134     }
135   }
136 #ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
137   aCurrentInfoStr = getObjectsInfo(myConflictingObjects);
138   qDebug(QString("RESULT: current objects count = %1:%2\n")
139                 .arg(myConflictingObjects.size()).arg(aCurrentInfoStr).toStdString().c_str());
140 #endif
141 }
142
143 bool PartSet_OverconstraintListener::appendConflictingObjects(
144                                                const std::set<ObjectPtr>& theConflictingObjects)
145 {
146   std::set<ObjectPtr> aModifiedObjects;
147
148   // set error state for new objects and append them in the internal map of objects
149   std::set<ObjectPtr>::const_iterator
150     anIt = theConflictingObjects.begin(), aLast = theConflictingObjects.end();
151   for (; anIt != aLast; anIt++) {
152     ObjectPtr anObject = *anIt;
153     if (myConflictingObjects.find(anObject) == myConflictingObjects.end()) { // it is not found
154       aModifiedObjects.insert(anObject);
155       myConflictingObjects.insert(anObject);
156     }
157   }
158   bool isUpdated = !aModifiedObjects.empty();
159   if (isUpdated)
160     redisplayObjects(aModifiedObjects);
161
162   return isUpdated;
163 }
164
165 bool PartSet_OverconstraintListener::repairConflictingObjects(
166                                               const std::set<ObjectPtr>& theConflictingObjects)
167 {
168   std::set<ObjectPtr> aModifiedObjects;
169   // erase error state of absent current objects in the parameter list
170   std::set<ObjectPtr>::const_iterator anIt, aLast;
171   for (anIt = theConflictingObjects.begin(), aLast = theConflictingObjects.end();
172        anIt != aLast; anIt++) {
173     ObjectPtr anObject = *anIt;
174     if (theConflictingObjects.find(anObject) != theConflictingObjects.end()) { // it is found
175       myConflictingObjects.erase(anObject);
176
177       aModifiedObjects.insert(anObject);
178     }
179   }
180   bool isUpdated = !aModifiedObjects.empty();
181   if (isUpdated)
182     redisplayObjects(aModifiedObjects);
183
184   return isUpdated;
185 }
186
187 void PartSet_OverconstraintListener::redisplayObjects(
188                                               const std::set<ObjectPtr>& theObjects)
189 {
190   static Events_Loop* aLoop = Events_Loop::loop();
191
192   static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
193   static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
194
195   std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
196   for (; anIt != aLast; anIt++)
197     aECreator->sendUpdated(*anIt, EVENT_DISP);
198
199   aLoop->flush(EVENT_DISP);
200 }
201
202 XGUI_Workshop* PartSet_OverconstraintListener::workshop() const
203 {
204   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
205   return aConnector->workshop();
206 }
207
208 #ifdef _DEBUG
209 QString PartSet_OverconstraintListener::getObjectsInfo(const std::set<ObjectPtr>& theObjects)
210 {
211   std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(),
212                                       aLast = theObjects.end();
213   QStringList anInfo;
214   for (; anIt != aLast; ++anIt)
215     anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
216
217   return anInfo.join(";\n");
218 }
219 #endif