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_Constraint.h>
31 #include <SketchPlugin_Sketch.h>
33 /// Global constraint manager object
34 static SketchSolver_Manager* myManager = SketchSolver_Manager::instance();
36 /// \brief Verifies is the feature valid
37 static bool isFeatureValid(FeaturePtr theFeature)
39 if (!theFeature || !theFeature->data() || !theFeature->data()->isValid())
42 SessionPtr aMgr = ModelAPI_Session::get();
43 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
44 return aFactory->validate(theFeature);
49 // ========================================================
50 // ========= SketchSolver_Manager ===============
51 // ========================================================
52 SketchSolver_Manager* SketchSolver_Manager::instance()
54 static SketchSolver_Manager* mySelf = 0; // Self pointer to implement singleton functionality
56 mySelf = new SketchSolver_Manager();
60 SketchSolver_Manager::SketchSolver_Manager()
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));
71 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_SKETCH_PREPARED));
74 SketchSolver_Manager::~SketchSolver_Manager()
79 bool SketchSolver_Manager::groupMessages()
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)
91 bool needToResolve = false;
92 bool isUpdateFlushed = false;
93 bool isMovedEvt = false;
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;
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();
114 isUpdateFlushed = stopSendUpdate();
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())
124 updateFeature(aFeature);
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);
131 ObjectPtr aMovedObject = aMoveMsg->movedObject();
132 std::shared_ptr<GeomDataAPI_Point2D> aMovedPoint =
133 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aMoveMsg->movedAttribute());
135 const std::shared_ptr<GeomAPI_Pnt2d>& aFrom = aMoveMsg->originalPosition();
136 const std::shared_ptr<GeomAPI_Pnt2d>& aTo = aMoveMsg->currentPosition();
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);
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();
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)
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);
169 (*aGroupIter)->repairConsistency();
173 myIsComputed = false;
176 // resolve constraints if needed
177 bool needToUpdate = needToResolve && resolveConstraints();
178 releaseFeaturesIfEventsBlocked();
180 // Features may be updated => now send events, but for all changed at once
184 myIsComputed = false;
186 // send update for movement in any case
187 if (needToUpdate || isMovedEvt)
188 Events_Loop::loop()->flush(anUpdateEvent);
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)
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);
203 aGroup->blockEvents(true);
205 std::shared_ptr<SketchPlugin_Constraint> aConstraint =
206 std::dynamic_pointer_cast<SketchPlugin_Constraint>(theFeature);
210 isOk = aGroup->changeConstraint(aConstraint);
212 isOk = aGroup->updateFeature(theFeature);
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)
225 SketchGroupPtr aGroup = findGroup(theMovedFeature);
229 aGroup->blockEvents(true);
230 return aGroup->moveFeature(theMovedFeature, theFrom, theTo);
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)
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;
247 aGroup = findGroup(aSketchFeature);
249 theMovedAttribute->setValue(theTo);
253 aGroup->blockEvents(true);
254 return aGroup->movePoint(theMovedAttribute, theFrom, theTo);
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)
264 if (!isFeatureValid(theFeature))
265 return SketchGroupPtr(); // do not process wrong features
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);
280 return SketchGroupPtr(); // not a sketch's feature
282 std::list<SketchGroupPtr>::const_iterator aGroupIt;
283 for (aGroupIt = myGroups.begin(); aGroupIt != myGroups.end(); ++aGroupIt)
284 if ((*aGroupIt)->getWorkplane() == aSketch)
287 // group for the sketch does not created yet
288 SketchGroupPtr aNewGroup = SketchGroupPtr(new SketchSolver_Group(aSketch));
289 myGroups.push_back(aNewGroup);
293 // ============================================================================
294 // Function: resolveConstraints
295 // Purpose: change entities according to available constraints
296 // ============================================================================
297 bool SketchSolver_Manager::resolveConstraints()
299 bool needToUpdate = false;
300 std::list<SketchGroupPtr>::const_iterator aGroupIter = myGroups.begin();
301 for (; aGroupIter != myGroups.end(); ++aGroupIter) {
302 if ((*aGroupIter)->resolveConstraints())
308 void SketchSolver_Manager::releaseFeaturesIfEventsBlocked() const
310 std::list<SketchGroupPtr>::const_iterator aGroupIter = myGroups.begin();
311 for (; aGroupIter != myGroups.end(); ++aGroupIter)
312 (*aGroupIter)->blockEvents(false);
315 bool SketchSolver_Manager::stopSendUpdate() const
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);
323 return isUpdateFlushed;
326 void SketchSolver_Manager::allowSendUpdate() const
328 Events_Loop::loop()->setFlushed(Events_Loop::eventByName(EVENT_OBJECT_UPDATED), true);