Salome HOME
Merge branch 'master' into cgt/devCEA
[modules/shaper.git] / src / SketchSolver / SketchSolver_Manager.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:    SketchSolver_Manager.cpp
4 // Created: 08 May 2014
5 // Author:  Artem ZHIDKOV
6
7 #include "SketchSolver_Manager.h"
8 #include "SketchSolver_Error.h"
9
10 #include <Events_Loop.h>
11 #include <ModelAPI_Events.h>
12 #include <ModelAPI_ResultConstruction.h>
13 #include <ModelAPI_Session.h>
14 #include <ModelAPI_Validator.h>
15 #include <SketchPlugin_Sketch.h>
16
17 /// Global constraint manager object
18 static SketchSolver_Manager* myManager = SketchSolver_Manager::instance();
19
20 /// \brief Verifies is the feature valid
21 static bool isFeatureValid(FeaturePtr theFeature)
22 {
23   if (!theFeature || !theFeature->data() || !theFeature->data()->isValid())
24     return false;
25
26   SessionPtr aMgr = ModelAPI_Session::get();
27   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
28   return aFactory->validate(theFeature);
29 }
30
31
32
33 // ========================================================
34 // ========= SketchSolver_Manager ===============
35 // ========================================================
36 SketchSolver_Manager* SketchSolver_Manager::instance()
37 {
38   static SketchSolver_Manager* mySelf = 0; // Self pointer to implement singleton functionality
39   if (!mySelf)
40     mySelf = new SketchSolver_Manager();
41   return mySelf;
42 }
43
44 SketchSolver_Manager::SketchSolver_Manager()
45 {
46   myGroups.clear();
47   myIsComputed = false;
48
49   // Register in event loop
50   Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
51   Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
52   Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
53   Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_MOVED));
54
55   ////Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_SOLVER_FAILED));
56   ////Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_SOLVER_REPAIRED));
57   Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_SKETCH_PREPARED));
58 }
59
60 SketchSolver_Manager::~SketchSolver_Manager()
61 {
62   myGroups.clear();
63 }
64
65 bool SketchSolver_Manager::groupMessages()
66 {
67   return true;
68 }
69
70 // ============================================================================
71 //  Function: processEvent
72 //  Purpose:  listen the event loop and process the message
73 // ============================================================================
74 void SketchSolver_Manager::processEvent(
75   const std::shared_ptr<Events_Message>& theMessage)
76 {
77   bool needToResolve = false;
78   bool isUpdateFlushed = false;
79   bool isMovedEvt = false;
80
81   static const Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
82   static const Events_ID aSketchPreparedEvent = Events_Loop::eventByName(EVENT_SKETCH_PREPARED);
83   // sketch is prepared for resolve: all the needed events
84   // are collected and must be processed by the solver
85   if (theMessage->eventID() == aSketchPreparedEvent) {
86     flushGrouped(anUpdateEvent);
87     needToResolve = true;
88   }
89
90   if (myIsComputed)
91     return;
92   myIsComputed = true;
93
94   if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)
95       || theMessage->eventID() == anUpdateEvent
96       || theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_MOVED)) {
97     std::shared_ptr<ModelAPI_ObjectUpdatedMessage> anUpdateMsg =
98         std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
99     std::set<ObjectPtr> aFeatures = anUpdateMsg->objects();
100
101     isUpdateFlushed = stopSendUpdate();
102
103     isMovedEvt = theMessage->eventID()
104           == Events_Loop::loop()->eventByName(EVENT_OBJECT_MOVED);
105
106     // Shows that the message has at least one feature applicable for solver
107     bool hasProperFeature = false;
108
109     // update sketch features only
110     std::set<ObjectPtr>::iterator aFeatIter;
111     for (aFeatIter = aFeatures.begin(); aFeatIter != aFeatures.end(); aFeatIter++) {
112       std::shared_ptr<SketchPlugin_Feature> aFeature =
113           std::dynamic_pointer_cast<SketchPlugin_Feature>(*aFeatIter);
114       if (!aFeature || aFeature->isMacro())
115         continue;
116
117       hasProperFeature = updateFeature(aFeature, isMovedEvt) || hasProperFeature;
118     }
119
120     if (isMovedEvt && hasProperFeature)
121       needToResolve = true;
122
123   } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)) {
124     std::shared_ptr<ModelAPI_ObjectDeletedMessage> aDeleteMsg =
125       std::dynamic_pointer_cast<ModelAPI_ObjectDeletedMessage>(theMessage);
126     const std::set<std::string>& aFeatureGroups = aDeleteMsg->groups();
127
128     // Find SketchPlugin_Sketch::ID() in groups.
129     // The constraint groups should be updated when an object removed from Sketch
130     std::set<std::string>::const_iterator aFGrIter;
131     for (aFGrIter = aFeatureGroups.begin(); aFGrIter != aFeatureGroups.end(); aFGrIter++)
132       if (aFGrIter->compare(ModelAPI_ResultConstruction::group()) == 0 ||
133           aFGrIter->compare(ModelAPI_Feature::group()) == 0)
134         break;
135
136     if (aFGrIter != aFeatureGroups.end()) {
137       std::list<SketchGroupPtr>::iterator aGroupIter = myGroups.begin();
138       while (aGroupIter != myGroups.end()) {
139         if (!(*aGroupIter)->isWorkplaneValid()) {  // the group should be removed
140           std::list<SketchGroupPtr>::iterator aRemoveIt = aGroupIter++;
141           myGroups.erase(aRemoveIt);
142           continue;
143         }
144
145         (*aGroupIter)->repairConsistency();
146         ++aGroupIter;
147       }
148     }
149     myIsComputed = false;
150   }
151
152   // resolve constraints if needed
153   bool needToUpdate = needToResolve && resolveConstraints();
154
155   // Features may be updated => now send events, but for all changed at once
156   if (isUpdateFlushed)
157     allowSendUpdate();
158
159   myIsComputed = false;
160
161   // send update for movement in any case
162   if (needToUpdate || isMovedEvt)
163     Events_Loop::loop()->flush(anUpdateEvent);
164 }
165
166 // ============================================================================
167 //  Function: changeConstraintOrEntity
168 //  Purpose:  create/update the constraint or the feature and place it into appropriate group
169 // ============================================================================
170 bool SketchSolver_Manager::updateFeature(std::shared_ptr<SketchPlugin_Feature> theFeature,
171                                          bool theMoved)
172 {
173   // Check feature validity and find a group to place it.
174   // If the feature is not valid, the returned group will be empty.
175   // This will protect to deal with wrong (not fully initialized) features.
176   SketchGroupPtr aGroup = findGroup(theFeature);
177   if (!aGroup)
178     return false;
179   aGroup->blockEvents(true);
180
181   std::shared_ptr<SketchPlugin_Constraint> aConstraint =
182       std::dynamic_pointer_cast<SketchPlugin_Constraint>(theFeature);
183
184   bool isOk = false;
185   if (aConstraint)
186     isOk = aGroup->changeConstraint(aConstraint);
187   else if (theMoved)
188     isOk = aGroup->moveFeature(theFeature);
189   else
190     isOk = aGroup->updateFeature(theFeature);
191   return isOk;
192 }
193
194 // ============================================================================
195 //  Function: findGroup
196 //  Purpose:  search groups of entities interacting with given feature
197 // ============================================================================
198 SketchGroupPtr SketchSolver_Manager::findGroup(
199     std::shared_ptr<SketchPlugin_Feature> theFeature)
200 {
201   if (!isFeatureValid(theFeature))
202     return SketchGroupPtr(); // do not process wrong features
203
204   // Obtain sketch, containing the feature
205   CompositeFeaturePtr aSketch;
206   const std::set<AttributePtr>& aRefsList = theFeature->data()->refsToMe();
207   std::set<AttributePtr>::const_iterator aRefIt = aRefsList.begin();
208   for (; aRefIt != aRefsList.end(); ++aRefIt) {
209     FeaturePtr anOwner = ModelAPI_Feature::feature((*aRefIt)->owner());
210     if (anOwner && anOwner->getKind() == SketchPlugin_Sketch::ID()) {
211       aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(anOwner);
212       break;
213     }
214   }
215
216   if (!aSketch)
217     return SketchGroupPtr(); // not a sketch's feature
218
219   std::list<SketchGroupPtr>::const_iterator aGroupIt;
220   for (aGroupIt = myGroups.begin(); aGroupIt != myGroups.end(); ++aGroupIt)
221     if ((*aGroupIt)->getWorkplane() == aSketch)
222       return *aGroupIt;
223
224   // group for the sketch does not created yet
225   SketchGroupPtr aNewGroup = SketchGroupPtr(new SketchSolver_Group(aSketch));
226   myGroups.push_back(aNewGroup);
227   return aNewGroup;
228 }
229
230 // ============================================================================
231 //  Function: resolveConstraints
232 //  Purpose:  change entities according to available constraints
233 // ============================================================================
234 bool SketchSolver_Manager::resolveConstraints()
235 {
236   bool needToUpdate = false;
237   std::list<SketchGroupPtr>::const_iterator aGroupIter = myGroups.begin();
238   for (; aGroupIter != myGroups.end(); ++aGroupIter) {
239     if ((*aGroupIter)->resolveConstraints())
240       needToUpdate = true;
241     (*aGroupIter)->blockEvents(false);
242   }
243   return needToUpdate;
244 }
245
246 bool SketchSolver_Manager::stopSendUpdate() const
247 {
248 static const Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
249   // to avoid redisplay of each segment on update by solver one by one in the viewer
250   bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
251   if (isUpdateFlushed) {
252     Events_Loop::loop()->setFlushed(anUpdateEvent, false);
253   }
254   return isUpdateFlushed;
255 }
256
257 void SketchSolver_Manager::allowSendUpdate() const
258 {
259   Events_Loop::loop()->setFlushed(Events_Loop::eventByName(EVENT_OBJECT_UPDATED), true);
260 }