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