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 <SketchPlugin_Arc.h>
13 #include <SketchPlugin_Circle.h>
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);
22 void SketchSolver_Storage::addConstraint(ConstraintPtr theConstraint,
23 ConstraintWrapperPtr theSolverConstraint)
25 std::list<ConstraintWrapperPtr> aConstrList(1, theSolverConstraint);
26 addConstraint(theConstraint, aConstrList);
29 void SketchSolver_Storage::addConstraint(
30 ConstraintPtr theConstraint,
31 std::list<ConstraintWrapperPtr> theSolverConstraints)
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);
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)
46 myConstraintMap[theConstraint] = theSolverConstraints;
47 // block events if necessary
48 if (myEventsBlocked && theConstraint->data() && theConstraint->data()->isValid())
49 theConstraint->data()->blockSendAttributeUpdated(myEventsBlocked);
52 void SketchSolver_Storage::addEntity(FeaturePtr theFeature,
53 EntityWrapperPtr theSolverEntity)
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
59 myFeatureMap[theFeature] = theSolverEntity;
60 // block events if necessary
61 if (myEventsBlocked && theFeature->data() && theFeature->data()->isValid())
62 theFeature->data()->blockSendAttributeUpdated(myEventsBlocked);
65 void SketchSolver_Storage::addEntity(AttributePtr theAttribute,
66 EntityWrapperPtr theSolverEntity)
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
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);
80 bool SketchSolver_Storage::update(FeaturePtr theFeature, const GroupID& theGroup)
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));
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));
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);
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);
114 addEntity(theFeature, aRelated);
115 } else if (theGroup != GID_UNKNOWN)
116 changeGroup(aRelated, theGroup);
117 return update(aRelated) || isUpdated;
120 bool SketchSolver_Storage::update(AttributePtr theAttribute, const GroupID& theGroup)
122 AttributePtr anAttribute = theAttribute;
123 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
125 if (aRefAttr->isObject()) {
126 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
127 return update(aFeature, theGroup);
129 anAttribute = aRefAttr->attr();
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);
139 addEntity(anAttribute, aRelated);
140 } else if (theGroup != GID_UNKNOWN)
141 changeGroup(aRelated, theGroup);
142 return update(aRelated);
147 const std::list<ConstraintWrapperPtr>& SketchSolver_Storage::constraint(
148 const ConstraintPtr& theConstraint) const
150 static std::list<ConstraintWrapperPtr> aDummy;
152 std::map<ConstraintPtr, std::list<ConstraintWrapperPtr>>::const_iterator
153 aFound = myConstraintMap.find(theConstraint);
154 if (aFound != myConstraintMap.end())
155 return aFound->second;
159 const EntityWrapperPtr& SketchSolver_Storage::entity(const FeaturePtr& theFeature) const
161 static EntityWrapperPtr aDummy;
163 std::map<FeaturePtr, EntityWrapperPtr>::const_iterator aFound = myFeatureMap.find(theFeature);
164 if (aFound != myFeatureMap.end())
165 return aFound->second;
169 const EntityWrapperPtr& SketchSolver_Storage::entity(const AttributePtr& theAttribute) const
171 static EntityWrapperPtr aDummy;
173 std::map<AttributePtr, EntityWrapperPtr>::const_iterator
174 aFound = myAttributeMap.find(theAttribute);
175 if (aFound != myAttributeMap.end())
176 return aFound->second;
178 AttributeRefAttrPtr aRefAttr =
179 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
181 if (aRefAttr->isObject()) {
182 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
183 return entity(aFeature);
185 return entity(aRefAttr->attr());
190 bool SketchSolver_Storage::removeConstraint(ConstraintPtr theConstraint)
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
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()) {
205 std::list<ConstraintWrapperPtr>::iterator aRemoveIt = anIt++;
206 aConstrList.erase(aRemoveIt);
208 isFullyRemoved = false;
213 myConstraintMap[theConstraint] = aConstrList;
214 return isFullyRemoved;
217 template <class ENT_TYPE>
218 static bool isUsed(ConstraintWrapperPtr theConstraint, ENT_TYPE theEntity)
220 std::list<EntityWrapperPtr>::const_iterator anEntIt = theConstraint->entities().begin();
221 for (; anEntIt != theConstraint->entities().end(); ++anEntIt)
222 if ((*anEntIt)->isBase(theEntity))
227 static bool isUsed(EntityWrapperPtr theFeature, AttributePtr theSubEntity)
229 std::list<EntityWrapperPtr>::const_iterator aSubIt = theFeature->subEntities().begin();
230 for (; aSubIt != theFeature->subEntities().end(); ++aSubIt)
231 if ((*aSubIt)->isBase(theSubEntity))
236 bool SketchSolver_Storage::isUsed(FeaturePtr theFeature) const
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))
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)
254 bool SketchSolver_Storage::isUsed(AttributePtr theAttribute) const
256 AttributePtr anAttribute = theAttribute;
257 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
259 if (aRefAttr->isObject())
260 return isUsed(ModelAPI_Feature::feature(aRefAttr->object()));
262 anAttribute = aRefAttr->attr();
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))
276 bool SketchSolver_Storage::removeEntity(FeaturePtr theFeature)
278 std::map<FeaturePtr, EntityWrapperPtr>::iterator aFound = myFeatureMap.find(theFeature);
279 if (aFound == myFeatureMap.end())
280 return false; // feature not found, nothing to delete
282 // Check the feature is not used by constraints
283 if (isUsed(theFeature))
284 return false; // the feature is used, don't remove it
287 EntityWrapperPtr anEntity = aFound->second;
288 myFeatureMap.erase(aFound);
289 if (remove(anEntity))
291 // feature is not removed, revert operation
292 myFeatureMap[theFeature] = anEntity;
296 bool SketchSolver_Storage::removeEntity(AttributePtr theAttribute)
298 std::map<AttributePtr, EntityWrapperPtr>::iterator aFound = myAttributeMap.find(theAttribute);
299 if (aFound == myAttributeMap.end())
300 return false; // attribute not found, nothing to delete
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
312 EntityWrapperPtr anEntity = aFound->second;
313 myAttributeMap.erase(aFound);
314 if (remove(anEntity))
316 // attribute is not removed, revert operation
317 myAttributeMap[theAttribute] = anEntity;
322 bool SketchSolver_Storage::remove(ConstraintWrapperPtr theConstraint)
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();
329 isFullyRemoved = SketchSolver_Storage::removeEntity(aBaseFeature) && isFullyRemoved;
331 isFullyRemoved = SketchSolver_Storage::removeEntity((*anIt)->baseAttribute()) && isFullyRemoved;
333 return isFullyRemoved;
336 bool SketchSolver_Storage::remove(EntityWrapperPtr theEntity)
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();
343 isFullyRemoved = SketchSolver_Storage::removeEntity(aBaseFeature) && isFullyRemoved;
345 isFullyRemoved = SketchSolver_Storage::removeEntity((*anEntIt)->baseAttribute()) && isFullyRemoved;
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;
355 bool SketchSolver_Storage::isInteract(const FeaturePtr& theFeature) const
359 if (myConstraintMap.empty())
360 return true; // empty storage interacts with each feature
362 ConstraintPtr aConstraint = std::dynamic_pointer_cast<SketchPlugin_Constraint>(theFeature);
364 if (myConstraintMap.find(aConstraint) != myConstraintMap.end())
366 } else if (myFeatureMap.find(theFeature) != myFeatureMap.end())
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))
378 bool SketchSolver_Storage::isInteract(const AttributePtr& theAttribute) const
383 AttributeRefAttrPtr aRefAttr =
384 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
386 return myAttributeMap.find(theAttribute) != myAttributeMap.end();
387 if (!aRefAttr->isObject())
388 return myAttributeMap.find(aRefAttr->attr()) != myAttributeMap.end();
390 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
391 return isInteract(aFeature);
394 bool SketchSolver_Storage::isConsistent() const
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())
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())
410 bool SketchSolver_Storage::isFixed(EntityWrapperPtr theEntity) const
412 if (theEntity->group() != myGroupID)
414 // no need additional checking for entities differ than point
415 if (theEntity->type() != ENTITY_POINT)
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)
423 std::set<EntityWrapperPtr>::const_iterator anEntIt = anIt->second.begin();
424 for (; anEntIt != anIt->second.end(); ++anEntIt)
425 if ((*anEntIt)->group() != myGroupID)
431 void SketchSolver_Storage::removeInvalidEntities()
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);
454 EntityWrapperPtr SketchSolver_Storage::getNormal() const
456 EntityWrapperPtr aSketch = sketch();
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)
466 return EntityWrapperPtr();
469 const EntityWrapperPtr& SketchSolver_Storage::sketch() const
471 static EntityWrapperPtr aDummySketch;
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)
477 if (aFIt == myFeatureMap.end())
482 void SketchSolver_Storage::setSketch(const EntityWrapperPtr& theSketch)
486 addEntity(FeaturePtr(), theSketch);
489 void SketchSolver_Storage::blockEvents(bool isBlocked)
491 if (isBlocked == myEventsBlocked)
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);
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);
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;
518 // ============== Auxiliary functions ====================================
519 bool isEqual(const std::list<ConstraintWrapperPtr>& theCVec1,
520 const std::list<ConstraintWrapperPtr>& theCVec2)
522 if (theCVec1.size() != theCVec2.size())
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) {
534 for (; anIt2 != theCVec2.end(); ++anIt2, ++aCheckIt)
535 if (!(*aCheckIt) && (*anIt1)->isEqual(*anIt2)) {
539 // the same constraint is not found
540 if (anIt2 == theCVec2.end())