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