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>
8 #include <ModelAPI_AttributeString.h>
10 #include "PartSet_OverconstraintListener.h"
11 #include <PartSet_Module.h>
12 #include <PartSet_SketcherMgr.h>
13 #include <PartSet_SketcherReentrantMgr.h>
15 #include "XGUI_ModuleConnector.h"
16 #include "XGUI_Workshop.h"
17 #include "XGUI_Displayer.h"
18 #include "XGUI_CustomPrs.h"
20 #include "SketcherPrs_SymbolPrs.h"
21 #include "SketchPlugin_SketchEntity.h"
22 #include "SketchPlugin_MacroArcReentrantMessage.h"
23 #include "SketchPlugin_Sketch.h"
25 #include "Events_Loop.h"
27 #include <GeomAPI_IPresentable.h>
28 #include <ModelAPI_Events.h>
29 #include <ModelAPI_EventReentrantMessage.h>
30 #include <ModuleBase_Tools.h>
34 //#define DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
36 PartSet_OverconstraintListener::PartSet_OverconstraintListener(ModuleBase_IWorkshop* theWorkshop)
37 : myWorkshop(theWorkshop), myIsActive(false), myIsFullyConstrained(false)
39 Events_Loop* aLoop = Events_Loop::loop();
40 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SOLVER_FAILED));
41 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SOLVER_REPAIRED));
43 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SKETCH_UNDER_CONSTRAINED));
44 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SKETCH_FULLY_CONSTRAINED));
46 aLoop->registerListener(this, ModelAPI_EventReentrantMessage::eventId());
47 aLoop->registerListener(this, SketchPlugin_MacroArcReentrantMessage::eventId());
50 void PartSet_OverconstraintListener::setActive(const bool& theActive)
52 if (myIsActive == theActive)
55 myIsActive = theActive;
56 myIsFullyConstrained = false; /// returned to default state, no custom color for it
59 PartSet_Module* aModule = module();
60 CompositeFeaturePtr aSketch = aModule->sketchMgr()->activeSketch();
62 std::string aDOFMessage = aSketch->string(SketchPlugin_Sketch::SOLVER_DOF())->value();
63 myIsFullyConstrained = QString(aDOFMessage.c_str()).contains("DoF = 0");
68 void PartSet_OverconstraintListener::getCustomColor(const ObjectPtr& theObject,
69 std::vector<int>& theColor)
74 if (myConflictingObjects.find(theObject) != myConflictingObjects.end()) {
75 theColor = Config_PropManager::color("Visualization", "sketch_overconstraint_color");
77 if (myIsFullyConstrained) {
78 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
79 // only entity features has custom color when sketch is fully constrained
80 if (aFeature.get() && PartSet_SketcherMgr::isEntity(aFeature->getKind())) {
81 PartSet_Module* aModule = module();
82 CompositeFeaturePtr aSketch = aModule->sketchMgr()->activeSketch();
83 // the given object is sub feature of the current sketch(created or edited)
84 if (ModelAPI_Tools::compositeOwner(aFeature) == aSketch)
85 theColor = Config_PropManager::color("Visualization", "sketch_fully_constrained_color");
90 void PartSet_OverconstraintListener::processEvent(
91 const std::shared_ptr<Events_Message>& theMessage)
96 #ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
97 bool isRepaired = theMessage->eventID() == Events_Loop::eventByName(EVENT_SOLVER_REPAIRED);
100 std::shared_ptr<ModelAPI_SolverFailedMessage> anErrorMsg =
101 std::dynamic_pointer_cast<ModelAPI_SolverFailedMessage>(theMessage);
103 if (anErrorMsg.get()) {
104 const std::set<ObjectPtr>& aConflictingObjects = anErrorMsg->objects();
105 aCount = aConflictingObjects.size();
106 anInfoStr = getObjectsInfo(aConflictingObjects);
109 QString aCurrentInfoStr = getObjectsInfo(myConflictingObjects);
111 QString aMsg("PartSet_OverconstraintListener::processEvent: %1,\nobjects "
112 "count = %2:%3\ncurrent objects count = %4:%5");
113 qDebug(aMsg.arg(isRepaired ? "REPAIRED" : "FAILED")
114 .arg(aCount).arg(anInfoStr).arg(myConflictingObjects.size())
115 .arg(aCurrentInfoStr).toStdString().c_str());
118 Events_ID anEventID = theMessage->eventID();
119 if (anEventID == Events_Loop::eventByName(EVENT_SOLVER_FAILED) ||
120 anEventID == Events_Loop::eventByName(EVENT_SOLVER_REPAIRED)) {
121 std::shared_ptr<ModelAPI_SolverFailedMessage> anErrorMsg =
122 std::dynamic_pointer_cast<ModelAPI_SolverFailedMessage>(theMessage);
123 bool anUpdated = false;
124 if (anErrorMsg.get()) {
125 const std::set<ObjectPtr>& aConflictingObjects = anErrorMsg->objects();
126 if (anEventID == Events_Loop::eventByName(EVENT_SOLVER_FAILED))
127 anUpdated = appendConflictingObjects(aConflictingObjects);
129 anUpdated = repairConflictingObjects(aConflictingObjects);
132 else if (anEventID == Events_Loop::eventByName(EVENT_SKETCH_UNDER_CONSTRAINED) ||
133 anEventID == Events_Loop::eventByName(EVENT_SKETCH_FULLY_CONSTRAINED)) {
134 bool aPrevFullyConstrained = myIsFullyConstrained;
135 myIsFullyConstrained = anEventID == Events_Loop::eventByName(EVENT_SKETCH_FULLY_CONSTRAINED);
137 if (aPrevFullyConstrained != myIsFullyConstrained) {
138 std::set<ObjectPtr> aModifiedObjects;
139 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
140 CompositeFeaturePtr aSketch = aModule->sketchMgr()->activeSketch();
142 for (int i = 0; i < aSketch->numberOfSubs(); i++) {
143 FeaturePtr aFeature = aSketch->subFeature(i);
144 aModifiedObjects.insert(aFeature); // is necessary to redisplay presentations
145 std::list<ResultPtr> aResults = aFeature->results();
146 for (std::list<ResultPtr>::const_iterator aIt = aResults.begin();
147 aIt != aResults.end(); ++aIt) {
148 aModifiedObjects.insert(*aIt);
151 redisplayObjects(aModifiedObjects);
155 else if (anEventID == ModelAPI_EventReentrantMessage::eventId() ||
156 anEventID == SketchPlugin_MacroArcReentrantMessage::eventId()) {
157 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
158 PartSet_SketcherReentrantMgr* aReentrantMgr = aModule->sketchReentranceMgr();
159 aReentrantMgr->setReentrantMessage(theMessage);
162 #ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
163 aCurrentInfoStr = getObjectsInfo(myConflictingObjects);
164 qDebug(QString("RESULT: current objects count = %1:%2\n")
165 .arg(myConflictingObjects.size()).arg(aCurrentInfoStr).toStdString().c_str());
169 bool PartSet_OverconstraintListener::appendConflictingObjects(
170 const std::set<ObjectPtr>& theConflictingObjects)
172 std::set<ObjectPtr> aModifiedObjects;
174 // set error state for new objects and append them in the internal map of objects
175 std::set<ObjectPtr>::const_iterator
176 anIt = theConflictingObjects.begin(), aLast = theConflictingObjects.end();
177 for (; anIt != aLast; anIt++) {
178 ObjectPtr anObject = *anIt;
179 if (myConflictingObjects.find(anObject) == myConflictingObjects.end()) { // it is not found
180 aModifiedObjects.insert(anObject);
181 myConflictingObjects.insert(anObject);
184 bool isUpdated = !aModifiedObjects.empty();
186 redisplayObjects(aModifiedObjects);
191 bool PartSet_OverconstraintListener::repairConflictingObjects(
192 const std::set<ObjectPtr>& theConflictingObjects)
194 std::set<ObjectPtr> aModifiedObjects;
195 // erase error state of absent current objects in the parameter list
196 std::set<ObjectPtr>::const_iterator anIt, aLast;
197 for (anIt = theConflictingObjects.begin(), aLast = theConflictingObjects.end();
198 anIt != aLast; anIt++) {
199 ObjectPtr anObject = *anIt;
200 if (theConflictingObjects.find(anObject) != theConflictingObjects.end()) { // it is found
201 myConflictingObjects.erase(anObject);
203 aModifiedObjects.insert(anObject);
206 bool isUpdated = !aModifiedObjects.empty();
208 redisplayObjects(aModifiedObjects);
213 void PartSet_OverconstraintListener::redisplayObjects(
214 const std::set<ObjectPtr>& theObjects)
216 static Events_Loop* aLoop = Events_Loop::loop();
218 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
219 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
221 std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
222 for (; anIt != aLast; anIt++)
223 aECreator->sendUpdated(*anIt, EVENT_DISP);
225 aLoop->flush(EVENT_DISP);
228 PartSet_Module* PartSet_OverconstraintListener::module() const
230 return dynamic_cast<PartSet_Module*>(myWorkshop->module());
234 QString PartSet_OverconstraintListener::getObjectsInfo(const std::set<ObjectPtr>& theObjects)
236 std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(),
237 aLast = theObjects.end();
239 for (; anIt != aLast; ++anIt)
240 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
242 return anInfo.join(";\n");