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