Salome HOME
Make Boolean operation conceal the selected argument: sub-body of compsolid or the...
[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_Builder.h>
10 #include <SketchSolver_Constraint.h>
11 #include <SketchSolver_ConstraintCoincidence.h>
12 #include <SketchSolver_Error.h>
13
14 #include <Events_Error.h>
15 #include <Events_Loop.h>
16 #include <GeomAPI_XY.h>
17 #include <GeomAPI_Dir2d.h>
18 #include <GeomAPI_Pnt2d.h>
19 #include <GeomDataAPI_Dir.h>
20 #include <GeomDataAPI_Point.h>
21 #include <GeomDataAPI_Point2D.h>
22 #include <ModelAPI_AttributeDouble.h>
23 #include <ModelAPI_Document.h>
24 #include <ModelAPI_Events.h>
25 #include <ModelAPI_ResultConstruction.h>
26
27 #include <SketchPlugin_Constraint.h>
28 #include <SketchPlugin_ConstraintEqual.h>
29 #include <SketchPlugin_ConstraintFillet.h>
30 #include <SketchPlugin_ConstraintLength.h>
31 #include <SketchPlugin_ConstraintCoincidence.h>
32 #include <SketchPlugin_ConstraintMirror.h>
33 #include <SketchPlugin_ConstraintRigid.h>
34 #include <SketchPlugin_ConstraintTangent.h>
35 #include <SketchPlugin_Feature.h>
36 #include <SketchPlugin_MultiRotation.h>
37 #include <SketchPlugin_MultiTranslation.h>
38
39 #include <SketchPlugin_Arc.h>
40 #include <SketchPlugin_Circle.h>
41 #include <SketchPlugin_Line.h>
42 #include <SketchPlugin_Point.h>
43 #include <SketchPlugin_Sketch.h>
44
45 #include <math.h>
46 #include <assert.h>
47
48
49 /// \brief This class is used to give unique index to the groups
50 class GroupIndexer
51 {
52 public:
53   /// \brief Return vacant index
54   static Slvs_hGroup NEW_GROUP() { return ++myGroupIndex; }
55   /// \brief Removes the index
56   static void REMOVE_GROUP(const Slvs_hGroup& theIndex) {
57     if (myGroupIndex == theIndex)
58       myGroupIndex--;
59   }
60
61 private:
62   GroupIndexer() {};
63
64   static Slvs_hGroup myGroupIndex; ///< index of the group
65 };
66
67 Slvs_hGroup GroupIndexer::myGroupIndex = 0;
68
69
70
71 // ========================================================
72 // =========  SketchSolver_Group  ===============
73 // ========================================================
74
75 SketchSolver_Group::SketchSolver_Group(
76     std::shared_ptr<ModelAPI_CompositeFeature> theWorkplane)
77     : myID(GroupIndexer::NEW_GROUP())
78 {
79   // Initialize workplane
80   myWorkplaneID = SLVS_E_UNKNOWN;
81 #ifndef NDEBUG
82   assert(addWorkplane(theWorkplane));
83 #else
84   addWorkplane(theWorkplane);
85 #endif
86 }
87
88 SketchSolver_Group::~SketchSolver_Group()
89 {
90   myConstraints.clear();
91   GroupIndexer::REMOVE_GROUP(myID);
92 }
93
94 // ============================================================================
95 //  Function: isBaseWorkplane
96 //  Class:    SketchSolver_Group
97 //  Purpose:  verify the group is based on the given workplane
98 // ============================================================================
99 bool SketchSolver_Group::isBaseWorkplane(CompositeFeaturePtr theWorkplane) const
100 {
101   return theWorkplane == mySketch;
102 }
103
104 // ============================================================================
105 //  Function: isInteract
106 //  Class:    SketchSolver_Group
107 //  Purpose:  verify are there any entities in the group used by given constraint
108 // ============================================================================
109 bool SketchSolver_Group::isInteract(
110     std::shared_ptr<SketchPlugin_Feature> theFeature) const
111 {
112   // Empty group interacts with everything
113   if (isEmpty()) return true;
114   ConstraintPtr aConstraint = std::dynamic_pointer_cast<SketchPlugin_Constraint>(theFeature);
115   if (aConstraint)
116     return myFeatureStorage->isInteract(aConstraint);
117   return myFeatureStorage->isInteract(std::dynamic_pointer_cast<ModelAPI_Feature>(theFeature));
118 }
119
120 // ============================================================================
121 //  Function: getFeatureId
122 //  Class:    SketchSolver_Group
123 //  Purpose:  Find the identifier of the feature, if it already exists in the group
124 // ============================================================================
125 Slvs_hEntity SketchSolver_Group::getFeatureId(FeaturePtr theFeature) const
126 {
127   Slvs_hEntity aResult = SLVS_E_UNKNOWN;
128   if (!myFeatureStorage)
129     return aResult;
130   std::set<ConstraintPtr> aConstraints = myFeatureStorage->getConstraints(theFeature);
131   if (aConstraints.empty())
132     return aResult;
133   std::set<ConstraintPtr>::iterator aConstrIter = aConstraints.begin();
134   for (; aConstrIter != aConstraints.end(); aConstrIter++) {
135     ConstraintConstraintMap::const_iterator aCIter = myConstraints.find(*aConstrIter);
136     if (aCIter == myConstraints.end())
137       continue;
138     aResult = aCIter->second->getId(theFeature);
139     if (aResult != SLVS_E_UNKNOWN)
140       return aResult;
141   }
142   return SLVS_E_UNKNOWN;
143 }
144
145 // ============================================================================
146 //  Function: getAttributeId
147 //  Class:    SketchSolver_Group
148 //  Purpose:  Find the identifier of the attribute, if it already exists in the group
149 // ============================================================================
150 Slvs_hEntity SketchSolver_Group::getAttributeId(AttributePtr theAttribute) const
151 {
152   Slvs_hEntity aResult = SLVS_E_UNKNOWN;
153   if (!myFeatureStorage)
154     return aResult;
155   std::set<ConstraintPtr> aConstraints = myFeatureStorage->getConstraints(theAttribute);
156   if (aConstraints.empty())
157     return aResult;
158   std::set<ConstraintPtr>::iterator aConstrIter = aConstraints.begin();
159   for (; aConstrIter != aConstraints.end(); aConstrIter++) {
160     ConstraintConstraintMap::const_iterator aCIter = myConstraints.find(*aConstrIter);
161     if (aCIter == myConstraints.end())
162       continue;
163     aResult = aCIter->second->getId(theAttribute);
164     if (aResult != SLVS_E_UNKNOWN)
165       return aResult;
166   }
167   return SLVS_E_UNKNOWN;
168 }
169
170 // ============================================================================
171 //  Function: changeConstraint
172 //  Class:    SketchSolver_Group
173 //  Purpose:  create/update the constraint in the group
174 // ============================================================================
175 bool SketchSolver_Group::changeConstraint(
176     std::shared_ptr<SketchPlugin_Constraint> theConstraint)
177 {
178   // There is no workplane yet, something wrong
179   if (myWorkplaneID == SLVS_E_UNKNOWN)
180     return false;
181
182   if (!theConstraint)
183     return false;
184
185   bool isNewConstraint = myConstraints.find(theConstraint) == myConstraints.end();
186   if (isNewConstraint) {
187     // Add constraint to the current group
188     SolverConstraintPtr aConstraint =
189         SketchSolver_Builder::getInstance()->createConstraint(theConstraint);
190     if (!aConstraint)
191       return false;
192     aConstraint->setGroup(this);
193     aConstraint->setStorage(myStorage);
194     if (!aConstraint->error().empty()) {
195       if (aConstraint->error() == SketchSolver_Error::NOT_INITIALIZED())
196         return false; // some attribute are not initialized yet, don't show message
197       Events_Error::send(aConstraint->error(), this);
198     }
199
200     // Additional verification of coincidence of several points
201     if (theConstraint->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
202       ConstraintConstraintMap::iterator aCIter = myConstraints.begin();
203       for (; aCIter != myConstraints.end(); aCIter++) {
204         std::shared_ptr<SketchSolver_ConstraintCoincidence> aCoincidence =
205           std::dynamic_pointer_cast<SketchSolver_ConstraintCoincidence>(aCIter->second);
206         if (!aCoincidence)
207           continue;
208         std::shared_ptr<SketchSolver_ConstraintCoincidence> aCoinc2 =
209           std::dynamic_pointer_cast<SketchSolver_ConstraintCoincidence>(aConstraint);
210         if (aCoincidence != aCoinc2 && aCoincidence->isCoincide(aCoinc2)) {
211           aCoinc2->attach(aCoincidence);
212           // update other coincidences
213           ConstraintConstraintMap::iterator anIt = aCIter;
214           for (++anIt; anIt != myConstraints.end(); ++anIt)
215             if (anIt->second == aCIter->second)
216               anIt->second = aCoinc2;
217           aCIter->second = aCoinc2;
218         }
219       }
220     }
221     myConstraints[theConstraint] = aConstraint;
222   }
223   else
224     myConstraints[theConstraint]->update();
225
226   // Fix base features for fillet
227   if (isNewConstraint && theConstraint->getKind() == SketchPlugin_ConstraintFillet::ID()) {
228     std::list<AttributePtr> anAttrList =
229         theConstraint->data()->attributes(ModelAPI_AttributeRefAttr::typeId());
230     std::list<AttributePtr>::iterator anAttrIter = anAttrList.begin();
231     for (; anAttrIter != anAttrList.end(); anAttrIter++) {
232       AttributeRefAttrPtr aRefAttr =
233           std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttrIter);
234       if (!aRefAttr || !aRefAttr->isObject())
235         continue;
236       FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
237       SolverConstraintPtr aConstraint =
238           SketchSolver_Builder::getInstance()->createRigidConstraint(aFeature);
239       if (!aConstraint)
240         continue;
241       aConstraint->setGroup(this);
242       aConstraint->setStorage(myStorage);
243       setTemporary(aConstraint);
244     }
245   }
246   // Fix mirror line
247   if (theConstraint->getKind() == SketchPlugin_ConstraintMirror::ID()) {
248     AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
249         theConstraint->attribute(SketchPlugin_ConstraintMirror::ENTITY_A()));
250     if (aRefAttr && aRefAttr->isObject()) {
251       FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
252       if (aFeature) {
253         SolverConstraintPtr aConstraint =
254             SketchSolver_Builder::getInstance()->createRigidConstraint(aFeature);
255         if (aConstraint) {
256           aConstraint->setGroup(this);
257           aConstraint->setStorage(myStorage);
258           setTemporary(aConstraint);
259         }
260       }
261     }
262   }
263
264   if (!myFeatureStorage)
265     myFeatureStorage = FeatureStoragePtr(new SketchSolver_FeatureStorage);
266   myFeatureStorage->changeConstraint(theConstraint);
267
268   return true;
269 }
270
271
272 bool SketchSolver_Group::updateFeature(std::shared_ptr<SketchPlugin_Feature> theFeature)
273 {
274   std::set<ConstraintPtr> aConstraints =
275       myFeatureStorage->getConstraints(std::dynamic_pointer_cast<ModelAPI_Feature>(theFeature));
276   if (aConstraints.empty())
277     return false;
278   std::set<ConstraintPtr>::iterator aCIter = aConstraints.begin();
279   std::set<SolverConstraintPtr> aPostponed; // postponed constraints Multi-Rotation and Multi-Translation
280   for (; aCIter != aConstraints.end(); aCIter++) {
281     ConstraintConstraintMap::iterator aSolConIter = myConstraints.find(*aCIter);
282     if (aSolConIter == myConstraints.end() || !aSolConIter->first->data() ||
283         !aSolConIter->first->data()->isValid())
284       continue;
285     myFeatureStorage->changeFeature(theFeature, aSolConIter->first);
286
287     if (aSolConIter->first->getKind() == SketchPlugin_MultiRotation::ID() ||
288         aSolConIter->first->getKind() == SketchPlugin_MultiTranslation::ID()) {
289       aPostponed.insert(aSolConIter->second);
290       continue;
291     }
292     aSolConIter->second->addFeature(theFeature);
293     aSolConIter->second->update();
294   }
295
296   // Update postponed constraints
297   std::set<SolverConstraintPtr>::iterator aSCIter = aPostponed.begin();
298   for (; aSCIter != aPostponed.end(); ++aSCIter) {
299     (*aSCIter)->addFeature(theFeature);
300     (*aSCIter)->update();
301   }
302   return true;
303 }
304
305 void SketchSolver_Group::moveFeature(std::shared_ptr<SketchPlugin_Feature> theFeature)
306 {
307   // Firstly, create temporary rigid constraint
308   SolverConstraintPtr aConstraint =
309       SketchSolver_Builder::getInstance()->createMovementConstraint(theFeature);
310   if (!aConstraint)
311     return;
312   aConstraint->setGroup(this);
313   aConstraint->setStorage(myStorage);
314   if (aConstraint->error().empty())
315     setTemporary(aConstraint);
316   // Secondly, update the feature
317   updateFeature(theFeature);
318 }
319
320 // ============================================================================
321 //  Function: fixFeaturesList
322 //  Class:    SketchSolver_Group
323 //  Purpose:  Apply temporary rigid constraints for the list of features
324 // ============================================================================
325 void SketchSolver_Group::fixFeaturesList(AttributeRefListPtr theList)
326 {
327   std::list<ObjectPtr> aList = theList->list();
328   std::list<ObjectPtr>::iterator anIt = aList.begin();
329   std::list<FeaturePtr> aFeatures;
330   // Sort features, at begining there are features used by Equal constraint
331   for (; anIt != aList.end(); anIt++) {
332     if (!(*anIt))
333       continue;
334     FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
335     std::set<ConstraintPtr> aConstraints = myFeatureStorage->getConstraints(aFeature);
336     std::set<ConstraintPtr>::iterator aCIter = aConstraints.begin();
337     for (; aCIter != aConstraints.end(); aCIter++)
338       if ((*aCIter)->getKind() == SketchPlugin_ConstraintEqual::ID())
339         break;
340     if (aCIter != aConstraints.end())
341       aFeatures.push_front(aFeature);
342     else
343       aFeatures.push_back(aFeature);
344   }
345
346   std::list<FeaturePtr>::iterator aFeatIter = aFeatures.begin();
347   for (; aFeatIter != aFeatures.end(); aFeatIter++) {
348     SolverConstraintPtr aConstraint =
349         SketchSolver_Builder::getInstance()->createRigidConstraint(*aFeatIter);
350     if (!aConstraint)
351       continue;
352     aConstraint->setGroup(this);
353     aConstraint->setStorage(myStorage);
354     setTemporary(aConstraint);
355   }
356 }
357
358 // ============================================================================
359 //  Function: addWorkplane
360 //  Class:    SketchSolver_Group
361 //  Purpose:  create workplane for the group
362 // ============================================================================
363 bool SketchSolver_Group::addWorkplane(CompositeFeaturePtr theSketch)
364 {
365   if (myWorkplaneID != SLVS_E_UNKNOWN || theSketch->getKind() != SketchPlugin_Sketch::ID())
366     return false;  // the workplane already exists or the function parameter is not Sketch
367
368   mySketch = theSketch;
369   updateWorkplane();
370   return true;
371 }
372
373 // ============================================================================
374 //  Function: updateWorkplane
375 //  Class:    SketchSolver_Group
376 //  Purpose:  update parameters of workplane
377 // ============================================================================
378 bool SketchSolver_Group::updateWorkplane()
379 {
380   if (!myStorage) // Create storage if not exists
381     myStorage = StoragePtr(new SketchSolver_Storage);
382   SketchSolver_Builder* aBuilder = SketchSolver_Builder::getInstance();
383
384   std::vector<Slvs_Entity> anEntities;
385   std::vector<Slvs_Param> aParams;
386   if (!aBuilder->createWorkplane(mySketch, anEntities, aParams))
387     return false;
388
389   if (myWorkplaneID == SLVS_E_UNKNOWN) {
390     myWorkplaneID = anEntities.back().h;
391     // Add new workplane elements
392     std::vector<Slvs_Param>::iterator aParIter = aParams.begin();
393     for (; aParIter != aParams.end(); aParIter++) {
394       aParIter->h = SLVS_E_UNKNOWN; // the ID should be generated by storage
395       aParIter->group = myID;
396       aParIter->h = myStorage->addParameter(*aParIter);
397     }
398     std::vector<Slvs_Entity>::iterator anEntIter = anEntities.begin();
399     for (; anEntIter != anEntities.end(); anEntIter++) {
400       anEntIter->h = SLVS_E_UNKNOWN; // the ID should be generated by storage
401       anEntIter->group = myID;
402       anEntIter->wrkpl = myWorkplaneID;
403       for (int i = 0; i < 4; i++)
404         if (anEntIter->param[i] != SLVS_E_UNKNOWN)
405           anEntIter->param[i] = aParams[anEntIter->param[i]-1].h;
406       for (int i = 0; i < 4; i++)
407         if (anEntIter->point[i] != SLVS_E_UNKNOWN)
408           anEntIter->point[i] = anEntities[anEntIter->point[i]-1].h;
409       anEntIter->h = myStorage->addEntity(*anEntIter);
410     }
411   } else {
412     // Update existent workplane
413     const Slvs_Entity& aWP = myStorage->getEntity(myWorkplaneID);
414     const Slvs_Entity& anOrigin = myStorage->getEntity(aWP.point[0]);
415     const Slvs_Entity& aNormal = myStorage->getEntity(aWP.normal);
416     // Get parameters and update them
417     Slvs_hParam aWPParams[7] = {
418         anOrigin.param[0], anOrigin.param[1], anOrigin.param[2],
419         aNormal.param[0], aNormal.param[1], aNormal.param[2], aNormal.param[3]
420       };
421     std::vector<Slvs_Param>::iterator aParIter = aParams.begin();
422     for (int i = 0; aParIter != aParams.end(); aParIter++, i++) {
423       Slvs_Param aParam = myStorage->getParameter(aWPParams[i]);
424       aParam.val = aParIter->val;
425       myStorage->updateParameter(aParam);
426     }
427   }
428   return myWorkplaneID > 0;
429 }
430
431 // ============================================================================
432 //  Function: resolveConstraints
433 //  Class:    SketchSolver_Group
434 //  Purpose:  solve the set of constraints for the current group
435 // ============================================================================
436 bool SketchSolver_Group::resolveConstraints()
437 {
438   bool aResolved = false;
439   if (myStorage->isNeedToResolve() && !isEmpty()) {
440     myConstrSolver.setGroupID(myID);
441     myStorage->initializeSolver(myConstrSolver);
442
443     int aResult = SLVS_RESULT_OKAY;
444     try {
445       if (myStorage->hasDuplicatedConstraint())
446         aResult = SLVS_RESULT_INCONSISTENT;
447       else {
448         // To avoid overconstraint situation, we will remove temporary constraints one-by-one
449         // and try to find the case without overconstraint
450         bool isLastChance = false;
451         int aNbTemp = myStorage->numberTemporary();
452         while (true) {
453           aResult = myConstrSolver.solve();
454           if (aResult == SLVS_RESULT_OKAY || isLastChance)
455             break;
456           if (aNbTemp <= 0) {
457             // try to update parameters and resolve once again
458             ConstraintConstraintMap::iterator aConstrIt = myConstraints.begin();
459             for (; aConstrIt != myConstraints.end(); ++aConstrIt)
460               aConstrIt->second->update();
461             isLastChance = true;
462           } else
463             aNbTemp = myStorage->deleteTemporaryConstraint();
464           myStorage->initializeSolver(myConstrSolver);
465         }
466       }
467     } catch (...) {
468       Events_Error::send(SketchSolver_Error::SOLVESPACE_CRASH(), this);
469       return false;
470     }
471     if (aResult == SLVS_RESULT_OKAY) {  // solution succeeded, store results into correspondent attributes
472       myFeatureStorage->blockEvents(true);
473       ConstraintConstraintMap::iterator aConstrIter = myConstraints.begin();
474       for (; aConstrIter != myConstraints.end(); aConstrIter++)
475         aConstrIter->second->refresh();
476       myFeatureStorage->blockEvents(false);
477     } else if (!myConstraints.empty())
478       Events_Error::send(SketchSolver_Error::CONSTRAINTS(), this);
479
480     aResolved = true;
481   }
482   removeTemporaryConstraints();
483   myStorage->setNeedToResolve(false);
484   return aResolved;
485 }
486
487 // ============================================================================
488 //  Function: mergeGroups
489 //  Class:    SketchSolver_Group
490 //  Purpose:  append specified group to the current group
491 // ============================================================================
492 void SketchSolver_Group::mergeGroups(const SketchSolver_Group& theGroup)
493 {
494   // If specified group is empty, no need to merge
495   if (theGroup.isEmpty())
496     return;
497   if (!myFeatureStorage)
498     myFeatureStorage = FeatureStoragePtr(new SketchSolver_FeatureStorage);
499
500   std::vector<ConstraintPtr> aComplexConstraints;
501   ConstraintConstraintMap::const_iterator aConstrIter = theGroup.myConstraints.begin();
502   // append simple constraints
503   for (; aConstrIter != theGroup.myConstraints.end(); aConstrIter++)
504     if (isComplexConstraint(aConstrIter->first))
505       aComplexConstraints.push_back(aConstrIter->first);
506     else
507       changeConstraint(aConstrIter->first);
508   // append complex constraints
509   std::vector<ConstraintPtr>::iterator aComplexIter = aComplexConstraints.begin();
510   for (; aComplexIter != aComplexConstraints.end(); aComplexIter++)
511       changeConstraint(*aComplexIter);
512 }
513
514 // ============================================================================
515 //  Function: splitGroup
516 //  Class:    SketchSolver_Group
517 //  Purpose:  divide the group into several subgroups
518 // ============================================================================
519 void SketchSolver_Group::splitGroup(std::vector<SketchSolver_Group*>& theCuts)
520 {
521   // Obtain constraints, which should be separated
522   FeatureStoragePtr aNewFeatStorage(new SketchSolver_FeatureStorage);
523   std::vector<ConstraintPtr> anUnusedConstraints;
524   ConstraintConstraintMap::iterator aCIter = myConstraints.begin();
525   for ( ; aCIter != myConstraints.end(); aCIter++) {
526     std::list<ConstraintPtr> aBaseConstraints = aCIter->second->constraints();
527     std::list<ConstraintPtr>::iterator anIter = aBaseConstraints.begin();
528     for (; anIter != aBaseConstraints.end(); anIter++)
529       if (aNewFeatStorage->isInteract(*anIter)) {
530         aNewFeatStorage->changeConstraint(*anIter);
531       } else
532         anUnusedConstraints.push_back(*anIter);
533   }
534
535   // Check the unused constraints once again, because they may become interacted with new storage since adding constraints
536   std::vector<ConstraintPtr>::iterator aUnuseIt = anUnusedConstraints.begin();
537   while (aUnuseIt != anUnusedConstraints.end()) {
538     if (aNewFeatStorage->isInteract(*aUnuseIt)) {
539       size_t aShift = aUnuseIt - anUnusedConstraints.begin();
540       anUnusedConstraints.erase(aUnuseIt);
541       aUnuseIt = anUnusedConstraints.begin() + aShift;
542       continue;
543     }
544     aUnuseIt++;
545   }
546
547   std::vector<SketchSolver_Group*>::iterator aCutsIter;
548   aUnuseIt = anUnusedConstraints.begin();
549   for ( ; aUnuseIt != anUnusedConstraints.end(); aUnuseIt++) {
550     // Remove unused constraints
551     removeConstraint(*aUnuseIt);
552     // Try to append constraint to already existent group
553     for (aCutsIter = theCuts.begin(); aCutsIter != theCuts.end(); aCutsIter++)
554       if ((*aCutsIter)->isInteract(*aUnuseIt)) {
555         (*aCutsIter)->changeConstraint(*aUnuseIt);
556         break;
557       }
558     if (aCutsIter == theCuts.end()) {
559       // Add new group
560       SketchSolver_Group* aGroup = new SketchSolver_Group(mySketch);
561       aGroup->changeConstraint(*aUnuseIt);
562       theCuts.push_back(aGroup);
563     }
564   }
565 }
566
567 // ============================================================================
568 //  Function: isConsistent
569 //  Class:    SketchSolver_Group
570 //  Purpose:  search removed entities and constraints
571 // ============================================================================
572 bool SketchSolver_Group::isConsistent()
573 {
574   if (!myFeatureStorage) // no one constraint is initialized yet
575     return true;
576
577   bool aResult = myFeatureStorage->isConsistent();
578   if (!aResult) {
579     // remove invalid entities
580     std::set<ConstraintPtr> anInvalidConstraints;
581     ConstraintConstraintMap::iterator aCIter = myConstraints.begin();
582     for (; aCIter != myConstraints.end(); ++aCIter) {
583       if (!aCIter->first->data() || !aCIter->first->data()->isValid())
584         anInvalidConstraints.insert(aCIter->first);
585     }
586     std::set<ConstraintPtr>::const_iterator aRemoveIt = anInvalidConstraints.begin();
587     for (; aRemoveIt != anInvalidConstraints.end(); ++aRemoveIt)
588       removeConstraint(*aRemoveIt);
589   }
590   return aResult;
591 }
592
593 // ============================================================================
594 //  Function: removeTemporaryConstraints
595 //  Class:    SketchSolver_Group
596 //  Purpose:  remove all transient SLVS_C_WHERE_DRAGGED constraints after
597 //            resolving the set of constraints
598 // ============================================================================
599 void SketchSolver_Group::removeTemporaryConstraints()
600 {
601   myTempConstraints.clear();
602   while (myStorage->numberTemporary())
603     myStorage->deleteTemporaryConstraint();
604   // Clean lists of removed entities in the storage
605   std::set<Slvs_hParam> aRemPar;
606   std::set<Slvs_hEntity> aRemEnt;
607   std::set<Slvs_hConstraint> aRemCon;
608   myStorage->getRemoved(aRemPar, aRemEnt, aRemCon);
609   myStorage->setNeedToResolve(false);
610 }
611
612 // ============================================================================
613 //  Function: removeConstraint
614 //  Class:    SketchSolver_Group
615 //  Purpose:  remove constraint and all unused entities
616 // ============================================================================
617 void SketchSolver_Group::removeConstraint(ConstraintPtr theConstraint)
618 {
619   bool isFullyRemoved = true;
620   myFeatureStorage->removeConstraint(theConstraint);
621   ConstraintConstraintMap::iterator aCIter = myConstraints.begin();
622   for (; aCIter != myConstraints.end(); aCIter++)
623     if (aCIter->second->hasConstraint(theConstraint)) {
624       if (!aCIter->second->remove(theConstraint)) // the constraint is not fully removed
625         isFullyRemoved = false;
626       break;
627     }
628   if (aCIter == myConstraints.end())
629     return;
630
631   if (isFullyRemoved)
632     myConstraints.erase(aCIter);
633   else if (aCIter != myConstraints.end() &&
634            aCIter->first->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
635     // Update multicoincidence
636     std::list<ConstraintPtr> aMultiCoinc;
637     SolverConstraintPtr aCoincidence = aCIter->second;
638     while (aCIter != myConstraints.end()) {
639       if (aCIter->second != aCoincidence) {
640         ++aCIter;
641         continue;
642       }
643       if (aCIter->first != theConstraint)
644         aMultiCoinc.push_back(aCIter->first);
645       aCIter->second->remove(aCIter->first);
646       ConstraintConstraintMap::iterator aRemoveIt = aCIter++;
647       myConstraints.erase(aRemoveIt);
648     }
649
650     std::list<ConstraintPtr>::iterator anIt = aMultiCoinc.begin();
651     for (; anIt != aMultiCoinc.end(); ++anIt)
652       changeConstraint(*anIt);
653   }
654 }
655
656 // ============================================================================
657 //  Function: isComplexConstraint
658 //  Class:    SketchSolver_Group
659 //  Purpose:  verifies the constraint is complex, i.e. it needs another constraints to be created before
660 // ============================================================================
661 bool SketchSolver_Group::isComplexConstraint(FeaturePtr theConstraint)
662 {
663   return theConstraint->getKind() == SketchPlugin_ConstraintFillet::ID() ||
664          theConstraint->getKind() == SketchPlugin_ConstraintMirror::ID() ||
665          theConstraint->getKind() == SketchPlugin_ConstraintTangent::ID();
666 }
667
668 // ============================================================================
669 //  Function: setTemporary
670 //  Class:    SketchSolver_Group
671 //  Purpose:  append given constraint to th group of temporary constraints
672 // ============================================================================
673 void SketchSolver_Group::setTemporary(SolverConstraintPtr theConstraint)
674 {
675   theConstraint->makeTemporary();
676   myTempConstraints.insert(theConstraint);
677 }
678