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 std::shared_ptr<SketchPlugin_Constraint> aConstraint =
230 std::dynamic_pointer_cast<SketchPlugin_Constraint>(theMovedFeature);
233 std::shared_ptr<GeomDataAPI_Point2D> aPntAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>
234 (aConstraint->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
235 aPntAttr->setValue(theTo);
236 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
240 aGroup->blockEvents(true);
241 return aGroup->moveFeature(theMovedFeature, theFrom, theTo);
244 // ============================================================================
245 // Function: moveAttribute
246 // Purpose: move given attribute in appropriate group
247 // ============================================================================
248 bool SketchSolver_Manager::moveAttribute(
249 const std::shared_ptr<GeomDataAPI_Point2D>& theMovedAttribute,
250 const std::shared_ptr<GeomAPI_Pnt2d>& theFrom,
251 const std::shared_ptr<GeomAPI_Pnt2d>& theTo)
253 FeaturePtr anOwner = ModelAPI_Feature::feature(theMovedAttribute->owner());
254 std::shared_ptr<SketchPlugin_Constraint> aConstraint =
255 std::dynamic_pointer_cast<SketchPlugin_Constraint>(anOwner);
258 theMovedAttribute->setValue(theTo);
259 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
263 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
264 std::dynamic_pointer_cast<SketchPlugin_Feature>(anOwner);
265 SketchGroupPtr aGroup;
267 aGroup = findGroup(aSketchFeature);
269 theMovedAttribute->setValue(theTo);
273 aGroup->blockEvents(true);
274 return aGroup->movePoint(theMovedAttribute, theFrom, theTo);
277 // ============================================================================
278 // Function: findGroup
279 // Purpose: search groups of entities interacting with given feature
280 // ============================================================================
281 SketchGroupPtr SketchSolver_Manager::findGroup(
282 std::shared_ptr<SketchPlugin_Feature> theFeature)
284 if (!isFeatureValid(theFeature))
285 return SketchGroupPtr(); // do not process wrong features
287 // Obtain sketch, containing the feature
288 CompositeFeaturePtr aSketch;
289 const std::set<AttributePtr>& aRefsList = theFeature->data()->refsToMe();
290 std::set<AttributePtr>::const_iterator aRefIt = aRefsList.begin();
291 for (; aRefIt != aRefsList.end(); ++aRefIt) {
292 FeaturePtr anOwner = ModelAPI_Feature::feature((*aRefIt)->owner());
293 if (anOwner && anOwner->getKind() == SketchPlugin_Sketch::ID()) {
294 aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(anOwner);
300 return SketchGroupPtr(); // not a sketch's feature
302 std::list<SketchGroupPtr>::const_iterator aGroupIt;
303 for (aGroupIt = myGroups.begin(); aGroupIt != myGroups.end(); ++aGroupIt)
304 if ((*aGroupIt)->getWorkplane() == aSketch)
307 // group for the sketch does not created yet
308 SketchGroupPtr aNewGroup = SketchGroupPtr(new SketchSolver_Group(aSketch));
309 myGroups.push_back(aNewGroup);
313 // ============================================================================
314 // Function: resolveConstraints
315 // Purpose: change entities according to available constraints
316 // ============================================================================
317 bool SketchSolver_Manager::resolveConstraints()
319 bool needToUpdate = false;
320 std::list<SketchGroupPtr>::const_iterator aGroupIter = myGroups.begin();
321 for (; aGroupIter != myGroups.end(); ++aGroupIter) {
322 if ((*aGroupIter)->resolveConstraints())
328 void SketchSolver_Manager::releaseFeaturesIfEventsBlocked() const
330 std::list<SketchGroupPtr>::const_iterator aGroupIter = myGroups.begin();
331 for (; aGroupIter != myGroups.end(); ++aGroupIter)
332 (*aGroupIter)->blockEvents(false);
335 bool SketchSolver_Manager::stopSendUpdate() const
337 static const Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
338 // to avoid redisplay of each segment on update by solver one by one in the viewer
339 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
340 if (isUpdateFlushed) {
341 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
343 return isUpdateFlushed;
346 void SketchSolver_Manager::allowSendUpdate() const
348 Events_Loop::loop()->setFlushed(Events_Loop::eventByName(EVENT_OBJECT_UPDATED), true);