+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;
+ }
+ }
+ return isFullyRemoved;
+}
+
+template <class ENT_TYPE>
+static bool isUsed(ConstraintWrapperPtr theConstraint, ENT_TYPE theEntity)
+{
+ if (!theConstraint || !theEntity)
+ return false;
+ 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)
+{
+ if (!theFeature || !theSubEntity)
+ return false;
+ std::list<EntityWrapperPtr>::const_iterator aSubIt = theFeature->subEntities().begin();
+ for (; aSubIt != theFeature->subEntities().end(); ++aSubIt)
+ if ((*aSubIt)->isBase(theSubEntity))
+ return true;
+ return false;
+}
+
+static bool isUsed(ConstraintPtr theConstraint, AttributePtr theAttribute)
+{
+ if (!theConstraint || !theAttribute)
+ return false;
+ std::list<AttributePtr> anAttrList = theConstraint->data()->attributes(std::string());
+ std::list<AttributePtr>::const_iterator anIt = anAttrList.begin();
+ for (; anIt != anAttrList.end(); ++anIt) {
+ if (*anIt == theAttribute)
+ return true;
+ AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anIt);
+ if (aRefAttr && !aRefAttr->isObject() && aRefAttr->attr() == theAttribute)
+ return true;
+ }
+ return false;
+}
+
+bool SketchSolver_Storage::isUsed(FeaturePtr theFeature) const
+{
+ if (myFeatureMap.find(theFeature) != myFeatureMap.end())
+ return true;
+ // check constraints
+ 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 = pointAttributes(theFeature);
+ 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();
+ }
+
+ if (myAttributeMap.find(theAttribute) != myAttributeMap.end())
+ return true;
+ // check in constraints
+ 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;
+ // Additional check for the Fixed constraints, which have no wrapper associated.
+ if (aCIt->first->getKind() == SketchPlugin_ConstraintRigid::ID() &&
+ ::isUsed(aCIt->first, anAttribute))
+ return true;
+ }
+ // check in features
+ std::map<FeaturePtr, EntityWrapperPtr>::const_iterator aFIt = myFeatureMap.begin();
+ for (; aFIt != myFeatureMap.end(); ++aFIt)
+ if (::isUsed(aFIt->second, 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 true; // feature not found, nothing to delete
+
+ EntityWrapperPtr anEntity = aFound->second;
+ myFeatureMap.erase(aFound);
+
+ // Check if the feature is not used by constraints, remove it
+ if (!anEntity || (!isUsed(theFeature) && remove(anEntity)))
+ return true;
+
+ // feature is not removed, revert operation
+ myFeatureMap[theFeature] = anEntity;
+ update(anEntity);
+ return false;
+}
+
+bool SketchSolver_Storage::removeEntity(AttributePtr theAttribute)
+{
+ std::map<AttributePtr, EntityWrapperPtr>::iterator aFound = myAttributeMap.find(theAttribute);
+ if (aFound == myAttributeMap.end())
+ return true; // attribute not found, nothing to delete
+
+ EntityWrapperPtr anEntity = aFound->second;
+ myAttributeMap.erase(aFound);
+
+ // Check if the attribute is not used by constraints and features, remove it
+ if (!anEntity || (!isUsed(theAttribute) && remove(anEntity)))
+ return true;
+
+ // attribute is not removed, revert operation
+ myAttributeMap[theAttribute] = anEntity;
+ update(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;
+}
+
+