+bool SketchSolver_Storage::removeConstraint(ConstraintPtr theConstraint)
+{
+ std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::iterator
+ aFound = myConstraintMap.find(theConstraint);
+ if (aFound == myConstraintMap.end())
+ return true; // no constraint, already deleted
+
+ // Remove constraint
+ std::list<ConstraintWrapperPtr> aConstrList = aFound->second;
+ myConstraintMap.erase(aFound);
+ // Remove SolveSpace constraints
+ bool isFullyRemoved = true;
+ std::list<ConstraintWrapperPtr>::iterator anIt = aConstrList.begin();
+ while (anIt != aConstrList.end()) {
+ if (remove(*anIt)) {
+ std::list<ConstraintWrapperPtr>::iterator aRemoveIt = anIt++;
+ aConstrList.erase(aRemoveIt);
+ } else {
+ isFullyRemoved = false;
+ ++anIt;
+ }
+ }
+ if (!isFullyRemoved)
+ myConstraintMap[theConstraint] = aConstrList;
+ return isFullyRemoved;
+}
+
+template <class ENT_TYPE>
+static bool isUsed(ConstraintWrapperPtr theConstraint, ENT_TYPE theEntity)
+{
+ std::list<EntityWrapperPtr>::const_iterator anEntIt = theConstraint->entities().begin();
+ for (; anEntIt != theConstraint->entities().end(); ++anEntIt)
+ if ((*anEntIt)->isBase(theEntity))
+ return true;
+ return false;
+}
+
+static bool isUsed(EntityWrapperPtr theFeature, AttributePtr theSubEntity)
+{
+ std::list<EntityWrapperPtr>::const_iterator aSubIt = theFeature->subEntities().begin();
+ for (; aSubIt != theFeature->subEntities().end(); ++aSubIt)
+ if ((*aSubIt)->isBase(theSubEntity))
+ return true;
+ return false;
+}
+
+bool SketchSolver_Storage::isUsed(FeaturePtr theFeature) const
+{
+ std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
+ aCIt = myConstraintMap.begin();
+ std::list<ConstraintWrapperPtr>::const_iterator aCWIt;
+ for (; aCIt != myConstraintMap.end(); ++aCIt)
+ for (aCWIt = aCIt->second.begin(); aCWIt != aCIt->second.end(); ++aCWIt)
+ if (::isUsed(*aCWIt, theFeature))
+ return true;
+ // check attributes
+ std::list<AttributePtr> anAttrList = theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
+ std::list<AttributePtr>::const_iterator anIt = anAttrList.begin();
+ for (; anIt != anAttrList.end(); ++anIt)
+ if (isUsed(*anIt))
+ return true;
+ return false;
+}
+
+bool SketchSolver_Storage::isUsed(AttributePtr theAttribute) const
+{
+ AttributePtr anAttribute = theAttribute;
+ AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
+ if (aRefAttr) {
+ if (aRefAttr->isObject())
+ return isUsed(ModelAPI_Feature::feature(aRefAttr->object()));
+ else
+ anAttribute = aRefAttr->attr();
+ }
+
+ std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
+ aCIt = myConstraintMap.begin();
+ std::list<ConstraintWrapperPtr>::const_iterator aCWIt;
+ for (; aCIt != myConstraintMap.end(); ++aCIt)
+ for (aCWIt = aCIt->second.begin(); aCWIt != aCIt->second.end(); ++aCWIt)
+ if (::isUsed(*aCWIt, anAttribute))
+ return true;
+ return false;
+}
+
+
+bool SketchSolver_Storage::removeEntity(FeaturePtr theFeature)
+{
+ std::map<FeaturePtr, EntityWrapperPtr>::iterator aFound = myFeatureMap.find(theFeature);
+ if (aFound == myFeatureMap.end())
+ return false; // feature not found, nothing to delete
+
+ // Check the feature is not used by constraints
+ if (isUsed(theFeature))
+ return false; // the feature is used, don't remove it
+
+ // Remove feature
+ EntityWrapperPtr anEntity = aFound->second;
+ myFeatureMap.erase(aFound);
+ if (remove(anEntity))
+ return true;
+ // feature is not removed, revert operation
+ myFeatureMap[theFeature] = anEntity;
+ return false;
+}
+
+bool SketchSolver_Storage::removeEntity(AttributePtr theAttribute)
+{
+ std::map<AttributePtr, EntityWrapperPtr>::iterator aFound = myAttributeMap.find(theAttribute);
+ if (aFound == myAttributeMap.end())
+ return false; // attribute not found, nothing to delete
+
+ // Check the attribute is not used by constraints
+ if (isUsed(theAttribute))
+ return false; // the attribute is used, don't remove it
+ // Check the attribute is not used by other features
+ std::map<FeaturePtr, EntityWrapperPtr>::const_iterator aFIt = myFeatureMap.begin();
+ for (; aFIt != myFeatureMap.end(); ++aFIt)
+ if (::isUsed(aFIt->second, theAttribute)) // the attribute is used, don't remove it
+ return false;
+
+ // Remove attribute
+ EntityWrapperPtr anEntity = aFound->second;
+ myAttributeMap.erase(aFound);
+ if (remove(anEntity))
+ return true;
+ // attribute is not removed, revert operation
+ myAttributeMap[theAttribute] = anEntity;
+ return false;
+}
+
+
+bool SketchSolver_Storage::remove(ConstraintWrapperPtr theConstraint)
+{
+ bool isFullyRemoved = true;
+ std::list<EntityWrapperPtr>::const_iterator anIt = theConstraint->entities().begin();
+ for (; anIt != theConstraint->entities().end(); ++anIt) {
+ FeaturePtr aBaseFeature = (*anIt)->baseFeature();
+ if (aBaseFeature)
+ isFullyRemoved = SketchSolver_Storage::removeEntity(aBaseFeature) && isFullyRemoved;
+ else
+ isFullyRemoved = SketchSolver_Storage::removeEntity((*anIt)->baseAttribute()) && isFullyRemoved;
+ }
+ return isFullyRemoved;
+}
+
+bool SketchSolver_Storage::remove(EntityWrapperPtr theEntity)
+{
+ bool isFullyRemoved = true;
+ std::list<EntityWrapperPtr>::const_iterator anEntIt = theEntity->subEntities().begin();
+ for (; anEntIt != theEntity->subEntities().end(); ++anEntIt) {
+ FeaturePtr aBaseFeature = (*anEntIt)->baseFeature();
+ if (aBaseFeature)
+ isFullyRemoved = SketchSolver_Storage::removeEntity(aBaseFeature) && isFullyRemoved;
+ else
+ isFullyRemoved = SketchSolver_Storage::removeEntity((*anEntIt)->baseAttribute()) && isFullyRemoved;
+ }
+
+ std::list<ParameterWrapperPtr>::const_iterator aParIt = theEntity->parameters().begin();
+ for (; aParIt != theEntity->parameters().end(); ++aParIt)
+ isFullyRemoved = remove(*aParIt) && isFullyRemoved;
+ return isFullyRemoved;
+}
+
+