1 // Copyright (C) 2014-2019 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 email : webmaster.salome@opencascade.com
20 #include "SketchSolver_Manager.h"
21 #include "SketchSolver_Error.h"
23 #include <Events_Loop.h>
24 #include <GeomDataAPI_Point2D.h>
25 #include <ModelAPI_Events.h>
26 #include <ModelAPI_ResultConstruction.h>
27 #include <ModelAPI_Session.h>
28 #include <ModelAPI_Validator.h>
29 #include <SketchPlugin_Sketch.h>
31 /// Global constraint manager object
32 static SketchSolver_Manager* myManager = SketchSolver_Manager::instance();
34 /// \brief Verifies is the feature valid
35 static bool isFeatureValid(FeaturePtr theFeature)
37 if (!theFeature || !theFeature->data() || !theFeature->data()->isValid())
40 SessionPtr aMgr = ModelAPI_Session::get();
41 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
42 return aFactory->validate(theFeature);
45 typedef std::map<int, std::shared_ptr<SketchPlugin_Feature>> IndexedFeatureMap;
47 static void featuresOrderedByCreation(const std::set<ObjectPtr>& theOriginalFeatures,
48 IndexedFeatureMap& theOrderedFeatures)
50 std::set<ObjectPtr>::iterator aFeatIter = theOriginalFeatures.begin();
51 for (; aFeatIter != theOriginalFeatures.end(); aFeatIter++) {
52 std::shared_ptr<SketchPlugin_Feature> aFeature =
53 std::dynamic_pointer_cast<SketchPlugin_Feature>(*aFeatIter);
54 if (aFeature && !aFeature->isMacro() && aFeature->data() && aFeature->data()->isValid()) {
55 theOrderedFeatures[aFeature->data()->featureId()] = aFeature;
60 static void featuresOrderedByType(const std::set<ObjectPtr>& theOriginalFeatures,
61 IndexedFeatureMap& theOrderedFeatures,
62 CompositeFeaturePtr& theSketch)
64 int aFeatureIndex = 0;
65 int aConstraintIndex = (int)theOriginalFeatures.size();
67 std::set<ObjectPtr>::iterator aFeatIter = theOriginalFeatures.begin();
68 for (; aFeatIter != theOriginalFeatures.end(); aFeatIter++) {
69 std::shared_ptr<SketchPlugin_Feature> aFeature =
70 std::dynamic_pointer_cast<SketchPlugin_Feature>(*aFeatIter);
72 if (!aFeature->isMacro() && aFeature->data() && aFeature->data()->isValid()) {
73 std::shared_ptr<SketchPlugin_Constraint> aConstraint =
74 std::dynamic_pointer_cast<SketchPlugin_Constraint>(aFeature);
76 theOrderedFeatures[++aConstraintIndex] = aFeature;
78 theOrderedFeatures[++aFeatureIndex] = aFeature;
82 CompositeFeaturePtr aSketch =
83 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFeatIter);
84 if (aSketch && aSketch->getKind() == SketchPlugin_Sketch::ID())
92 // ========================================================
93 // ========= SketchSolver_Manager ===============
94 // ========================================================
95 SketchSolver_Manager* SketchSolver_Manager::instance()
97 static SketchSolver_Manager* mySelf = 0; // Self pointer to implement singleton functionality
99 mySelf = new SketchSolver_Manager();
103 SketchSolver_Manager::SketchSolver_Manager()
106 myIsComputed = false;
108 // Register in event loop
109 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
110 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
111 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
112 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_MOVED));
114 ////Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_SOLVER_FAILED));
115 ////Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_SOLVER_REPAIRED));
116 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_SKETCH_PREPARED));
119 SketchSolver_Manager::~SketchSolver_Manager()
124 bool SketchSolver_Manager::groupMessages()
129 // ============================================================================
130 // Function: processEvent
131 // Purpose: listen the event loop and process the message
132 // ============================================================================
133 void SketchSolver_Manager::processEvent(
134 const std::shared_ptr<Events_Message>& theMessage)
136 bool needToResolve = false;
137 bool isUpdateFlushed = false;
138 bool isMovedEvt = false;
140 static const Events_ID aCreatedEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
141 static const Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
142 static const Events_ID aSketchPreparedEvent = Events_Loop::eventByName(EVENT_SKETCH_PREPARED);
143 // sketch is prepared for resolve: all the needed events
144 // are collected and must be processed by the solver
145 if (theMessage->eventID() == aSketchPreparedEvent) {
146 flushGrouped(anUpdateEvent);
147 needToResolve = true;
154 if (theMessage->eventID() == aCreatedEvent || theMessage->eventID() == anUpdateEvent) {
155 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> anUpdateMsg =
156 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
158 isUpdateFlushed = stopSendUpdate();
160 // update sketch features only
161 const std::set<ObjectPtr>& aFeatures = anUpdateMsg->objects();
162 IndexedFeatureMap anOrderedFeatures;
163 CompositeFeaturePtr aSketchFeature;
164 // try to keep order as features were created if there are several created features: #2229
165 if (theMessage->eventID() == aCreatedEvent && aFeatures.size() > 1) {
166 featuresOrderedByCreation(aFeatures, anOrderedFeatures);
167 } else { // order is not important, just process features before constraints
168 featuresOrderedByType(aFeatures, anOrderedFeatures, aSketchFeature);
171 IndexedFeatureMap::iterator aFeat;
172 for (aFeat = anOrderedFeatures.begin(); aFeat != anOrderedFeatures.end(); aFeat++) {
173 updateFeature(aFeat->second);
175 updateSketch(aSketchFeature);
177 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_MOVED)) {
178 std::shared_ptr<ModelAPI_ObjectMovedMessage> aMoveMsg =
179 std::dynamic_pointer_cast<ModelAPI_ObjectMovedMessage>(theMessage);
181 ObjectPtr aMovedObject = aMoveMsg->movedObject();
182 std::shared_ptr<GeomDataAPI_Point2D> aMovedPoint =
183 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aMoveMsg->movedAttribute());
185 const std::shared_ptr<GeomAPI_Pnt2d>& aFrom = aMoveMsg->originalPosition();
186 const std::shared_ptr<GeomAPI_Pnt2d>& aTo = aMoveMsg->currentPosition();
189 FeaturePtr aMovedFeature = ModelAPI_Feature::feature(aMovedObject);
190 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
191 std::dynamic_pointer_cast<SketchPlugin_Feature>(aMovedFeature);
192 if (aSketchFeature && !aSketchFeature->isMacro())
193 needToResolve = moveFeature(aSketchFeature, aFrom, aTo);
194 } else if (aMovedPoint)
195 needToResolve = moveAttribute(aMovedPoint, aFrom, aTo);
197 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)) {
198 std::shared_ptr<ModelAPI_ObjectDeletedMessage> aDeleteMsg =
199 std::dynamic_pointer_cast<ModelAPI_ObjectDeletedMessage>(theMessage);
200 const std::list<std::pair<std::shared_ptr<ModelAPI_Document>, std::string>>& aFeatureGroups =
201 aDeleteMsg->groups();
203 // Find SketchPlugin_Sketch::ID() in groups.
204 // The constraint groups should be updated when an object removed from Sketch
205 std::list<std::pair<std::shared_ptr<ModelAPI_Document>, std::string>>::const_iterator aFGrIter;
206 for (aFGrIter = aFeatureGroups.begin(); aFGrIter != aFeatureGroups.end(); aFGrIter++)
207 if (aFGrIter->second == ModelAPI_ResultConstruction::group() ||
208 aFGrIter->second == ModelAPI_Feature::group())
211 if (aFGrIter != aFeatureGroups.end()) {
212 std::list<SketchGroupPtr>::iterator aGroupIter = myGroups.begin();
213 while (aGroupIter != myGroups.end()) {
214 if (!(*aGroupIter)->isWorkplaneValid()) { // the group should be removed
215 std::list<SketchGroupPtr>::iterator aRemoveIt = aGroupIter++;
216 myGroups.erase(aRemoveIt);
220 (*aGroupIter)->repairConsistency();
224 myIsComputed = false;
226 //// else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURES_OF_FREEDOM)) {
227 //// FeaturePtr aComposite = theMessage->sketch();
228 //// std::shared_ptr<SketchPlugin_Feature> aSketch =
229 //// std::dynamic_pointer_cast<SketchPlugin_Feature>(aComposite);
231 //// SketchGroupPtr aGroup = findGroup(aSketch);
233 //// std::set<FeaturePtr> aFreeFeatures;
234 //// aGroup->underconstrainedFeatures(aFreeFeatures);
236 //// // TODO: send features to GUI
240 // resolve constraints if needed
241 bool needToUpdate = needToResolve && resolveConstraints();
242 releaseFeaturesIfEventsBlocked();
244 // Features may be updated => now send events, but for all changed at once
248 myIsComputed = false;
250 // send update for movement in any case
251 if (needToUpdate || isMovedEvt)
252 Events_Loop::loop()->flush(anUpdateEvent);
255 // ============================================================================
256 // Function: updateSketch
257 // Purpose: update sketch plane in appropriate group
258 // ============================================================================
259 bool SketchSolver_Manager::updateSketch(const CompositeFeaturePtr& theSketch)
265 std::list<SketchGroupPtr>::const_iterator aGroupIt;
266 for (aGroupIt = myGroups.begin(); aGroupIt != myGroups.end(); ++aGroupIt)
267 if ((*aGroupIt)->getWorkplane() == theSketch) {
268 (*aGroupIt)->updateSketch(theSketch);
274 // ============================================================================
275 // Function: updateFeature
276 // Purpose: create/update constraint or feature in appropriate group
277 // ============================================================================
278 bool SketchSolver_Manager::updateFeature(const std::shared_ptr<SketchPlugin_Feature>& theFeature)
280 // Check feature validity and find a group to place it.
281 // If the feature is not valid, the returned group will be empty.
282 // This will protect to deal with wrong (not fully initialized) features.
283 SketchGroupPtr aGroup = findGroup(theFeature);
286 aGroup->blockEvents(true);
288 std::shared_ptr<SketchPlugin_Constraint> aConstraint =
289 std::dynamic_pointer_cast<SketchPlugin_Constraint>(theFeature);
293 isOk = aGroup->changeConstraint(aConstraint);
295 isOk = aGroup->updateFeature(theFeature);
299 // ============================================================================
300 // Function: moveFeature
301 // Purpose: move given feature in appropriate group
302 // ============================================================================
303 bool SketchSolver_Manager::moveFeature(
304 const std::shared_ptr<SketchPlugin_Feature>& theMovedFeature,
305 const std::shared_ptr<GeomAPI_Pnt2d>& theFrom,
306 const std::shared_ptr<GeomAPI_Pnt2d>& theTo)
308 SketchGroupPtr aGroup = findGroup(theMovedFeature);
312 std::shared_ptr<SketchPlugin_Constraint> aConstraint =
313 std::dynamic_pointer_cast<SketchPlugin_Constraint>(theMovedFeature);
316 std::shared_ptr<GeomDataAPI_Point2D> aPntAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>
317 (aConstraint->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
320 aPntAttr->setValue(theTo);
321 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
326 aGroup->blockEvents(true);
327 return aGroup->moveFeature(theMovedFeature, theFrom, theTo);
330 // ============================================================================
331 // Function: moveAttribute
332 // Purpose: move given attribute in appropriate group
333 // ============================================================================
334 bool SketchSolver_Manager::moveAttribute(
335 const std::shared_ptr<GeomDataAPI_Point2D>& theMovedAttribute,
336 const std::shared_ptr<GeomAPI_Pnt2d>& theFrom,
337 const std::shared_ptr<GeomAPI_Pnt2d>& theTo)
339 FeaturePtr anOwner = ModelAPI_Feature::feature(theMovedAttribute->owner());
340 std::shared_ptr<SketchPlugin_Constraint> aConstraint =
341 std::dynamic_pointer_cast<SketchPlugin_Constraint>(anOwner);
344 theMovedAttribute->setValue(theTo);
345 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
349 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
350 std::dynamic_pointer_cast<SketchPlugin_Feature>(anOwner);
351 SketchGroupPtr aGroup;
353 aGroup = findGroup(aSketchFeature);
355 theMovedAttribute->setValue(theTo);
359 aGroup->blockEvents(true);
360 return aGroup->movePoint(theMovedAttribute, theFrom, theTo);
363 // ============================================================================
364 // Function: findGroup
365 // Purpose: search groups of entities interacting with given feature
366 // ============================================================================
367 SketchGroupPtr SketchSolver_Manager::findGroup(
368 std::shared_ptr<SketchPlugin_Feature> theFeature)
370 if (!isFeatureValid(theFeature))
371 return SketchGroupPtr(); // do not process wrong features
373 // Obtain sketch, containing the feature
374 CompositeFeaturePtr aSketch;
375 const std::set<AttributePtr>& aRefsList = theFeature->data()->refsToMe();
376 std::set<AttributePtr>::const_iterator aRefIt = aRefsList.begin();
377 for (; aRefIt != aRefsList.end(); ++aRefIt) {
378 FeaturePtr anOwner = ModelAPI_Feature::feature((*aRefIt)->owner());
379 if (anOwner && anOwner->getKind() == SketchPlugin_Sketch::ID()) {
380 aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(anOwner);
386 return SketchGroupPtr(); // not a sketch's feature
388 std::list<SketchGroupPtr>::const_iterator aGroupIt;
389 for (aGroupIt = myGroups.begin(); aGroupIt != myGroups.end(); ++aGroupIt)
390 if ((*aGroupIt)->getWorkplane() == aSketch)
393 // group for the sketch does not created yet
394 SketchGroupPtr aNewGroup = SketchGroupPtr(new SketchSolver_Group(aSketch));
395 myGroups.push_back(aNewGroup);
399 // ============================================================================
400 // Function: resolveConstraints
401 // Purpose: change entities according to available constraints
402 // ============================================================================
403 bool SketchSolver_Manager::resolveConstraints()
405 bool needToUpdate = false;
406 std::list<SketchGroupPtr>::const_iterator aGroupIter = myGroups.begin();
407 for (; aGroupIter != myGroups.end(); ++aGroupIter) {
408 if ((*aGroupIter)->resolveConstraints())
414 void SketchSolver_Manager::releaseFeaturesIfEventsBlocked() const
416 std::list<SketchGroupPtr>::const_iterator aGroupIter = myGroups.begin();
417 for (; aGroupIter != myGroups.end(); ++aGroupIter)
418 (*aGroupIter)->blockEvents(false);
421 bool SketchSolver_Manager::stopSendUpdate() const
423 static const Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
424 // to avoid redisplay of each segment on update by solver one by one in the viewer
425 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
426 if (isUpdateFlushed) {
427 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
429 return isUpdateFlushed;
432 void SketchSolver_Manager::allowSendUpdate() const
434 Events_Loop::loop()->setFlushed(Events_Loop::eventByName(EVENT_OBJECT_UPDATED), true);