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