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)
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 bool aPrevFullyConstrained = myIsFullyConstrained;
114 myIsFullyConstrained = anEventID == Events_Loop::eventByName(EVENT_SKETCH_FULLY_CONSTRAINED);
116 if (aPrevFullyConstrained != myIsFullyConstrained) {
117 std::set<ObjectPtr> aModifiedObjects;
118 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
119 CompositeFeaturePtr aSketch = aModule->sketchMgr()->activeSketch();
120 for (int i = 0; i < aSketch->numberOfSubs(); i++) {
121 FeaturePtr aFeature = aSketch->subFeature(i);
122 aModifiedObjects.insert(aFeature); // is necessary to redisplay presentations
123 std::list<ResultPtr> aResults = aFeature->results();
124 for (std::list<ResultPtr>::const_iterator aIt = aResults.begin();
125 aIt != aResults.end(); ++aIt) {
126 aModifiedObjects.insert(*aIt);
129 redisplayObjects(aModifiedObjects);
133 #ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
134 aCurrentInfoStr = getObjectsInfo(myConflictingObjects);
135 qDebug(QString("RESULT: current objects count = %1:%2\n")
136 .arg(myConflictingObjects.size()).arg(aCurrentInfoStr).toStdString().c_str());
140 bool PartSet_OverconstraintListener::appendConflictingObjects(
141 const std::set<ObjectPtr>& theConflictingObjects)
143 std::set<ObjectPtr> aModifiedObjects;
145 // set error state for new objects and append them in the internal map of objects
146 std::set<ObjectPtr>::const_iterator
147 anIt = theConflictingObjects.begin(), aLast = theConflictingObjects.end();
148 for (; anIt != aLast; anIt++) {
149 ObjectPtr anObject = *anIt;
150 if (myConflictingObjects.find(anObject) == myConflictingObjects.end()) { // it is not found
151 aModifiedObjects.insert(anObject);
152 myConflictingObjects.insert(anObject);
155 bool isUpdated = !aModifiedObjects.empty();
157 redisplayObjects(aModifiedObjects);
162 bool PartSet_OverconstraintListener::repairConflictingObjects(
163 const std::set<ObjectPtr>& theConflictingObjects)
165 std::set<ObjectPtr> aModifiedObjects;
166 // erase error state of absent current objects in the parameter list
167 std::set<ObjectPtr>::const_iterator anIt, aLast;
168 for (anIt = theConflictingObjects.begin(), aLast = theConflictingObjects.end();
169 anIt != aLast; anIt++) {
170 ObjectPtr anObject = *anIt;
171 if (theConflictingObjects.find(anObject) != theConflictingObjects.end()) { // it is found
172 myConflictingObjects.erase(anObject);
174 aModifiedObjects.insert(anObject);
177 bool isUpdated = !aModifiedObjects.empty();
179 redisplayObjects(aModifiedObjects);
184 void PartSet_OverconstraintListener::redisplayObjects(
185 const std::set<ObjectPtr>& theObjects)
187 static Events_Loop* aLoop = Events_Loop::loop();
189 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
190 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
192 std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
193 for (; anIt != aLast; anIt++)
194 aECreator->sendUpdated(*anIt, EVENT_DISP);
196 aLoop->flush(EVENT_DISP);
199 XGUI_Workshop* PartSet_OverconstraintListener::workshop() const
201 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
202 return aConnector->workshop();
206 QString PartSet_OverconstraintListener::getObjectsInfo(const std::set<ObjectPtr>& theObjects)
208 std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(),
209 aLast = theObjects.end();
211 for (; anIt != aLast; ++anIt)
212 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
214 return anInfo.join(";\n");