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)
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 theColor = Config_PropManager::color("Visualization", "sketch_overconstraint_color");
51 if (myIsFullyConstrained) {
52 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
53 // only entity features has custom color when sketch is fully constrained
54 if (aFeature.get() && PartSet_SketcherMgr::isEntity(aFeature->getKind())) {
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 theColor = Config_PropManager::color("Visualization", "sketch_fully_constrained_color");
64 void PartSet_OverconstraintListener::processEvent(
65 const std::shared_ptr<Events_Message>& theMessage)
67 #ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
68 bool isRepaired = theMessage->eventID() == Events_Loop::eventByName(EVENT_SOLVER_REPAIRED);
71 std::shared_ptr<ModelAPI_SolverFailedMessage> anErrorMsg =
72 std::dynamic_pointer_cast<ModelAPI_SolverFailedMessage>(theMessage);
74 if (anErrorMsg.get()) {
75 const std::set<ObjectPtr>& aConflictingObjects = anErrorMsg->objects();
76 aCount = aConflictingObjects.size();
77 anInfoStr = getObjectsInfo(aConflictingObjects);
80 QString aCurrentInfoStr = getObjectsInfo(myConflictingObjects);
82 QString aMsg("PartSet_OverconstraintListener::processEvent: %1,\nobjects "
83 "count = %2:%3\ncurrent objects count = %4:%5");
84 qDebug(aMsg.arg(isRepaired ? "REPAIRED" : "FAILED")
85 .arg(aCount).arg(anInfoStr).arg(myConflictingObjects.size())
86 .arg(aCurrentInfoStr).toStdString().c_str());
89 Events_ID anEventID = theMessage->eventID();
90 if (anEventID == Events_Loop::eventByName(EVENT_SOLVER_FAILED) ||
91 anEventID == Events_Loop::eventByName(EVENT_SOLVER_REPAIRED)) {
92 std::shared_ptr<ModelAPI_SolverFailedMessage> anErrorMsg =
93 std::dynamic_pointer_cast<ModelAPI_SolverFailedMessage>(theMessage);
94 bool anUpdated = false;
95 if (anErrorMsg.get()) {
96 const std::set<ObjectPtr>& aConflictingObjects = anErrorMsg->objects();
97 if (anEventID == Events_Loop::eventByName(EVENT_SOLVER_FAILED))
98 anUpdated = appendConflictingObjects(aConflictingObjects);
100 anUpdated = repairConflictingObjects(aConflictingObjects);
103 else if (anEventID == Events_Loop::eventByName(EVENT_SKETCH_UNDER_CONSTRAINED) ||
104 anEventID == Events_Loop::eventByName(EVENT_SKETCH_FULLY_CONSTRAINED)) {
105 bool aPrevFullyConstrained = myIsFullyConstrained;
106 myIsFullyConstrained = anEventID == Events_Loop::eventByName(EVENT_SKETCH_FULLY_CONSTRAINED);
108 if (aPrevFullyConstrained != myIsFullyConstrained) {
109 std::set<ObjectPtr> aModifiedObjects;
110 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
111 CompositeFeaturePtr aSketch = aModule->sketchMgr()->activeSketch();
113 for (int i = 0; i < aSketch->numberOfSubs(); i++) {
114 FeaturePtr aFeature = aSketch->subFeature(i);
115 aModifiedObjects.insert(aFeature); // is necessary to redisplay presentations
116 std::list<ResultPtr> aResults = aFeature->results();
117 for (std::list<ResultPtr>::const_iterator aIt = aResults.begin();
118 aIt != aResults.end(); ++aIt) {
119 aModifiedObjects.insert(*aIt);
122 redisplayObjects(aModifiedObjects);
126 else if (anEventID == Events_Loop::eventByName(EVENT_OBJECT_CREATED)) {
127 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
128 PartSet_SketcherReentrantMgr* aReentrantMgr = aModule->sketchReentranceMgr();
129 if (aReentrantMgr->isInternalEditActive()) {
130 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
131 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
132 std::set<ObjectPtr> anObjects = aUpdMsg->objects();
133 aReentrantMgr->appendCreatedObjects(anObjects);
136 #ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
137 aCurrentInfoStr = getObjectsInfo(myConflictingObjects);
138 qDebug(QString("RESULT: current objects count = %1:%2\n")
139 .arg(myConflictingObjects.size()).arg(aCurrentInfoStr).toStdString().c_str());
143 bool PartSet_OverconstraintListener::appendConflictingObjects(
144 const std::set<ObjectPtr>& theConflictingObjects)
146 std::set<ObjectPtr> aModifiedObjects;
148 // set error state for new objects and append them in the internal map of objects
149 std::set<ObjectPtr>::const_iterator
150 anIt = theConflictingObjects.begin(), aLast = theConflictingObjects.end();
151 for (; anIt != aLast; anIt++) {
152 ObjectPtr anObject = *anIt;
153 if (myConflictingObjects.find(anObject) == myConflictingObjects.end()) { // it is not found
154 aModifiedObjects.insert(anObject);
155 myConflictingObjects.insert(anObject);
158 bool isUpdated = !aModifiedObjects.empty();
160 redisplayObjects(aModifiedObjects);
165 bool PartSet_OverconstraintListener::repairConflictingObjects(
166 const std::set<ObjectPtr>& theConflictingObjects)
168 std::set<ObjectPtr> aModifiedObjects;
169 // erase error state of absent current objects in the parameter list
170 std::set<ObjectPtr>::const_iterator anIt, aLast;
171 for (anIt = theConflictingObjects.begin(), aLast = theConflictingObjects.end();
172 anIt != aLast; anIt++) {
173 ObjectPtr anObject = *anIt;
174 if (theConflictingObjects.find(anObject) != theConflictingObjects.end()) { // it is found
175 myConflictingObjects.erase(anObject);
177 aModifiedObjects.insert(anObject);
180 bool isUpdated = !aModifiedObjects.empty();
182 redisplayObjects(aModifiedObjects);
187 void PartSet_OverconstraintListener::redisplayObjects(
188 const std::set<ObjectPtr>& theObjects)
190 static Events_Loop* aLoop = Events_Loop::loop();
192 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
193 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
195 std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
196 for (; anIt != aLast; anIt++)
197 aECreator->sendUpdated(*anIt, EVENT_DISP);
199 aLoop->flush(EVENT_DISP);
202 XGUI_Workshop* PartSet_OverconstraintListener::workshop() const
204 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
205 return aConnector->workshop();
209 QString PartSet_OverconstraintListener::getObjectsInfo(const std::set<ObjectPtr>& theObjects)
211 std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(),
212 aLast = theObjects.end();
214 for (; anIt != aLast; ++anIt)
215 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
217 return anInfo.join(";\n");