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