Salome HOME
Fix regression in TestRectangle.py
[modules/shaper.git] / src / SketchSolver / SketchSolver_Manager.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:    SketchSolver_Manager.cpp
4 // Created: 08 May 2014
5 // Author:  Artem ZHIDKOV
6
7 #include "SketchSolver_Manager.h"
8 #include "SketchSolver_Error.h"
9
10 #include <Events_Loop.h>
11 #include <ModelAPI_AttributeDouble.h>
12 #include <ModelAPI_AttributeRefList.h>
13 #include <ModelAPI_Data.h>
14 #include <ModelAPI_Events.h>
15 #include <ModelAPI_Object.h>
16 #include <ModelAPI_ResultConstruction.h>
17 #include <ModelAPI_Attribute.h>
18 #include <ModelAPI_AttributeInteger.h>
19 #include <ModelAPI_AttributeString.h>
20
21 #include <SketchPlugin_Constraint.h>
22 #include <SketchPlugin_ConstraintAngle.h>
23 #include <SketchPlugin_ConstraintCoincidence.h>
24 #include <SketchPlugin_ConstraintCollinear.h>
25 #include <SketchPlugin_ConstraintDistance.h>
26 #include <SketchPlugin_ConstraintEqual.h>
27 #include <SketchPlugin_ConstraintHorizontal.h>
28 #include <SketchPlugin_ConstraintLength.h>
29 #include <SketchPlugin_ConstraintMiddle.h>
30 #include <SketchPlugin_ConstraintMirror.h>
31 #include <SketchPlugin_ConstraintParallel.h>
32 #include <SketchPlugin_ConstraintPerpendicular.h>
33 #include <SketchPlugin_ConstraintRadius.h>
34 #include <SketchPlugin_ConstraintRigid.h>
35 #include <SketchPlugin_ConstraintTangent.h>
36 #include <SketchPlugin_ConstraintVertical.h>
37 #include <SketchPlugin_MultiRotation.h>
38 #include <SketchPlugin_MultiTranslation.h>
39
40 #include <SketchPlugin_Arc.h>
41 #include <SketchPlugin_Circle.h>
42 #include <SketchPlugin_Line.h>
43 #include <SketchPlugin_Point.h>
44 #include <SketchPlugin_Sketch.h>
45 #include <SketchPlugin_Feature.h>
46
47 #include <assert.h>
48 #include <list>
49 #include <set>
50 #include <memory>
51 #include <sstream>
52
53 static const Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
54
55 // Initialization of constraint manager self pointer
56 SketchSolver_Manager* SketchSolver_Manager::mySelf = 0;
57
58 /// Global constraint manager object
59 SketchSolver_Manager* myManager = SketchSolver_Manager::instance();
60
61
62 // ========================================================
63 // ========= SketchSolver_Manager ===============
64 // ========================================================
65 SketchSolver_Manager* SketchSolver_Manager::instance()
66 {
67   if (!mySelf)
68     mySelf = new SketchSolver_Manager();
69   return mySelf;
70 }
71
72 SketchSolver_Manager::SketchSolver_Manager()
73 {
74   myGroups.clear();
75   myIsComputed = false;
76
77   // Register in event loop
78   Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
79   Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
80   Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
81   Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_MOVED));
82
83   Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_SOLVER_FAILED));
84   Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_SOLVER_REPAIRED));
85 }
86
87 SketchSolver_Manager::~SketchSolver_Manager()
88 {
89   myGroups.clear();
90 }
91
92 void SketchSolver_Manager::setBuilder(BuilderPtr theBuilder)
93 {
94   myBuilder = theBuilder;
95 }
96
97 BuilderPtr SketchSolver_Manager::builder()
98 {
99   return myBuilder;
100 }
101
102 // ============================================================================
103 //  Function: processEvent
104 //  Purpose:  listen the event loop and process the message
105 // ============================================================================
106 void SketchSolver_Manager::processEvent(
107   const std::shared_ptr<Events_Message>& theMessage)
108 {
109   checkConflictingConstraints(theMessage);
110   if (myIsComputed)
111     return;
112   myIsComputed = true;
113   if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)
114       || theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)
115       || theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_MOVED)) {
116     std::shared_ptr<ModelAPI_ObjectUpdatedMessage> anUpdateMsg =
117         std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
118     std::set<ObjectPtr> aFeatures = anUpdateMsg->objects();
119
120     bool isUpdateFlushed = stopSendUpdate();
121     // Shows the message has at least one feature applicable for solver
122     bool hasProperFeature = false;
123
124     bool isMovedEvt = theMessage->eventID()
125           == Events_Loop::loop()->eventByName(EVENT_OBJECT_MOVED);
126     if (isMovedEvt) {
127       std::set<ObjectPtr>::iterator aFeatIter;
128       for (aFeatIter = aFeatures.begin(); aFeatIter != aFeatures.end(); aFeatIter++) {
129         std::shared_ptr<SketchPlugin_Feature> aSFeature = 
130             std::dynamic_pointer_cast<SketchPlugin_Feature>(*aFeatIter);
131         if (aSFeature) {
132           moveEntity(aSFeature);
133           hasProperFeature = true;
134         }
135       }
136     } else {
137       std::list<FeaturePtr> aSketchFeatures = SketchSolver_Group::selectApplicableFeatures(aFeatures);
138       std::list<FeaturePtr>::iterator aFeatIter = aSketchFeatures.begin();
139       for (; aFeatIter != aSketchFeatures.end(); ++aFeatIter) {
140         if ((*aFeatIter)->getKind() == SketchPlugin_Sketch::ID()) {
141           std::shared_ptr<ModelAPI_CompositeFeature> aSketch = 
142               std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFeatIter);
143           hasProperFeature = changeWorkplane(aSketch) || hasProperFeature;
144           continue;
145         }
146         std::shared_ptr<SketchPlugin_Feature> aFeature = 
147             std::dynamic_pointer_cast<SketchPlugin_Feature>(*aFeatIter);
148         if (!aFeature)
149           continue;
150         hasProperFeature = changeFeature(aFeature) || hasProperFeature;
151       }
152     }
153
154     bool needToUpdate = false;
155     // Solve the set of constraints
156     if (hasProperFeature)
157       needToUpdate = resolveConstraints();
158
159     // Features may be updated => now send events, but for all changed at once
160     if (isUpdateFlushed)
161       allowSendUpdate();
162     // send update for movement in any case
163     if (needToUpdate || isMovedEvt)
164       Events_Loop::loop()->flush(anUpdateEvent);
165
166   } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)) {
167     std::shared_ptr<ModelAPI_ObjectDeletedMessage> aDeleteMsg =
168       std::dynamic_pointer_cast<ModelAPI_ObjectDeletedMessage>(theMessage);
169     const std::set<std::string>& aFeatureGroups = aDeleteMsg->groups();
170
171     // Find SketchPlugin_Sketch::ID() in groups. The constraint groups should be updated when an object removed from Sketch
172     std::set<std::string>::const_iterator aFGrIter;
173     for (aFGrIter = aFeatureGroups.begin(); aFGrIter != aFeatureGroups.end(); aFGrIter++)
174       if (aFGrIter->compare(ModelAPI_ResultConstruction::group()) == 0 ||
175         aFGrIter->compare(ModelAPI_Feature::group()) == 0)
176         break;
177
178     if (aFGrIter != aFeatureGroups.end()) {
179       std::list<SketchSolver_Group*> aGroupsToResolve;
180       std::list<SketchSolver_Group*>::iterator aGroupIter = myGroups.begin();
181       std::list<SketchSolver_Group*> aSeparatedGroups;
182       while (aGroupIter != myGroups.end()) {
183         if (!(*aGroupIter)->isWorkplaneValid()) {  // the group should be removed
184           delete *aGroupIter;
185           std::list<SketchSolver_Group*>::iterator aRemoveIt = aGroupIter++;
186           myGroups.erase(aRemoveIt);
187           continue;
188         }
189         if (!(*aGroupIter)->isConsistent()) {  // some constraints were removed, try to split the group
190           (*aGroupIter)->splitGroup(aSeparatedGroups);
191           //if (!(*aGroupIter)->getWorkplane()->string(
192           //    SketchPlugin_Sketch::SOLVER_ERROR())->value().empty())
193             aGroupsToResolve.push_back(*aGroupIter);
194         }
195         aGroupIter++;
196       }
197       if (aSeparatedGroups.size() > 0) {
198         myGroups.insert(myGroups.end(), aSeparatedGroups.begin(), aSeparatedGroups.end());
199         aGroupsToResolve.insert(aGroupsToResolve.end(),
200             aSeparatedGroups.begin(), aSeparatedGroups.end());
201       }
202
203       if (!aGroupsToResolve.empty())
204         resolveConstraints(aGroupsToResolve);
205     }
206   }
207   degreesOfFreedom();
208   myIsComputed = false;
209 }
210
211 void SketchSolver_Manager::checkConflictingConstraints(const std::shared_ptr<Events_Message>& theMessage)
212 {
213   if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_SOLVER_REPAIRED)) {
214     std::shared_ptr<ModelAPI_SolverFailedMessage> aMessage =
215         std::dynamic_pointer_cast<ModelAPI_SolverFailedMessage>(theMessage);
216     std::set<ObjectPtr> aSentObjs = aMessage->objects();
217     if (!aSentObjs.empty()) {
218       // Obtain sketch where the constraints are placed.
219       // It is enough to check only one constraint.
220       CompositeFeaturePtr aSketch;
221       FeaturePtr aConstraint = ModelAPI_Feature::feature(*aSentObjs.begin());
222       std::list<SketchSolver_Group*>::const_iterator aGrIt = myGroups.begin();
223       for (; aGrIt != myGroups.end(); ++aGrIt)
224         if ((*aGrIt)->isInteract(aConstraint)) {
225           aSketch = (*aGrIt)->getWorkplane();
226           break;
227         }
228
229       // Search failed groups built on the same sketch
230       if (aSketch) {
231         for (aGrIt = myGroups.begin(); aGrIt != myGroups.end(); ++aGrIt) {
232           SketchSolver_Group* aGroup = *aGrIt;
233           if (aGroup->isBaseWorkplane(aSketch) && aGroup->isFailed() &&
234               !aGroup->isInteract(aConstraint)) {
235             // reset error message on the sketch
236             aGroup->getWorkplane()->string(SketchPlugin_Sketch::SOLVER_ERROR())->setValue(
237                 SketchSolver_Error::CONSTRAINTS());
238             break;
239           }
240         }
241       }
242     }
243   }
244 }
245
246 // ============================================================================
247 //  Function: changeWorkplane
248 //  Purpose:  update workplane by given parameters of the sketch
249 // ============================================================================
250 bool SketchSolver_Manager::changeWorkplane(CompositeFeaturePtr theSketch)
251 {
252   bool aResult = true;  // changed when a workplane wrongly updated
253   bool isUpdated = false;
254   // Try to update specified workplane in all groups
255   std::list<SketchSolver_Group*>::iterator aGroupIter;
256   for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
257     if ((*aGroupIter)->isBaseWorkplane(theSketch)) {
258       isUpdated = true;
259       aResult = false;
260     }
261   // If the workplane is not updated, so this is a new workplane
262   if (!isUpdated) {
263     SketchSolver_Group* aNewGroup = new SketchSolver_Group(theSketch);
264     // Verify that the group is created successfully
265     if (!aNewGroup->isBaseWorkplane(theSketch) || !aNewGroup->isWorkplaneValid()) {
266       delete aNewGroup;
267       return false;
268     }
269     myGroups.push_back(aNewGroup);
270   }
271   return aResult;
272 }
273
274 // ============================================================================
275 //  Function: changeConstraintOrEntity
276 //  Purpose:  create/update the constraint or the feature and place it into appropriate group
277 // ============================================================================
278 bool SketchSolver_Manager::changeFeature(std::shared_ptr<SketchPlugin_Feature> theFeature)
279 {
280   // Search the groups which this feature touches
281   std::set<GroupID> aGroups;
282   findGroups(theFeature, aGroups);
283
284   std::shared_ptr<SketchPlugin_Constraint> aConstraint = 
285       std::dynamic_pointer_cast<SketchPlugin_Constraint>(theFeature);
286
287   // Process the groups list
288   if (aGroups.size() == 0) {
289     // There are no groups applicable for this constraint => create new one
290     // The group will be created only for constraints, not for features
291     if (!aConstraint) return false;
292     std::shared_ptr<ModelAPI_CompositeFeature> aWP = findWorkplane(aConstraint);
293     if (!aWP)
294       return false;
295     SketchSolver_Group* aGroup = new SketchSolver_Group(aWP);
296     if (!aGroup->changeConstraint(aConstraint)) {
297       delete aGroup;
298       return false;
299     }
300     myGroups.push_back(aGroup);
301     return true;
302   } else if (aGroups.size() == 1) {  // Only one group => add feature into it
303     GroupID aGroupId = *(aGroups.begin());
304     std::list<SketchSolver_Group*>::iterator aGroupIter;
305     for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
306       if ((*aGroupIter)->getId() == aGroupId) {
307         // If the group is empty, the feature is not added (the constraint only)
308         if (!aConstraint && !(*aGroupIter)->isEmpty())
309           return (*aGroupIter)->updateFeature(theFeature);
310         return (*aGroupIter)->changeConstraint(aConstraint);
311       }
312   } else if (aGroups.size() > 1) {  // Several groups applicable for this feature => need to merge them
313     std::set<GroupID>::const_iterator aGroupsIter = aGroups.begin();
314
315     // Search first group
316     std::list<SketchSolver_Group*>::iterator aFirstGroupIter;
317     for (aFirstGroupIter = myGroups.begin(); aFirstGroupIter != myGroups.end(); aFirstGroupIter++)
318       if ((*aFirstGroupIter)->getId() == *aGroupsIter)
319         break;
320     if (aFirstGroupIter == myGroups.end())
321       return false;
322
323     // Append other groups to the first one
324     std::list<SketchSolver_Group*>::iterator anOtherGroupIter = aFirstGroupIter;
325     ++anOtherGroupIter;
326     for (aGroupsIter++; aGroupsIter != aGroups.end(); aGroupsIter++) {
327       for (; anOtherGroupIter != myGroups.end(); anOtherGroupIter++)
328         if ((*anOtherGroupIter)->getId() == *aGroupsIter)
329           break;
330       if (anOtherGroupIter == myGroups.end()) {  // Group disappears
331         anOtherGroupIter = aFirstGroupIter;
332         ++anOtherGroupIter;
333         continue;
334       }
335
336       (*aFirstGroupIter)->mergeGroups(**anOtherGroupIter);
337       std::list<SketchSolver_Group*>::iterator aRemoveIt = anOtherGroupIter++;
338       delete *aRemoveIt;
339       myGroups.erase(aRemoveIt);
340     }
341
342     if (aConstraint)
343       (*aFirstGroupIter)->changeConstraint(aConstraint);
344     else
345       (*aFirstGroupIter)->updateFeature(theFeature);
346     // groups are merged => need to resolve them
347     return true;
348   }
349
350   // Something goes wrong
351   return false;
352 }
353
354 // ============================================================================
355 //  Function: moveEntity
356 //  Purpose:  update element moved on the sketch, which is used by constraints
357 // ============================================================================
358 void SketchSolver_Manager::moveEntity(std::shared_ptr<SketchPlugin_Feature> theFeature)
359 {
360   bool isMoved = false;
361   std::list<SketchSolver_Group*>::iterator aGroupIt = myGroups.begin();
362   for (; aGroupIt != myGroups.end(); aGroupIt++)
363     if (!(*aGroupIt)->isEmpty() && (*aGroupIt)->isInteract(theFeature)) {
364       (*aGroupIt)->moveFeature(theFeature);
365       isMoved = true;
366     }
367
368   if (!isMoved && theFeature->getKind() == SketchPlugin_Arc::ID()) {
369     // Workaround to move arc.
370     // If the arc has not been constrained, we will push it into empty group and apply movement.
371     for (aGroupIt = myGroups.begin(); aGroupIt != myGroups.end(); aGroupIt++)
372       if ((*aGroupIt)->isEmpty())
373         (*aGroupIt)->moveFeature(theFeature);
374   }
375 }
376
377 // ============================================================================
378 //  Function: findGroups
379 //  Purpose:  search groups of entities interacting with given feature
380 // ============================================================================
381 void SketchSolver_Manager::findGroups(
382     std::shared_ptr<SketchPlugin_Feature> theFeature,
383     std::set<GroupID>& theGroupIDs) const
384 {
385   std::shared_ptr<ModelAPI_CompositeFeature> aWP = findWorkplane(theFeature);
386
387   SketchSolver_Group* anEmptyGroup = 0;  // appropriate empty group for specified constraint
388   std::list<SketchSolver_Group*>::const_iterator aGroupIter;
389   for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
390     if (aWP == (*aGroupIter)->getWorkplane() && (*aGroupIter)->isInteract(theFeature)) {
391       if (!(*aGroupIter)->isEmpty())
392         theGroupIDs.insert((*aGroupIter)->getId());
393       else if (!anEmptyGroup)
394         anEmptyGroup = *aGroupIter;
395     }
396
397   // When only empty group is found, use it
398   if (anEmptyGroup && theGroupIDs.empty())
399     theGroupIDs.insert(anEmptyGroup->getId());
400 }
401
402 // ============================================================================
403 //  Function: findWorkplane
404 //  Purpose:  search workplane containing given feature
405 // ============================================================================
406 std::shared_ptr<ModelAPI_CompositeFeature> SketchSolver_Manager
407 ::findWorkplane(std::shared_ptr<SketchPlugin_Feature> theFeature) const
408 {
409   // Already verified workplanes
410   std::set<std::shared_ptr<ModelAPI_CompositeFeature> > aVerified;
411
412   std::list<SketchSolver_Group*>::const_iterator aGroupIter;
413   for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++) {
414     std::shared_ptr<ModelAPI_CompositeFeature> aWP = (*aGroupIter)->getWorkplane();
415     if (aVerified.find(aWP) != aVerified.end())
416       continue;
417
418     DataPtr aData = aWP->data();
419     if (aData->isValid()) {
420       std::shared_ptr<ModelAPI_AttributeRefList> aWPFeatures = std::dynamic_pointer_cast<
421           ModelAPI_AttributeRefList>(aData->attribute(SketchPlugin_Sketch::FEATURES_ID()));
422       std::list<ObjectPtr> aFeaturesList = aWPFeatures->list();
423       std::list<ObjectPtr>::const_iterator anIter;
424       for (anIter = aFeaturesList.begin(); anIter != aFeaturesList.end(); anIter++)
425         if (*anIter == theFeature)
426           return aWP;  // workplane is found
427     }
428     aVerified.insert(aWP);
429   }
430
431   return std::shared_ptr<ModelAPI_CompositeFeature>();
432 }
433
434 // ============================================================================
435 //  Function: resolveConstraints
436 //  Purpose:  change entities according to available constraints
437 // ============================================================================
438 bool SketchSolver_Manager::resolveConstraints(const std::list<SketchSolver_Group*>& theGroups)
439 {
440   bool needToUpdate = false;
441   const std::list<SketchSolver_Group*>& aGroupsToResolve = theGroups.empty() ? myGroups : theGroups;
442   std::list<SketchSolver_Group*>::const_iterator aGroupIter = aGroupsToResolve.begin();
443   for (; aGroupIter != aGroupsToResolve.end(); aGroupIter++)
444     if ((*aGroupIter)->resolveConstraints())
445       needToUpdate = true;
446   return needToUpdate;
447 }
448
449 // ============================================================================
450 //  Function: degreesOfFreedom
451 //  Purpose:  calculate DoFs for each sketch
452 // ============================================================================
453 void SketchSolver_Manager::degreesOfFreedom()
454 {
455   static std::map<std::string, int> aDoFDelta; // indicates how many DoF adds or decreases a feature
456   static bool isNeedInit = true;
457   if (isNeedInit) {
458     aDoFDelta[SketchPlugin_Point::ID()] = 2;
459     aDoFDelta[SketchPlugin_Line::ID()] = 4;
460     aDoFDelta[SketchPlugin_Circle::ID()] = 3;
461     aDoFDelta[SketchPlugin_Arc::ID()] = 5;
462
463     aDoFDelta[SketchPlugin_ConstraintAngle::ID()] = -1;
464     aDoFDelta[SketchPlugin_ConstraintCollinear::ID()] = -1;
465     aDoFDelta[SketchPlugin_ConstraintDistance::ID()] = -1;
466     aDoFDelta[SketchPlugin_ConstraintEqual::ID()] = -1;
467     aDoFDelta[SketchPlugin_ConstraintHorizontal::ID()] = -1;
468     aDoFDelta[SketchPlugin_ConstraintLength::ID()] = -1;
469     aDoFDelta[SketchPlugin_ConstraintMiddle::ID()] = -1;
470     aDoFDelta[SketchPlugin_ConstraintParallel::ID()] = -1;
471     aDoFDelta[SketchPlugin_ConstraintPerpendicular::ID()] = -1;
472     aDoFDelta[SketchPlugin_ConstraintRadius::ID()] = -1;
473     aDoFDelta[SketchPlugin_ConstraintTangent::ID()] = -1;
474     aDoFDelta[SketchPlugin_ConstraintVertical::ID()] = -1;
475
476     isNeedInit = false;
477   }
478
479   std::map<CompositeFeaturePtr, int> aSketchDoF;
480
481   std::list<SketchSolver_Group*>::const_iterator aGroupIt = myGroups.begin();
482   for (; aGroupIt != myGroups.end(); ++aGroupIt) {
483     CompositeFeaturePtr aSketch = (*aGroupIt)->getWorkplane();
484     if (!aSketch->data()->isValid()) {
485       myDoF.erase(aSketch);
486       continue;
487     }
488
489     // check conflicting constraints in the group
490     if ((*aGroupIt)->isFailed())
491       aSketchDoF[aSketch] = -1;
492     // check the sketch is already processed
493     if (aSketchDoF.find(aSketch) != aSketchDoF.end() || aSketchDoF[aSketch] < 0)
494       continue;
495
496     int aDoF = 0;
497     int aNbSubs = aSketch->numberOfSubs();
498     for (int i = 0; i < aNbSubs; ++i) {
499       FeaturePtr aFeature = aSketch->subFeature(i);
500       // check DoF delta for invariant types
501       std::map<std::string, int>::const_iterator aFound = aDoFDelta.find(aFeature->getKind());
502       if (aFound != aDoFDelta.end()) {
503         aDoF += aFound->second;
504         continue;
505       }
506
507       // DoF delta in specific cases
508       if (aFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
509         for (int j = 0; j < 2; ++j) {
510           AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
511               aFeature->attribute(SketchPlugin_Constraint::ATTRIBUTE(j)));
512           if (!aRefAttr)
513             continue;
514           bool isPoint = !aRefAttr->isObject();
515           if (!isPoint) {
516             FeaturePtr anAttr = ModelAPI_Feature::feature(aRefAttr->object());
517             isPoint = anAttr && anAttr->getKind() == SketchPlugin_Point::ID();
518           }
519           if (isPoint)
520             aDoF -= 1;
521         }
522       }
523       else if (aFeature->getKind() == SketchPlugin_ConstraintRigid::ID()) {
524         AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
525             aFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
526         assert(aRefAttr);
527         if (!aRefAttr->isObject())
528           aDoF -= 2; // attribute is a point
529         else {
530           FeaturePtr anAttr = ModelAPI_Feature::feature(aRefAttr->object());
531           assert(anAttr);
532           aDoF -= aDoFDelta[anAttr->getKind()];
533         }
534       }
535       else if (aFeature->getKind() == SketchPlugin_ConstraintMirror::ID() ||
536                aFeature->getKind() == SketchPlugin_MultiRotation::ID() ||
537                aFeature->getKind() == SketchPlugin_MultiTranslation::ID()) {
538         int aNbCopies = 1;
539         std::string anAttrName;
540         if (aFeature->getKind() == SketchPlugin_ConstraintMirror::ID())
541           anAttrName = SketchPlugin_Constraint::ENTITY_B();
542         else {
543           if (aFeature->getKind() == SketchPlugin_MultiRotation::ID())
544             aNbCopies = aFeature->integer(SketchPlugin_MultiRotation::NUMBER_OF_OBJECTS_ID())->value() - 1;
545           else if (aFeature->getKind() == SketchPlugin_MultiTranslation::ID())
546             aNbCopies = aFeature->integer(SketchPlugin_MultiTranslation::NUMBER_OF_OBJECTS_ID())->value() - 1;
547           anAttrName = SketchPlugin_Constraint::ENTITY_A();
548         }
549
550         AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
551             aFeature->attribute(anAttrName));
552         std::list<ObjectPtr> anObjList = aRefListOfShapes->list();
553         std::list<ObjectPtr>::const_iterator anObjIt = anObjList.begin();
554         for (; anObjIt != anObjList.end(); ++anObjIt) {
555           FeaturePtr aSub = ModelAPI_Feature::feature(*anObjIt);
556           aDoF -= aDoFDelta[aSub->getKind()] * aNbCopies;
557         }
558       }
559     }
560
561     aSketchDoF[aSketch] = aDoF;
562   }
563
564   // Check the degrees of freedom are changed
565   std::map<CompositeFeaturePtr, int>::const_iterator aDoFIt = aSketchDoF.begin();
566   std::map<CompositeFeaturePtr, int>::iterator aFound;
567   for (; aDoFIt != aSketchDoF.end(); ++aDoFIt) {
568     if (aDoFIt->second < 0)
569       continue; // conflicting constraints on the current sketch
570     aFound = myDoF.find(aDoFIt->first);
571     if (aFound != myDoF.end() && aFound->second == aDoFIt->second)
572       continue; // nothing is changed
573     myDoF[aDoFIt->first] = aDoFIt->second;
574     // change attribute value
575     std::ostringstream aStream;
576     aStream << "DOF(degree of freedom) = " << aDoFIt->second;
577     aDoFIt->first->data()->string(SketchPlugin_Sketch::SOLVER_DOF())->setValue(aStream.str());
578   }
579 }
580
581 bool SketchSolver_Manager::stopSendUpdate() const
582 {
583   // to avoid redisplay of each segment on update by solver one by one in the viewer
584   bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
585   if (isUpdateFlushed) {
586     Events_Loop::loop()->setFlushed(anUpdateEvent, false);
587   }
588   return isUpdateFlushed;
589 }
590
591 void SketchSolver_Manager::allowSendUpdate() const
592 {
593   Events_Loop::loop()->setFlushed(anUpdateEvent, true);
594 }