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);
44 theColor.push_back(aColor.Red()*255.);
45 theColor.push_back(aColor.Green()*255.);
46 theColor.push_back(aColor.Blue()*255.);
49 void PartSet_OverconstraintListener::processEvent(
50 const std::shared_ptr<Events_Message>& theMessage)
53 #ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
56 anIt = theConflictingObjects.begin();
57 aLast = theConflictingObjects.end();
60 for (; anIt != aLast; ++anIt) {
61 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
63 QString anInfoStr = anInfo.join(";\n");*/
64 bool isRepaired = theMessage->eventID() == Events_Loop::eventByName(EVENT_SOLVER_REPAIRED);
65 qDebug(QString("PartSet_OverconstraintListener::processEvent:\n %1").arg(isRepaired ? "REPAIRED" : "FAILED").toStdString().c_str());
68 if (theMessage->eventID() == Events_Loop::eventByName(EVENT_SOLVER_FAILED) ||
69 theMessage->eventID() == Events_Loop::eventByName(EVENT_SOLVER_REPAIRED)) {
70 std::shared_ptr<ModelAPI_SolverFailedMessage> anErrorMsg =
71 std::dynamic_pointer_cast<ModelAPI_SolverFailedMessage>(theMessage);
72 bool anUpdated = false;
73 if (anErrorMsg.get()) {
74 const std::set<ObjectPtr>& aConflictingObjects = anErrorMsg->objects();
75 anUpdated = updateConflictingObjects(aConflictingObjects);
78 anUpdated = updateConflictingObjects(std::set<ObjectPtr>());
82 bool PartSet_OverconstraintListener::updateConflictingObjects(
83 const std::set<ObjectPtr>& theConflictingObjects)
85 std::set<ObjectPtr>::const_iterator anIt, aLast;
87 #ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
88 anIt = theConflictingObjects.begin();
89 aLast = theConflictingObjects.end();
92 for (; anIt != aLast; ++anIt) {
93 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
95 QString anInfoStr = anInfo.join(";\n");
96 qDebug(QString("PartSet_OverconstraintListener::updateConflictingObjects: %1: \n%2").arg(theConflictingObjects.size())
97 .arg(anInfoStr).toStdString().c_str());
100 bool isUpdated = false;
101 std::set<ObjectPtr> aModifiedObjects;
102 // erase error state of absent current objects in the parameter list
103 for (anIt = myConflictingObjects.begin(), aLast = myConflictingObjects.end() ; anIt != aLast; anIt++) {
104 ObjectPtr anObject = *anIt;
105 if (theConflictingObjects.find(anObject) == theConflictingObjects.end()) { // it is not found
106 //setConflictingObject(anObject, false);
107 aModifiedObjects.insert(anObject);
111 // erase absent objects from the internal container
112 for (anIt = aModifiedObjects.begin(), aLast = aModifiedObjects.end(); anIt != aLast; anIt++) {
113 myConflictingObjects.erase(*anIt);
116 // set error state for new objects and append them in the internal map of objects
117 for (anIt = theConflictingObjects.begin(), aLast = theConflictingObjects.end() ; anIt != aLast; anIt++) {
118 ObjectPtr anObject = *anIt;
119 if (myConflictingObjects.find(anObject) == myConflictingObjects.end()) { // it is not found
120 //setConflictingObject(anObject, true);
121 aModifiedObjects.insert(anObject);
122 myConflictingObjects.insert(anObject);
125 isUpdated = !aModifiedObjects.empty();
127 redisplayObjects(aModifiedObjects);
132 void PartSet_OverconstraintListener::redisplayObjects(
133 const std::set<ObjectPtr>& theObjects)
135 /*static Events_Loop* aLoop = Events_Loop::loop();
136 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_UPDATED);
138 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
140 std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
141 for (; anIt != aLast; anIt++) {
142 aECreator->sendUpdated(*anIt, EVENT_DISP);
144 //#ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
145 // QString anInfoStr = ModuleBase_Tools::objectInfo(*anIt);
146 // qDebug(QString("PartSet_OverconstraintListener::SEND UPDATED: %1").arg(anInfoStr).toStdString().c_str());
149 aLoop->flush(EVENT_DISP);*/
151 XGUI_Displayer* aDisplayer = workshop()->displayer();
152 //QObjectPtrList aObjects = aDisplayer->displayedObjects();
154 std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
155 for (; anIt != aLast; anIt++) {
156 ObjectPtr anObject = *anIt;
157 //foreach(ObjectPtr aObj, aObjects) {
158 //TODO: replace by redisplay event.
159 aHidden = !anObject->data() || !anObject->data()->isValid() ||
160 anObject->isDisabled() || (!anObject->isDisplayed());
162 aDisplayer->redisplay(anObject, false);
164 aDisplayer->updateViewer();
167 void PartSet_OverconstraintListener::setConflictingObject(const ObjectPtr& theObject,
168 const bool theConflicting)
172 AISObjectPtr anAISObject;
173 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
175 if (aPrs.get() != NULL) {
176 XGUI_Workshop* aWorkshop = workshop();
177 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
179 anAISObject = aPrs->getAISObject(aDisplayer->getAISObject(theObject));
180 if (anAISObject.get()) {
181 Handle(AIS_InteractiveObject) anAISIO = anAISObject->impl<Handle(AIS_InteractiveObject)>();
182 if (!anAISIO.IsNull()) {
183 if (!Handle(SketcherPrs_SymbolPrs)::DownCast(anAISIO).IsNull()) {
184 Handle(SketcherPrs_SymbolPrs) aPrs = Handle(SketcherPrs_SymbolPrs)::DownCast(anAISIO);
185 //if (!aPrs.IsNull())
186 // aPrs->setConflictingConstraint(theConflicting);
193 XGUI_Workshop* PartSet_OverconstraintListener::workshop() const
195 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
196 return aConnector->workshop();