Salome HOME
First phase of SketchSolver refactoring
[modules/shaper.git] / src / SketchSolver / SketchSolver_Group.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:    SketchSolver_Group.cpp
4 // Created: 27 May 2014
5 // Author:  Artem ZHIDKOV
6
7 #include "SketchSolver_Group.h"
8
9 #include <SketchSolver_Constraint.h>
10 #include <SketchSolver_ConstraintCoincidence.h>
11 #include <SketchSolver_ConstraintMulti.h>
12 #include <SketchSolver_Error.h>
13 #include <SketchSolver_Manager.h>
14
15 #include <Events_Error.h>
16 #include <Events_Loop.h>
17 #include <ModelAPI_AttributeString.h>
18 #include <ModelAPI_Events.h>
19 #include <ModelAPI_Session.h>
20 #include <ModelAPI_Validator.h>
21
22 #include <SketchPlugin_ConstraintAngle.h>
23 #include <SketchPlugin_ConstraintCoincidence.h>
24 #include <SketchPlugin_ConstraintDistance.h>
25 #include <SketchPlugin_ConstraintEqual.h>
26 #include <SketchPlugin_ConstraintHorizontal.h>
27 #include <SketchPlugin_ConstraintLength.h>
28 #include <SketchPlugin_ConstraintFillet.h>
29 #include <SketchPlugin_ConstraintMirror.h>
30 #include <SketchPlugin_ConstraintParallel.h>
31 #include <SketchPlugin_ConstraintPerpendicular.h>
32 #include <SketchPlugin_ConstraintRadius.h>
33 #include <SketchPlugin_ConstraintRigid.h>
34 #include <SketchPlugin_ConstraintTangent.h>
35 #include <SketchPlugin_ConstraintVertical.h>
36 #include <SketchPlugin_MultiRotation.h>
37 #include <SketchPlugin_MultiTranslation.h>
38
39 #include <math.h>
40 #include <assert.h>
41
42
43 /// \brief This class is used to give unique index to the groups
44 class GroupIndexer
45 {
46 public:
47   /// \brief Return vacant index
48   static GroupID NEW_GROUP() { return ++myGroupIndex; }
49   /// \brief Removes the index
50   static void REMOVE_GROUP(const GroupID& theIndex) {
51     if (myGroupIndex == theIndex)
52       myGroupIndex--;
53   }
54
55 private:
56   GroupIndexer() {};
57
58   static GroupID myGroupIndex; ///< index of the group
59 };
60
61 GroupID GroupIndexer::myGroupIndex = GID_OUTOFGROUP;
62
63
64 static void sendMessage(const char* theMessageName)
65 {
66   std::shared_ptr<Events_Message> aMessage = std::shared_ptr<Events_Message>(
67       new Events_Message(Events_Loop::eventByName(theMessageName)));
68   Events_Loop::loop()->send(aMessage);
69 }
70
71
72
73 // ========================================================
74 // =========  SketchSolver_Group  ===============
75 // ========================================================
76
77 SketchSolver_Group::SketchSolver_Group(
78     std::shared_ptr<ModelAPI_CompositeFeature> theWorkplane)
79     : myID(GroupIndexer::NEW_GROUP()),
80       myPrevSolved(true)
81 {
82   // Initialize workplane
83   myWorkplaneID = EID_UNKNOWN;
84   addWorkplane(theWorkplane);
85 }
86
87 SketchSolver_Group::~SketchSolver_Group()
88 {
89   myConstraints.clear();
90   GroupIndexer::REMOVE_GROUP(myID);
91 }
92
93 // ============================================================================
94 //  Function: isBaseWorkplane
95 //  Class:    SketchSolver_Group
96 //  Purpose:  verify the group is based on the given workplane
97 // ============================================================================
98 bool SketchSolver_Group::isBaseWorkplane(CompositeFeaturePtr theWorkplane) const
99 {
100   return theWorkplane == mySketch;
101 }
102
103 // ============================================================================
104 //  Function: isInteract
105 //  Class:    SketchSolver_Group
106 //  Purpose:  verify are there any entities in the group used by given constraint
107 // ============================================================================
108 bool SketchSolver_Group::isInteract(FeaturePtr theFeature) const
109 {
110   // Empty group interacts with everything
111   if (isEmpty())
112     return true;
113   // Check interaction with the storage
114   return myStorage->isInteract(theFeature);
115 }
116
117 // ============================================================================
118 //  Function: changeConstraint
119 //  Class:    SketchSolver_Group
120 //  Purpose:  create/update the constraint in the group
121 // ============================================================================
122 bool SketchSolver_Group::changeConstraint(
123     std::shared_ptr<SketchPlugin_Constraint> theConstraint)
124 {
125   // There is no workplane yet, something wrong
126   if (myWorkplaneID == EID_UNKNOWN)
127     return false;
128
129   if (!theConstraint || !theConstraint->data())
130     return false;
131
132   if (!checkFeatureValidity(theConstraint))
133     return false;
134
135   BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
136
137   bool isNewConstraint = myConstraints.find(theConstraint) == myConstraints.end();
138   if (isNewConstraint) {
139     // Add constraint to the current group
140     SolverConstraintPtr aConstraint = aBuilder->createConstraint(theConstraint);
141     if (!aConstraint)
142       return false;
143     aConstraint->process(myStorage, getId(), getWorkplaneId());
144     if (!aConstraint->error().empty()) {
145       if (aConstraint->error() == SketchSolver_Error::NOT_INITIALIZED())
146         return false; // some attribute are not initialized yet, don't show message
147       Events_Error::send(aConstraint->error(), this);
148     }
149     myConstraints[theConstraint] = aConstraint;
150   }
151   else
152     myConstraints[theConstraint]->update();
153
154   // Fix mirror line
155   if (theConstraint->getKind() == SketchPlugin_ConstraintMirror::ID()) {
156     AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
157         theConstraint->attribute(SketchPlugin_ConstraintMirror::ENTITY_A()));
158     if (aRefAttr && aRefAttr->isObject()) {
159       std::shared_ptr<SketchPlugin_Feature> aFeature =
160           std::dynamic_pointer_cast<SketchPlugin_Feature>(
161           ModelAPI_Feature::feature(aRefAttr->object()));
162       if (aFeature) {
163         SolverConstraintPtr aConstraint = aBuilder->createFixedConstraint(aFeature);
164         if (aConstraint) {
165           aConstraint->process(myStorage, getId(), getWorkplaneId());
166           setTemporary(aConstraint);
167         }
168       }
169     }
170   }
171   return true;
172 }
173
174
175 void SketchSolver_Group::updateConstraints()
176 {
177   std::set<SolverConstraintPtr> aPostponed; // postponed constraints Multi-Rotation and Multi-Translation
178
179   ConstraintConstraintMap::iterator anIt = myConstraints.begin();
180   for (; anIt != myConstraints.end(); ++anIt) {
181     if (myChangedConstraints.find(anIt->first) == myChangedConstraints.end())
182       continue;
183     if (anIt->first->getKind() == SketchPlugin_MultiRotation::ID() ||
184         anIt->first->getKind() == SketchPlugin_MultiTranslation::ID())
185       aPostponed.insert(anIt->second);
186     else
187       anIt->second->update();
188   }
189
190   // Update postponed constraints
191   std::set<SolverConstraintPtr>::iterator aSCIter = aPostponed.begin();
192   for (; aSCIter != aPostponed.end(); ++aSCIter)
193     (*aSCIter)->update();
194
195   myChangedConstraints.clear();
196 }
197
198 bool SketchSolver_Group::updateFeature(FeaturePtr theFeature)
199 {
200   if (!checkFeatureValidity(theFeature))
201     return false;
202   myStorage->refresh(true);
203   return myStorage->update(theFeature);
204 }
205
206 void SketchSolver_Group::moveFeature(FeaturePtr theFeature)
207 {
208   BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
209
210   // Firstly, revert changes in the fixed entities
211   myStorage->refresh(true);
212
213   // Secondly, search attributes of the feature in the list of the Multi constraints and update them
214   ConstraintConstraintMap::iterator aCIt = myConstraints.begin();
215   for (; aCIt != myConstraints.end(); ++aCIt) {
216     if ((aCIt->second->getType() == CONSTRAINT_MULTI_ROTATION ||
217          aCIt->second->getType() == CONSTRAINT_MULTI_TRANSLATION)
218         && aCIt->second->isUsed(theFeature))
219       std::dynamic_pointer_cast<SketchSolver_ConstraintMulti>(aCIt->second)->update(true);
220   }
221
222   // Then, create temporary rigid constraint
223   SolverConstraintPtr aConstraint = aBuilder->createMovementConstraint(theFeature);
224   if (!aConstraint)
225     return;
226   aConstraint->process(myStorage, getId(), getWorkplaneId());
227   if (aConstraint->error().empty())
228     setTemporary(aConstraint);
229 }
230
231 // ============================================================================
232 //  Function: addWorkplane
233 //  Class:    SketchSolver_Group
234 //  Purpose:  create workplane for the group
235 // ============================================================================
236 bool SketchSolver_Group::addWorkplane(CompositeFeaturePtr theSketch)
237 {
238   if (myWorkplaneID != EID_UNKNOWN || theSketch->getKind() != SketchPlugin_Sketch::ID())
239     return false;  // the workplane already exists or the function parameter is not Sketch
240
241   mySketch = theSketch;
242   if (!updateWorkplane()) {
243     mySketch = CompositeFeaturePtr();
244     return false;
245   }
246   return true;
247 }
248
249 // ============================================================================
250 //  Function: updateWorkplane
251 //  Class:    SketchSolver_Group
252 //  Purpose:  update parameters of workplane
253 // ============================================================================
254 bool SketchSolver_Group::updateWorkplane()
255 {
256   BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
257   if (!myStorage) // Create storage if not exists
258     myStorage = aBuilder->createStorage(getId());
259
260   // sketch should be unchanged, set it out of current group
261   bool isUpdated = myStorage->update(FeaturePtr(mySketch), GID_OUTOFGROUP);
262   if (isUpdated) {
263     EntityWrapperPtr anEntity = myStorage->entity(FeaturePtr(mySketch));
264     myWorkplaneID = anEntity->id();
265   }
266   return isUpdated;
267 }
268
269 // ============================================================================
270 //  Function: resolveConstraints
271 //  Class:    SketchSolver_Group
272 //  Purpose:  solve the set of constraints for the current group
273 // ============================================================================
274 bool SketchSolver_Group::resolveConstraints()
275 {
276   if (!myChangedConstraints.empty())
277     updateConstraints();
278
279   bool aResolved = false;
280   bool isGroupEmpty = isEmpty();
281   if (myStorage->isNeedToResolve() && !isGroupEmpty) {
282     if (!mySketchSolver)
283       mySketchSolver = SketchSolver_Manager::instance()->builder()->createSolver();
284
285     mySketchSolver->setGroup(myID);
286     mySketchSolver->calculateFailedConstraints(false);
287     myStorage->initializeSolver(mySketchSolver);
288
289     SketchSolver_SolveStatus aResult = STATUS_OK;
290     try {
291       if (myStorage->hasDuplicatedConstraint())
292         aResult = STATUS_INCONSISTENT;
293       else {
294         // To avoid overconstraint situation, we will remove temporary constraints one-by-one
295         // and try to find the case without overconstraint
296         bool isLastChance = false;
297         size_t aNbTemp = myStorage->nbTemporary();
298         while (true) {
299           aResult = mySketchSolver->solve();
300           if (aResult == STATUS_OK || aResult == STATUS_EMPTYSET || isLastChance)
301             break;
302           if (aNbTemp == 0) {
303             // try to update parameters and resolve once again
304             ConstraintConstraintMap::iterator aConstrIt = myConstraints.begin();
305             for (; aConstrIt != myConstraints.end(); ++aConstrIt)
306               aConstrIt->second->update();
307             isLastChance = true;
308           } else
309             aNbTemp = myStorage->removeTemporary();
310           mySketchSolver->calculateFailedConstraints(true); // something failed => need to find it
311           myStorage->initializeSolver(mySketchSolver);
312         }
313       }
314     } catch (...) {
315 //      Events_Error::send(SketchSolver_Error::SOLVESPACE_CRASH(), this);
316       getWorkplane()->string(SketchPlugin_Sketch::SOLVER_ERROR())->setValue(SketchSolver_Error::SOLVESPACE_CRASH());
317       if (myPrevSolved) {
318         // the error message should be changed before sending the message
319         sendMessage(EVENT_SOLVER_FAILED);
320         myPrevSolved = false;
321       }
322       return false;
323     }
324     if (aResult == STATUS_OK || aResult == STATUS_EMPTYSET) {  // solution succeeded, store results into correspondent attributes
325       myStorage->refresh();
326       if (!myPrevSolved) {
327         getWorkplane()->string(SketchPlugin_Sketch::SOLVER_ERROR())->setValue("");
328         // the error message should be changed before sending the message
329         sendMessage(EVENT_SOLVER_REPAIRED);
330         myPrevSolved = true;
331       }
332     } else if (!myConstraints.empty()) {
333 //      Events_Error::send(SketchSolver_Error::CONSTRAINTS(), this);
334       getWorkplane()->string(SketchPlugin_Sketch::SOLVER_ERROR())->setValue(SketchSolver_Error::CONSTRAINTS());
335       if (myPrevSolved) {
336         // the error message should be changed before sending the message
337         sendMessage(EVENT_SOLVER_FAILED);
338         myPrevSolved = false;
339       }
340     }
341
342     aResolved = true;
343   } else if (!isGroupEmpty) {
344     // Check there are constraints Fixed. If they exist, update parameters by stored values
345     ConstraintConstraintMap::iterator aCIt = myConstraints.begin();
346     for (; aCIt != myConstraints.end(); ++aCIt)
347       if (aCIt->first->getKind() == SketchPlugin_ConstraintRigid::ID()) {
348         aResolved = true;
349         break;
350       }
351     if (aCIt != myConstraints.end())
352       myStorage->refresh();
353   }
354   removeTemporaryConstraints();
355   myStorage->setNeedToResolve(false);
356   return aResolved;
357 }
358
359 // ============================================================================
360 //  Function: mergeGroups
361 //  Class:    SketchSolver_Group
362 //  Purpose:  append specified group to the current group
363 // ============================================================================
364 void SketchSolver_Group::mergeGroups(const SketchSolver_Group& theGroup)
365 {
366   // If specified group is empty, no need to merge
367   if (theGroup.isEmpty())
368     return;
369
370   std::set<ObjectPtr> aConstraints;
371   ConstraintConstraintMap::const_iterator aConstrIter = theGroup.myConstraints.begin();
372   for (; aConstrIter != theGroup.myConstraints.end(); aConstrIter++)
373     aConstraints.insert(aConstrIter->first);
374
375   std::list<FeaturePtr> aSortedConstraints = selectApplicableFeatures(aConstraints);
376   std::list<FeaturePtr>::iterator aSCIter = aSortedConstraints.begin();
377   for (; aSCIter != aSortedConstraints.end(); ++aSCIter) {
378     ConstraintPtr aConstr = std::dynamic_pointer_cast<SketchPlugin_Constraint>(*aSCIter);
379     if (!aConstr)
380       continue;
381     changeConstraint(aConstr);
382   }
383 }
384
385 // ============================================================================
386 //  Function: splitGroup
387 //  Class:    SketchSolver_Group
388 //  Purpose:  divide the group into several subgroups
389 // ============================================================================
390 void SketchSolver_Group::splitGroup(std::list<SketchSolver_Group*>& theCuts)
391 {
392   // New storage will be used in trimmed way to store the list of constraint interacted together.
393   StoragePtr aNewStorage = SketchSolver_Manager::instance()->builder()->createStorage(getId());
394   std::list<ConstraintWrapperPtr> aDummyVec; // empty vector to avoid creation of solver's constraints
395
396   // Obtain constraints, which should be separated
397   std::list<ConstraintPtr> anUnusedConstraints;
398   ConstraintConstraintMap::iterator aCIter = myConstraints.begin();
399   for ( ; aCIter != myConstraints.end(); aCIter++) {
400     if (aNewStorage->isInteract(FeaturePtr(aCIter->first)))
401       aNewStorage->addConstraint(aCIter->first, aDummyVec);
402     else
403       anUnusedConstraints.push_back(aCIter->first);
404   }
405
406   // Check the unused constraints once again, because they may become interacted with new storage since adding constraints
407   std::list<ConstraintPtr>::iterator aUnuseIt = anUnusedConstraints.begin();
408   while (aUnuseIt != anUnusedConstraints.end()) {
409     if (aNewStorage->isInteract(FeaturePtr(*aUnuseIt))) {
410       aNewStorage->addConstraint(*aUnuseIt, aDummyVec);
411       anUnusedConstraints.erase(aUnuseIt);
412       aUnuseIt = anUnusedConstraints.begin();
413       continue;
414     }
415     aUnuseIt++;
416   }
417
418   std::list<SketchSolver_Group*>::iterator aCutsIter;
419   aUnuseIt = anUnusedConstraints.begin();
420   for ( ; aUnuseIt != anUnusedConstraints.end(); ++aUnuseIt) {
421     // Remove unused constraints
422     removeConstraint(*aUnuseIt);
423     // Try to append constraint to already existent group
424     for (aCutsIter = theCuts.begin(); aCutsIter != theCuts.end(); ++aCutsIter)
425       if ((*aCutsIter)->isInteract(*aUnuseIt)) {
426         (*aCutsIter)->changeConstraint(*aUnuseIt);
427         break;
428       }
429     if (aCutsIter == theCuts.end()) {
430       // Add new group
431       SketchSolver_Group* aGroup = new SketchSolver_Group(mySketch);
432       aGroup->changeConstraint(*aUnuseIt);
433       theCuts.push_back(aGroup);
434     } else {
435       // Find other groups interacting with constraint
436       std::list<SketchSolver_Group*>::iterator aBaseGroupIt = aCutsIter;
437       for (++aCutsIter; aCutsIter != theCuts.end(); ++aCutsIter)
438         if ((*aCutsIter)->isInteract(*aUnuseIt)) {
439           (*aBaseGroupIt)->mergeGroups(**aCutsIter);
440           std::list<SketchSolver_Group*>::iterator aRemoveIt = aCutsIter--;
441           theCuts.erase(aRemoveIt);
442         }
443     }
444   }
445 }
446
447 // ============================================================================
448 //  Function: isConsistent
449 //  Class:    SketchSolver_Group
450 //  Purpose:  search removed entities and constraints
451 // ============================================================================
452 bool SketchSolver_Group::isConsistent()
453 {
454   if (isEmpty()) // no one constraint is initialized yet
455     return true;
456
457   // Check the features and constraint is the storage are valid
458   bool aResult = myStorage->isConsistent();
459   if (aResult) {
460     // additional check of consistency of the Fixed constraint,
461     // because they are not added to the storage
462     ConstraintConstraintMap::iterator aCIter = myConstraints.begin();
463     for (; aCIter != myConstraints.end(); ++aCIter)
464       if (aCIter->first->getKind() == SketchPlugin_ConstraintRigid::ID() &&
465          (!aCIter->first->data() || !aCIter->first->data()->isValid())) {
466         aResult = false;
467         break;
468       }
469   }
470   if (!aResult) {
471     // remove invalid constraints
472     std::set<ConstraintPtr> anInvalidConstraints;
473     ConstraintConstraintMap::iterator aCIter = myConstraints.begin();
474     for (; aCIter != myConstraints.end(); ++aCIter) {
475       if (!aCIter->first->data() || !aCIter->first->data()->isValid())
476         anInvalidConstraints.insert(aCIter->first);
477     }
478     std::set<ConstraintPtr>::const_iterator aRemoveIt = anInvalidConstraints.begin();
479     for (; aRemoveIt != anInvalidConstraints.end(); ++aRemoveIt)
480       removeConstraint(*aRemoveIt);
481     // remove invalid features
482     myStorage->removeInvalidEntities();
483   }
484   return aResult;
485 }
486
487 // ============================================================================
488 //  Function: removeTemporaryConstraints
489 //  Class:    SketchSolver_Group
490 //  Purpose:  remove all transient SLVS_C_WHERE_DRAGGED constraints after
491 //            resolving the set of constraints
492 // ============================================================================
493 void SketchSolver_Group::removeTemporaryConstraints()
494 {
495   std::set<SolverConstraintPtr>::iterator aTmpIt = myTempConstraints.begin();
496   for (; aTmpIt != myTempConstraints.end(); ++aTmpIt)
497     (*aTmpIt)->remove();
498
499   size_t aNbTemp = myStorage->nbTemporary();
500   if (aNbTemp > 0)
501     myStorage->removeTemporary(aNbTemp);
502
503   if (!myTempConstraints.empty())
504     myStorage->verifyFixed();
505   myStorage->setNeedToResolve(false);
506   myTempConstraints.clear();
507 }
508
509 // ============================================================================
510 //  Function: removeConstraint
511 //  Class:    SketchSolver_Group
512 //  Purpose:  remove constraint and all unused entities
513 // ============================================================================
514 void SketchSolver_Group::removeConstraint(ConstraintPtr theConstraint)
515 {
516   bool isFullyRemoved = true;
517   ConstraintConstraintMap::iterator aCIter = myConstraints.begin();
518   for (; aCIter != myConstraints.end(); aCIter++)
519     if (aCIter->first == theConstraint) {
520       if (!aCIter->second->remove()) // the constraint is not fully removed
521         isFullyRemoved = false;
522       break;
523     }
524   if (aCIter == myConstraints.end())
525     return;
526
527   if (isFullyRemoved)
528     myConstraints.erase(aCIter);
529   else if (aCIter != myConstraints.end() &&
530            aCIter->first->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
531     // Update multicoincidence
532     std::list<ConstraintPtr> aMultiCoinc;
533     SolverConstraintPtr aCoincidence = aCIter->second;
534     while (aCIter != myConstraints.end()) {
535       if (aCIter->second != aCoincidence) {
536         ++aCIter;
537         continue;
538       }
539       if (aCIter->first != theConstraint)
540         aMultiCoinc.push_back(aCIter->first);
541       aCIter->second->remove();
542       ConstraintConstraintMap::iterator aRemoveIt = aCIter++;
543       myConstraints.erase(aRemoveIt);
544     }
545
546     std::list<ConstraintPtr>::iterator anIt = aMultiCoinc.begin();
547     for (; anIt != aMultiCoinc.end(); ++anIt)
548       changeConstraint(*anIt);
549   }
550 }
551
552 // ============================================================================
553 //  Function: isComplexConstraint
554 //  Class:    SketchSolver_Group
555 //  Purpose:  verifies the constraint is complex, i.e. it needs another constraints to be created before
556 // ============================================================================
557 bool SketchSolver_Group::isComplexConstraint(FeaturePtr theConstraint)
558 {
559   return theConstraint->getKind() == SketchPlugin_ConstraintFillet::ID() ||
560          theConstraint->getKind() == SketchPlugin_ConstraintMirror::ID() ||
561          theConstraint->getKind() == SketchPlugin_ConstraintTangent::ID();
562 }
563
564 // ============================================================================
565 //  Function: setTemporary
566 //  Class:    SketchSolver_Group
567 //  Purpose:  append given constraint to the group of temporary constraints
568 // ============================================================================
569 void SketchSolver_Group::setTemporary(SolverConstraintPtr theConstraint)
570 {
571   theConstraint->makeTemporary();
572   myTempConstraints.insert(theConstraint);
573 }
574
575
576 // ============================================================================
577 //  Function: checkFeatureValidity
578 //  Class:    SketchSolver_Group
579 //  Purpose:  verifies is the feature valid
580 // ============================================================================
581 bool SketchSolver_Group::checkFeatureValidity(FeaturePtr theFeature)
582 {
583   if (!theFeature || !theFeature->data()->isValid())
584     return true;
585
586   SessionPtr aMgr = ModelAPI_Session::get();
587   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
588   return aFactory->validate(theFeature);
589 }
590
591
592
593
594 // ===========   Auxiliary functions   ========================================
595 static double featureToVal(FeaturePtr theFeature)
596 {
597   if (theFeature->getKind() == SketchPlugin_Sketch::ID())
598     return 0.0; // sketch
599   ConstraintPtr aConstraint = std::dynamic_pointer_cast<SketchPlugin_Constraint>(theFeature);
600   if (!aConstraint)
601     return 1.0; // features (arc, circle, line, point)
602
603   const std::string& anID = aConstraint->getKind();
604   if (anID == SketchPlugin_ConstraintCoincidence::ID()) {
605     AttributeRefAttrPtr anAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
606         aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
607     AttributeRefAttrPtr anAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
608         aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
609     if (anAttrA && anAttrB && (anAttrA->isObject() || anAttrB->isObject()))
610       return 2.0; // point-on-line and point-on-circle should go before points coincidence constraint
611     return 2.5;
612   }
613   if (anID == SketchPlugin_ConstraintDistance::ID() ||
614       anID == SketchPlugin_ConstraintLength::ID() ||
615       anID == SketchPlugin_ConstraintRadius::ID())
616     return 3.0;
617   if (anID == SketchPlugin_ConstraintAngle::ID())
618     return 3.5;
619   if (anID == SketchPlugin_ConstraintHorizontal::ID() ||
620       anID == SketchPlugin_ConstraintVertical::ID() ||
621       anID == SketchPlugin_ConstraintParallel::ID() ||
622       anID == SketchPlugin_ConstraintPerpendicular::ID())
623     return 4.0;
624   if (anID == SketchPlugin_ConstraintEqual::ID())
625     return 5.0;
626   if (anID == SketchPlugin_ConstraintTangent::ID() ||
627       anID == SketchPlugin_ConstraintMirror::ID())
628     return 6.0;
629   if (anID == SketchPlugin_ConstraintRigid::ID())
630     return 7.0;
631   if (anID == SketchPlugin_MultiRotation::ID() ||
632       anID == SketchPlugin_MultiTranslation::ID())
633     return 8.0;
634
635   // all other constraints are placed between Equal and Tangent constraints
636   return 5.5;
637 }
638
639 static bool isLess(FeaturePtr theFeature1, FeaturePtr theFeature2)
640 {
641   return featureToVal(theFeature1) < featureToVal(theFeature2);
642 }
643
644 std::list<FeaturePtr> SketchSolver_Group::selectApplicableFeatures(const std::set<ObjectPtr>& theObjects)
645 {
646   std::list<FeaturePtr> aResult;
647   std::list<FeaturePtr>::iterator aResIt;
648
649   std::set<ObjectPtr>::const_iterator anObjIter = theObjects.begin();
650   for (; anObjIter != theObjects.end(); ++anObjIter) {
651     // Operate sketch itself and SketchPlugin features only.
652     // Also, the Fillet need to be skipped, because there are several separated constraints composing it.
653     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIter);
654     if (!aFeature)
655       continue;
656     std::shared_ptr<SketchPlugin_Feature> aSketchFeature = 
657         std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
658     if ((aFeature->getKind() != SketchPlugin_Sketch::ID() && !aSketchFeature) ||
659         aFeature->getKind() == SketchPlugin_ConstraintFillet::ID())
660       continue;
661
662     // Find the place where to insert a feature
663     for (aResIt = aResult.begin(); aResIt != aResult.end(); ++aResIt)
664       if (isLess(aFeature, *aResIt))
665         break;
666     aResult.insert(aResIt, aFeature);
667   }
668
669   return aResult;
670 }
671