Salome HOME
Rectangle problem correction. Wrong case is: create rectangle, move by the first...
[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
114   // Shows that the message has at least one feature applicable for solver
115   bool hasProperFeature = false;
116
117   if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)
118       || theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)
119       || theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_MOVED)) {
120     std::shared_ptr<ModelAPI_ObjectUpdatedMessage> anUpdateMsg =
121         std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
122     std::set<ObjectPtr> aFeatures = anUpdateMsg->objects();
123
124     bool isUpdateFlushed = stopSendUpdate();
125
126     bool isMovedEvt = theMessage->eventID()
127           == Events_Loop::loop()->eventByName(EVENT_OBJECT_MOVED);
128     if (isMovedEvt) {
129       std::set<ObjectPtr>::iterator aFeatIter;
130       for (aFeatIter = aFeatures.begin(); aFeatIter != aFeatures.end(); aFeatIter++) {
131         std::shared_ptr<SketchPlugin_Feature> aSFeature = 
132             std::dynamic_pointer_cast<SketchPlugin_Feature>(*aFeatIter);
133         if (aSFeature) {
134           moveEntity(aSFeature);
135           hasProperFeature = true;
136         }
137       }
138       if (!hasProperFeature) // in this iteration it will compute nothing, so, no problem with recursion
139         // it is important that solver flushes signal updated after processing move signal as there is
140         // optimization that relies on this update, might be found by key "optimization"
141         myIsComputed = false;
142     } else {
143       std::list<FeaturePtr> aSketchFeatures = SketchSolver_Group::selectApplicableFeatures(aFeatures);
144       std::list<FeaturePtr>::iterator aFeatIter = aSketchFeatures.begin();
145       for (; aFeatIter != aSketchFeatures.end(); ++aFeatIter) {
146         if ((*aFeatIter)->getKind() == SketchPlugin_Sketch::ID()) {
147           std::shared_ptr<ModelAPI_CompositeFeature> aSketch = 
148               std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFeatIter);
149           hasProperFeature = changeWorkplane(aSketch) || hasProperFeature;
150           continue;
151         }
152         std::shared_ptr<SketchPlugin_Feature> aFeature = 
153             std::dynamic_pointer_cast<SketchPlugin_Feature>(*aFeatIter);
154         if (!aFeature)
155           continue;
156         hasProperFeature = changeFeature(aFeature) || hasProperFeature;
157       }
158     }
159
160     bool needToUpdate = false;
161     // Solve the set of constraints
162     if (hasProperFeature)
163       needToUpdate = resolveConstraints();
164
165     // Features may be updated => now send events, but for all changed at once
166     if (isUpdateFlushed)
167       allowSendUpdate();
168     // send update for movement in any case
169     if (needToUpdate || isMovedEvt)
170       Events_Loop::loop()->flush(anUpdateEvent);
171
172   } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)) {
173     std::shared_ptr<ModelAPI_ObjectDeletedMessage> aDeleteMsg =
174       std::dynamic_pointer_cast<ModelAPI_ObjectDeletedMessage>(theMessage);
175     const std::set<std::string>& aFeatureGroups = aDeleteMsg->groups();
176
177     // Find SketchPlugin_Sketch::ID() in groups. The constraint groups should be updated when an object removed from Sketch
178     std::set<std::string>::const_iterator aFGrIter;
179     for (aFGrIter = aFeatureGroups.begin(); aFGrIter != aFeatureGroups.end(); aFGrIter++)
180       if (aFGrIter->compare(ModelAPI_ResultConstruction::group()) == 0 ||
181         aFGrIter->compare(ModelAPI_Feature::group()) == 0)
182         break;
183
184     if (aFGrIter != aFeatureGroups.end()) {
185       hasProperFeature = true;
186       std::list<SketchSolver_Group*> aGroupsToResolve;
187       std::list<SketchSolver_Group*>::iterator aGroupIter = myGroups.begin();
188       std::list<SketchSolver_Group*> aSeparatedGroups;
189       while (aGroupIter != myGroups.end()) {
190         if (!(*aGroupIter)->isWorkplaneValid()) {  // the group should be removed
191           delete *aGroupIter;
192           std::list<SketchSolver_Group*>::iterator aRemoveIt = aGroupIter++;
193           myGroups.erase(aRemoveIt);
194           continue;
195         }
196         if (!(*aGroupIter)->isConsistent()) {  // some constraints were removed, try to split the group
197           (*aGroupIter)->splitGroup(aSeparatedGroups);
198           //if (!(*aGroupIter)->getWorkplane()->string(
199           //    SketchPlugin_Sketch::SOLVER_ERROR())->value().empty())
200             aGroupsToResolve.push_back(*aGroupIter);
201         }
202         aGroupIter++;
203       }
204       if (aSeparatedGroups.size() > 0) {
205         myGroups.insert(myGroups.end(), aSeparatedGroups.begin(), aSeparatedGroups.end());
206         aGroupsToResolve.insert(aGroupsToResolve.end(),
207             aSeparatedGroups.begin(), aSeparatedGroups.end());
208       }
209
210       if (!aGroupsToResolve.empty())
211         resolveConstraints(aGroupsToResolve);
212     }
213   }
214
215   if (hasProperFeature)
216     degreesOfFreedom();
217   myIsComputed = false;
218 }
219
220 void SketchSolver_Manager::checkConflictingConstraints(const std::shared_ptr<Events_Message>& theMessage)
221 {
222   if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_SOLVER_REPAIRED)) {
223     std::shared_ptr<ModelAPI_SolverFailedMessage> aMessage =
224         std::dynamic_pointer_cast<ModelAPI_SolverFailedMessage>(theMessage);
225     std::set<ObjectPtr> aSentObjs = aMessage->objects();
226     if (!aSentObjs.empty()) {
227       // Obtain sketch where the constraints are placed.
228       // It is enough to check only one constraint.
229       CompositeFeaturePtr aSketch;
230       FeaturePtr aConstraint = ModelAPI_Feature::feature(*aSentObjs.begin());
231       std::list<SketchSolver_Group*>::const_iterator aGrIt = myGroups.begin();
232       for (; aGrIt != myGroups.end(); ++aGrIt)
233         if ((*aGrIt)->isInteract(aConstraint)) {
234           aSketch = (*aGrIt)->getWorkplane();
235           break;
236         }
237
238       // Search failed groups built on the same sketch
239       if (aSketch) {
240         for (aGrIt = myGroups.begin(); aGrIt != myGroups.end(); ++aGrIt) {
241           SketchSolver_Group* aGroup = *aGrIt;
242           if (aGroup->isBaseWorkplane(aSketch) && aGroup->isFailed() &&
243               !aGroup->isInteract(aConstraint)) {
244             // reset error message on the sketch
245             aGroup->getWorkplane()->string(SketchPlugin_Sketch::SOLVER_ERROR())->setValue(
246                 SketchSolver_Error::CONSTRAINTS());
247             break;
248           }
249         }
250       }
251     }
252   }
253 }
254
255 // ============================================================================
256 //  Function: changeWorkplane
257 //  Purpose:  update workplane by given parameters of the sketch
258 // ============================================================================
259 bool SketchSolver_Manager::changeWorkplane(CompositeFeaturePtr theSketch)
260 {
261   bool aResult = true;  // changed when a workplane wrongly updated
262   bool isUpdated = false;
263   // Try to update specified workplane in all groups
264   std::list<SketchSolver_Group*>::iterator aGroupIter;
265   for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
266     if ((*aGroupIter)->isBaseWorkplane(theSketch)) {
267       isUpdated = true;
268       aResult = false;
269     }
270   // If the workplane is not updated, so this is a new workplane
271   if (!isUpdated) {
272     SketchSolver_Group* aNewGroup = new SketchSolver_Group(theSketch);
273     // Verify that the group is created successfully
274     if (!aNewGroup->isBaseWorkplane(theSketch) || !aNewGroup->isWorkplaneValid()) {
275       delete aNewGroup;
276       return false;
277     }
278     myGroups.push_back(aNewGroup);
279   }
280   return aResult;
281 }
282
283 // ============================================================================
284 //  Function: changeConstraintOrEntity
285 //  Purpose:  create/update the constraint or the feature and place it into appropriate group
286 // ============================================================================
287 bool SketchSolver_Manager::changeFeature(std::shared_ptr<SketchPlugin_Feature> theFeature)
288 {
289   // Search the groups which this feature touches
290   std::set<GroupID> aGroups;
291   findGroups(theFeature, aGroups);
292
293   std::shared_ptr<SketchPlugin_Constraint> aConstraint = 
294       std::dynamic_pointer_cast<SketchPlugin_Constraint>(theFeature);
295
296   // Process the groups list
297   if (aGroups.size() == 0) {
298     // There are no groups applicable for this constraint => create new one
299     // The group will be created only for constraints, not for features
300     if (!aConstraint) return false;
301     std::shared_ptr<ModelAPI_CompositeFeature> aWP = findWorkplane(aConstraint);
302     if (!aWP)
303       return false;
304     SketchSolver_Group* aGroup = new SketchSolver_Group(aWP);
305     if (!aGroup->changeConstraint(aConstraint)) {
306       delete aGroup;
307       return false;
308     }
309     myGroups.push_back(aGroup);
310     return true;
311   } else if (aGroups.size() == 1) {  // Only one group => add feature into it
312     GroupID aGroupId = *(aGroups.begin());
313     std::list<SketchSolver_Group*>::iterator aGroupIter;
314     for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
315       if ((*aGroupIter)->getId() == aGroupId) {
316         // If the group is empty, the feature is not added (the constraint only)
317         if (!aConstraint && !(*aGroupIter)->isEmpty())
318           return (*aGroupIter)->updateFeature(theFeature);
319         return (*aGroupIter)->changeConstraint(aConstraint);
320       }
321   } else if (aGroups.size() > 1) {  // Several groups applicable for this feature => need to merge them
322     std::set<GroupID>::const_iterator aGroupsIter = aGroups.begin();
323
324     // Search first group
325     std::list<SketchSolver_Group*>::iterator aFirstGroupIter;
326     for (aFirstGroupIter = myGroups.begin(); aFirstGroupIter != myGroups.end(); aFirstGroupIter++)
327       if ((*aFirstGroupIter)->getId() == *aGroupsIter)
328         break;
329     if (aFirstGroupIter == myGroups.end())
330       return false;
331
332     // Append other groups to the first one
333     std::list<SketchSolver_Group*>::iterator anOtherGroupIter = aFirstGroupIter;
334     ++anOtherGroupIter;
335     for (aGroupsIter++; aGroupsIter != aGroups.end(); aGroupsIter++) {
336       for (; anOtherGroupIter != myGroups.end(); anOtherGroupIter++)
337         if ((*anOtherGroupIter)->getId() == *aGroupsIter)
338           break;
339       if (anOtherGroupIter == myGroups.end()) {  // Group disappears
340         anOtherGroupIter = aFirstGroupIter;
341         ++anOtherGroupIter;
342         continue;
343       }
344
345       (*aFirstGroupIter)->mergeGroups(**anOtherGroupIter);
346       std::list<SketchSolver_Group*>::iterator aRemoveIt = anOtherGroupIter++;
347       delete *aRemoveIt;
348       myGroups.erase(aRemoveIt);
349     }
350
351     if (aConstraint)
352       (*aFirstGroupIter)->changeConstraint(aConstraint);
353     else
354       (*aFirstGroupIter)->updateFeature(theFeature);
355     // groups are merged => need to resolve them
356     return true;
357   }
358
359   // Something goes wrong
360   return false;
361 }
362
363 // ============================================================================
364 //  Function: moveEntity
365 //  Purpose:  update element moved on the sketch, which is used by constraints
366 // ============================================================================
367 void SketchSolver_Manager::moveEntity(std::shared_ptr<SketchPlugin_Feature> theFeature)
368 {
369   bool isMoved = false;
370   std::list<SketchSolver_Group*>::iterator aGroupIt = myGroups.begin();
371   for (; aGroupIt != myGroups.end(); aGroupIt++)
372     if (!(*aGroupIt)->isEmpty() && (*aGroupIt)->isInteract(theFeature)) {
373       (*aGroupIt)->moveFeature(theFeature);
374       isMoved = true;
375     }
376
377   if (!isMoved && theFeature->getKind() == SketchPlugin_Arc::ID()) {
378     // Workaround to move arc.
379     // If the arc has not been constrained, we will push it into empty group and apply movement.
380     for (aGroupIt = myGroups.begin(); aGroupIt != myGroups.end(); aGroupIt++)
381       if ((*aGroupIt)->isEmpty())
382         (*aGroupIt)->moveFeature(theFeature);
383   }
384 }
385
386 // ============================================================================
387 //  Function: findGroups
388 //  Purpose:  search groups of entities interacting with given feature
389 // ============================================================================
390 void SketchSolver_Manager::findGroups(
391     std::shared_ptr<SketchPlugin_Feature> theFeature,
392     std::set<GroupID>& theGroupIDs) const
393 {
394   std::shared_ptr<ModelAPI_CompositeFeature> aWP = findWorkplane(theFeature);
395
396   SketchSolver_Group* anEmptyGroup = 0;  // appropriate empty group for specified constraint
397   std::list<SketchSolver_Group*>::const_iterator aGroupIter;
398   for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
399     if (aWP == (*aGroupIter)->getWorkplane() && (*aGroupIter)->isInteract(theFeature)) {
400       if (!(*aGroupIter)->isEmpty())
401         theGroupIDs.insert((*aGroupIter)->getId());
402       else if (!anEmptyGroup)
403         anEmptyGroup = *aGroupIter;
404     }
405
406   // When only empty group is found, use it
407   if (anEmptyGroup && theGroupIDs.empty())
408     theGroupIDs.insert(anEmptyGroup->getId());
409 }
410
411 // ============================================================================
412 //  Function: findWorkplane
413 //  Purpose:  search workplane containing given feature
414 // ============================================================================
415 std::shared_ptr<ModelAPI_CompositeFeature> SketchSolver_Manager
416 ::findWorkplane(std::shared_ptr<SketchPlugin_Feature> theFeature) const
417 {
418   // Already verified workplanes
419   std::set<std::shared_ptr<ModelAPI_CompositeFeature> > aVerified;
420
421   std::list<SketchSolver_Group*>::const_iterator aGroupIter;
422   for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++) {
423     std::shared_ptr<ModelAPI_CompositeFeature> aWP = (*aGroupIter)->getWorkplane();
424     if (aVerified.find(aWP) != aVerified.end())
425       continue;
426
427     DataPtr aData = aWP->data();
428     if (aData->isValid()) {
429       std::shared_ptr<ModelAPI_AttributeRefList> aWPFeatures = std::dynamic_pointer_cast<
430           ModelAPI_AttributeRefList>(aData->attribute(SketchPlugin_Sketch::FEATURES_ID()));
431       std::list<ObjectPtr> aFeaturesList = aWPFeatures->list();
432       std::list<ObjectPtr>::const_iterator anIter;
433       for (anIter = aFeaturesList.begin(); anIter != aFeaturesList.end(); anIter++)
434         if (*anIter == theFeature)
435           return aWP;  // workplane is found
436     }
437     aVerified.insert(aWP);
438   }
439
440   return std::shared_ptr<ModelAPI_CompositeFeature>();
441 }
442
443 // ============================================================================
444 //  Function: resolveConstraints
445 //  Purpose:  change entities according to available constraints
446 // ============================================================================
447 bool SketchSolver_Manager::resolveConstraints(const std::list<SketchSolver_Group*>& theGroups)
448 {
449   bool needToUpdate = false;
450   const std::list<SketchSolver_Group*>& aGroupsToResolve = theGroups.empty() ? myGroups : theGroups;
451   std::list<SketchSolver_Group*>::const_iterator aGroupIter = aGroupsToResolve.begin();
452   for (; aGroupIter != aGroupsToResolve.end(); aGroupIter++)
453     if ((*aGroupIter)->resolveConstraints())
454       needToUpdate = true;
455   return needToUpdate;
456 }
457
458 // ============================================================================
459 //  Function: degreesOfFreedom
460 //  Purpose:  calculate DoFs for each sketch
461 // ============================================================================
462 void SketchSolver_Manager::degreesOfFreedom()
463 {
464   static std::map<std::string, int> aDoFDelta; // indicates how many DoF adds or decreases a feature
465   static bool isNeedInit = true;
466   if (isNeedInit) {
467     aDoFDelta[SketchPlugin_Point::ID()] = 2;
468     aDoFDelta[SketchPlugin_Line::ID()] = 4;
469     aDoFDelta[SketchPlugin_Circle::ID()] = 3;
470     aDoFDelta[SketchPlugin_Arc::ID()] = 5;
471
472     aDoFDelta[SketchPlugin_ConstraintAngle::ID()] = -1;
473     aDoFDelta[SketchPlugin_ConstraintCollinear::ID()] = -1;
474     aDoFDelta[SketchPlugin_ConstraintDistance::ID()] = -1;
475     aDoFDelta[SketchPlugin_ConstraintEqual::ID()] = -1;
476     aDoFDelta[SketchPlugin_ConstraintHorizontal::ID()] = -1;
477     aDoFDelta[SketchPlugin_ConstraintLength::ID()] = -1;
478     aDoFDelta[SketchPlugin_ConstraintMiddle::ID()] = -1;
479     aDoFDelta[SketchPlugin_ConstraintParallel::ID()] = -1;
480     aDoFDelta[SketchPlugin_ConstraintPerpendicular::ID()] = -1;
481     aDoFDelta[SketchPlugin_ConstraintRadius::ID()] = -1;
482     aDoFDelta[SketchPlugin_ConstraintTangent::ID()] = -1;
483     aDoFDelta[SketchPlugin_ConstraintVertical::ID()] = -1;
484
485     isNeedInit = false;
486   }
487
488   std::map<CompositeFeaturePtr, int> aSketchDoF;
489
490   std::list<SketchSolver_Group*>::const_iterator aGroupIt = myGroups.begin();
491   for (; aGroupIt != myGroups.end(); ++aGroupIt) {
492     CompositeFeaturePtr aSketch = (*aGroupIt)->getWorkplane();
493     if (!aSketch->data()->isValid()) {
494       myDoF.erase(aSketch);
495       continue;
496     }
497
498     // check conflicting constraints in the group
499     if ((*aGroupIt)->isFailed())
500       aSketchDoF[aSketch] = -1;
501     // check the sketch is already processed
502     if (aSketchDoF.find(aSketch) != aSketchDoF.end() || aSketchDoF[aSketch] < 0)
503       continue;
504
505     std::set<AttributePtr> aCoincidentPoints;
506     int aDoF = 0;
507     int aNbSubs = aSketch->numberOfSubs();
508     for (int i = 0; i < aNbSubs; ++i) {
509       FeaturePtr aFeature = aSketch->subFeature(i);
510       // check DoF delta for invariant types
511       std::map<std::string, int>::const_iterator aFound = aDoFDelta.find(aFeature->getKind());
512       if (aFound != aDoFDelta.end()) {
513         aDoF += aFound->second;
514         continue;
515       }
516
517       // DoF delta in specific cases
518       if (aFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
519         AttributePtr aCoincPoint[2] = {AttributePtr(), AttributePtr()};
520         for (int j = 0; j < 2; ++j) {
521           AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
522               aFeature->attribute(SketchPlugin_Constraint::ATTRIBUTE(j)));
523           if (!aRefAttr)
524             continue;
525           bool isPoint = !aRefAttr->isObject();
526           if (isPoint)
527             aCoincPoint[j] = aRefAttr->attr();
528           else {
529             FeaturePtr anAttr = ModelAPI_Feature::feature(aRefAttr->object());
530             isPoint = anAttr && anAttr->getKind() == SketchPlugin_Point::ID();
531             if (isPoint)
532               aCoincPoint[j] = anAttr->attribute(SketchPlugin_Point::COORD_ID());
533           }
534         }
535         if (aCoincPoint[0] && aCoincPoint[1]) {
536           // point-point coincidence
537           if (aCoincidentPoints.find(aCoincPoint[0]) == aCoincidentPoints.end() ||
538               aCoincidentPoints.find(aCoincPoint[1]) == aCoincidentPoints.end())
539             aDoF -= 2;
540         } else 
541           aDoF -= 1;
542         for (int j = 0; j < 2; ++j)
543           if (aCoincPoint[j])
544             aCoincidentPoints.insert(aCoincPoint[j]);
545       }
546       else if (aFeature->getKind() == SketchPlugin_ConstraintRigid::ID()) {
547         AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
548             aFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
549         assert(aRefAttr);
550         if (!aRefAttr->isObject())
551           aDoF -= 2; // attribute is a point
552         else {
553           FeaturePtr anAttr = ModelAPI_Feature::feature(aRefAttr->object());
554           if (anAttr)
555             aDoF -= aDoFDelta[anAttr->getKind()];
556         }
557       }
558       else if (aFeature->getKind() == SketchPlugin_ConstraintMirror::ID() ||
559                aFeature->getKind() == SketchPlugin_MultiRotation::ID() ||
560                aFeature->getKind() == SketchPlugin_MultiTranslation::ID()) {
561         int aNbCopies = 1;
562         std::string anAttrName;
563         if (aFeature->getKind() == SketchPlugin_ConstraintMirror::ID())
564           anAttrName = SketchPlugin_Constraint::ENTITY_B();
565         else {
566           if (aFeature->getKind() == SketchPlugin_MultiRotation::ID())
567             aNbCopies = aFeature->integer(SketchPlugin_MultiRotation::NUMBER_OF_OBJECTS_ID())->value() - 1;
568           else if (aFeature->getKind() == SketchPlugin_MultiTranslation::ID())
569             aNbCopies = aFeature->integer(SketchPlugin_MultiTranslation::NUMBER_OF_OBJECTS_ID())->value() - 1;
570           anAttrName = SketchPlugin_Constraint::ENTITY_A();
571         }
572
573         AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
574             aFeature->attribute(anAttrName));
575         std::list<ObjectPtr> anObjList = aRefListOfShapes->list();
576         std::list<ObjectPtr>::const_iterator anObjIt = anObjList.begin();
577         for (; anObjIt != anObjList.end(); ++anObjIt) {
578           FeaturePtr aSub = ModelAPI_Feature::feature(*anObjIt);
579           aDoF -= aDoFDelta[aSub->getKind()] * aNbCopies;
580         }
581       }
582     }
583
584     aSketchDoF[aSketch] = aDoF;
585   }
586
587   // Check the degrees of freedom are changed
588   std::map<CompositeFeaturePtr, int>::const_iterator aDoFIt = aSketchDoF.begin();
589   std::map<CompositeFeaturePtr, int>::iterator aFound;
590   for (; aDoFIt != aSketchDoF.end(); ++aDoFIt) {
591     if (aDoFIt->second < 0)
592       continue; // conflicting constraints on the current sketch
593     aFound = myDoF.find(aDoFIt->first);
594     if (aFound != myDoF.end() && aFound->second == aDoFIt->second)
595       continue; // nothing is changed
596     myDoF[aDoFIt->first] = aDoFIt->second;
597     // change attribute value
598     std::ostringstream aStream;
599     if (aDoFIt->second == 0)
600       aStream << "Sketch fully fixed (DOF = " << aDoFIt->second << ")";
601     else
602       aStream << "DOF (degree of freedom) = " << aDoFIt->second;
603     aDoFIt->first->data()->string(SketchPlugin_Sketch::SOLVER_DOF())->setValue(aStream.str());
604   }
605 }
606
607 bool SketchSolver_Manager::stopSendUpdate() const
608 {
609   // to avoid redisplay of each segment on update by solver one by one in the viewer
610   bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
611   if (isUpdateFlushed) {
612     Events_Loop::loop()->setFlushed(anUpdateEvent, false);
613   }
614   return isUpdateFlushed;
615 }
616
617 void SketchSolver_Manager::allowSendUpdate() const
618 {
619   Events_Loop::loop()->setFlushed(anUpdateEvent, true);
620 }