Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[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 email : webmaster.salome@opencascade.com<mailto: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   if (myConflictingObjects.find(theObject) != myConflictingObjects.end()) {
90     theColor = Config_PropManager::color("Visualization", "sketch_overconstraint_color");
91   }
92   if (myIsFullyConstrained) {
93     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
94     // only entity features has custom color when sketch is fully constrained
95     if (aFeature.get() && PartSet_SketcherMgr::isEntity(aFeature->getKind()) &&
96         !PartSet_SketcherMgr::isExternalFeature(aFeature)) {
97       PartSet_Module* aModule = module();
98       CompositeFeaturePtr aSketch = aModule->sketchMgr()->activeSketch();
99       // the given object is sub feature of the current sketch(created or edited)
100       if (ModelAPI_Tools::compositeOwner(aFeature) == aSketch)
101         theColor = Config_PropManager::color("Visualization", "sketch_fully_constrained_color");
102     }
103   }
104 }
105
106 void PartSet_OverconstraintListener::processEvent(
107                                                  const std::shared_ptr<Events_Message>& theMessage)
108 {
109   if (!myIsActive)
110     return;
111
112 #ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
113   bool isRepaired = theMessage->eventID() == Events_Loop::eventByName(EVENT_SOLVER_REPAIRED);
114   int aCount = 0;
115
116   std::shared_ptr<ModelAPI_SolverFailedMessage> anErrorMsg =
117                   std::dynamic_pointer_cast<ModelAPI_SolverFailedMessage>(theMessage);
118   QString anInfoStr;
119   if (anErrorMsg.get()) {
120     const std::set<ObjectPtr>& aConflictingObjects = anErrorMsg->objects();
121     aCount = aConflictingObjects.size();
122     anInfoStr = getObjectsInfo(aConflictingObjects);
123   }
124
125   QString aCurrentInfoStr = getObjectsInfo(myConflictingObjects);
126
127   QString aMsg("PartSet_OverconstraintListener::processEvent: %1,\nobjects "
128                "count = %2:%3\ncurrent objects count = %4:%5");
129   qDebug(aMsg.arg(isRepaired ? "REPAIRED" : "FAILED")
130              .arg(aCount).arg(anInfoStr).arg(myConflictingObjects.size())
131              .arg(aCurrentInfoStr).toStdString().c_str());
132 #endif
133
134   Events_ID anEventID = theMessage->eventID();
135   if (anEventID == Events_Loop::eventByName(EVENT_SOLVER_FAILED) ||
136       anEventID == Events_Loop::eventByName(EVENT_SOLVER_REPAIRED)) {
137     std::shared_ptr<ModelAPI_SolverFailedMessage> anErrorMsg =
138                    std::dynamic_pointer_cast<ModelAPI_SolverFailedMessage>(theMessage);
139     bool anUpdated = false;
140     if (anErrorMsg.get()) {
141       const std::set<ObjectPtr>& aConflictingObjects = anErrorMsg->objects();
142       if (anEventID == Events_Loop::eventByName(EVENT_SOLVER_FAILED))
143         anUpdated = appendConflictingObjects(aConflictingObjects);
144       else
145         anUpdated = repairConflictingObjects(aConflictingObjects);
146     }
147   }
148   else if (anEventID == Events_Loop::eventByName(EVENT_SKETCH_UNDER_CONSTRAINED) ||
149            anEventID == Events_Loop::eventByName(EVENT_SKETCH_FULLY_CONSTRAINED)) {
150     bool aPrevFullyConstrained = myIsFullyConstrained;
151     myIsFullyConstrained = anEventID == Events_Loop::eventByName(EVENT_SKETCH_FULLY_CONSTRAINED);
152
153     if (aPrevFullyConstrained != myIsFullyConstrained) {
154       std::set<ObjectPtr> aModifiedObjects;
155       PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
156       CompositeFeaturePtr aSketch = aModule->sketchMgr()->activeSketch();
157       if (aSketch.get()) {
158         for (int i = 0; i < aSketch->numberOfSubs(); i++) {
159           FeaturePtr aFeature = aSketch->subFeature(i);
160           aModifiedObjects.insert(aFeature); // is necessary to redisplay presentations
161           std::list<ResultPtr> aResults = aFeature->results();
162           for (std::list<ResultPtr>::const_iterator aIt = aResults.begin();
163                aIt != aResults.end(); ++aIt) {
164             aModifiedObjects.insert(*aIt);
165           }
166         }
167         redisplayObjects(aModifiedObjects);
168       }
169     }
170   }
171   else if (anEventID == ModelAPI_EventReentrantMessage::eventId() ||
172            anEventID == SketchPlugin_MacroArcReentrantMessage::eventId()) {
173     // the message is sent to sketcher reentrant manager only if the name of feature
174     // sender is equal to feature name of the current operation. E.g. Horizontal create operation
175     // is active. Sketch Line feature is changed, so execute is called, it will send message
176     // This Line's message should not be processed, as the reentrant operation is not for Line
177     // It is not enoght of kind, the name should be used, e.g. restarted Lines on auxiliary
178     // cirlce sometimes causes previous line change, kind the same, but feature line is different
179     std::string aCurrentFeatureName;
180     ModuleBase_Operation* anOperation =
181                 XGUI_Tools::workshop(myWorkshop)->operationMgr()->currentOperation();
182     if (anOperation) {
183       ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
184                                                            (anOperation);
185       if (aFOperation) {
186         FeaturePtr aFeature = aFOperation->feature();
187         // data valid is necessary if the feature has been already deleted
188         // (e.g. Esc of Lenght if lenght value is modified)
189         if (aFeature.get() && aFeature->data()->isValid())
190           aCurrentFeatureName = aFeature->data()->name();
191       }
192     }
193     if (theMessage->sender()) {
194       ModelAPI_Object* aSender = static_cast<ModelAPI_Object*>(theMessage->sender());
195       if (aSender) {
196         FeaturePtr aFeatureSender =
197           std::dynamic_pointer_cast<ModelAPI_Feature>(aSender->data()->owner());
198         if (aFeatureSender.get() && aFeatureSender->data()->name() == aCurrentFeatureName) {
199           PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
200           PartSet_SketcherReentrantMgr* aReentrantMgr = aModule->sketchReentranceMgr();
201           aReentrantMgr->setReentrantMessage(theMessage);
202         }
203       }
204     }
205   }
206
207 #ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
208   aCurrentInfoStr = getObjectsInfo(myConflictingObjects);
209   qDebug(QString("RESULT: current objects count = %1:%2\n")
210                 .arg(myConflictingObjects.size()).arg(aCurrentInfoStr).toStdString().c_str());
211 #endif
212 }
213
214 bool PartSet_OverconstraintListener::appendConflictingObjects(
215                                                const std::set<ObjectPtr>& theConflictingObjects)
216 {
217   std::set<ObjectPtr> aModifiedObjects;
218
219   // set error state for new objects and append them in the internal map of objects
220   std::set<ObjectPtr>::const_iterator
221     anIt = theConflictingObjects.begin(), aLast = theConflictingObjects.end();
222   for (; anIt != aLast; anIt++) {
223     ObjectPtr anObject = *anIt;
224     if (myConflictingObjects.find(anObject) == myConflictingObjects.end()) { // it is not found
225       aModifiedObjects.insert(anObject);
226       myConflictingObjects.insert(anObject);
227     }
228   }
229   bool isUpdated = !aModifiedObjects.empty();
230   if (isUpdated)
231     redisplayObjects(aModifiedObjects);
232
233   return isUpdated;
234 }
235
236 bool PartSet_OverconstraintListener::repairConflictingObjects(
237                                               const std::set<ObjectPtr>& theConflictingObjects)
238 {
239   std::set<ObjectPtr> aModifiedObjects;
240   // erase error state of absent current objects in the parameter list
241   std::set<ObjectPtr>::const_iterator anIt, aLast;
242   for (anIt = theConflictingObjects.begin(), aLast = theConflictingObjects.end();
243        anIt != aLast; anIt++) {
244     ObjectPtr anObject = *anIt;
245     if (theConflictingObjects.find(anObject) != theConflictingObjects.end()) { // it is found
246       myConflictingObjects.erase(anObject);
247
248       aModifiedObjects.insert(anObject);
249     }
250   }
251   bool isUpdated = !aModifiedObjects.empty();
252   if (isUpdated)
253     redisplayObjects(aModifiedObjects);
254
255   return isUpdated;
256 }
257
258 void PartSet_OverconstraintListener::redisplayObjects(
259                                               const std::set<ObjectPtr>& theObjects)
260 {
261   static Events_Loop* aLoop = Events_Loop::loop();
262
263   static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
264   static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
265
266   std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
267   for (; anIt != aLast; anIt++)
268     aECreator->sendUpdated(*anIt, EVENT_DISP);
269
270   aLoop->flush(EVENT_DISP);
271 }
272
273 PartSet_Module* PartSet_OverconstraintListener::module() const
274 {
275   return dynamic_cast<PartSet_Module*>(myWorkshop->module());
276 }
277
278 #ifdef _DEBUG
279 QString PartSet_OverconstraintListener::getObjectsInfo(const std::set<ObjectPtr>& theObjects)
280 {
281   std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(),
282                                       aLast = theObjects.end();
283   QStringList anInfo;
284   for (; anIt != aLast; ++anIt)
285     anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
286
287   return anInfo.join(";\n");
288 }
289 #endif