Salome HOME
Merge branch 'Pre_2.8.0_development'
[modules/shaper.git] / src / SketchSolver / SketchSolver_Manager.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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "SketchSolver_Manager.h"
22 #include "SketchSolver_Error.h"
23
24 #include <Events_Loop.h>
25 #include <GeomDataAPI_Point2D.h>
26 #include <ModelAPI_Events.h>
27 #include <ModelAPI_ResultConstruction.h>
28 #include <ModelAPI_Session.h>
29 #include <ModelAPI_Validator.h>
30 #include <SketchPlugin_Constraint.h>
31 #include <SketchPlugin_Sketch.h>
32
33 /// Global constraint manager object
34 static SketchSolver_Manager* myManager = SketchSolver_Manager::instance();
35
36 /// \brief Verifies is the feature valid
37 static bool isFeatureValid(FeaturePtr theFeature)
38 {
39   if (!theFeature || !theFeature->data() || !theFeature->data()->isValid())
40     return false;
41
42   SessionPtr aMgr = ModelAPI_Session::get();
43   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
44   return aFactory->validate(theFeature);
45 }
46
47
48
49 // ========================================================
50 // ========= SketchSolver_Manager ===============
51 // ========================================================
52 SketchSolver_Manager* SketchSolver_Manager::instance()
53 {
54   static SketchSolver_Manager* mySelf = 0; // Self pointer to implement singleton functionality
55   if (!mySelf)
56     mySelf = new SketchSolver_Manager();
57   return mySelf;
58 }
59
60 SketchSolver_Manager::SketchSolver_Manager()
61 {
62   myGroups.clear();
63   myIsComputed = false;
64
65   // Register in event loop
66   Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
67   Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
68   Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
69   Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_MOVED));
70
71   Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_SKETCH_PREPARED));
72 }
73
74 SketchSolver_Manager::~SketchSolver_Manager()
75 {
76   myGroups.clear();
77 }
78
79 bool SketchSolver_Manager::groupMessages()
80 {
81   return true;
82 }
83
84 // ============================================================================
85 //  Function: processEvent
86 //  Purpose:  listen the event loop and process the message
87 // ============================================================================
88 void SketchSolver_Manager::processEvent(
89   const std::shared_ptr<Events_Message>& theMessage)
90 {
91   bool needToResolve = false;
92   bool isUpdateFlushed = false;
93   bool isMovedEvt = false;
94
95   static const Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
96   static const Events_ID aSketchPreparedEvent = Events_Loop::eventByName(EVENT_SKETCH_PREPARED);
97   // sketch is prepared for resolve: all the needed events
98   // are collected and must be processed by the solver
99   if (theMessage->eventID() == aSketchPreparedEvent) {
100     flushGrouped(anUpdateEvent);
101     needToResolve = true;
102   }
103
104   if (myIsComputed)
105     return;
106   myIsComputed = true;
107
108   if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)
109       || theMessage->eventID() == anUpdateEvent) {
110     std::shared_ptr<ModelAPI_ObjectUpdatedMessage> anUpdateMsg =
111         std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
112     std::set<ObjectPtr> aFeatures = anUpdateMsg->objects();
113
114     isUpdateFlushed = stopSendUpdate();
115
116     // update sketch features only
117     std::set<ObjectPtr>::iterator aFeatIter;
118     for (aFeatIter = aFeatures.begin(); aFeatIter != aFeatures.end(); aFeatIter++) {
119       std::shared_ptr<SketchPlugin_Feature> aFeature =
120           std::dynamic_pointer_cast<SketchPlugin_Feature>(*aFeatIter);
121       if (!aFeature || aFeature->isMacro())
122         continue;
123
124       updateFeature(aFeature);
125     }
126
127   } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_MOVED)) {
128     std::shared_ptr<ModelAPI_ObjectMovedMessage> aMoveMsg =
129         std::dynamic_pointer_cast<ModelAPI_ObjectMovedMessage>(theMessage);
130
131     ObjectPtr aMovedObject = aMoveMsg->movedObject();
132     std::shared_ptr<GeomDataAPI_Point2D> aMovedPoint =
133         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aMoveMsg->movedAttribute());
134
135     const std::shared_ptr<GeomAPI_Pnt2d>& aFrom = aMoveMsg->originalPosition();
136     const std::shared_ptr<GeomAPI_Pnt2d>& aTo = aMoveMsg->currentPosition();
137
138     if (aMovedObject) {
139       FeaturePtr aMovedFeature = ModelAPI_Feature::feature(aMovedObject);
140       std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
141           std::dynamic_pointer_cast<SketchPlugin_Feature>(aMovedFeature);
142       if (aSketchFeature && !aSketchFeature->isMacro())
143         needToResolve = moveFeature(aSketchFeature, aFrom, aTo);
144     } else if (aMovedPoint)
145       needToResolve = moveAttribute(aMovedPoint, aFrom, aTo);
146
147   } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)) {
148     std::shared_ptr<ModelAPI_ObjectDeletedMessage> aDeleteMsg =
149       std::dynamic_pointer_cast<ModelAPI_ObjectDeletedMessage>(theMessage);
150     const std::set<std::string>& aFeatureGroups = aDeleteMsg->groups();
151
152     // Find SketchPlugin_Sketch::ID() in groups.
153     // The constraint groups should be updated when an object removed from Sketch
154     std::set<std::string>::const_iterator aFGrIter;
155     for (aFGrIter = aFeatureGroups.begin(); aFGrIter != aFeatureGroups.end(); aFGrIter++)
156       if (aFGrIter->compare(ModelAPI_ResultConstruction::group()) == 0 ||
157           aFGrIter->compare(ModelAPI_Feature::group()) == 0)
158         break;
159
160     if (aFGrIter != aFeatureGroups.end()) {
161       std::list<SketchGroupPtr>::iterator aGroupIter = myGroups.begin();
162       while (aGroupIter != myGroups.end()) {
163         if (!(*aGroupIter)->isWorkplaneValid()) {  // the group should be removed
164           std::list<SketchGroupPtr>::iterator aRemoveIt = aGroupIter++;
165           myGroups.erase(aRemoveIt);
166           continue;
167         }
168
169         (*aGroupIter)->repairConsistency();
170         ++aGroupIter;
171       }
172     }
173     myIsComputed = false;
174   }
175
176   // resolve constraints if needed
177   bool needToUpdate = needToResolve && resolveConstraints();
178   releaseFeaturesIfEventsBlocked();
179
180   // Features may be updated => now send events, but for all changed at once
181   if (isUpdateFlushed)
182     allowSendUpdate();
183
184   myIsComputed = false;
185
186   // send update for movement in any case
187   if (needToUpdate || isMovedEvt)
188     Events_Loop::loop()->flush(anUpdateEvent);
189 }
190
191 // ============================================================================
192 //  Function: updateFeature
193 //  Purpose:  create/update constraint or feature in appropriate group
194 // ============================================================================
195 bool SketchSolver_Manager::updateFeature(const std::shared_ptr<SketchPlugin_Feature>& theFeature)
196 {
197   // Check feature validity and find a group to place it.
198   // If the feature is not valid, the returned group will be empty.
199   // This will protect to deal with wrong (not fully initialized) features.
200   SketchGroupPtr aGroup = findGroup(theFeature);
201   if (!aGroup)
202     return false;
203   aGroup->blockEvents(true);
204
205   std::shared_ptr<SketchPlugin_Constraint> aConstraint =
206       std::dynamic_pointer_cast<SketchPlugin_Constraint>(theFeature);
207
208   bool isOk = false;
209   if (aConstraint)
210     isOk = aGroup->changeConstraint(aConstraint);
211   else
212     isOk = aGroup->updateFeature(theFeature);
213   return isOk;
214 }
215
216 // ============================================================================
217 //  Function: moveFeature
218 //  Purpose:  move given feature in appropriate group
219 // ============================================================================
220 bool SketchSolver_Manager::moveFeature(
221     const std::shared_ptr<SketchPlugin_Feature>& theMovedFeature,
222     const std::shared_ptr<GeomAPI_Pnt2d>& theFrom,
223     const std::shared_ptr<GeomAPI_Pnt2d>& theTo)
224 {
225   SketchGroupPtr aGroup = findGroup(theMovedFeature);
226   if (!aGroup)
227     return false;
228
229   aGroup->blockEvents(true);
230   return aGroup->moveFeature(theMovedFeature, theFrom, theTo);
231 }
232
233 // ============================================================================
234 //  Function: moveAttribute
235 //  Purpose:  move given attribute in appropriate group
236 // ============================================================================
237 bool SketchSolver_Manager::moveAttribute(
238     const std::shared_ptr<GeomDataAPI_Point2D>& theMovedAttribute,
239     const std::shared_ptr<GeomAPI_Pnt2d>& theFrom,
240     const std::shared_ptr<GeomAPI_Pnt2d>& theTo)
241 {
242   FeaturePtr anOwner = ModelAPI_Feature::feature(theMovedAttribute->owner());
243   std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
244       std::dynamic_pointer_cast<SketchPlugin_Feature>(anOwner);
245   SketchGroupPtr aGroup;
246   if (aSketchFeature)
247     aGroup = findGroup(aSketchFeature);
248   if (!aGroup) {
249     theMovedAttribute->setValue(theTo);
250     return false;
251   }
252
253   aGroup->blockEvents(true);
254   return aGroup->movePoint(theMovedAttribute, theFrom, theTo);
255 }
256
257 // ============================================================================
258 //  Function: findGroup
259 //  Purpose:  search groups of entities interacting with given feature
260 // ============================================================================
261 SketchGroupPtr SketchSolver_Manager::findGroup(
262     std::shared_ptr<SketchPlugin_Feature> theFeature)
263 {
264   if (!isFeatureValid(theFeature))
265     return SketchGroupPtr(); // do not process wrong features
266
267   // Obtain sketch, containing the feature
268   CompositeFeaturePtr aSketch;
269   const std::set<AttributePtr>& aRefsList = theFeature->data()->refsToMe();
270   std::set<AttributePtr>::const_iterator aRefIt = aRefsList.begin();
271   for (; aRefIt != aRefsList.end(); ++aRefIt) {
272     FeaturePtr anOwner = ModelAPI_Feature::feature((*aRefIt)->owner());
273     if (anOwner && anOwner->getKind() == SketchPlugin_Sketch::ID()) {
274       aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(anOwner);
275       break;
276     }
277   }
278
279   if (!aSketch)
280     return SketchGroupPtr(); // not a sketch's feature
281
282   std::list<SketchGroupPtr>::const_iterator aGroupIt;
283   for (aGroupIt = myGroups.begin(); aGroupIt != myGroups.end(); ++aGroupIt)
284     if ((*aGroupIt)->getWorkplane() == aSketch)
285       return *aGroupIt;
286
287   // group for the sketch does not created yet
288   SketchGroupPtr aNewGroup = SketchGroupPtr(new SketchSolver_Group(aSketch));
289   myGroups.push_back(aNewGroup);
290   return aNewGroup;
291 }
292
293 // ============================================================================
294 //  Function: resolveConstraints
295 //  Purpose:  change entities according to available constraints
296 // ============================================================================
297 bool SketchSolver_Manager::resolveConstraints()
298 {
299   bool needToUpdate = false;
300   std::list<SketchGroupPtr>::const_iterator aGroupIter = myGroups.begin();
301   for (; aGroupIter != myGroups.end(); ++aGroupIter) {
302     if ((*aGroupIter)->resolveConstraints())
303       needToUpdate = true;
304   }
305   return needToUpdate;
306 }
307
308 void SketchSolver_Manager::releaseFeaturesIfEventsBlocked() const
309 {
310   std::list<SketchGroupPtr>::const_iterator aGroupIter = myGroups.begin();
311   for (; aGroupIter != myGroups.end(); ++aGroupIter)
312     (*aGroupIter)->blockEvents(false);
313 }
314
315 bool SketchSolver_Manager::stopSendUpdate() const
316 {
317 static const Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
318   // to avoid redisplay of each segment on update by solver one by one in the viewer
319   bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
320   if (isUpdateFlushed) {
321     Events_Loop::loop()->setFlushed(anUpdateEvent, false);
322   }
323   return isUpdateFlushed;
324 }
325
326 void SketchSolver_Manager::allowSendUpdate() const
327 {
328   Events_Loop::loop()->setFlushed(Events_Loop::eventByName(EVENT_OBJECT_UPDATED), true);
329 }