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