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;
227 // resolve constraints if needed
228 bool needToUpdate = needToResolve && resolveConstraints();
229 releaseFeaturesIfEventsBlocked();
231 // Features may be updated => now send events, but for all changed at once
235 myIsComputed = false;
237 // send update for movement in any case
238 if (needToUpdate || isMovedEvt)
239 Events_Loop::loop()->flush(anUpdateEvent);
242 // ============================================================================
243 // Function: updateSketch
244 // Purpose: update sketch plane in appropriate group
245 // ============================================================================
246 bool SketchSolver_Manager::updateSketch(const CompositeFeaturePtr& theSketch)
252 std::list<SketchGroupPtr>::const_iterator aGroupIt;
253 for (aGroupIt = myGroups.begin(); aGroupIt != myGroups.end(); ++aGroupIt)
254 if ((*aGroupIt)->getWorkplane() == theSketch) {
255 (*aGroupIt)->updateSketch(theSketch);
261 // ============================================================================
262 // Function: updateFeature
263 // Purpose: create/update constraint or feature in appropriate group
264 // ============================================================================
265 bool SketchSolver_Manager::updateFeature(const std::shared_ptr<SketchPlugin_Feature>& theFeature)
267 // Check feature validity and find a group to place it.
268 // If the feature is not valid, the returned group will be empty.
269 // This will protect to deal with wrong (not fully initialized) features.
270 SketchGroupPtr aGroup = findGroup(theFeature);
273 aGroup->blockEvents(true);
275 std::shared_ptr<SketchPlugin_Constraint> aConstraint =
276 std::dynamic_pointer_cast<SketchPlugin_Constraint>(theFeature);
280 isOk = aGroup->changeConstraint(aConstraint);
282 isOk = aGroup->updateFeature(theFeature);
286 // ============================================================================
287 // Function: moveFeature
288 // Purpose: move given feature in appropriate group
289 // ============================================================================
290 bool SketchSolver_Manager::moveFeature(
291 const std::shared_ptr<SketchPlugin_Feature>& theMovedFeature,
292 const std::shared_ptr<GeomAPI_Pnt2d>& theFrom,
293 const std::shared_ptr<GeomAPI_Pnt2d>& theTo)
295 SketchGroupPtr aGroup = findGroup(theMovedFeature);
299 std::shared_ptr<SketchPlugin_Constraint> aConstraint =
300 std::dynamic_pointer_cast<SketchPlugin_Constraint>(theMovedFeature);
303 std::shared_ptr<GeomDataAPI_Point2D> aPntAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>
304 (aConstraint->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
307 aPntAttr->setValue(theTo);
308 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
313 aGroup->blockEvents(true);
314 return aGroup->moveFeature(theMovedFeature, theFrom, theTo);
317 // ============================================================================
318 // Function: moveAttribute
319 // Purpose: move given attribute in appropriate group
320 // ============================================================================
321 bool SketchSolver_Manager::moveAttribute(
322 const std::shared_ptr<GeomDataAPI_Point2D>& theMovedAttribute,
323 const std::shared_ptr<GeomAPI_Pnt2d>& theFrom,
324 const std::shared_ptr<GeomAPI_Pnt2d>& theTo)
326 FeaturePtr anOwner = ModelAPI_Feature::feature(theMovedAttribute->owner());
327 std::shared_ptr<SketchPlugin_Constraint> aConstraint =
328 std::dynamic_pointer_cast<SketchPlugin_Constraint>(anOwner);
331 theMovedAttribute->setValue(theTo);
332 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
336 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
337 std::dynamic_pointer_cast<SketchPlugin_Feature>(anOwner);
338 SketchGroupPtr aGroup;
340 aGroup = findGroup(aSketchFeature);
342 theMovedAttribute->setValue(theTo);
346 aGroup->blockEvents(true);
347 return aGroup->movePoint(theMovedAttribute, theFrom, theTo);
350 // ============================================================================
351 // Function: findGroup
352 // Purpose: search groups of entities interacting with given feature
353 // ============================================================================
354 SketchGroupPtr SketchSolver_Manager::findGroup(
355 std::shared_ptr<SketchPlugin_Feature> theFeature)
357 if (!isFeatureValid(theFeature))
358 return SketchGroupPtr(); // do not process wrong features
360 // Obtain sketch, containing the feature
361 CompositeFeaturePtr aSketch;
362 const std::set<AttributePtr>& aRefsList = theFeature->data()->refsToMe();
363 std::set<AttributePtr>::const_iterator aRefIt = aRefsList.begin();
364 for (; aRefIt != aRefsList.end(); ++aRefIt) {
365 FeaturePtr anOwner = ModelAPI_Feature::feature((*aRefIt)->owner());
366 if (anOwner && anOwner->getKind() == SketchPlugin_Sketch::ID()) {
367 aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(anOwner);
373 return SketchGroupPtr(); // not a sketch's feature
375 std::list<SketchGroupPtr>::const_iterator aGroupIt;
376 for (aGroupIt = myGroups.begin(); aGroupIt != myGroups.end(); ++aGroupIt)
377 if ((*aGroupIt)->getWorkplane() == aSketch)
380 // group for the sketch does not created yet
381 SketchGroupPtr aNewGroup = SketchGroupPtr(new SketchSolver_Group(aSketch));
382 myGroups.push_back(aNewGroup);
386 // ============================================================================
387 // Function: resolveConstraints
388 // Purpose: change entities according to available constraints
389 // ============================================================================
390 bool SketchSolver_Manager::resolveConstraints()
392 bool needToUpdate = false;
393 std::list<SketchGroupPtr>::const_iterator aGroupIter = myGroups.begin();
394 for (; aGroupIter != myGroups.end(); ++aGroupIter) {
395 if ((*aGroupIter)->resolveConstraints())
401 void SketchSolver_Manager::releaseFeaturesIfEventsBlocked() const
403 std::list<SketchGroupPtr>::const_iterator aGroupIter = myGroups.begin();
404 for (; aGroupIter != myGroups.end(); ++aGroupIter)
405 (*aGroupIter)->blockEvents(false);
408 bool SketchSolver_Manager::stopSendUpdate() const
410 static const Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
411 // to avoid redisplay of each segment on update by solver one by one in the viewer
412 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
413 if (isUpdateFlushed) {
414 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
416 return isUpdateFlushed;
419 void SketchSolver_Manager::allowSendUpdate() const
421 Events_Loop::loop()->setFlushed(Events_Loop::eventByName(EVENT_OBJECT_UPDATED), true);