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)
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 bool PartSet_OverconstraintListener::hasCustomColor(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.);
53 if (myIsFullyConstrained) {
54 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
56 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
57 CompositeFeaturePtr aCompositeFeature = aModule->sketchMgr()->activeSketch();
58 // the given object is sub feature of the current sketch(created or edited)
59 if (ModelAPI_Tools::compositeOwner(aFeature) == aCompositeFeature) {
60 Quantity_Color aColor = ModuleBase_Tools::color("Visualization",
61 "sketch_fully_constrained_color");
62 theColor.push_back(aColor.Red()*255.);
63 theColor.push_back(aColor.Green()*255.);
64 theColor.push_back(aColor.Blue()*255.);
72 void PartSet_OverconstraintListener::processEvent(
73 const std::shared_ptr<Events_Message>& theMessage)
75 #ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
76 bool isRepaired = theMessage->eventID() == Events_Loop::eventByName(EVENT_SOLVER_REPAIRED);
79 std::shared_ptr<ModelAPI_SolverFailedMessage> anErrorMsg =
80 std::dynamic_pointer_cast<ModelAPI_SolverFailedMessage>(theMessage);
82 if (anErrorMsg.get()) {
83 const std::set<ObjectPtr>& aConflictingObjects = anErrorMsg->objects();
84 aCount = aConflictingObjects.size();
85 anInfoStr = getObjectsInfo(aConflictingObjects);
88 QString aCurrentInfoStr = getObjectsInfo(myConflictingObjects);
90 QString aMsg("PartSet_OverconstraintListener::processEvent: %1,\nobjects "
91 "count = %2:%3\ncurrent objects count = %4:%5");
92 qDebug(aMsg.arg(isRepaired ? "REPAIRED" : "FAILED")
93 .arg(aCount).arg(anInfoStr).arg(myConflictingObjects.size())
94 .arg(aCurrentInfoStr).toStdString().c_str());
97 Events_ID anEventID = theMessage->eventID();
98 if (anEventID == Events_Loop::eventByName(EVENT_SOLVER_FAILED) ||
99 anEventID == Events_Loop::eventByName(EVENT_SOLVER_REPAIRED)) {
100 std::shared_ptr<ModelAPI_SolverFailedMessage> anErrorMsg =
101 std::dynamic_pointer_cast<ModelAPI_SolverFailedMessage>(theMessage);
102 bool anUpdated = false;
103 if (anErrorMsg.get()) {
104 const std::set<ObjectPtr>& aConflictingObjects = anErrorMsg->objects();
105 if (anEventID == Events_Loop::eventByName(EVENT_SOLVER_FAILED))
106 anUpdated = appendConflictingObjects(aConflictingObjects);
108 anUpdated = repairConflictingObjects(aConflictingObjects);
111 else if (anEventID == Events_Loop::eventByName(EVENT_SKETCH_UNDER_CONSTRAINED) ||
112 anEventID == Events_Loop::eventByName(EVENT_SKETCH_FULLY_CONSTRAINED)) {
113 myIsFullyConstrained = anEventID == Events_Loop::eventByName(EVENT_SKETCH_FULLY_CONSTRAINED);
116 #ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
117 aCurrentInfoStr = getObjectsInfo(myConflictingObjects);
118 qDebug(QString("RESULT: current objects count = %1:%2\n")
119 .arg(myConflictingObjects.size()).arg(aCurrentInfoStr).toStdString().c_str());
123 bool PartSet_OverconstraintListener::appendConflictingObjects(
124 const std::set<ObjectPtr>& theConflictingObjects)
126 std::set<ObjectPtr> aModifiedObjects;
127 //std::vector<int> aColor;
128 //getConflictingColor(aColor);
130 // set error state for new objects and append them in the internal map of objects
131 std::set<ObjectPtr>::const_iterator
132 anIt = theConflictingObjects.begin(), aLast = theConflictingObjects.end();
133 for (; anIt != aLast; anIt++) {
134 ObjectPtr anObject = *anIt;
135 if (myConflictingObjects.find(anObject) == myConflictingObjects.end()) { // it is not found
136 aModifiedObjects.insert(anObject);
137 myConflictingObjects.insert(anObject);
140 bool isUpdated = !aModifiedObjects.empty();
142 redisplayObjects(aModifiedObjects);
147 bool PartSet_OverconstraintListener::repairConflictingObjects(
148 const std::set<ObjectPtr>& theConflictingObjects)
150 std::set<ObjectPtr> aModifiedObjects;
151 // erase error state of absent current objects in the parameter list
152 std::set<ObjectPtr>::const_iterator anIt, aLast;
153 for (anIt = theConflictingObjects.begin(), aLast = theConflictingObjects.end();
154 anIt != aLast; anIt++) {
155 ObjectPtr anObject = *anIt;
156 if (theConflictingObjects.find(anObject) != theConflictingObjects.end()) { // it is found
157 myConflictingObjects.erase(anObject);
159 aModifiedObjects.insert(anObject);
162 bool isUpdated = !aModifiedObjects.empty();
164 redisplayObjects(aModifiedObjects);
169 void PartSet_OverconstraintListener::redisplayObjects(
170 const std::set<ObjectPtr>& theObjects)
172 static Events_Loop* aLoop = Events_Loop::loop();
174 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
175 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
177 std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
178 for (; anIt != aLast; anIt++)
179 aECreator->sendUpdated(*anIt, EVENT_DISP);
181 aLoop->flush(EVENT_DISP);
184 XGUI_Workshop* PartSet_OverconstraintListener::workshop() const
186 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
187 return aConnector->workshop();
191 QString PartSet_OverconstraintListener::getObjectsInfo(const std::set<ObjectPtr>& theObjects)
193 std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(),
194 aLast = theObjects.end();
196 for (; anIt != aLast; ++anIt)
197 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
199 return anInfo.join(";\n");