Salome HOME
Correction for case: create auxiliary cirle, try to create several lines coincident...
[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_CustomPrs.h"
16 #include "XGUI_Displayer.h"
17 #include "XGUI_ModuleConnector.h"
18 #include "XGUI_OperationMgr.h"
19 #include "XGUI_Tools.h"
20 #include "XGUI_Workshop.h"
21
22 #include "SketcherPrs_SymbolPrs.h"
23 #include "SketchPlugin_SketchEntity.h"
24 #include "SketchPlugin_MacroArcReentrantMessage.h"
25 #include "SketchPlugin_Sketch.h"
26
27 #include "Events_Loop.h"
28
29 #include <GeomAPI_IPresentable.h>
30 #include <ModelAPI_Events.h>
31 #include <ModelAPI_EventReentrantMessage.h>
32 #include <ModuleBase_Tools.h>
33
34 #include <QString>
35
36 //#define DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
37
38 PartSet_OverconstraintListener::PartSet_OverconstraintListener(ModuleBase_IWorkshop* theWorkshop)
39 : myWorkshop(theWorkshop), myIsActive(false), myIsFullyConstrained(false)
40 {
41   Events_Loop* aLoop = Events_Loop::loop();
42   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SOLVER_FAILED));
43   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SOLVER_REPAIRED));
44
45   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SKETCH_UNDER_CONSTRAINED));
46   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SKETCH_FULLY_CONSTRAINED));
47
48   aLoop->registerListener(this, ModelAPI_EventReentrantMessage::eventId());
49   aLoop->registerListener(this, SketchPlugin_MacroArcReentrantMessage::eventId());
50 }
51
52 void PartSet_OverconstraintListener::setActive(const bool& theActive)
53 {
54   if (myIsActive == theActive)
55     return;
56
57   myIsActive = theActive;
58   myIsFullyConstrained = false; /// returned to default state, no custom color for it
59
60   if (myIsActive) {
61     PartSet_Module* aModule = module();
62     CompositeFeaturePtr aSketch = aModule->sketchMgr()->activeSketch();
63     if (aSketch.get()) {
64       std::string aDOFMessage = aSketch->string(SketchPlugin_Sketch::SOLVER_DOF())->value();
65       myIsFullyConstrained = QString(aDOFMessage.c_str()).contains("DoF = 0");
66     }
67   }
68 }
69
70 void PartSet_OverconstraintListener::getCustomColor(const ObjectPtr& theObject,
71                                                     std::vector<int>& theColor)
72 {
73   if (!myIsActive)
74     return;
75
76   if (myConflictingObjects.find(theObject) != myConflictingObjects.end()) {
77     theColor = Config_PropManager::color("Visualization", "sketch_overconstraint_color");
78   }
79   if (myIsFullyConstrained) {
80     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
81     // only entity features has custom color when sketch is fully constrained
82     if (aFeature.get() && PartSet_SketcherMgr::isEntity(aFeature->getKind()) &&
83         !PartSet_SketcherMgr::isExternalFeature(aFeature)) {
84       PartSet_Module* aModule = module();
85       CompositeFeaturePtr aSketch = aModule->sketchMgr()->activeSketch();
86       // the given object is sub feature of the current sketch(created or edited)
87       if (ModelAPI_Tools::compositeOwner(aFeature) == aSketch)
88         theColor = Config_PropManager::color("Visualization", "sketch_fully_constrained_color");
89     }
90   }
91 }
92
93 void PartSet_OverconstraintListener::processEvent(
94                                                  const std::shared_ptr<Events_Message>& theMessage)
95 {
96   if (!myIsActive)
97     return;
98
99 #ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
100   bool isRepaired = theMessage->eventID() == Events_Loop::eventByName(EVENT_SOLVER_REPAIRED);
101   int aCount = 0;
102
103   std::shared_ptr<ModelAPI_SolverFailedMessage> anErrorMsg =
104                   std::dynamic_pointer_cast<ModelAPI_SolverFailedMessage>(theMessage);
105   QString anInfoStr;
106   if (anErrorMsg.get()) {
107     const std::set<ObjectPtr>& aConflictingObjects = anErrorMsg->objects();
108     aCount = aConflictingObjects.size();
109     anInfoStr = getObjectsInfo(aConflictingObjects);
110   }
111
112   QString aCurrentInfoStr = getObjectsInfo(myConflictingObjects);
113
114   QString aMsg("PartSet_OverconstraintListener::processEvent: %1,\nobjects "
115                "count = %2:%3\ncurrent objects count = %4:%5");
116   qDebug(aMsg.arg(isRepaired ? "REPAIRED" : "FAILED")
117              .arg(aCount).arg(anInfoStr).arg(myConflictingObjects.size())
118              .arg(aCurrentInfoStr).toStdString().c_str());
119 #endif
120
121   Events_ID anEventID = theMessage->eventID();
122   if (anEventID == Events_Loop::eventByName(EVENT_SOLVER_FAILED) ||
123       anEventID == Events_Loop::eventByName(EVENT_SOLVER_REPAIRED)) {
124     std::shared_ptr<ModelAPI_SolverFailedMessage> anErrorMsg =
125                    std::dynamic_pointer_cast<ModelAPI_SolverFailedMessage>(theMessage);
126     bool anUpdated = false;
127     if (anErrorMsg.get()) {
128       const std::set<ObjectPtr>& aConflictingObjects = anErrorMsg->objects();
129       if (anEventID == Events_Loop::eventByName(EVENT_SOLVER_FAILED))
130         anUpdated = appendConflictingObjects(aConflictingObjects);
131       else
132         anUpdated = repairConflictingObjects(aConflictingObjects);
133     }
134   }
135   else if (anEventID == Events_Loop::eventByName(EVENT_SKETCH_UNDER_CONSTRAINED) ||
136            anEventID == Events_Loop::eventByName(EVENT_SKETCH_FULLY_CONSTRAINED)) {
137     bool aPrevFullyConstrained = myIsFullyConstrained;
138     myIsFullyConstrained = anEventID == Events_Loop::eventByName(EVENT_SKETCH_FULLY_CONSTRAINED);
139
140     if (aPrevFullyConstrained != myIsFullyConstrained) {
141       std::set<ObjectPtr> aModifiedObjects;
142       PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
143       CompositeFeaturePtr aSketch = aModule->sketchMgr()->activeSketch();
144       if (aSketch.get()) {
145         for (int i = 0; i < aSketch->numberOfSubs(); i++) {
146           FeaturePtr aFeature = aSketch->subFeature(i);
147           aModifiedObjects.insert(aFeature); // is necessary to redisplay presentations
148           std::list<ResultPtr> aResults = aFeature->results();
149           for (std::list<ResultPtr>::const_iterator aIt = aResults.begin();
150                aIt != aResults.end(); ++aIt) {
151             aModifiedObjects.insert(*aIt);
152           }
153         }
154         redisplayObjects(aModifiedObjects);
155       }
156     }
157   }
158   else if (anEventID == ModelAPI_EventReentrantMessage::eventId() ||
159            anEventID == SketchPlugin_MacroArcReentrantMessage::eventId()) {
160     // the message is sent to sketcher reentrant manager only if the name of feature
161     // sender is equal to feature name of the current operation. E.g. Horizontal create operation
162     // is active. Sketch Line feature is changed, so execute is called, it will send message
163     // This Line's message should not be processed, as the reentrant operation is not for Line
164     // It is not enoght of kind, the name should be used, e.g. restarted Lines on auxiliary
165     // cirlce sometimes causes previous line change, kind the same, but feature line is different
166     std::string aCurrentFeatureName;
167     ModuleBase_Operation* anOperation =
168                 XGUI_Tools::workshop(myWorkshop)->operationMgr()->currentOperation();
169     if (anOperation) {
170       ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
171                                                            (anOperation);
172       if (aFOperation) {
173         FeaturePtr aFeature = aFOperation->feature();
174         if (aFeature.get())
175           aCurrentFeatureName = aFeature->data()->name();
176       }
177     }
178     if (theMessage->sender()) {
179       ModelAPI_Object* aSender = static_cast<ModelAPI_Object*>(theMessage->sender());
180       if (aSender) {
181         FeaturePtr aFeatureSender =
182           std::dynamic_pointer_cast<ModelAPI_Feature>(aSender->data()->owner());
183         if (aFeatureSender.get() && aFeatureSender->data()->name() == aCurrentFeatureName) {
184           PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
185           PartSet_SketcherReentrantMgr* aReentrantMgr = aModule->sketchReentranceMgr();
186           aReentrantMgr->setReentrantMessage(theMessage);
187         }
188       }
189     }
190   }
191
192 #ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
193   aCurrentInfoStr = getObjectsInfo(myConflictingObjects);
194   qDebug(QString("RESULT: current objects count = %1:%2\n")
195                 .arg(myConflictingObjects.size()).arg(aCurrentInfoStr).toStdString().c_str());
196 #endif
197 }
198
199 bool PartSet_OverconstraintListener::appendConflictingObjects(
200                                                const std::set<ObjectPtr>& theConflictingObjects)
201 {
202   std::set<ObjectPtr> aModifiedObjects;
203
204   // set error state for new objects and append them in the internal map of objects
205   std::set<ObjectPtr>::const_iterator
206     anIt = theConflictingObjects.begin(), aLast = theConflictingObjects.end();
207   for (; anIt != aLast; anIt++) {
208     ObjectPtr anObject = *anIt;
209     if (myConflictingObjects.find(anObject) == myConflictingObjects.end()) { // it is not found
210       aModifiedObjects.insert(anObject);
211       myConflictingObjects.insert(anObject);
212     }
213   }
214   bool isUpdated = !aModifiedObjects.empty();
215   if (isUpdated)
216     redisplayObjects(aModifiedObjects);
217
218   return isUpdated;
219 }
220
221 bool PartSet_OverconstraintListener::repairConflictingObjects(
222                                               const std::set<ObjectPtr>& theConflictingObjects)
223 {
224   std::set<ObjectPtr> aModifiedObjects;
225   // erase error state of absent current objects in the parameter list
226   std::set<ObjectPtr>::const_iterator anIt, aLast;
227   for (anIt = theConflictingObjects.begin(), aLast = theConflictingObjects.end();
228        anIt != aLast; anIt++) {
229     ObjectPtr anObject = *anIt;
230     if (theConflictingObjects.find(anObject) != theConflictingObjects.end()) { // it is found
231       myConflictingObjects.erase(anObject);
232
233       aModifiedObjects.insert(anObject);
234     }
235   }
236   bool isUpdated = !aModifiedObjects.empty();
237   if (isUpdated)
238     redisplayObjects(aModifiedObjects);
239
240   return isUpdated;
241 }
242
243 void PartSet_OverconstraintListener::redisplayObjects(
244                                               const std::set<ObjectPtr>& theObjects)
245 {
246   static Events_Loop* aLoop = Events_Loop::loop();
247
248   static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
249   static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
250
251   std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
252   for (; anIt != aLast; anIt++)
253     aECreator->sendUpdated(*anIt, EVENT_DISP);
254
255   aLoop->flush(EVENT_DISP);
256 }
257
258 PartSet_Module* PartSet_OverconstraintListener::module() const
259 {
260   return dynamic_cast<PartSet_Module*>(myWorkshop->module());
261 }
262
263 #ifdef _DEBUG
264 QString PartSet_OverconstraintListener::getObjectsInfo(const std::set<ObjectPtr>& theObjects)
265 {
266   std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(),
267                                       aLast = theObjects.end();
268   QStringList anInfo;
269   for (; anIt != aLast; ++anIt)
270     anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
271
272   return anInfo.join(";\n");
273 }
274 #endif