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>
12 #include <PartSet_SketcherReentrantMgr.h>
14 #include "XGUI_ModuleConnector.h"
15 #include "XGUI_Workshop.h"
16 #include "XGUI_Displayer.h"
17 #include "XGUI_CustomPrs.h"
19 #include "SketcherPrs_SymbolPrs.h"
20 #include "SketchPlugin_SketchEntity.h"
22 #include "Events_Loop.h"
24 #include <GeomAPI_IPresentable.h>
25 #include <ModelAPI_Events.h>
26 #include <ModuleBase_Tools.h>
30 //#define DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
32 PartSet_OverconstraintListener::PartSet_OverconstraintListener(ModuleBase_IWorkshop* theWorkshop)
33 : myWorkshop(theWorkshop), myIsFullyConstrained(false)//, myIsNeedUpdateCustomColor(false)
35 Events_Loop* aLoop = Events_Loop::loop();
36 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SOLVER_FAILED));
37 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SOLVER_REPAIRED));
39 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SKETCH_UNDER_CONSTRAINED));
40 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SKETCH_FULLY_CONSTRAINED));
42 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
45 void PartSet_OverconstraintListener::getCustomColor(const ObjectPtr& theObject,
46 std::vector<int>& theColor)
48 if (myConflictingObjects.find(theObject) != myConflictingObjects.end()) {
49 Quantity_Color aColor = ModuleBase_Tools::color("Visualization",
50 "sketch_overconstraint_color");
51 theColor.push_back(aColor.Red()*255.);
52 theColor.push_back(aColor.Green()*255.);
53 theColor.push_back(aColor.Blue()*255.);
55 if (myIsFullyConstrained) {
56 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
58 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
59 CompositeFeaturePtr aCompositeFeature = aModule->sketchMgr()->activeSketch();
60 // the given object is sub feature of the current sketch(created or edited)
61 if (ModelAPI_Tools::compositeOwner(aFeature) == aCompositeFeature) {
62 Quantity_Color aColor = ModuleBase_Tools::color("Visualization",
63 "sketch_fully_constrained_color");
64 theColor.push_back(aColor.Red()*0.);
65 theColor.push_back(aColor.Green()*255.);
66 theColor.push_back(aColor.Blue()*0.);
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 bool aPrevFullyConstrained = myIsFullyConstrained;
114 myIsFullyConstrained = anEventID == Events_Loop::eventByName(EVENT_SKETCH_FULLY_CONSTRAINED);
116 if (aPrevFullyConstrained != myIsFullyConstrained) {
117 //myIsNeedUpdateCustomColor = true;
118 std::set<ObjectPtr> aModifiedObjects;
119 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
120 CompositeFeaturePtr aSketch = aModule->sketchMgr()->activeSketch();
122 for (int i = 0; i < aSketch->numberOfSubs(); i++) {
123 FeaturePtr aFeature = aSketch->subFeature(i);
124 aModifiedObjects.insert(aFeature); // is necessary to redisplay presentations
125 std::list<ResultPtr> aResults = aFeature->results();
126 for (std::list<ResultPtr>::const_iterator aIt = aResults.begin();
127 aIt != aResults.end(); ++aIt) {
128 aModifiedObjects.insert(*aIt);
131 redisplayObjects(aModifiedObjects);
133 //myIsNeedUpdateCustomColor = false;
136 else if (anEventID == Events_Loop::eventByName(EVENT_OBJECT_CREATED)) {
137 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
138 PartSet_SketcherReentrantMgr* aReentrantMgr = aModule->sketchReentranceMgr();
139 if (aReentrantMgr->isInternalEditActive()) {
140 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
141 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
142 std::set<ObjectPtr> anObjects = aUpdMsg->objects();
143 aReentrantMgr->appendCreatedObjects(anObjects);
146 #ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
147 aCurrentInfoStr = getObjectsInfo(myConflictingObjects);
148 qDebug(QString("RESULT: current objects count = %1:%2\n")
149 .arg(myConflictingObjects.size()).arg(aCurrentInfoStr).toStdString().c_str());
153 bool PartSet_OverconstraintListener::appendConflictingObjects(
154 const std::set<ObjectPtr>& theConflictingObjects)
156 std::set<ObjectPtr> aModifiedObjects;
158 // set error state for new objects and append them in the internal map of objects
159 std::set<ObjectPtr>::const_iterator
160 anIt = theConflictingObjects.begin(), aLast = theConflictingObjects.end();
161 for (; anIt != aLast; anIt++) {
162 ObjectPtr anObject = *anIt;
163 if (myConflictingObjects.find(anObject) == myConflictingObjects.end()) { // it is not found
164 aModifiedObjects.insert(anObject);
165 myConflictingObjects.insert(anObject);
168 bool isUpdated = !aModifiedObjects.empty();
170 redisplayObjects(aModifiedObjects);
175 bool PartSet_OverconstraintListener::repairConflictingObjects(
176 const std::set<ObjectPtr>& theConflictingObjects)
178 std::set<ObjectPtr> aModifiedObjects;
179 // erase error state of absent current objects in the parameter list
180 std::set<ObjectPtr>::const_iterator anIt, aLast;
181 for (anIt = theConflictingObjects.begin(), aLast = theConflictingObjects.end();
182 anIt != aLast; anIt++) {
183 ObjectPtr anObject = *anIt;
184 if (theConflictingObjects.find(anObject) != theConflictingObjects.end()) { // it is found
185 myConflictingObjects.erase(anObject);
187 aModifiedObjects.insert(anObject);
190 bool isUpdated = !aModifiedObjects.empty();
192 redisplayObjects(aModifiedObjects);
197 void PartSet_OverconstraintListener::redisplayObjects(
198 const std::set<ObjectPtr>& theObjects)
200 static Events_Loop* aLoop = Events_Loop::loop();
202 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
203 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
205 std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
206 for (; anIt != aLast; anIt++)
207 aECreator->sendUpdated(*anIt, EVENT_DISP);
209 aLoop->flush(EVENT_DISP);
212 XGUI_Workshop* PartSet_OverconstraintListener::workshop() const
214 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
215 return aConnector->workshop();
219 QString PartSet_OverconstraintListener::getObjectsInfo(const std::set<ObjectPtr>& theObjects)
221 std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(),
222 aLast = theObjects.end();
224 for (; anIt != aLast; ++anIt)
225 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
227 return anInfo.join(";\n");