1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "SketchSolver_Manager.h"
22 #include "SketchSolver_Error.h"
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_Sketch.h>
32 /// Global constraint manager object
33 static SketchSolver_Manager* myManager = SketchSolver_Manager::instance();
35 /// \brief Verifies is the feature valid
36 static bool isFeatureValid(FeaturePtr theFeature)
38 if (!theFeature || !theFeature->data() || !theFeature->data()->isValid())
41 SessionPtr aMgr = ModelAPI_Session::get();
42 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
43 return aFactory->validate(theFeature);
48 // ========================================================
49 // ========= SketchSolver_Manager ===============
50 // ========================================================
51 SketchSolver_Manager* SketchSolver_Manager::instance()
53 static SketchSolver_Manager* mySelf = 0; // Self pointer to implement singleton functionality
55 mySelf = new SketchSolver_Manager();
59 SketchSolver_Manager::SketchSolver_Manager()
64 // Register in event loop
65 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
66 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
67 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
68 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_MOVED));
70 ////Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_SOLVER_FAILED));
71 ////Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_SOLVER_REPAIRED));
72 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_SKETCH_PREPARED));
75 SketchSolver_Manager::~SketchSolver_Manager()
80 bool SketchSolver_Manager::groupMessages()
85 // ============================================================================
86 // Function: processEvent
87 // Purpose: listen the event loop and process the message
88 // ============================================================================
89 void SketchSolver_Manager::processEvent(
90 const std::shared_ptr<Events_Message>& theMessage)
92 bool needToResolve = false;
93 bool isUpdateFlushed = false;
94 bool isMovedEvt = false;
96 static const Events_ID aCreatedEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
97 static const Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
98 static const Events_ID aSketchPreparedEvent = Events_Loop::eventByName(EVENT_SKETCH_PREPARED);
99 // sketch is prepared for resolve: all the needed events
100 // are collected and must be processed by the solver
101 if (theMessage->eventID() == aSketchPreparedEvent) {
102 flushGrouped(anUpdateEvent);
103 needToResolve = true;
110 if (theMessage->eventID() == aCreatedEvent || theMessage->eventID() == anUpdateEvent) {
111 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> anUpdateMsg =
112 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
114 isUpdateFlushed = stopSendUpdate();
116 // update sketch features only
117 const std::set<ObjectPtr>& aFeatures = anUpdateMsg->objects();
118 // try to keep order as features were created if there are several created features: #2229
119 if (theMessage->eventID() == aCreatedEvent && aFeatures.size() > 1) {
120 std::map<int, std::shared_ptr<SketchPlugin_Feature>> anOrderedFeatures;
121 std::set<ObjectPtr>::iterator aFeatIter;
122 for (aFeatIter = aFeatures.begin(); aFeatIter != aFeatures.end(); aFeatIter++) {
123 std::shared_ptr<SketchPlugin_Feature> aFeature =
124 std::dynamic_pointer_cast<SketchPlugin_Feature>(*aFeatIter);
125 if (aFeature && !aFeature->isMacro() && aFeature->data() && aFeature->data()->isValid()) {
126 anOrderedFeatures[aFeature->data()->featureId()] = aFeature;
129 std::map<int, std::shared_ptr<SketchPlugin_Feature>>::iterator aFeat;
130 for(aFeat = anOrderedFeatures.begin(); aFeat != anOrderedFeatures.end(); aFeat++) {
131 updateFeature(aFeat->second);
133 } else { // order is not important
134 std::set<ObjectPtr>::iterator aFeatIter;
135 for (aFeatIter = aFeatures.begin(); aFeatIter != aFeatures.end(); aFeatIter++) {
136 std::shared_ptr<SketchPlugin_Feature> aFeature =
137 std::dynamic_pointer_cast<SketchPlugin_Feature>(*aFeatIter);
138 if (aFeature && !aFeature->isMacro())
139 updateFeature(aFeature);
143 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_MOVED)) {
144 std::shared_ptr<ModelAPI_ObjectMovedMessage> aMoveMsg =
145 std::dynamic_pointer_cast<ModelAPI_ObjectMovedMessage>(theMessage);
147 ObjectPtr aMovedObject = aMoveMsg->movedObject();
148 std::shared_ptr<GeomDataAPI_Point2D> aMovedPoint =
149 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aMoveMsg->movedAttribute());
151 const std::shared_ptr<GeomAPI_Pnt2d>& aFrom = aMoveMsg->originalPosition();
152 const std::shared_ptr<GeomAPI_Pnt2d>& aTo = aMoveMsg->currentPosition();
155 FeaturePtr aMovedFeature = ModelAPI_Feature::feature(aMovedObject);
156 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
157 std::dynamic_pointer_cast<SketchPlugin_Feature>(aMovedFeature);
158 if (aSketchFeature && !aSketchFeature->isMacro())
159 needToResolve = moveFeature(aSketchFeature, aFrom, aTo);
160 } else if (aMovedPoint)
161 needToResolve = moveAttribute(aMovedPoint, aFrom, aTo);
163 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)) {
164 std::shared_ptr<ModelAPI_ObjectDeletedMessage> aDeleteMsg =
165 std::dynamic_pointer_cast<ModelAPI_ObjectDeletedMessage>(theMessage);
166 const std::set<std::string>& aFeatureGroups = aDeleteMsg->groups();
168 // Find SketchPlugin_Sketch::ID() in groups.
169 // The constraint groups should be updated when an object removed from Sketch
170 std::set<std::string>::const_iterator aFGrIter;
171 for (aFGrIter = aFeatureGroups.begin(); aFGrIter != aFeatureGroups.end(); aFGrIter++)
172 if (aFGrIter->compare(ModelAPI_ResultConstruction::group()) == 0 ||
173 aFGrIter->compare(ModelAPI_Feature::group()) == 0)
176 if (aFGrIter != aFeatureGroups.end()) {
177 std::list<SketchGroupPtr>::iterator aGroupIter = myGroups.begin();
178 while (aGroupIter != myGroups.end()) {
179 if (!(*aGroupIter)->isWorkplaneValid()) { // the group should be removed
180 std::list<SketchGroupPtr>::iterator aRemoveIt = aGroupIter++;
181 myGroups.erase(aRemoveIt);
185 (*aGroupIter)->repairConsistency();
189 myIsComputed = false;
192 // resolve constraints if needed
193 bool needToUpdate = needToResolve && resolveConstraints();
194 releaseFeaturesIfEventsBlocked();
196 // Features may be updated => now send events, but for all changed at once
200 myIsComputed = false;
202 // send update for movement in any case
203 if (needToUpdate || isMovedEvt)
204 Events_Loop::loop()->flush(anUpdateEvent);
207 // ============================================================================
208 // Function: updateFeature
209 // Purpose: create/update constraint or feature in appropriate group
210 // ============================================================================
211 bool SketchSolver_Manager::updateFeature(const std::shared_ptr<SketchPlugin_Feature>& theFeature)
213 // Check feature validity and find a group to place it.
214 // If the feature is not valid, the returned group will be empty.
215 // This will protect to deal with wrong (not fully initialized) features.
216 SketchGroupPtr aGroup = findGroup(theFeature);
219 aGroup->blockEvents(true);
221 std::shared_ptr<SketchPlugin_Constraint> aConstraint =
222 std::dynamic_pointer_cast<SketchPlugin_Constraint>(theFeature);
226 isOk = aGroup->changeConstraint(aConstraint);
228 isOk = aGroup->updateFeature(theFeature);
232 // ============================================================================
233 // Function: moveFeature
234 // Purpose: move given feature in appropriate group
235 // ============================================================================
236 bool SketchSolver_Manager::moveFeature(
237 const std::shared_ptr<SketchPlugin_Feature>& theMovedFeature,
238 const std::shared_ptr<GeomAPI_Pnt2d>& theFrom,
239 const std::shared_ptr<GeomAPI_Pnt2d>& theTo)
241 SketchGroupPtr aGroup = findGroup(theMovedFeature);
245 std::shared_ptr<SketchPlugin_Constraint> aConstraint =
246 std::dynamic_pointer_cast<SketchPlugin_Constraint>(theMovedFeature);
249 std::shared_ptr<GeomDataAPI_Point2D> aPntAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>
250 (aConstraint->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
253 aPntAttr->setValue(theTo);
254 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
259 aGroup->blockEvents(true);
260 return aGroup->moveFeature(theMovedFeature, theFrom, theTo);
263 // ============================================================================
264 // Function: moveAttribute
265 // Purpose: move given attribute in appropriate group
266 // ============================================================================
267 bool SketchSolver_Manager::moveAttribute(
268 const std::shared_ptr<GeomDataAPI_Point2D>& theMovedAttribute,
269 const std::shared_ptr<GeomAPI_Pnt2d>& theFrom,
270 const std::shared_ptr<GeomAPI_Pnt2d>& theTo)
272 FeaturePtr anOwner = ModelAPI_Feature::feature(theMovedAttribute->owner());
273 std::shared_ptr<SketchPlugin_Constraint> aConstraint =
274 std::dynamic_pointer_cast<SketchPlugin_Constraint>(anOwner);
277 theMovedAttribute->setValue(theTo);
278 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
282 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
283 std::dynamic_pointer_cast<SketchPlugin_Feature>(anOwner);
284 SketchGroupPtr aGroup;
286 aGroup = findGroup(aSketchFeature);
288 theMovedAttribute->setValue(theTo);
292 aGroup->blockEvents(true);
293 return aGroup->movePoint(theMovedAttribute, theFrom, theTo);
296 // ============================================================================
297 // Function: findGroup
298 // Purpose: search groups of entities interacting with given feature
299 // ============================================================================
300 SketchGroupPtr SketchSolver_Manager::findGroup(
301 std::shared_ptr<SketchPlugin_Feature> theFeature)
303 if (!isFeatureValid(theFeature))
304 return SketchGroupPtr(); // do not process wrong features
306 // Obtain sketch, containing the feature
307 CompositeFeaturePtr aSketch;
308 const std::set<AttributePtr>& aRefsList = theFeature->data()->refsToMe();
309 std::set<AttributePtr>::const_iterator aRefIt = aRefsList.begin();
310 for (; aRefIt != aRefsList.end(); ++aRefIt) {
311 FeaturePtr anOwner = ModelAPI_Feature::feature((*aRefIt)->owner());
312 if (anOwner && anOwner->getKind() == SketchPlugin_Sketch::ID()) {
313 aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(anOwner);
319 return SketchGroupPtr(); // not a sketch's feature
321 std::list<SketchGroupPtr>::const_iterator aGroupIt;
322 for (aGroupIt = myGroups.begin(); aGroupIt != myGroups.end(); ++aGroupIt)
323 if ((*aGroupIt)->getWorkplane() == aSketch)
326 // group for the sketch does not created yet
327 SketchGroupPtr aNewGroup = SketchGroupPtr(new SketchSolver_Group(aSketch));
328 myGroups.push_back(aNewGroup);
332 // ============================================================================
333 // Function: resolveConstraints
334 // Purpose: change entities according to available constraints
335 // ============================================================================
336 bool SketchSolver_Manager::resolveConstraints()
338 bool needToUpdate = false;
339 std::list<SketchGroupPtr>::const_iterator aGroupIter = myGroups.begin();
340 for (; aGroupIter != myGroups.end(); ++aGroupIter) {
341 if ((*aGroupIter)->resolveConstraints())
347 void SketchSolver_Manager::releaseFeaturesIfEventsBlocked() const
349 std::list<SketchGroupPtr>::const_iterator aGroupIter = myGroups.begin();
350 for (; aGroupIter != myGroups.end(); ++aGroupIter)
351 (*aGroupIter)->blockEvents(false);
354 bool SketchSolver_Manager::stopSendUpdate() const
356 static const Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
357 // to avoid redisplay of each segment on update by solver one by one in the viewer
358 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
359 if (isUpdateFlushed) {
360 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
362 return isUpdateFlushed;
365 void SketchSolver_Manager::allowSendUpdate() const
367 Events_Loop::loop()->setFlushed(Events_Loop::eventByName(EVENT_OBJECT_UPDATED), true);