1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: SketcherPrs_Angle.cpp
4 // Created: 20 August 2015
5 // Author: Vitaly SMETANNIKOV
7 #include "PartSet_OverconstraintListener.h"
9 #include "XGUI_ModuleConnector.h"
10 #include "XGUI_Workshop.h"
11 #include "XGUI_Displayer.h"
13 #include "SketcherPrs_SymbolPrs.h"
14 #include "SketchPlugin_SketchEntity.h"
16 #include "Events_Loop.h"
18 #include <GeomAPI_IPresentable.h>
19 #include <ModelAPI_Events.h>
20 #include <ModuleBase_Tools.h>
24 //#define DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
26 PartSet_OverconstraintListener::PartSet_OverconstraintListener(ModuleBase_IWorkshop* theWorkshop)
27 : myWorkshop(theWorkshop)
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));
34 bool PartSet_OverconstraintListener::isConflictingObject(const ObjectPtr& theObject)
36 return myConflictingObjects.find(theObject) != myConflictingObjects.end();
39 void PartSet_OverconstraintListener::getConflictingColor(std::vector<int>& theColor)
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.);
48 void PartSet_OverconstraintListener::processEvent(
49 const std::shared_ptr<Events_Message>& theMessage)
51 #ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
52 bool isRepaired = theMessage->eventID() == Events_Loop::eventByName(EVENT_SOLVER_REPAIRED);
55 std::shared_ptr<ModelAPI_SolverFailedMessage> anErrorMsg =
56 std::dynamic_pointer_cast<ModelAPI_SolverFailedMessage>(theMessage);
58 if (anErrorMsg.get()) {
59 const std::set<ObjectPtr>& aConflictingObjects = anErrorMsg->objects();
60 aCount = aConflictingObjects.size();
61 anInfoStr = getObjectsInfo(aConflictingObjects);
64 QString aCurrentInfoStr = getObjectsInfo(myConflictingObjects);
66 QString aMsg("PartSet_OverconstraintListener::processEvent: %1,\nobjects "
67 "count = %2:%3\ncurrent objects count = %4:%5");
68 qDebug(aMsg.arg(isRepaired ? "REPAIRED" : "FAILED")
69 .arg(aCount).arg(anInfoStr).arg(myConflictingObjects.size())
70 .arg(aCurrentInfoStr).toStdString().c_str());
73 if (theMessage->eventID() == Events_Loop::eventByName(EVENT_SOLVER_FAILED)) {
74 std::shared_ptr<ModelAPI_SolverFailedMessage> anErrorMsg =
75 std::dynamic_pointer_cast<ModelAPI_SolverFailedMessage>(theMessage);
76 bool anUpdated = false;
77 if (anErrorMsg.get()) {
78 const std::set<ObjectPtr>& aConflictingObjects = anErrorMsg->objects();
79 anUpdated = appendConflictingObjects(aConflictingObjects);
82 // there is a crash in the solver. All objects are invalid
83 //anUpdated = appendConflictingObjects(std::set<ObjectPtr>());
86 if (theMessage->eventID() == Events_Loop::eventByName(EVENT_SOLVER_REPAIRED)) {
87 std::shared_ptr<ModelAPI_SolverFailedMessage> anErrorMsg =
88 std::dynamic_pointer_cast<ModelAPI_SolverFailedMessage>(theMessage);
89 bool anUpdated = false;
90 if (anErrorMsg.get()) {
91 const std::set<ObjectPtr>& aConflictingObjects = anErrorMsg->objects();
92 anUpdated = repairConflictingObjects(aConflictingObjects);
95 // there is no repaired objects, do nothing
99 #ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
100 aCurrentInfoStr = getObjectsInfo(myConflictingObjects);
101 qDebug(QString("RESULT: current objects count = %1:%2\n")
102 .arg(myConflictingObjects.size()).arg(aCurrentInfoStr).toStdString().c_str());
106 bool PartSet_OverconstraintListener::appendConflictingObjects(
107 const std::set<ObjectPtr>& theConflictingObjects)
109 std::set<ObjectPtr> aModifiedObjects;
110 std::vector<int> aColor;
111 getConflictingColor(aColor);
113 // set error state for new objects and append them in the internal map of objects
114 std::set<ObjectPtr>::const_iterator
115 anIt = theConflictingObjects.begin(), aLast = theConflictingObjects.end();
116 for (; anIt != aLast; anIt++) {
117 ObjectPtr anObject = *anIt;
118 if (myConflictingObjects.find(anObject) == myConflictingObjects.end()) { // it is not found
119 aModifiedObjects.insert(anObject);
120 myConflictingObjects.insert(anObject);
123 bool isUpdated = !aModifiedObjects.empty();
125 redisplayObjects(aModifiedObjects);
130 bool PartSet_OverconstraintListener::repairConflictingObjects(
131 const std::set<ObjectPtr>& theConflictingObjects)
133 std::set<ObjectPtr> aModifiedObjects;
134 // erase error state of absent current objects in the parameter list
135 std::set<ObjectPtr>::const_iterator anIt, aLast;
136 for (anIt = theConflictingObjects.begin(), aLast = theConflictingObjects.end();
137 anIt != aLast; anIt++) {
138 ObjectPtr anObject = *anIt;
139 if (theConflictingObjects.find(anObject) != theConflictingObjects.end()) { // it is found
140 myConflictingObjects.erase(anObject);
142 aModifiedObjects.insert(anObject);
145 bool isUpdated = !aModifiedObjects.empty();
147 redisplayObjects(aModifiedObjects);
152 void PartSet_OverconstraintListener::redisplayObjects(
153 const std::set<ObjectPtr>& theObjects)
155 static Events_Loop* aLoop = Events_Loop::loop();
157 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
158 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
160 std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
161 for (; anIt != aLast; anIt++)
162 aECreator->sendUpdated(*anIt, EVENT_DISP);
164 aLoop->flush(EVENT_DISP);
167 XGUI_Workshop* PartSet_OverconstraintListener::workshop() const
169 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
170 return aConnector->workshop();
174 QString PartSet_OverconstraintListener::getObjectsInfo(const std::set<ObjectPtr>& theObjects)
176 std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(),
177 aLast = theObjects.end();
179 for (; anIt != aLast; ++anIt)
180 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
182 return anInfo.join(";\n");