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