Salome HOME
742ef54bdda3aff1ce1650389a118d8a13b23ab4
[modules/shaper.git] / src / SketchSolver / SketchSolver_Storage.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:    SketchSolver_Storage.cpp
4 // Created: 30 Nov 2015
5 // Author:  Artem ZHIDKOV
6
7 #include <SketchSolver_Storage.h>
8 #include <SketchSolver_Manager.h>
9
10 #include <GeomDataAPI_Point2D.h>
11 #include <ModelAPI_AttributeRefAttr.h>
12 #include <ModelAPI_AttributeRefList.h>
13 #include <SketchPlugin_Arc.h>
14 #include <SketchPlugin_Circle.h>
15
16
17 /// \brief Verify two vectors of constraints are equal.
18 ///        Vectors differ by the order of elements are equal.
19 static bool isEqual(const std::list<ConstraintWrapperPtr>& theCVec1,
20                     const std::list<ConstraintWrapperPtr>& theCVec2);
21
22
23 void SketchSolver_Storage::addConstraint(ConstraintPtr        theConstraint,
24                                          ConstraintWrapperPtr theSolverConstraint)
25 {
26   if (theSolverConstraint) {
27     std::list<ConstraintWrapperPtr> aConstrList(1, theSolverConstraint);
28     addConstraint(theConstraint, aConstrList);
29   } else
30     addConstraint(theConstraint, std::list<ConstraintWrapperPtr>());
31 }
32
33 void SketchSolver_Storage::addConstraint(
34     ConstraintPtr                   theConstraint,
35     std::list<ConstraintWrapperPtr> theSolverConstraints)
36 {
37   std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
38       aFound = myConstraintMap.find(theConstraint);
39   if (aFound == myConstraintMap.end() || !isEqual(aFound->second, theSolverConstraints))
40     setNeedToResolve(true);
41
42   if (theSolverConstraints.empty()) {
43     // constraint links to the empty list, add its attributes linked to the empty entities
44     std::list<AttributePtr> aRefAttrs =
45         theConstraint->data()->attributes(ModelAPI_AttributeRefAttr::typeId());
46     std::list<AttributePtr>::const_iterator anAttrIt = aRefAttrs.begin();
47     for (; anAttrIt != aRefAttrs.end(); ++anAttrIt) {
48       AttributeRefAttrPtr aRef = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttrIt);
49       if (aRef->isObject()) {
50         FeaturePtr aFeature = ModelAPI_Feature::feature(aRef->object());
51         if (aFeature) addEntity(aFeature, EntityWrapperPtr());
52       } else
53         addEntity(aRef->attr(), EntityWrapperPtr());
54     }
55     std::list<AttributePtr> aRefLists =
56         theConstraint->data()->attributes(ModelAPI_AttributeRefList::typeId());
57     for (anAttrIt = aRefLists.begin(); anAttrIt != aRefLists.end(); ++anAttrIt) {
58       AttributeRefListPtr aRef = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(*anAttrIt);
59       std::list<ObjectPtr> anObj = aRef->list();
60       std::list<ObjectPtr>::iterator anIt = anObj.begin();
61       for (; anIt != anObj.end(); ++anIt) {
62         FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
63         if (aFeature) addEntity(aFeature, EntityWrapperPtr());
64       }
65     }
66   }
67   else if (theSolverConstraints.front()->type() != CONSTRAINT_PT_PT_COINCIDENT) {
68     // Do not add point-point coincidence, because it is already made by setting
69     // the same parameters for both points
70     std::list<ConstraintWrapperPtr>::iterator aCIt = theSolverConstraints.begin();
71     for (; aCIt != theSolverConstraints.end(); ++aCIt)
72       update(*aCIt);
73   }
74   myConstraintMap[theConstraint] = theSolverConstraints;
75   // block events if necessary
76   if (myEventsBlocked && theConstraint->data() && theConstraint->data()->isValid())
77     theConstraint->data()->blockSendAttributeUpdated(myEventsBlocked);
78 }
79
80 void SketchSolver_Storage::addEntity(FeaturePtr       theFeature,
81                                      EntityWrapperPtr theSolverEntity)
82 {
83   std::map<FeaturePtr, EntityWrapperPtr>::const_iterator aFound = myFeatureMap.find(theFeature);
84   if (aFound == myFeatureMap.end() || !aFound->second ||
85      (theSolverEntity && !aFound->second->isEqual(theSolverEntity)))
86     setNeedToResolve(true); // the entity is new or modified
87
88   myFeatureMap[theFeature] = theSolverEntity;
89   // block events if necessary
90   if (myEventsBlocked && theFeature->data() && theFeature->data()->isValid())
91     theFeature->data()->blockSendAttributeUpdated(myEventsBlocked);
92 }
93
94 void SketchSolver_Storage::addEntity(AttributePtr     theAttribute,
95                                      EntityWrapperPtr theSolverEntity)
96 {
97   std::map<AttributePtr, EntityWrapperPtr>::const_iterator aFound = myAttributeMap.find(theAttribute);
98   if (aFound == myAttributeMap.end() || !aFound->second ||
99      (theSolverEntity && !aFound->second->isEqual(theSolverEntity)))
100     setNeedToResolve(true); // the entity is new or modified
101
102   myAttributeMap[theAttribute] = theSolverEntity;
103   // block events if necessary
104   if (myEventsBlocked && theAttribute->owner() &&
105       theAttribute->owner()->data() && theAttribute->owner()->data()->isValid())
106     theAttribute->owner()->data()->blockSendAttributeUpdated(myEventsBlocked);
107 }
108
109
110 bool SketchSolver_Storage::update(FeaturePtr theFeature, const GroupID& theGroup)
111 {
112   bool isUpdated = false;
113   EntityWrapperPtr aRelated = entity(theFeature);
114   if (!aRelated) { // Feature is not exist, create it
115     std::list<EntityWrapperPtr> aSubs;
116     // Reserve the feature in the map of features (do not want to add several copies of it)
117     myFeatureMap[theFeature] = aRelated;
118     // Firstly, create/update its attributes
119     std::list<AttributePtr> anAttrs =
120         theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
121     std::list<AttributePtr>::const_iterator anIt = anAttrs.begin();
122     for (; anIt != anAttrs.end(); ++anIt) {
123       isUpdated = update(*anIt, theGroup) || isUpdated;
124       aSubs.push_back(entity(*anIt));
125     }
126     // If the feature is a circle, add its radius as a sub
127     if (theFeature->getKind() == SketchPlugin_Circle::ID()) {
128       AttributePtr aRadius = theFeature->attribute(SketchPlugin_Circle::RADIUS_ID());
129       isUpdated = update(aRadius, theGroup) || isUpdated;
130       aSubs.push_back(entity(aRadius));
131     }
132     // If the feature if circle or arc, we need to add normal of the sketch to the list of subs
133     if (theFeature->getKind() == SketchPlugin_Arc::ID() ||
134         theFeature->getKind() == SketchPlugin_Circle::ID()) {
135       EntityWrapperPtr aNormal = getNormal();
136       if (aNormal) aSubs.push_back(aNormal);
137     }
138     // Secondly, convert feature
139     BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
140     GroupID aGroup = theGroup != GID_UNKNOWN ? theGroup : myGroupID;
141     aRelated = aBuilder->createFeature(theFeature, aSubs, aGroup);
142     if (!aRelated)
143       return false;
144     addEntity(theFeature, aRelated);
145   } else if (theGroup != GID_UNKNOWN)
146     changeGroup(aRelated, theGroup);
147   return update(aRelated) || isUpdated;
148 }
149
150 bool SketchSolver_Storage::update(AttributePtr theAttribute, const GroupID& theGroup)
151 {
152   AttributePtr anAttribute = theAttribute;
153   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
154   if (aRefAttr) {
155     if (aRefAttr->isObject()) {
156       FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
157       return update(aFeature, theGroup);
158     } else
159       anAttribute = aRefAttr->attr();
160   }
161
162   EntityWrapperPtr aRelated = entity(anAttribute);
163   if (!aRelated) { // Attribute is not exist, create it
164     // verify the attribute is a point of arc and add whole arc
165     if (anAttribute->owner()) {
166       FeaturePtr aFeature = ModelAPI_Feature::feature(anAttribute->owner());
167       if (aFeature->getKind() == SketchPlugin_Arc::ID() &&
168           myFeatureMap.find(aFeature) == myFeatureMap.end()) {
169         // Additional checking that all attributes are initialized
170         if (aFeature->attribute(SketchPlugin_Arc::CENTER_ID())->isInitialized() && 
171             aFeature->attribute(SketchPlugin_Arc::START_ID())->isInitialized() && 
172             aFeature->attribute(SketchPlugin_Arc::END_ID())->isInitialized()) {
173 ////          myFeatureMap[aFeature] = EntityWrapperPtr();
174           return SketchSolver_Storage::update(aFeature);
175         }
176       }
177     }
178     BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
179     GroupID aGroup = theGroup != GID_UNKNOWN ? theGroup : myGroupID;
180     aRelated = aBuilder->createAttribute(anAttribute, aGroup);
181     if (!aRelated)
182       return false;
183     addEntity(anAttribute, aRelated);
184   } else if (theGroup != GID_UNKNOWN)
185     changeGroup(aRelated, theGroup);
186   return update(aRelated);
187 }
188
189
190
191 const std::list<ConstraintWrapperPtr>& SketchSolver_Storage::constraint(
192     const ConstraintPtr& theConstraint) const
193 {
194   static std::list<ConstraintWrapperPtr> aDummy;
195
196   std::map<ConstraintPtr, std::list<ConstraintWrapperPtr>>::const_iterator
197       aFound = myConstraintMap.find(theConstraint);
198   if (aFound != myConstraintMap.end())
199     return aFound->second;
200   return aDummy;
201 }
202
203 const EntityWrapperPtr& SketchSolver_Storage::entity(const FeaturePtr& theFeature) const
204 {
205   static EntityWrapperPtr aDummy;
206
207   std::map<FeaturePtr, EntityWrapperPtr>::const_iterator aFound = myFeatureMap.find(theFeature);
208   if (aFound != myFeatureMap.end())
209     return aFound->second;
210   return aDummy;
211 }
212
213 const EntityWrapperPtr& SketchSolver_Storage::entity(const AttributePtr& theAttribute) const
214 {
215   static EntityWrapperPtr aDummy;
216
217   std::map<AttributePtr, EntityWrapperPtr>::const_iterator
218       aFound = myAttributeMap.find(theAttribute);
219   if (aFound != myAttributeMap.end())
220     return aFound->second;
221
222   AttributeRefAttrPtr aRefAttr =
223       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
224   if (aRefAttr) {
225     if (aRefAttr->isObject()) {
226       FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
227       return entity(aFeature);
228     } else
229       return entity(aRefAttr->attr());
230   }
231   return aDummy;
232 }
233
234 bool SketchSolver_Storage::removeConstraint(ConstraintPtr theConstraint)
235 {
236   std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::iterator
237       aFound = myConstraintMap.find(theConstraint);
238   if (aFound == myConstraintMap.end())
239     return true; // no constraint, already deleted
240
241   // Remove constraint
242   std::list<ConstraintWrapperPtr> aConstrList = aFound->second;
243   myConstraintMap.erase(aFound);
244   // Remove SolveSpace constraints
245   bool isFullyRemoved = true;
246   std::list<ConstraintWrapperPtr>::iterator anIt = aConstrList.begin();
247   while (anIt != aConstrList.end()) {
248     if (remove(*anIt)) {
249       std::list<ConstraintWrapperPtr>::iterator aRemoveIt = anIt++;
250       aConstrList.erase(aRemoveIt);
251     } else {
252       isFullyRemoved = false;
253       ++anIt;
254     }
255   }
256   return isFullyRemoved;
257 }
258
259 template <class ENT_TYPE>
260 static bool isUsed(ConstraintWrapperPtr theConstraint, ENT_TYPE theEntity)
261 {
262   std::list<EntityWrapperPtr>::const_iterator anEntIt = theConstraint->entities().begin();
263   for (; anEntIt != theConstraint->entities().end(); ++anEntIt)
264     if ((*anEntIt)->isBase(theEntity))
265       return true;
266   return false;
267 }
268
269 static bool isUsed(EntityWrapperPtr theFeature, AttributePtr theSubEntity)
270 {
271   std::list<EntityWrapperPtr>::const_iterator aSubIt = theFeature->subEntities().begin();
272   for (; aSubIt != theFeature->subEntities().end(); ++aSubIt)
273     if ((*aSubIt)->isBase(theSubEntity))
274       return true;
275   return false;
276 }
277
278 bool SketchSolver_Storage::isUsed(FeaturePtr theFeature) const
279 {
280   if (myFeatureMap.find(theFeature) != myFeatureMap.end())
281     return true;
282   // check constraints
283   std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
284       aCIt = myConstraintMap.begin();
285   std::list<ConstraintWrapperPtr>::const_iterator aCWIt;
286   for (; aCIt != myConstraintMap.end(); ++aCIt)
287     for (aCWIt = aCIt->second.begin(); aCWIt != aCIt->second.end(); ++aCWIt)
288       if (::isUsed(*aCWIt, theFeature))
289         return true;
290   // check attributes
291   std::list<AttributePtr> anAttrList = theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
292   std::list<AttributePtr>::const_iterator anIt = anAttrList.begin();
293   for (; anIt != anAttrList.end(); ++anIt)
294     if (isUsed(*anIt))
295       return true;
296   return false;
297 }
298
299 bool SketchSolver_Storage::isUsed(AttributePtr theAttribute) const
300 {
301   AttributePtr anAttribute = theAttribute;
302   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
303   if (aRefAttr) {
304     if (aRefAttr->isObject())
305       return isUsed(ModelAPI_Feature::feature(aRefAttr->object()));
306     else
307       anAttribute = aRefAttr->attr();
308   }
309
310   if (myAttributeMap.find(theAttribute) != myAttributeMap.end())
311     return true;
312   // check in constraints
313   std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
314       aCIt = myConstraintMap.begin();
315   std::list<ConstraintWrapperPtr>::const_iterator aCWIt;
316   for (; aCIt != myConstraintMap.end(); ++aCIt)
317     for (aCWIt = aCIt->second.begin(); aCWIt != aCIt->second.end(); ++aCWIt)
318       if (::isUsed(*aCWIt, anAttribute))
319         return true;
320   // check in features
321   std::map<FeaturePtr, EntityWrapperPtr>::const_iterator aFIt = myFeatureMap.begin();
322   for (; aFIt != myFeatureMap.end(); ++aFIt)
323     if (::isUsed(aFIt->second, anAttribute))
324       return true;
325   return false;
326 }
327
328
329 bool SketchSolver_Storage::removeEntity(FeaturePtr theFeature)
330 {
331   std::map<FeaturePtr, EntityWrapperPtr>::iterator aFound = myFeatureMap.find(theFeature);
332   if (aFound == myFeatureMap.end())
333     return false; // feature not found, nothing to delete
334
335   EntityWrapperPtr anEntity = aFound->second;
336   myFeatureMap.erase(aFound);
337
338   // Check if the feature is not used by constraints, remove it
339   if (!isUsed(theFeature) && remove(anEntity))
340     return true;
341
342   // feature is not removed, revert operation
343   myFeatureMap[theFeature] = anEntity;
344   update(anEntity);
345   return false;
346 }
347
348 bool SketchSolver_Storage::removeEntity(AttributePtr theAttribute)
349 {
350   std::map<AttributePtr, EntityWrapperPtr>::iterator aFound = myAttributeMap.find(theAttribute);
351   if (aFound == myAttributeMap.end())
352     return false; // attribute not found, nothing to delete
353
354   EntityWrapperPtr anEntity = aFound->second;
355   myAttributeMap.erase(aFound);
356
357   // Check if the attribute is not used by constraints and features, remove it
358   if (!isUsed(theAttribute) && remove(anEntity))
359     return true;
360
361   // attribute is not removed, revert operation
362   myAttributeMap[theAttribute] = anEntity;
363   update(anEntity);
364   return false;
365 }
366
367
368 bool SketchSolver_Storage::remove(ConstraintWrapperPtr theConstraint)
369 {
370   bool isFullyRemoved = true;
371   std::list<EntityWrapperPtr>::const_iterator anIt = theConstraint->entities().begin();
372   for (; anIt != theConstraint->entities().end(); ++anIt) {
373     FeaturePtr aBaseFeature = (*anIt)->baseFeature();
374     if (aBaseFeature)
375       isFullyRemoved = SketchSolver_Storage::removeEntity(aBaseFeature) && isFullyRemoved;
376     else
377       isFullyRemoved = SketchSolver_Storage::removeEntity((*anIt)->baseAttribute()) && isFullyRemoved;
378   }
379   return isFullyRemoved;
380 }
381
382 bool SketchSolver_Storage::remove(EntityWrapperPtr theEntity)
383 {
384   bool isFullyRemoved = true;
385   std::list<EntityWrapperPtr>::const_iterator anEntIt = theEntity->subEntities().begin();
386   for (; anEntIt != theEntity->subEntities().end(); ++anEntIt) {
387     FeaturePtr aBaseFeature = (*anEntIt)->baseFeature();
388     if (aBaseFeature)
389       isFullyRemoved = SketchSolver_Storage::removeEntity(aBaseFeature) && isFullyRemoved;
390     else
391       isFullyRemoved = SketchSolver_Storage::removeEntity((*anEntIt)->baseAttribute()) && isFullyRemoved;
392   }
393
394   std::list<ParameterWrapperPtr>::const_iterator aParIt = theEntity->parameters().begin();
395   for (; aParIt != theEntity->parameters().end(); ++aParIt)
396     isFullyRemoved = remove(*aParIt) && isFullyRemoved;
397   return isFullyRemoved;
398 }
399
400
401 bool SketchSolver_Storage::isInteract(const FeaturePtr& theFeature) const
402 {
403   if (!theFeature)
404     return false;
405   if (myConstraintMap.empty())
406     return true; // empty storage interacts with each feature
407
408   ConstraintPtr aConstraint = std::dynamic_pointer_cast<SketchPlugin_Constraint>(theFeature);
409   if (aConstraint) {
410     if (myConstraintMap.find(aConstraint) != myConstraintMap.end())
411       return true;
412   } else if (myFeatureMap.find(theFeature) != myFeatureMap.end())
413     return true;
414
415   std::list<AttributePtr> anAttrList = theFeature->data()->attributes(std::string());
416   std::list<AttributePtr>::const_iterator anIt = anAttrList.begin();
417   for (; anIt != anAttrList.end(); ++anIt)
418     if (isInteract(*anIt))
419       return true;
420
421   return false;
422 }
423
424 bool SketchSolver_Storage::isInteract(const AttributePtr& theAttribute) const
425 {
426   if (!theAttribute)
427     return false;
428
429   AttributeRefListPtr aRefList = 
430       std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
431   if (aRefList) {
432     std::list<ObjectPtr> anObjects = aRefList->list();
433     std::list<ObjectPtr>::iterator anObjIt = anObjects.begin();
434     for (; anObjIt != anObjects.end(); ++anObjIt) {
435       FeaturePtr aFeature = ModelAPI_Feature::feature(*anObjIt);
436       if (isInteract(aFeature))
437         return true;
438     }
439     return false;
440   }
441
442   AttributeRefAttrPtr aRefAttr =
443       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
444   if (!aRefAttr)
445     return myAttributeMap.find(theAttribute) != myAttributeMap.end();
446   if (!aRefAttr->isObject())
447     return myAttributeMap.find(aRefAttr->attr()) != myAttributeMap.end();
448
449   FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
450   return isInteract(aFeature);
451 }
452
453 bool SketchSolver_Storage::isConsistent() const
454 {
455   // Check the constraints are valid
456   std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
457       aCIter = myConstraintMap.begin();
458   for (; aCIter != myConstraintMap.end(); ++aCIter)
459     if (!aCIter->first->data() || !aCIter->first->data()->isValid())
460       return false;
461   // Check the features are valid
462   std::map<FeaturePtr, EntityWrapperPtr>::const_iterator aFIter = myFeatureMap.begin();
463   for (; aFIter != myFeatureMap.end(); aFIter++)
464     if (!aFIter->first->data() || !aFIter->first->data()->isValid())
465       return false;
466   return true;
467 }
468
469 bool SketchSolver_Storage::isFixed(EntityWrapperPtr theEntity) const
470 {
471   if (theEntity->group() != myGroupID)
472     return true;
473   // no need additional checking for entities differ than point
474   if (theEntity->type() != ENTITY_POINT)
475     return false;
476
477   CoincidentPointsMap::const_iterator anIt = myCoincidentPoints.begin();
478   for (; anIt != myCoincidentPoints.end(); ++anIt)
479     if (anIt->first == theEntity || anIt->second.find(theEntity) != anIt->second.end()) {
480       if (anIt->first->group() != myGroupID)
481         return true;
482       std::set<EntityWrapperPtr>::const_iterator anEntIt = anIt->second.begin();
483       for (; anEntIt != anIt->second.end(); ++anEntIt)
484         if ((*anEntIt)->group() != myGroupID)
485           return true;
486     }
487
488   std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator aCIt = myConstraintMap.begin();
489   std::list<ConstraintWrapperPtr>::const_iterator aCWIt;
490   for (; aCIt != myConstraintMap.end(); ++aCIt) {
491     if (aCIt->second.empty())
492       continue;
493     aCWIt = aCIt->second.begin();
494     if ((*aCWIt)->type() != CONSTRAINT_FIXED)
495       continue;
496     for (; aCWIt != aCIt->second.end(); ++aCIt)
497       if ((theEntity->baseAttribute() && (*aCWIt)->isUsed(theEntity->baseAttribute())) ||
498           (theEntity->baseFeature() && (*aCWIt)->isUsed(theEntity->baseFeature())))
499         return true;
500   }
501
502   return false;
503 }
504
505 void SketchSolver_Storage::removeInvalidEntities()
506 {
507   // Remove invalid constraints
508   std::list<ConstraintPtr> anInvalidConstraints;
509   std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
510       aCIter = myConstraintMap.begin();
511   for (; aCIter != myConstraintMap.end(); ++aCIter)
512     if (!aCIter->first->data() || !aCIter->first->data()->isValid())
513       anInvalidConstraints.push_back(aCIter->first);
514   std::list<ConstraintPtr>::const_iterator anInvCIt = anInvalidConstraints.begin();
515   for (; anInvCIt != anInvalidConstraints.end(); ++anInvCIt)
516     removeConstraint(*anInvCIt);
517   // Remove invalid features
518   std::list<FeaturePtr> anInvalidFeatures;
519   std::map<FeaturePtr, EntityWrapperPtr>::const_iterator aFIter = myFeatureMap.begin();
520   for (; aFIter != myFeatureMap.end(); aFIter++)
521     if (!aFIter->first->data() || !aFIter->first->data()->isValid())
522       anInvalidFeatures.push_back(aFIter->first);
523   std::list<FeaturePtr>::const_iterator anInvFIt = anInvalidFeatures.begin();
524   for (; anInvFIt != anInvalidFeatures.end(); ++anInvFIt)
525     removeEntity(*anInvFIt);
526 }
527
528 EntityWrapperPtr SketchSolver_Storage::getNormal() const
529 {
530   EntityWrapperPtr aSketch = sketch();
531   if (!aSketch)
532     return aSketch;
533
534   // Find normal entity
535   const std::list<EntityWrapperPtr>& aSketchSubs = aSketch->subEntities();
536   std::list<EntityWrapperPtr>::const_iterator aSIt = aSketchSubs.begin();
537   for (; aSIt != aSketchSubs.end(); ++aSIt)
538     if ((*aSIt)->type() == ENTITY_NORMAL)
539       return *aSIt;
540   return EntityWrapperPtr();
541 }
542
543 const EntityWrapperPtr& SketchSolver_Storage::sketch() const
544 {
545   static EntityWrapperPtr aDummySketch;
546
547   std::map<FeaturePtr, EntityWrapperPtr>::const_iterator aFIt = myFeatureMap.begin();
548   for (; aFIt != myFeatureMap.end(); ++aFIt)
549     if (aFIt->second && aFIt->second->type() == ENTITY_SKETCH)
550       break;
551   if (aFIt == myFeatureMap.end())
552     return aDummySketch;
553   return aFIt->second;
554 }
555
556 void SketchSolver_Storage::setSketch(const EntityWrapperPtr& theSketch)
557 {
558   if (sketch())
559     return;
560   addEntity(FeaturePtr(), theSketch);
561 }
562
563 void SketchSolver_Storage::blockEvents(bool isBlocked)
564 {
565   if (isBlocked == myEventsBlocked)
566     return;
567
568   std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
569       aCIter = myConstraintMap.begin();
570   for (; aCIter != myConstraintMap.end(); aCIter++)
571     if (aCIter->first->data() && aCIter->first->data()->isValid())
572       aCIter->first->data()->blockSendAttributeUpdated(isBlocked);
573
574   std::map<FeaturePtr, EntityWrapperPtr>::const_iterator aFIter = myFeatureMap.begin();
575   for (; aFIter != myFeatureMap.end(); aFIter++)
576     if (aFIter->first->data() && aFIter->first->data()->isValid())
577       aFIter->first->data()->blockSendAttributeUpdated(isBlocked);
578
579   std::map<AttributePtr, EntityWrapperPtr>::const_iterator anAtIter = myAttributeMap.begin();
580   for (; anAtIter != myAttributeMap.end(); anAtIter++)
581     if (anAtIter->first->owner() && anAtIter->first->owner()->data() &&
582         anAtIter->first->owner()->data()->isValid())
583       anAtIter->first->owner()->data()->blockSendAttributeUpdated(isBlocked);
584   myEventsBlocked = isBlocked;
585 }
586
587
588
589
590
591
592 // ==============   Auxiliary functions   ====================================
593 bool isEqual(const std::list<ConstraintWrapperPtr>& theCVec1,
594              const std::list<ConstraintWrapperPtr>& theCVec2)
595 {
596   if (theCVec1.size() != theCVec2.size())
597     return false;
598
599   std::list<bool> aChecked(theCVec2.size(), false);
600   std::list<ConstraintWrapperPtr>::const_iterator anIt1 = theCVec1.begin();
601   for (; anIt1 != theCVec1.end(); ++anIt1) {
602     std::list<ConstraintWrapperPtr>::const_iterator anIt2 = theCVec2.begin();
603     std::list<bool>::iterator aCheckIt = aChecked.begin();
604     while (aCheckIt != aChecked.end() && *aCheckIt) {
605       ++aCheckIt;
606       ++anIt2;
607     }
608     for (; anIt2 != theCVec2.end(); ++anIt2, ++aCheckIt)
609       if (!(*aCheckIt) && (*anIt1)->isEqual(*anIt2)) {
610         *aCheckIt = true;
611         break;
612       }
613     // the same constraint is not found
614     if (anIt2 == theCVec2.end())
615       return false;
616   }
617   return true;
618 }