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