Salome HOME
Merge branch 'master' into cgt/devCEA
[modules/shaper.git] / src / PartSet / PartSet_OverconstraintListener.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <ModelAPI_Tools.h>
22 #include <ModelAPI_AttributeString.h>
23
24 #include "PartSet_OverconstraintListener.h"
25 #include <PartSet_Module.h>
26 #include <PartSet_SketcherMgr.h>
27 #include <PartSet_SketcherReentrantMgr.h>
28
29 #include "XGUI_CustomPrs.h"
30 #include "XGUI_Displayer.h"
31 #include "XGUI_ModuleConnector.h"
32 #include "XGUI_OperationMgr.h"
33 #include "XGUI_Tools.h"
34 #include "XGUI_Workshop.h"
35
36 #include "SketcherPrs_SymbolPrs.h"
37 #include "SketchPlugin_SketchEntity.h"
38 #include "SketchPlugin_MacroArcReentrantMessage.h"
39 #include "SketchPlugin_Sketch.h"
40
41 #include "Events_Loop.h"
42
43 #include <GeomAPI_IPresentable.h>
44 #include <ModelAPI_Events.h>
45 #include <ModelAPI_EventReentrantMessage.h>
46 #include <ModuleBase_Tools.h>
47
48 #include <QString>
49
50 //#define DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
51
52 PartSet_OverconstraintListener::PartSet_OverconstraintListener(ModuleBase_IWorkshop* theWorkshop)
53 : myWorkshop(theWorkshop), myIsActive(false), myIsFullyConstrained(false)
54 {
55   Events_Loop* aLoop = Events_Loop::loop();
56   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SOLVER_FAILED));
57   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SOLVER_REPAIRED));
58
59   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SKETCH_UNDER_CONSTRAINED));
60   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SKETCH_FULLY_CONSTRAINED));
61
62   aLoop->registerListener(this, ModelAPI_EventReentrantMessage::eventId());
63   aLoop->registerListener(this, SketchPlugin_MacroArcReentrantMessage::eventId());
64 }
65
66 void PartSet_OverconstraintListener::setActive(const bool& theActive)
67 {
68   if (myIsActive == theActive)
69     return;
70
71   myIsActive = theActive;
72   myIsFullyConstrained = false; /// returned to default state, no custom color for it
73
74   if (myIsActive) {
75     PartSet_Module* aModule = module();
76     CompositeFeaturePtr aSketch = aModule->sketchMgr()->activeSketch();
77     if (aSketch.get()) {
78       std::string aDOFMessage = aSketch->string(SketchPlugin_Sketch::SOLVER_DOF())->value();
79       myIsFullyConstrained = QString(aDOFMessage.c_str()).contains("DoF = 0");
80     }
81   }
82 }
83
84 void PartSet_OverconstraintListener::getCustomColor(const ObjectPtr& theObject,
85                                                     std::vector<int>& theColor)
86 {
87   if (!myIsActive)
88     return;
89
90   if (myConflictingObjects.find(theObject) != myConflictingObjects.end()) {
91     theColor = Config_PropManager::color("Visualization", "sketch_overconstraint_color");
92   }
93   if (myIsFullyConstrained) {
94     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
95     // only entity features has custom color when sketch is fully constrained
96     if (aFeature.get() && PartSet_SketcherMgr::isEntity(aFeature->getKind()) &&
97         !PartSet_SketcherMgr::isExternalFeature(aFeature)) {
98       PartSet_Module* aModule = module();
99       CompositeFeaturePtr aSketch = aModule->sketchMgr()->activeSketch();
100       // the given object is sub feature of the current sketch(created or edited)
101       if (ModelAPI_Tools::compositeOwner(aFeature) == aSketch)
102         theColor = Config_PropManager::color("Visualization", "sketch_fully_constrained_color");
103     }
104   }
105 }
106
107 void PartSet_OverconstraintListener::processEvent(
108                                                  const std::shared_ptr<Events_Message>& theMessage)
109 {
110   if (!myIsActive)
111     return;
112
113 #ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
114   bool isRepaired = theMessage->eventID() == Events_Loop::eventByName(EVENT_SOLVER_REPAIRED);
115   int aCount = 0;
116
117   std::shared_ptr<ModelAPI_SolverFailedMessage> anErrorMsg =
118                   std::dynamic_pointer_cast<ModelAPI_SolverFailedMessage>(theMessage);
119   QString anInfoStr;
120   if (anErrorMsg.get()) {
121     const std::set<ObjectPtr>& aConflictingObjects = anErrorMsg->objects();
122     aCount = aConflictingObjects.size();
123     anInfoStr = getObjectsInfo(aConflictingObjects);
124   }
125
126   QString aCurrentInfoStr = getObjectsInfo(myConflictingObjects);
127
128   QString aMsg("PartSet_OverconstraintListener::processEvent: %1,\nobjects "
129                "count = %2:%3\ncurrent objects count = %4:%5");
130   qDebug(aMsg.arg(isRepaired ? "REPAIRED" : "FAILED")
131              .arg(aCount).arg(anInfoStr).arg(myConflictingObjects.size())
132              .arg(aCurrentInfoStr).toStdString().c_str());
133 #endif
134
135   Events_ID anEventID = theMessage->eventID();
136   if (anEventID == Events_Loop::eventByName(EVENT_SOLVER_FAILED) ||
137       anEventID == Events_Loop::eventByName(EVENT_SOLVER_REPAIRED)) {
138     std::shared_ptr<ModelAPI_SolverFailedMessage> anErrorMsg =
139                    std::dynamic_pointer_cast<ModelAPI_SolverFailedMessage>(theMessage);
140     bool anUpdated = false;
141     if (anErrorMsg.get()) {
142       const std::set<ObjectPtr>& aConflictingObjects = anErrorMsg->objects();
143       if (anEventID == Events_Loop::eventByName(EVENT_SOLVER_FAILED))
144         anUpdated = appendConflictingObjects(aConflictingObjects);
145       else
146         anUpdated = repairConflictingObjects(aConflictingObjects);
147     }
148   }
149   else if (anEventID == Events_Loop::eventByName(EVENT_SKETCH_UNDER_CONSTRAINED) ||
150            anEventID == Events_Loop::eventByName(EVENT_SKETCH_FULLY_CONSTRAINED)) {
151     bool aPrevFullyConstrained = myIsFullyConstrained;
152     myIsFullyConstrained = anEventID == Events_Loop::eventByName(EVENT_SKETCH_FULLY_CONSTRAINED);
153
154     if (aPrevFullyConstrained != myIsFullyConstrained) {
155       std::set<ObjectPtr> aModifiedObjects;
156       PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
157       CompositeFeaturePtr aSketch = aModule->sketchMgr()->activeSketch();
158       if (aSketch.get()) {
159         for (int i = 0; i < aSketch->numberOfSubs(); i++) {
160           FeaturePtr aFeature = aSketch->subFeature(i);
161           aModifiedObjects.insert(aFeature); // is necessary to redisplay presentations
162           std::list<ResultPtr> aResults = aFeature->results();
163           for (std::list<ResultPtr>::const_iterator aIt = aResults.begin();
164                aIt != aResults.end(); ++aIt) {
165             aModifiedObjects.insert(*aIt);
166           }
167         }
168         redisplayObjects(aModifiedObjects);
169       }
170     }
171   }
172   else if (anEventID == ModelAPI_EventReentrantMessage::eventId() ||
173            anEventID == SketchPlugin_MacroArcReentrantMessage::eventId()) {
174     // the message is sent to sketcher reentrant manager only if the name of feature
175     // sender is equal to feature name of the current operation. E.g. Horizontal create operation
176     // is active. Sketch Line feature is changed, so execute is called, it will send message
177     // This Line's message should not be processed, as the reentrant operation is not for Line
178     // It is not enoght of kind, the name should be used, e.g. restarted Lines on auxiliary
179     // cirlce sometimes causes previous line change, kind the same, but feature line is different
180     std::string aCurrentFeatureName;
181     ModuleBase_Operation* anOperation =
182                 XGUI_Tools::workshop(myWorkshop)->operationMgr()->currentOperation();
183     if (anOperation) {
184       ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
185                                                            (anOperation);
186       if (aFOperation) {
187         FeaturePtr aFeature = aFOperation->feature();
188         // data valid is necessary if the feature has been already deleted
189         // (e.g. Esc of Lenght if lenght value is modified)
190         if (aFeature.get() && aFeature->data()->isValid())
191           aCurrentFeatureName = aFeature->data()->name();
192       }
193     }
194     if (theMessage->sender()) {
195       ModelAPI_Object* aSender = static_cast<ModelAPI_Object*>(theMessage->sender());
196       if (aSender) {
197         FeaturePtr aFeatureSender =
198           std::dynamic_pointer_cast<ModelAPI_Feature>(aSender->data()->owner());
199         if (aFeatureSender.get() && aFeatureSender->data()->name() == aCurrentFeatureName) {
200           PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
201           PartSet_SketcherReentrantMgr* aReentrantMgr = aModule->sketchReentranceMgr();
202           aReentrantMgr->setReentrantMessage(theMessage);
203         }
204       }
205     }
206   }
207
208 #ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
209   aCurrentInfoStr = getObjectsInfo(myConflictingObjects);
210   qDebug(QString("RESULT: current objects count = %1:%2\n")
211                 .arg(myConflictingObjects.size()).arg(aCurrentInfoStr).toStdString().c_str());
212 #endif
213 }
214
215 bool PartSet_OverconstraintListener::appendConflictingObjects(
216                                                const std::set<ObjectPtr>& theConflictingObjects)
217 {
218   std::set<ObjectPtr> aModifiedObjects;
219
220   // set error state for new objects and append them in the internal map of objects
221   std::set<ObjectPtr>::const_iterator
222     anIt = theConflictingObjects.begin(), aLast = theConflictingObjects.end();
223   for (; anIt != aLast; anIt++) {
224     ObjectPtr anObject = *anIt;
225     if (myConflictingObjects.find(anObject) == myConflictingObjects.end()) { // it is not found
226       aModifiedObjects.insert(anObject);
227       myConflictingObjects.insert(anObject);
228     }
229   }
230   bool isUpdated = !aModifiedObjects.empty();
231   if (isUpdated)
232     redisplayObjects(aModifiedObjects);
233
234   return isUpdated;
235 }
236
237 bool PartSet_OverconstraintListener::repairConflictingObjects(
238                                               const std::set<ObjectPtr>& theConflictingObjects)
239 {
240   std::set<ObjectPtr> aModifiedObjects;
241   // erase error state of absent current objects in the parameter list
242   std::set<ObjectPtr>::const_iterator anIt, aLast;
243   for (anIt = theConflictingObjects.begin(), aLast = theConflictingObjects.end();
244        anIt != aLast; anIt++) {
245     ObjectPtr anObject = *anIt;
246     if (theConflictingObjects.find(anObject) != theConflictingObjects.end()) { // it is found
247       myConflictingObjects.erase(anObject);
248
249       aModifiedObjects.insert(anObject);
250     }
251   }
252   bool isUpdated = !aModifiedObjects.empty();
253   if (isUpdated)
254     redisplayObjects(aModifiedObjects);
255
256   return isUpdated;
257 }
258
259 void PartSet_OverconstraintListener::redisplayObjects(
260                                               const std::set<ObjectPtr>& theObjects)
261 {
262   static Events_Loop* aLoop = Events_Loop::loop();
263
264   static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
265   static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
266
267   std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
268   for (; anIt != aLast; anIt++)
269     aECreator->sendUpdated(*anIt, EVENT_DISP);
270
271   aLoop->flush(EVENT_DISP);
272 }
273
274 PartSet_Module* PartSet_OverconstraintListener::module() const
275 {
276   return dynamic_cast<PartSet_Module*>(myWorkshop->module());
277 }
278
279 #ifdef _DEBUG
280 QString PartSet_OverconstraintListener::getObjectsInfo(const std::set<ObjectPtr>& theObjects)
281 {
282   std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(),
283                                       aLast = theObjects.end();
284   QStringList anInfo;
285   for (; anIt != aLast; ++anIt)
286     anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
287
288   return anInfo.join(";\n");
289 }
290 #endif