Salome HOME
Delete key regression corrections: in previous implementation sketch entities did...
[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     // Check external feature
157     std::shared_ptr<SketchPlugin_Feature> aSketchFeature = 
158         std::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature);
159     if (aSketchFeature && aSketchFeature->isExternal())
160       aGroup = GID_OUTOFGROUP;
161     aRelated = aBuilder->createFeature(theFeature, aSubs, aGroup);
162     if (!aRelated)
163       return false;
164     addEntity(theFeature, aRelated);
165   } else if (theGroup != GID_UNKNOWN)
166     changeGroup(aRelated, theGroup);
167   return update(aRelated) || isUpdated;
168 }
169
170 bool SketchSolver_Storage::update(AttributePtr theAttribute, const GroupID& theGroup)
171 {
172   AttributePtr anAttribute = theAttribute;
173   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
174   if (aRefAttr) {
175     if (aRefAttr->isObject()) {
176       FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
177       return update(aFeature, theGroup);
178     } else
179       anAttribute = aRefAttr->attr();
180   }
181
182   EntityWrapperPtr aRelated = entity(anAttribute);
183   if (!aRelated) { // Attribute is not exist, create it
184     // verify the attribute is a point of arc and add whole arc
185     if (anAttribute->owner()) {
186       FeaturePtr aFeature = ModelAPI_Feature::feature(anAttribute->owner());
187       if (aFeature->getKind() == SketchPlugin_Arc::ID() &&
188           myFeatureMap.find(aFeature) == myFeatureMap.end()) {
189         // Additional checking that all attributes are initialized
190         if (aFeature->attribute(SketchPlugin_Arc::CENTER_ID())->isInitialized() && 
191             aFeature->attribute(SketchPlugin_Arc::START_ID())->isInitialized() && 
192             aFeature->attribute(SketchPlugin_Arc::END_ID())->isInitialized()) {
193           return SketchSolver_Storage::update(aFeature);
194         } else {
195           myFeatureMap[aFeature] = EntityWrapperPtr();
196           myExistArc = true;
197         }
198       }
199     }
200     BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
201     GroupID aGroup = theGroup != GID_UNKNOWN ? theGroup : myGroupID;
202     // Check attribute of external features
203     std::shared_ptr<SketchPlugin_Feature> aSketchFeature = 
204         std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
205     if (aSketchFeature && aSketchFeature->isExternal())
206       aGroup = GID_OUTOFGROUP;
207     aRelated = aBuilder->createAttribute(anAttribute, aGroup);
208     if (!aRelated)
209       return false;
210     addEntity(anAttribute, aRelated);
211   } else if (theGroup != GID_UNKNOWN)
212     changeGroup(aRelated, theGroup);
213   return update(aRelated);
214 }
215
216
217
218 const std::list<ConstraintWrapperPtr>& SketchSolver_Storage::constraint(
219     const ConstraintPtr& theConstraint) const
220 {
221   static std::list<ConstraintWrapperPtr> aDummy;
222
223   std::map<ConstraintPtr, std::list<ConstraintWrapperPtr>>::const_iterator
224       aFound = myConstraintMap.find(theConstraint);
225   if (aFound != myConstraintMap.end())
226     return aFound->second;
227   return aDummy;
228 }
229
230 const EntityWrapperPtr& SketchSolver_Storage::entity(const FeaturePtr& theFeature) const
231 {
232   static EntityWrapperPtr aDummy;
233
234   std::map<FeaturePtr, EntityWrapperPtr>::const_iterator aFound = myFeatureMap.find(theFeature);
235   if (aFound != myFeatureMap.end())
236     return aFound->second;
237   return aDummy;
238 }
239
240 const EntityWrapperPtr& SketchSolver_Storage::entity(const AttributePtr& theAttribute) const
241 {
242   static EntityWrapperPtr aDummy;
243
244   std::map<AttributePtr, EntityWrapperPtr>::const_iterator
245       aFound = myAttributeMap.find(theAttribute);
246   if (aFound != myAttributeMap.end())
247     return aFound->second;
248
249   AttributeRefAttrPtr aRefAttr =
250       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
251   if (aRefAttr) {
252     if (aRefAttr->isObject()) {
253       FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
254       return entity(aFeature);
255     } else
256       return entity(aRefAttr->attr());
257   }
258   return aDummy;
259 }
260
261 bool SketchSolver_Storage::removeConstraint(ConstraintPtr theConstraint)
262 {
263   std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::iterator
264       aFound = myConstraintMap.find(theConstraint);
265   if (aFound == myConstraintMap.end())
266     return true; // no constraint, already deleted
267
268   // Remove constraint
269   std::list<ConstraintWrapperPtr> aConstrList = aFound->second;
270   myConstraintMap.erase(aFound);
271   // Remove SolveSpace constraints
272   bool isFullyRemoved = true;
273   std::list<ConstraintWrapperPtr>::iterator anIt = aConstrList.begin();
274   while (anIt != aConstrList.end()) {
275     if (remove(*anIt)) {
276       std::list<ConstraintWrapperPtr>::iterator aRemoveIt = anIt++;
277       aConstrList.erase(aRemoveIt);
278     } else {
279       isFullyRemoved = false;
280       ++anIt;
281     }
282   }
283   return isFullyRemoved;
284 }
285
286 template <class ENT_TYPE>
287 static bool isUsed(ConstraintWrapperPtr theConstraint, ENT_TYPE theEntity)
288 {
289   if (!theConstraint || !theEntity)
290     return false;
291   std::list<EntityWrapperPtr>::const_iterator anEntIt = theConstraint->entities().begin();
292   for (; anEntIt != theConstraint->entities().end(); ++anEntIt)
293     if ((*anEntIt)->isBase(theEntity))
294       return true;
295   return false;
296 }
297
298 static bool isUsed(EntityWrapperPtr theFeature, AttributePtr theSubEntity)
299 {
300   if (!theFeature || !theSubEntity)
301     return false;
302   std::list<EntityWrapperPtr>::const_iterator aSubIt = theFeature->subEntities().begin();
303   for (; aSubIt != theFeature->subEntities().end(); ++aSubIt)
304     if ((*aSubIt)->isBase(theSubEntity))
305       return true;
306   return false;
307 }
308
309 static bool isUsed(ConstraintPtr theConstraint, AttributePtr theAttribute)
310 {
311   if (!theConstraint || !theAttribute)
312     return false;
313   std::list<AttributePtr> anAttrList = theConstraint->data()->attributes(std::string());
314   std::list<AttributePtr>::const_iterator anIt = anAttrList.begin();
315   for (; anIt != anAttrList.end(); ++anIt) {
316     if (*anIt == theAttribute)
317       return true;
318     AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anIt);
319     if (aRefAttr && !aRefAttr->isObject() && aRefAttr->attr() == theAttribute)
320       return true;
321   }
322   return false;
323 }
324
325 bool SketchSolver_Storage::isUsed(FeaturePtr theFeature) const
326 {
327   if (myFeatureMap.find(theFeature) != myFeatureMap.end())
328     return true;
329   // check constraints
330   std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
331       aCIt = myConstraintMap.begin();
332   std::list<ConstraintWrapperPtr>::const_iterator aCWIt;
333   for (; aCIt != myConstraintMap.end(); ++aCIt)
334     for (aCWIt = aCIt->second.begin(); aCWIt != aCIt->second.end(); ++aCWIt)
335       if (::isUsed(*aCWIt, theFeature))
336         return true;
337   // check attributes
338   std::list<AttributePtr> anAttrList = theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
339   std::list<AttributePtr>::const_iterator anIt = anAttrList.begin();
340   for (; anIt != anAttrList.end(); ++anIt)
341     if (isUsed(*anIt))
342       return true;
343   return false;
344 }
345
346 bool SketchSolver_Storage::isUsed(AttributePtr theAttribute) const
347 {
348   AttributePtr anAttribute = theAttribute;
349   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
350   if (aRefAttr) {
351     if (aRefAttr->isObject())
352       return isUsed(ModelAPI_Feature::feature(aRefAttr->object()));
353     else
354       anAttribute = aRefAttr->attr();
355   }
356
357   if (myAttributeMap.find(theAttribute) != myAttributeMap.end())
358     return true;
359   // check in constraints
360   std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
361       aCIt = myConstraintMap.begin();
362   std::list<ConstraintWrapperPtr>::const_iterator aCWIt;
363   for (; aCIt != myConstraintMap.end(); ++aCIt) {
364     for (aCWIt = aCIt->second.begin(); aCWIt != aCIt->second.end(); ++aCWIt)
365       if (::isUsed(*aCWIt, anAttribute))
366         return true;
367     // Additional check for the Fixed constraints, which have no wrapper associated.
368     if (aCIt->first->getKind() == SketchPlugin_ConstraintRigid::ID() &&
369         ::isUsed(aCIt->first, anAttribute))
370       return true;
371   }
372   // check in features
373   std::map<FeaturePtr, EntityWrapperPtr>::const_iterator aFIt = myFeatureMap.begin();
374   for (; aFIt != myFeatureMap.end(); ++aFIt)
375     if (::isUsed(aFIt->second, anAttribute))
376       return true;
377   return false;
378 }
379
380
381 bool SketchSolver_Storage::removeEntity(FeaturePtr theFeature)
382 {
383   std::map<FeaturePtr, EntityWrapperPtr>::iterator aFound = myFeatureMap.find(theFeature);
384   if (aFound == myFeatureMap.end())
385     return true; // feature not found, nothing to delete
386
387   EntityWrapperPtr anEntity = aFound->second;
388   myFeatureMap.erase(aFound);
389
390   // Check if the feature is not used by constraints, remove it
391   if (!anEntity || (!isUsed(theFeature) && remove(anEntity)))
392     return true;
393
394   // feature is not removed, revert operation
395   myFeatureMap[theFeature] = anEntity;
396   update(anEntity);
397   return false;
398 }
399
400 bool SketchSolver_Storage::removeEntity(AttributePtr theAttribute)
401 {
402   std::map<AttributePtr, EntityWrapperPtr>::iterator aFound = myAttributeMap.find(theAttribute);
403   if (aFound == myAttributeMap.end())
404     return true; // attribute not found, nothing to delete
405
406   EntityWrapperPtr anEntity = aFound->second;
407   myAttributeMap.erase(aFound);
408
409   // Check if the attribute is not used by constraints and features, remove it
410   if (!anEntity || (!isUsed(theAttribute) && remove(anEntity)))
411     return true;
412
413   // attribute is not removed, revert operation
414   myAttributeMap[theAttribute] = anEntity;
415   update(anEntity);
416   return false;
417 }
418
419
420 bool SketchSolver_Storage::remove(ConstraintWrapperPtr theConstraint)
421 {
422   bool isFullyRemoved = true;
423   std::list<EntityWrapperPtr>::const_iterator anIt = theConstraint->entities().begin();
424   for (; anIt != theConstraint->entities().end(); ++anIt) {
425     FeaturePtr aBaseFeature = (*anIt)->baseFeature();
426     if (aBaseFeature)
427       isFullyRemoved = SketchSolver_Storage::removeEntity(aBaseFeature) && isFullyRemoved;
428     else
429       isFullyRemoved = SketchSolver_Storage::removeEntity((*anIt)->baseAttribute()) && isFullyRemoved;
430   }
431   return isFullyRemoved;
432 }
433
434 bool SketchSolver_Storage::remove(EntityWrapperPtr theEntity)
435 {
436   bool isFullyRemoved = true;
437   std::list<EntityWrapperPtr>::const_iterator anEntIt = theEntity->subEntities().begin();
438   for (; anEntIt != theEntity->subEntities().end(); ++anEntIt) {
439     FeaturePtr aBaseFeature = (*anEntIt)->baseFeature();
440     if (aBaseFeature)
441       isFullyRemoved = SketchSolver_Storage::removeEntity(aBaseFeature) && isFullyRemoved;
442     else
443       isFullyRemoved = SketchSolver_Storage::removeEntity((*anEntIt)->baseAttribute()) && isFullyRemoved;
444   }
445
446   std::list<ParameterWrapperPtr>::const_iterator aParIt = theEntity->parameters().begin();
447   for (; aParIt != theEntity->parameters().end(); ++aParIt)
448     isFullyRemoved = remove(*aParIt) && isFullyRemoved;
449   return isFullyRemoved;
450 }
451
452
453 bool SketchSolver_Storage::isInteract(const FeaturePtr& theFeature) const
454 {
455   if (!theFeature)
456     return false;
457   if (myConstraintMap.empty())
458     return true; // empty storage interacts with each feature
459
460   ConstraintPtr aConstraint = std::dynamic_pointer_cast<SketchPlugin_Constraint>(theFeature);
461   if (aConstraint) {
462     if (myConstraintMap.find(aConstraint) != myConstraintMap.end())
463       return true;
464   } else if (myFeatureMap.find(theFeature) != myFeatureMap.end())
465     return true;
466
467   std::list<AttributePtr> anAttrList = theFeature->data()->attributes(std::string());
468   std::list<AttributePtr>::const_iterator anIt = anAttrList.begin();
469   for (; anIt != anAttrList.end(); ++anIt)
470     if (isInteract(*anIt))
471       return true;
472
473   return false;
474 }
475
476 bool SketchSolver_Storage::isInteract(const AttributePtr& theAttribute) const
477 {
478   if (!theAttribute)
479     return false;
480
481   AttributeRefListPtr aRefList = 
482       std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
483   if (aRefList) {
484     std::list<ObjectPtr> anObjects = aRefList->list();
485     std::list<ObjectPtr>::iterator anObjIt = anObjects.begin();
486     for (; anObjIt != anObjects.end(); ++anObjIt) {
487       FeaturePtr aFeature = ModelAPI_Feature::feature(*anObjIt);
488       if (isInteract(aFeature))
489         return true;
490     }
491     return false;
492   }
493
494   AttributeRefAttrPtr aRefAttr =
495       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
496   if (!aRefAttr)
497     return myAttributeMap.find(theAttribute) != myAttributeMap.end();
498   if (!aRefAttr->isObject())
499     return myAttributeMap.find(aRefAttr->attr()) != myAttributeMap.end();
500
501   FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
502   return isInteract(aFeature);
503 }
504
505 bool SketchSolver_Storage::isConsistent() const
506 {
507   // Check the constraints are valid
508   std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
509       aCIter = myConstraintMap.begin();
510   for (; aCIter != myConstraintMap.end(); ++aCIter)
511     if (!aCIter->first->data() || !aCIter->first->data()->isValid())
512       return false;
513   // Check the features are valid
514   std::map<FeaturePtr, EntityWrapperPtr>::const_iterator aFIter = myFeatureMap.begin();
515   for (; aFIter != myFeatureMap.end(); aFIter++)
516     if (!aFIter->first->data() || !aFIter->first->data()->isValid())
517       return false;
518   return true;
519 }
520
521 bool SketchSolver_Storage::isFixed(EntityWrapperPtr theEntity) const
522 {
523   if (theEntity->group() != myGroupID)
524     return true;
525   // no need additional checking for entities differ than point
526   if (theEntity->type() != ENTITY_POINT)
527     return false;
528
529   CoincidentPointsMap::const_iterator anIt = myCoincidentPoints.begin();
530   for (; anIt != myCoincidentPoints.end(); ++anIt)
531     if (anIt->first == theEntity || anIt->second.find(theEntity) != anIt->second.end()) {
532       if (anIt->first->group() != myGroupID)
533         return true;
534       std::set<EntityWrapperPtr>::const_iterator anEntIt = anIt->second.begin();
535       for (; anEntIt != anIt->second.end(); ++anEntIt)
536         if ((*anEntIt)->group() != myGroupID)
537           return true;
538     }
539
540   std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator aCIt = myConstraintMap.begin();
541   std::list<ConstraintWrapperPtr>::const_iterator aCWIt;
542   for (; aCIt != myConstraintMap.end(); ++aCIt) {
543     if (aCIt->second.empty())
544       continue;
545     aCWIt = aCIt->second.begin();
546     if ((*aCWIt)->type() != CONSTRAINT_FIXED)
547       continue;
548     for (; aCWIt != aCIt->second.end(); ++aCIt)
549       if ((theEntity->baseAttribute() && (*aCWIt)->isUsed(theEntity->baseAttribute())) ||
550           (theEntity->baseFeature() && (*aCWIt)->isUsed(theEntity->baseFeature())))
551         return true;
552   }
553
554   return false;
555 }
556
557 void SketchSolver_Storage::removeInvalidEntities()
558 {
559   // Remove invalid constraints
560   std::list<ConstraintPtr> anInvalidConstraints;
561   std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
562       aCIter = myConstraintMap.begin();
563   for (; aCIter != myConstraintMap.end(); ++aCIter)
564     if (!aCIter->first->data() || !aCIter->first->data()->isValid())
565       anInvalidConstraints.push_back(aCIter->first);
566   std::list<ConstraintPtr>::const_iterator anInvCIt = anInvalidConstraints.begin();
567   for (; anInvCIt != anInvalidConstraints.end(); ++anInvCIt)
568     removeConstraint(*anInvCIt);
569   // Remove invalid features
570   std::list<FeaturePtr> anInvalidFeatures;
571   std::map<FeaturePtr, EntityWrapperPtr>::const_iterator aFIter = myFeatureMap.begin();
572   for (; aFIter != myFeatureMap.end(); aFIter++)
573     if (!aFIter->first->data() || !aFIter->first->data()->isValid())
574       anInvalidFeatures.push_back(aFIter->first);
575   std::list<FeaturePtr>::const_iterator anInvFIt = anInvalidFeatures.begin();
576   for (; anInvFIt != anInvalidFeatures.end(); ++anInvFIt)
577     removeEntity(*anInvFIt);
578 }
579
580 EntityWrapperPtr SketchSolver_Storage::getNormal() const
581 {
582   EntityWrapperPtr aSketch = sketch();
583   if (!aSketch)
584     return aSketch;
585
586   // Find normal entity
587   const std::list<EntityWrapperPtr>& aSketchSubs = aSketch->subEntities();
588   std::list<EntityWrapperPtr>::const_iterator aSIt = aSketchSubs.begin();
589   for (; aSIt != aSketchSubs.end(); ++aSIt)
590     if ((*aSIt)->type() == ENTITY_NORMAL)
591       return *aSIt;
592   return EntityWrapperPtr();
593 }
594
595 const EntityWrapperPtr& SketchSolver_Storage::sketch() const
596 {
597   static EntityWrapperPtr aDummySketch;
598
599   std::map<FeaturePtr, EntityWrapperPtr>::const_iterator aFIt = myFeatureMap.begin();
600   for (; aFIt != myFeatureMap.end(); ++aFIt)
601     if (aFIt->second && aFIt->second->type() == ENTITY_SKETCH)
602       break;
603   if (aFIt == myFeatureMap.end())
604     return aDummySketch;
605   return aFIt->second;
606 }
607
608 void SketchSolver_Storage::setSketch(const EntityWrapperPtr& theSketch)
609 {
610   if (sketch())
611     return;
612   addEntity(FeaturePtr(), theSketch);
613 }
614
615 void SketchSolver_Storage::processArcs()
616 {
617   myExistArc = false;
618   std::map<FeaturePtr, EntityWrapperPtr>::iterator aFIt = myFeatureMap.begin();
619   for (; aFIt != myFeatureMap.end(); ++aFIt)
620     if (!aFIt->second && aFIt->first->getKind() == SketchPlugin_Arc::ID()) {
621       // Additional checking the attributes are initialized
622       if (aFIt->first->attribute(SketchPlugin_Arc::CENTER_ID())->isInitialized() && 
623           aFIt->first->attribute(SketchPlugin_Arc::START_ID())->isInitialized() && 
624           aFIt->first->attribute(SketchPlugin_Arc::END_ID())->isInitialized())
625         update(aFIt->first);
626       else
627         myExistArc = true;
628     }
629 }
630
631 void SketchSolver_Storage::blockEvents(bool isBlocked)
632 {
633   if (isBlocked == myEventsBlocked)
634     return;
635
636   std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
637       aCIter = myConstraintMap.begin();
638   for (; aCIter != myConstraintMap.end(); aCIter++)
639     if (aCIter->first->data() && aCIter->first->data()->isValid())
640       aCIter->first->data()->blockSendAttributeUpdated(isBlocked);
641
642   std::map<FeaturePtr, EntityWrapperPtr>::const_iterator aFIter = myFeatureMap.begin();
643   for (; aFIter != myFeatureMap.end(); aFIter++)
644     if (aFIter->first->data() && aFIter->first->data()->isValid())
645       aFIter->first->data()->blockSendAttributeUpdated(isBlocked);
646
647   std::map<AttributePtr, EntityWrapperPtr>::const_iterator anAtIter = myAttributeMap.begin();
648   for (; anAtIter != myAttributeMap.end(); anAtIter++)
649     if (anAtIter->first->owner() && anAtIter->first->owner()->data() &&
650         anAtIter->first->owner()->data()->isValid())
651       anAtIter->first->owner()->data()->blockSendAttributeUpdated(isBlocked);
652   myEventsBlocked = isBlocked;
653 }
654
655
656
657
658
659
660 // ==============   Auxiliary functions   ====================================
661 bool isEqual(const std::list<ConstraintWrapperPtr>& theCVec1,
662              const std::list<ConstraintWrapperPtr>& theCVec2)
663 {
664   if (theCVec1.size() != theCVec2.size())
665     return false;
666
667   std::list<bool> aChecked(theCVec2.size(), false);
668   std::list<ConstraintWrapperPtr>::const_iterator anIt1 = theCVec1.begin();
669   for (; anIt1 != theCVec1.end(); ++anIt1) {
670     std::list<ConstraintWrapperPtr>::const_iterator anIt2 = theCVec2.begin();
671     std::list<bool>::iterator aCheckIt = aChecked.begin();
672     while (aCheckIt != aChecked.end() && *aCheckIt) {
673       ++aCheckIt;
674       ++anIt2;
675     }
676     for (; anIt2 != theCVec2.end(); ++anIt2, ++aCheckIt)
677       if (!(*aCheckIt) && (*anIt1)->isEqual(*anIt2)) {
678         *aCheckIt = true;
679         break;
680       }
681     // the same constraint is not found
682     if (anIt2 == theCVec2.end())
683       return false;
684   }
685   return true;
686 }