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 qDebug(QString("PartSet_OverconstraintListener::processEvent: %1,\nobjects count = %2:%3\ncurrent objects count = %4:%5")
67 .arg(isRepaired ? "REPAIRED" : "FAILED")
68 .arg(aCount).arg(anInfoStr).arg(myConflictingObjects.size()).arg(aCurrentInfoStr).toStdString().c_str());
71 if (theMessage->eventID() == Events_Loop::eventByName(EVENT_SOLVER_FAILED)) {
72 std::shared_ptr<ModelAPI_SolverFailedMessage> anErrorMsg =
73 std::dynamic_pointer_cast<ModelAPI_SolverFailedMessage>(theMessage);
74 bool anUpdated = false;
75 if (anErrorMsg.get()) {
76 const std::set<ObjectPtr>& aConflictingObjects = anErrorMsg->objects();
77 anUpdated = appendConflictingObjects(aConflictingObjects);
80 // there is a crash in the solver. All objects are invalid
81 //anUpdated = appendConflictingObjects(std::set<ObjectPtr>());
84 if (theMessage->eventID() == Events_Loop::eventByName(EVENT_SOLVER_REPAIRED)) {
85 std::shared_ptr<ModelAPI_SolverFailedMessage> anErrorMsg =
86 std::dynamic_pointer_cast<ModelAPI_SolverFailedMessage>(theMessage);
87 bool anUpdated = false;
88 if (anErrorMsg.get()) {
89 const std::set<ObjectPtr>& aConflictingObjects = anErrorMsg->objects();
90 anUpdated = repairConflictingObjects(aConflictingObjects);
93 // there is no repaired objects, do nothing
97 #ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
98 aCurrentInfoStr = getObjectsInfo(myConflictingObjects);
99 qDebug(QString("RESULT: current objects count = %1:%2\n")
100 .arg(myConflictingObjects.size()).arg(aCurrentInfoStr).toStdString().c_str());
104 bool PartSet_OverconstraintListener::appendConflictingObjects(
105 const std::set<ObjectPtr>& theConflictingObjects)
107 std::set<ObjectPtr> aModifiedObjects;
108 // set error state for new objects and append them in the internal map of objects
109 std::set<ObjectPtr>::const_iterator anIt = theConflictingObjects.begin(), aLast = theConflictingObjects.end();
110 for (; anIt != aLast; anIt++) {
111 ObjectPtr anObject = *anIt;
112 if (myConflictingObjects.find(anObject) == myConflictingObjects.end()) { // it is not found
113 setConflictingObject(anObject, true);
114 aModifiedObjects.insert(anObject);
115 myConflictingObjects.insert(anObject);
118 bool isUpdated = !aModifiedObjects.empty();
120 redisplayObjects(aModifiedObjects);
125 bool PartSet_OverconstraintListener::repairConflictingObjects(
126 const std::set<ObjectPtr>& theConflictingObjects)
128 std::set<ObjectPtr> aModifiedObjects;
129 // erase error state of absent current objects in the parameter list
130 std::set<ObjectPtr>::const_iterator anIt, aLast;
131 for (anIt = theConflictingObjects.begin(), aLast = theConflictingObjects.end() ; anIt != aLast; anIt++) {
132 ObjectPtr anObject = *anIt;
133 if (theConflictingObjects.find(anObject) != theConflictingObjects.end()) { // it is found
134 setConflictingObject(anObject, false);
135 myConflictingObjects.erase(anObject);
137 aModifiedObjects.insert(anObject);
140 bool isUpdated = !aModifiedObjects.empty();
142 redisplayObjects(aModifiedObjects);
147 void PartSet_OverconstraintListener::redisplayObjects(
148 const std::set<ObjectPtr>& theObjects)
150 /*static Events_Loop* aLoop = Events_Loop::loop();
151 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_UPDATED);
153 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
155 std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
156 for (; anIt != aLast; anIt++) {
157 aECreator->sendUpdated(*anIt, EVENT_DISP);
159 //#ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
160 // QString anInfoStr = ModuleBase_Tools::objectInfo(*anIt);
161 // qDebug(QString("PartSet_OverconstraintListener::SEND UPDATED: %1").arg(anInfoStr).toStdString().c_str());
164 aLoop->flush(EVENT_DISP);*/
166 XGUI_Displayer* aDisplayer = workshop()->displayer();
168 std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
169 for (; anIt != aLast; anIt++) {
170 ObjectPtr anObject = *anIt;
171 aHidden = !anObject->data() || !anObject->data()->isValid() ||
172 anObject->isDisabled() || (!anObject->isDisplayed());
174 aDisplayer->redisplay(anObject, false);
176 aDisplayer->updateViewer();
179 void PartSet_OverconstraintListener::setConflictingObject(const ObjectPtr& theObject,
180 const bool theConflicting)
182 if (!theObject.get() || !theObject->data()->isValid())
185 AISObjectPtr anAISObject;
186 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
188 if (aPrs.get() != NULL) {
189 XGUI_Workshop* aWorkshop = workshop();
190 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
192 anAISObject = aPrs->getAISObject(aDisplayer->getAISObject(theObject));
193 if (anAISObject.get()) {
194 Handle(AIS_InteractiveObject) anAISIO = anAISObject->impl<Handle(AIS_InteractiveObject)>();
195 if (!anAISIO.IsNull()) {
196 if (!Handle(SketcherPrs_SymbolPrs)::DownCast(anAISIO).IsNull()) {
197 Handle(SketcherPrs_SymbolPrs) aPrs = Handle(SketcherPrs_SymbolPrs)::DownCast(anAISIO);
199 aPrs->SetConflictingConstraint(theConflicting);
206 XGUI_Workshop* PartSet_OverconstraintListener::workshop() const
208 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
209 return aConnector->workshop();
212 QString PartSet_OverconstraintListener::getObjectsInfo(const std::set<ObjectPtr>& theObjects)
214 std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(),
215 aLast = theObjects.end();
217 for (; anIt != aLast; ++anIt)
218 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
220 return anInfo.join(";\n");