1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: SketchSolver_Storage.cpp
4 // Created: 30 Nov 2015
5 // Author: Artem ZHIDKOV
7 #include <SketchSolver_Storage.h>
8 #include <SketchSolver_Manager.h>
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>
17 /// \brief Verify two vectors of constraints are equal.
18 /// Vectors differ by the order of elements are equal.
19 static bool isEqual(const std::list<ConstraintWrapperPtr>& theCVec1,
20 const std::list<ConstraintWrapperPtr>& theCVec2);
23 void SketchSolver_Storage::addConstraint(ConstraintPtr theConstraint,
24 ConstraintWrapperPtr theSolverConstraint)
26 std::list<ConstraintWrapperPtr> aConstrList(1, theSolverConstraint);
27 addConstraint(theConstraint, aConstrList);
30 void SketchSolver_Storage::addConstraint(
31 ConstraintPtr theConstraint,
32 std::list<ConstraintWrapperPtr> theSolverConstraints)
34 std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
35 aFound = myConstraintMap.find(theConstraint);
36 if (aFound == myConstraintMap.end() || !isEqual(aFound->second, theSolverConstraints))
37 setNeedToResolve(true);
39 // Do not add point-point coincidence, because it is already made by setting
40 // the same parameters for both points
41 if (!theSolverConstraints.empty() &&
42 theSolverConstraints.front()->type() != CONSTRAINT_PT_PT_COINCIDENT) {
43 std::list<ConstraintWrapperPtr>::iterator aCIt = theSolverConstraints.begin();
44 for (; aCIt != theSolverConstraints.end(); ++aCIt)
47 myConstraintMap[theConstraint] = theSolverConstraints;
48 // block events if necessary
49 if (myEventsBlocked && theConstraint->data() && theConstraint->data()->isValid())
50 theConstraint->data()->blockSendAttributeUpdated(myEventsBlocked);
53 void SketchSolver_Storage::addEntity(FeaturePtr theFeature,
54 EntityWrapperPtr theSolverEntity)
56 std::map<FeaturePtr, EntityWrapperPtr>::const_iterator aFound = myFeatureMap.find(theFeature);
57 if (aFound == myFeatureMap.end() || !aFound->second || !aFound->second->isEqual(theSolverEntity))
58 setNeedToResolve(true); // the entity is new or modified
60 myFeatureMap[theFeature] = theSolverEntity;
61 // block events if necessary
62 if (myEventsBlocked && theFeature->data() && theFeature->data()->isValid())
63 theFeature->data()->blockSendAttributeUpdated(myEventsBlocked);
66 void SketchSolver_Storage::addEntity(AttributePtr theAttribute,
67 EntityWrapperPtr theSolverEntity)
69 std::map<AttributePtr, EntityWrapperPtr>::const_iterator aFound = myAttributeMap.find(theAttribute);
70 if (aFound == myAttributeMap.end() || !aFound->second || !aFound->second->isEqual(theSolverEntity))
71 setNeedToResolve(true); // the entity is new or modified
73 myAttributeMap[theAttribute] = theSolverEntity;
74 // block events if necessary
75 if (myEventsBlocked && theAttribute->owner() &&
76 theAttribute->owner()->data() && theAttribute->owner()->data()->isValid())
77 theAttribute->owner()->data()->blockSendAttributeUpdated(myEventsBlocked);
81 bool SketchSolver_Storage::update(FeaturePtr theFeature, const GroupID& theGroup)
83 bool isUpdated = false;
84 EntityWrapperPtr aRelated = entity(theFeature);
85 if (!aRelated) { // Feature is not exist, create it
86 std::list<EntityWrapperPtr> aSubs;
87 // Reserve the feature in the map of features (do not want to add several copies of it)
88 myFeatureMap[theFeature] = aRelated;
89 // Firstly, create/update its attributes
90 std::list<AttributePtr> anAttrs =
91 theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
92 std::list<AttributePtr>::const_iterator anIt = anAttrs.begin();
93 for (; anIt != anAttrs.end(); ++anIt) {
94 isUpdated = update(*anIt, theGroup) || isUpdated;
95 aSubs.push_back(entity(*anIt));
97 // If the feature is a circle, add its radius as a sub
98 if (theFeature->getKind() == SketchPlugin_Circle::ID()) {
99 AttributePtr aRadius = theFeature->attribute(SketchPlugin_Circle::RADIUS_ID());
100 isUpdated = update(aRadius, theGroup) || isUpdated;
101 aSubs.push_back(entity(aRadius));
103 // If the feature if circle or arc, we need to add normal of the sketch to the list of subs
104 if (theFeature->getKind() == SketchPlugin_Arc::ID() ||
105 theFeature->getKind() == SketchPlugin_Circle::ID()) {
106 EntityWrapperPtr aNormal = getNormal();
107 if (aNormal) aSubs.push_back(aNormal);
109 // Secondly, convert feature
110 BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
111 GroupID aGroup = theGroup != GID_UNKNOWN ? theGroup : myGroupID;
112 aRelated = aBuilder->createFeature(theFeature, aSubs, aGroup);
115 addEntity(theFeature, aRelated);
116 } else if (theGroup != GID_UNKNOWN)
117 changeGroup(aRelated, theGroup);
118 return update(aRelated) || isUpdated;
121 bool SketchSolver_Storage::update(AttributePtr theAttribute, const GroupID& theGroup)
123 AttributePtr anAttribute = theAttribute;
124 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
126 if (aRefAttr->isObject()) {
127 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
128 return update(aFeature, theGroup);
130 anAttribute = aRefAttr->attr();
133 EntityWrapperPtr aRelated = entity(anAttribute);
134 if (!aRelated) { // Attribute is not exist, create it
135 BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
136 GroupID aGroup = theGroup != GID_UNKNOWN ? theGroup : myGroupID;
137 aRelated = aBuilder->createAttribute(anAttribute, aGroup);
140 addEntity(anAttribute, aRelated);
141 } else if (theGroup != GID_UNKNOWN)
142 changeGroup(aRelated, theGroup);
143 return update(aRelated);
148 const std::list<ConstraintWrapperPtr>& SketchSolver_Storage::constraint(
149 const ConstraintPtr& theConstraint) const
151 static std::list<ConstraintWrapperPtr> aDummy;
153 std::map<ConstraintPtr, std::list<ConstraintWrapperPtr>>::const_iterator
154 aFound = myConstraintMap.find(theConstraint);
155 if (aFound != myConstraintMap.end())
156 return aFound->second;
160 const EntityWrapperPtr& SketchSolver_Storage::entity(const FeaturePtr& theFeature) const
162 static EntityWrapperPtr aDummy;
164 std::map<FeaturePtr, EntityWrapperPtr>::const_iterator aFound = myFeatureMap.find(theFeature);
165 if (aFound != myFeatureMap.end())
166 return aFound->second;
170 const EntityWrapperPtr& SketchSolver_Storage::entity(const AttributePtr& theAttribute) const
172 static EntityWrapperPtr aDummy;
174 std::map<AttributePtr, EntityWrapperPtr>::const_iterator
175 aFound = myAttributeMap.find(theAttribute);
176 if (aFound != myAttributeMap.end())
177 return aFound->second;
179 AttributeRefAttrPtr aRefAttr =
180 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
182 if (aRefAttr->isObject()) {
183 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
184 return entity(aFeature);
186 return entity(aRefAttr->attr());
191 bool SketchSolver_Storage::removeConstraint(ConstraintPtr theConstraint)
193 std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::iterator
194 aFound = myConstraintMap.find(theConstraint);
195 if (aFound == myConstraintMap.end())
196 return true; // no constraint, already deleted
199 std::list<ConstraintWrapperPtr> aConstrList = aFound->second;
200 myConstraintMap.erase(aFound);
201 // Remove SolveSpace constraints
202 bool isFullyRemoved = true;
203 std::list<ConstraintWrapperPtr>::iterator anIt = aConstrList.begin();
204 while (anIt != aConstrList.end()) {
206 std::list<ConstraintWrapperPtr>::iterator aRemoveIt = anIt++;
207 aConstrList.erase(aRemoveIt);
209 isFullyRemoved = false;
213 if (!isFullyRemoved) {
214 // revert removed constraint
215 addConstraint(theConstraint, aConstrList);
217 return isFullyRemoved;
220 template <class ENT_TYPE>
221 static bool isUsed(ConstraintWrapperPtr theConstraint, ENT_TYPE theEntity)
223 std::list<EntityWrapperPtr>::const_iterator anEntIt = theConstraint->entities().begin();
224 for (; anEntIt != theConstraint->entities().end(); ++anEntIt)
225 if ((*anEntIt)->isBase(theEntity))
230 static bool isUsed(EntityWrapperPtr theFeature, AttributePtr theSubEntity)
232 std::list<EntityWrapperPtr>::const_iterator aSubIt = theFeature->subEntities().begin();
233 for (; aSubIt != theFeature->subEntities().end(); ++aSubIt)
234 if ((*aSubIt)->isBase(theSubEntity))
239 bool SketchSolver_Storage::isUsed(FeaturePtr theFeature) const
241 if (myFeatureMap.find(theFeature) != myFeatureMap.end())
244 std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
245 aCIt = myConstraintMap.begin();
246 std::list<ConstraintWrapperPtr>::const_iterator aCWIt;
247 for (; aCIt != myConstraintMap.end(); ++aCIt)
248 for (aCWIt = aCIt->second.begin(); aCWIt != aCIt->second.end(); ++aCWIt)
249 if (::isUsed(*aCWIt, theFeature))
252 std::list<AttributePtr> anAttrList = theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
253 std::list<AttributePtr>::const_iterator anIt = anAttrList.begin();
254 for (; anIt != anAttrList.end(); ++anIt)
260 bool SketchSolver_Storage::isUsed(AttributePtr theAttribute) const
262 AttributePtr anAttribute = theAttribute;
263 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
265 if (aRefAttr->isObject())
266 return isUsed(ModelAPI_Feature::feature(aRefAttr->object()));
268 anAttribute = aRefAttr->attr();
271 if (myAttributeMap.find(theAttribute) != myAttributeMap.end())
273 // check in constraints
274 std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
275 aCIt = myConstraintMap.begin();
276 std::list<ConstraintWrapperPtr>::const_iterator aCWIt;
277 for (; aCIt != myConstraintMap.end(); ++aCIt)
278 for (aCWIt = aCIt->second.begin(); aCWIt != aCIt->second.end(); ++aCWIt)
279 if (::isUsed(*aCWIt, anAttribute))
282 std::map<FeaturePtr, EntityWrapperPtr>::const_iterator aFIt = myFeatureMap.begin();
283 for (; aFIt != myFeatureMap.end(); ++aFIt)
284 if (::isUsed(aFIt->second, anAttribute))
290 bool SketchSolver_Storage::removeEntity(FeaturePtr theFeature)
292 std::map<FeaturePtr, EntityWrapperPtr>::iterator aFound = myFeatureMap.find(theFeature);
293 if (aFound == myFeatureMap.end())
294 return false; // feature not found, nothing to delete
296 // Check the feature is not used by constraints
297 if (isUsed(theFeature))
298 return false; // the feature is used, don't remove it
301 EntityWrapperPtr anEntity = aFound->second;
302 myFeatureMap.erase(aFound);
303 if (remove(anEntity))
305 // feature is not removed, revert operation
306 myFeatureMap[theFeature] = anEntity;
311 bool SketchSolver_Storage::removeEntity(AttributePtr theAttribute)
313 std::map<AttributePtr, EntityWrapperPtr>::iterator aFound = myAttributeMap.find(theAttribute);
314 if (aFound == myAttributeMap.end())
315 return false; // attribute not found, nothing to delete
317 // Check the attribute is not used by constraints
318 if (isUsed(theAttribute))
319 return false; // the attribute is used, don't remove it
320 // Check the attribute is not used by other features
321 std::map<FeaturePtr, EntityWrapperPtr>::const_iterator aFIt = myFeatureMap.begin();
322 for (; aFIt != myFeatureMap.end(); ++aFIt)
323 if (::isUsed(aFIt->second, theAttribute)) // the attribute is used, don't remove it
327 EntityWrapperPtr anEntity = aFound->second;
328 myAttributeMap.erase(aFound);
329 if (remove(anEntity))
331 // attribute is not removed, revert operation
332 myAttributeMap[theAttribute] = anEntity;
338 bool SketchSolver_Storage::remove(ConstraintWrapperPtr theConstraint)
340 bool isFullyRemoved = true;
341 std::list<EntityWrapperPtr>::const_iterator anIt = theConstraint->entities().begin();
342 for (; anIt != theConstraint->entities().end(); ++anIt) {
343 FeaturePtr aBaseFeature = (*anIt)->baseFeature();
345 isFullyRemoved = SketchSolver_Storage::removeEntity(aBaseFeature) && isFullyRemoved;
347 isFullyRemoved = SketchSolver_Storage::removeEntity((*anIt)->baseAttribute()) && isFullyRemoved;
349 return isFullyRemoved;
352 bool SketchSolver_Storage::remove(EntityWrapperPtr theEntity)
354 bool isFullyRemoved = true;
355 std::list<EntityWrapperPtr>::const_iterator anEntIt = theEntity->subEntities().begin();
356 for (; anEntIt != theEntity->subEntities().end(); ++anEntIt) {
357 FeaturePtr aBaseFeature = (*anEntIt)->baseFeature();
359 isFullyRemoved = SketchSolver_Storage::removeEntity(aBaseFeature) && isFullyRemoved;
361 isFullyRemoved = SketchSolver_Storage::removeEntity((*anEntIt)->baseAttribute()) && isFullyRemoved;
364 std::list<ParameterWrapperPtr>::const_iterator aParIt = theEntity->parameters().begin();
365 for (; aParIt != theEntity->parameters().end(); ++aParIt)
366 isFullyRemoved = remove(*aParIt) && isFullyRemoved;
367 return isFullyRemoved;
371 bool SketchSolver_Storage::isInteract(const FeaturePtr& theFeature) const
375 if (myConstraintMap.empty())
376 return true; // empty storage interacts with each feature
378 ConstraintPtr aConstraint = std::dynamic_pointer_cast<SketchPlugin_Constraint>(theFeature);
380 if (myConstraintMap.find(aConstraint) != myConstraintMap.end())
382 } else if (myFeatureMap.find(theFeature) != myFeatureMap.end())
385 std::list<AttributePtr> anAttrList = theFeature->data()->attributes(std::string());
386 std::list<AttributePtr>::const_iterator anIt = anAttrList.begin();
387 for (; anIt != anAttrList.end(); ++anIt)
388 if (isInteract(*anIt))
394 bool SketchSolver_Storage::isInteract(const AttributePtr& theAttribute) const
399 AttributeRefListPtr aRefList =
400 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
402 std::list<ObjectPtr> anObjects = aRefList->list();
403 std::list<ObjectPtr>::iterator anObjIt = anObjects.begin();
404 for (; anObjIt != anObjects.end(); ++anObjIt) {
405 FeaturePtr aFeature = ModelAPI_Feature::feature(*anObjIt);
406 if (isInteract(aFeature))
412 AttributeRefAttrPtr aRefAttr =
413 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
415 return myAttributeMap.find(theAttribute) != myAttributeMap.end();
416 if (!aRefAttr->isObject())
417 return myAttributeMap.find(aRefAttr->attr()) != myAttributeMap.end();
419 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
420 return isInteract(aFeature);
423 bool SketchSolver_Storage::isConsistent() const
425 // Check the constraints are valid
426 std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
427 aCIter = myConstraintMap.begin();
428 for (; aCIter != myConstraintMap.end(); ++aCIter)
429 if (!aCIter->first->data() || !aCIter->first->data()->isValid())
431 // Check the features are valid
432 std::map<FeaturePtr, EntityWrapperPtr>::const_iterator aFIter = myFeatureMap.begin();
433 for (; aFIter != myFeatureMap.end(); aFIter++)
434 if (!aFIter->first->data() || !aFIter->first->data()->isValid())
439 bool SketchSolver_Storage::isFixed(EntityWrapperPtr theEntity) const
441 if (theEntity->group() != myGroupID)
443 // no need additional checking for entities differ than point
444 if (theEntity->type() != ENTITY_POINT)
447 CoincidentPointsMap::const_iterator anIt = myCoincidentPoints.begin();
448 for (; anIt != myCoincidentPoints.end(); ++anIt)
449 if (anIt->first == theEntity || anIt->second.find(theEntity) != anIt->second.end()) {
450 if (anIt->first->group() != myGroupID)
452 std::set<EntityWrapperPtr>::const_iterator anEntIt = anIt->second.begin();
453 for (; anEntIt != anIt->second.end(); ++anEntIt)
454 if ((*anEntIt)->group() != myGroupID)
460 void SketchSolver_Storage::removeInvalidEntities()
462 // Remove invalid constraints
463 std::list<ConstraintPtr> anInvalidConstraints;
464 std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
465 aCIter = myConstraintMap.begin();
466 for (; aCIter != myConstraintMap.end(); ++aCIter)
467 if (!aCIter->first->data() || !aCIter->first->data()->isValid())
468 anInvalidConstraints.push_back(aCIter->first);
469 std::list<ConstraintPtr>::const_iterator anInvCIt = anInvalidConstraints.begin();
470 for (; anInvCIt != anInvalidConstraints.end(); ++anInvCIt)
471 removeConstraint(*anInvCIt);
472 // Remove invalid features
473 std::list<FeaturePtr> anInvalidFeatures;
474 std::map<FeaturePtr, EntityWrapperPtr>::const_iterator aFIter = myFeatureMap.begin();
475 for (; aFIter != myFeatureMap.end(); aFIter++)
476 if (!aFIter->first->data() || !aFIter->first->data()->isValid())
477 anInvalidFeatures.push_back(aFIter->first);
478 std::list<FeaturePtr>::const_iterator anInvFIt = anInvalidFeatures.begin();
479 for (; anInvFIt != anInvalidFeatures.end(); ++anInvFIt)
480 removeEntity(*anInvFIt);
483 EntityWrapperPtr SketchSolver_Storage::getNormal() const
485 EntityWrapperPtr aSketch = sketch();
489 // Find normal entity
490 const std::list<EntityWrapperPtr>& aSketchSubs = aSketch->subEntities();
491 std::list<EntityWrapperPtr>::const_iterator aSIt = aSketchSubs.begin();
492 for (; aSIt != aSketchSubs.end(); ++aSIt)
493 if ((*aSIt)->type() == ENTITY_NORMAL)
495 return EntityWrapperPtr();
498 const EntityWrapperPtr& SketchSolver_Storage::sketch() const
500 static EntityWrapperPtr aDummySketch;
502 std::map<FeaturePtr, EntityWrapperPtr>::const_iterator aFIt = myFeatureMap.begin();
503 for (; aFIt != myFeatureMap.end(); ++aFIt)
504 if (aFIt->second && aFIt->second->type() == ENTITY_SKETCH)
506 if (aFIt == myFeatureMap.end())
511 void SketchSolver_Storage::setSketch(const EntityWrapperPtr& theSketch)
515 addEntity(FeaturePtr(), theSketch);
518 void SketchSolver_Storage::blockEvents(bool isBlocked)
520 if (isBlocked == myEventsBlocked)
523 std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
524 aCIter = myConstraintMap.begin();
525 for (; aCIter != myConstraintMap.end(); aCIter++)
526 if (aCIter->first->data() && aCIter->first->data()->isValid())
527 aCIter->first->data()->blockSendAttributeUpdated(isBlocked);
529 std::map<FeaturePtr, EntityWrapperPtr>::const_iterator aFIter = myFeatureMap.begin();
530 for (; aFIter != myFeatureMap.end(); aFIter++)
531 if (aFIter->first->data() && aFIter->first->data()->isValid())
532 aFIter->first->data()->blockSendAttributeUpdated(isBlocked);
534 std::map<AttributePtr, EntityWrapperPtr>::const_iterator anAtIter = myAttributeMap.begin();
535 for (; anAtIter != myAttributeMap.end(); anAtIter++)
536 if (anAtIter->first->owner() && anAtIter->first->owner()->data() &&
537 anAtIter->first->owner()->data()->isValid())
538 anAtIter->first->owner()->data()->blockSendAttributeUpdated(isBlocked);
539 myEventsBlocked = isBlocked;
547 // ============== Auxiliary functions ====================================
548 bool isEqual(const std::list<ConstraintWrapperPtr>& theCVec1,
549 const std::list<ConstraintWrapperPtr>& theCVec2)
551 if (theCVec1.size() != theCVec2.size())
554 std::list<bool> aChecked(theCVec2.size(), false);
555 std::list<ConstraintWrapperPtr>::const_iterator anIt1 = theCVec1.begin();
556 for (; anIt1 != theCVec1.end(); ++anIt1) {
557 std::list<ConstraintWrapperPtr>::const_iterator anIt2 = theCVec2.begin();
558 std::list<bool>::iterator aCheckIt = aChecked.begin();
559 while (aCheckIt != aChecked.end() && *aCheckIt) {
563 for (; anIt2 != theCVec2.end(); ++anIt2, ++aCheckIt)
564 if (!(*aCheckIt) && (*anIt1)->isEqual(*anIt2)) {
568 // the same constraint is not found
569 if (anIt2 == theCVec2.end())