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 <ModelAPI_Tools.h>
9 #include "PartSet_OverconstraintListener.h"
10 #include <PartSet_Module.h>
11 #include <PartSet_SketcherMgr.h>
13 #include "XGUI_ModuleConnector.h"
14 #include "XGUI_Workshop.h"
15 #include "XGUI_Displayer.h"
16 #include "XGUI_CustomPrs.h"
18 #include "SketcherPrs_SymbolPrs.h"
19 #include "SketchPlugin_SketchEntity.h"
21 #include "Events_Loop.h"
23 #include <GeomAPI_IPresentable.h>
24 #include <ModelAPI_Events.h>
25 #include <ModuleBase_Tools.h>
29 //#define DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
31 PartSet_OverconstraintListener::PartSet_OverconstraintListener(ModuleBase_IWorkshop* theWorkshop)
32 : myWorkshop(theWorkshop), myIsFullyConstrained(false), myIsNeedUpdateCustomColor(false)
34 Events_Loop* aLoop = Events_Loop::loop();
35 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SOLVER_FAILED));
36 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SOLVER_REPAIRED));
38 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SKETCH_UNDER_CONSTRAINED));
39 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SKETCH_FULLY_CONSTRAINED));
42 void PartSet_OverconstraintListener::getCustomColor(const ObjectPtr& theObject,
43 std::vector<int>& theColor)
45 if (myConflictingObjects.find(theObject) != myConflictingObjects.end()) {
46 Quantity_Color aColor = ModuleBase_Tools::color("Visualization",
47 "sketch_overconstraint_color");
48 theColor.push_back(aColor.Red()*255.);
49 theColor.push_back(aColor.Green()*255.);
50 theColor.push_back(aColor.Blue()*255.);
52 if (myIsFullyConstrained) {
53 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
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 Quantity_Color aColor = ModuleBase_Tools::color("Visualization",
60 "sketch_fully_constrained_color");
61 theColor.push_back(aColor.Red()*0.);
62 theColor.push_back(aColor.Green()*255.);
63 theColor.push_back(aColor.Blue()*0.);
69 void PartSet_OverconstraintListener::processEvent(
70 const std::shared_ptr<Events_Message>& theMessage)
72 #ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
73 bool isRepaired = theMessage->eventID() == Events_Loop::eventByName(EVENT_SOLVER_REPAIRED);
76 std::shared_ptr<ModelAPI_SolverFailedMessage> anErrorMsg =
77 std::dynamic_pointer_cast<ModelAPI_SolverFailedMessage>(theMessage);
79 if (anErrorMsg.get()) {
80 const std::set<ObjectPtr>& aConflictingObjects = anErrorMsg->objects();
81 aCount = aConflictingObjects.size();
82 anInfoStr = getObjectsInfo(aConflictingObjects);
85 QString aCurrentInfoStr = getObjectsInfo(myConflictingObjects);
87 QString aMsg("PartSet_OverconstraintListener::processEvent: %1,\nobjects "
88 "count = %2:%3\ncurrent objects count = %4:%5");
89 qDebug(aMsg.arg(isRepaired ? "REPAIRED" : "FAILED")
90 .arg(aCount).arg(anInfoStr).arg(myConflictingObjects.size())
91 .arg(aCurrentInfoStr).toStdString().c_str());
94 Events_ID anEventID = theMessage->eventID();
95 if (anEventID == Events_Loop::eventByName(EVENT_SOLVER_FAILED) ||
96 anEventID == Events_Loop::eventByName(EVENT_SOLVER_REPAIRED)) {
97 std::shared_ptr<ModelAPI_SolverFailedMessage> anErrorMsg =
98 std::dynamic_pointer_cast<ModelAPI_SolverFailedMessage>(theMessage);
99 bool anUpdated = false;
100 if (anErrorMsg.get()) {
101 const std::set<ObjectPtr>& aConflictingObjects = anErrorMsg->objects();
102 if (anEventID == Events_Loop::eventByName(EVENT_SOLVER_FAILED))
103 anUpdated = appendConflictingObjects(aConflictingObjects);
105 anUpdated = repairConflictingObjects(aConflictingObjects);
108 else if (anEventID == Events_Loop::eventByName(EVENT_SKETCH_UNDER_CONSTRAINED) ||
109 anEventID == Events_Loop::eventByName(EVENT_SKETCH_FULLY_CONSTRAINED)) {
110 bool aPrevFullyConstrained = myIsFullyConstrained;
111 myIsFullyConstrained = anEventID == Events_Loop::eventByName(EVENT_SKETCH_FULLY_CONSTRAINED);
113 if (aPrevFullyConstrained != myIsFullyConstrained) {
114 myIsNeedUpdateCustomColor = true;
115 std::set<ObjectPtr> aModifiedObjects;
116 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
117 CompositeFeaturePtr aSketch = aModule->sketchMgr()->activeSketch();
118 for (int i = 0; i < aSketch->numberOfSubs(); i++) {
119 FeaturePtr aFeature = aSketch->subFeature(i);
120 aModifiedObjects.insert(aFeature); // is necessary to redisplay presentations
121 std::list<ResultPtr> aResults = aFeature->results();
122 for (std::list<ResultPtr>::const_iterator aIt = aResults.begin();
123 aIt != aResults.end(); ++aIt) {
124 aModifiedObjects.insert(*aIt);
127 redisplayObjects(aModifiedObjects);
128 myIsNeedUpdateCustomColor = false;
132 #ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
133 aCurrentInfoStr = getObjectsInfo(myConflictingObjects);
134 qDebug(QString("RESULT: current objects count = %1:%2\n")
135 .arg(myConflictingObjects.size()).arg(aCurrentInfoStr).toStdString().c_str());
139 bool PartSet_OverconstraintListener::appendConflictingObjects(
140 const std::set<ObjectPtr>& theConflictingObjects)
142 std::set<ObjectPtr> aModifiedObjects;
144 // set error state for new objects and append them in the internal map of objects
145 std::set<ObjectPtr>::const_iterator
146 anIt = theConflictingObjects.begin(), aLast = theConflictingObjects.end();
147 for (; anIt != aLast; anIt++) {
148 ObjectPtr anObject = *anIt;
149 if (myConflictingObjects.find(anObject) == myConflictingObjects.end()) { // it is not found
150 aModifiedObjects.insert(anObject);
151 myConflictingObjects.insert(anObject);
154 bool isUpdated = !aModifiedObjects.empty();
156 redisplayObjects(aModifiedObjects);
161 bool PartSet_OverconstraintListener::repairConflictingObjects(
162 const std::set<ObjectPtr>& theConflictingObjects)
164 std::set<ObjectPtr> aModifiedObjects;
165 // erase error state of absent current objects in the parameter list
166 std::set<ObjectPtr>::const_iterator anIt, aLast;
167 for (anIt = theConflictingObjects.begin(), aLast = theConflictingObjects.end();
168 anIt != aLast; anIt++) {
169 ObjectPtr anObject = *anIt;
170 if (theConflictingObjects.find(anObject) != theConflictingObjects.end()) { // it is found
171 myConflictingObjects.erase(anObject);
173 aModifiedObjects.insert(anObject);
176 bool isUpdated = !aModifiedObjects.empty();
178 redisplayObjects(aModifiedObjects);
183 void PartSet_OverconstraintListener::redisplayObjects(
184 const std::set<ObjectPtr>& theObjects)
186 static Events_Loop* aLoop = Events_Loop::loop();
188 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
189 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
191 std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
192 for (; anIt != aLast; anIt++)
193 aECreator->sendUpdated(*anIt, EVENT_DISP);
195 aLoop->flush(EVENT_DISP);
198 XGUI_Workshop* PartSet_OverconstraintListener::workshop() const
200 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
201 return aConnector->workshop();
205 QString PartSet_OverconstraintListener::getObjectsInfo(const std::set<ObjectPtr>& theObjects)
207 std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(),
208 aLast = theObjects.end();
210 for (; anIt != aLast; ++anIt)
211 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
213 return anInfo.join(";\n");