Salome HOME
"Conflicting constraints" should appear when a center of fillet is coincident with...
[modules/shaper.git] / src / SketchSolver / SketchSolver_Storage.cpp
index eecc86653a9c66fc4041ba9c3eb28ce5602686ce..f5617bbea6d11168816e2f8e720bf191c313121d 100644 (file)
@@ -9,6 +9,7 @@
 
 #include <GeomDataAPI_Point2D.h>
 #include <ModelAPI_AttributeRefAttr.h>
+#include <ModelAPI_AttributeRefList.h>
 #include <SketchPlugin_Arc.h>
 #include <SketchPlugin_Circle.h>
 
@@ -22,8 +23,11 @@ static bool isEqual(const std::list<ConstraintWrapperPtr>& theCVec1,
 void SketchSolver_Storage::addConstraint(ConstraintPtr        theConstraint,
                                          ConstraintWrapperPtr theSolverConstraint)
 {
-  std::list<ConstraintWrapperPtr> aConstrList(1, theSolverConstraint);
-  addConstraint(theConstraint, aConstrList);
+  if (theSolverConstraint) {
+    std::list<ConstraintWrapperPtr> aConstrList(1, theSolverConstraint);
+    addConstraint(theConstraint, aConstrList);
+  } else
+    addConstraint(theConstraint, std::list<ConstraintWrapperPtr>());
 }
 
 void SketchSolver_Storage::addConstraint(
@@ -35,35 +39,80 @@ void SketchSolver_Storage::addConstraint(
   if (aFound == myConstraintMap.end() || !isEqual(aFound->second, theSolverConstraints))
     setNeedToResolve(true);
 
-  // Do not add point-point coincidence, because it is already made by setting
-  // the same parameters for both points
-  if (!theSolverConstraints.empty() &&
-      theSolverConstraints.front()->type() != CONSTRAINT_PT_PT_COINCIDENT) {
+  if (theSolverConstraints.empty()) {
+    // constraint links to the empty list, add its attributes linked to the empty entities
+    std::list<AttributePtr> aRefAttrs =
+        theConstraint->data()->attributes(ModelAPI_AttributeRefAttr::typeId());
+    std::list<AttributePtr>::const_iterator anAttrIt = aRefAttrs.begin();
+    for (; anAttrIt != aRefAttrs.end(); ++anAttrIt) {
+      AttributeRefAttrPtr aRef = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttrIt);
+      if (aRef->isObject()) {
+        FeaturePtr aFeature = ModelAPI_Feature::feature(aRef->object());
+        if (aFeature) addEntity(aFeature, EntityWrapperPtr());
+      } else
+        addEntity(aRef->attr(), EntityWrapperPtr());
+    }
+    std::list<AttributePtr> aRefLists =
+        theConstraint->data()->attributes(ModelAPI_AttributeRefList::typeId());
+    for (anAttrIt = aRefLists.begin(); anAttrIt != aRefLists.end(); ++anAttrIt) {
+      AttributeRefListPtr aRef = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(*anAttrIt);
+      std::list<ObjectPtr> anObj = aRef->list();
+      std::list<ObjectPtr>::iterator anIt = anObj.begin();
+      for (; anIt != anObj.end(); ++anIt) {
+        FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
+        if (aFeature) addEntity(aFeature, EntityWrapperPtr());
+      }
+    }
+  }
+  else if (theSolverConstraints.front()->type() != CONSTRAINT_PT_PT_COINCIDENT) {
+    // Do not add point-point coincidence, because it is already made by setting
+    // the same parameters for both points
     std::list<ConstraintWrapperPtr>::iterator aCIt = theSolverConstraints.begin();
     for (; aCIt != theSolverConstraints.end(); ++aCIt)
       update(*aCIt);
   }
   myConstraintMap[theConstraint] = theSolverConstraints;
+  // block events if necessary
+  if (myEventsBlocked && theConstraint->data() && theConstraint->data()->isValid())
+    theConstraint->data()->blockSendAttributeUpdated(myEventsBlocked);
 }
 
 void SketchSolver_Storage::addEntity(FeaturePtr       theFeature,
                                      EntityWrapperPtr theSolverEntity)
 {
   std::map<FeaturePtr, EntityWrapperPtr>::const_iterator aFound = myFeatureMap.find(theFeature);
-  if (aFound == myFeatureMap.end() || !aFound->second->isEqual(theSolverEntity))
+  if (aFound == myFeatureMap.end() || !aFound->second ||
+     (theSolverEntity && !aFound->second->isEqual(theSolverEntity)))
     setNeedToResolve(true); // the entity is new or modified
 
+  if (!theSolverEntity) {
+    // feature links to the empty entity, add its attributes
+    std::list<AttributePtr> aPntAttrs =
+        theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
+    std::list<AttributePtr>::const_iterator anAttrIt = aPntAttrs.begin();
+    for (; anAttrIt != aPntAttrs.end(); ++anAttrIt)
+        addEntity(*anAttrIt, EntityWrapperPtr());
+  }
+
   myFeatureMap[theFeature] = theSolverEntity;
+  // block events if necessary
+  if (myEventsBlocked && theFeature->data() && theFeature->data()->isValid())
+    theFeature->data()->blockSendAttributeUpdated(myEventsBlocked);
 }
 
 void SketchSolver_Storage::addEntity(AttributePtr     theAttribute,
                                      EntityWrapperPtr theSolverEntity)
 {
   std::map<AttributePtr, EntityWrapperPtr>::const_iterator aFound = myAttributeMap.find(theAttribute);
-  if (aFound == myAttributeMap.end() || !aFound->second->isEqual(theSolverEntity))
+  if (aFound == myAttributeMap.end() || !aFound->second ||
+     (theSolverEntity && !aFound->second->isEqual(theSolverEntity)))
     setNeedToResolve(true); // the entity is new or modified
 
   myAttributeMap[theAttribute] = theSolverEntity;
+  // block events if necessary
+  if (myEventsBlocked && theAttribute->owner() &&
+      theAttribute->owner()->data() && theAttribute->owner()->data()->isValid())
+    theAttribute->owner()->data()->blockSendAttributeUpdated(myEventsBlocked);
 }
 
 
@@ -73,6 +122,8 @@ bool SketchSolver_Storage::update(FeaturePtr theFeature, const GroupID& theGroup
   EntityWrapperPtr aRelated = entity(theFeature);
   if (!aRelated) { // Feature is not exist, create it
     std::list<EntityWrapperPtr> aSubs;
+    // Reserve the feature in the map of features (do not want to add several copies of it)
+    myFeatureMap[theFeature] = aRelated;
     // Firstly, create/update its attributes
     std::list<AttributePtr> anAttrs =
         theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
@@ -95,7 +146,8 @@ bool SketchSolver_Storage::update(FeaturePtr theFeature, const GroupID& theGroup
     }
     // Secondly, convert feature
     BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
-    aRelated = aBuilder->createFeature(theFeature, aSubs, theGroup);
+    GroupID aGroup = theGroup != GID_UNKNOWN ? theGroup : myGroupID;
+    aRelated = aBuilder->createFeature(theFeature, aSubs, aGroup);
     if (!aRelated)
       return false;
     addEntity(theFeature, aRelated);
@@ -118,8 +170,23 @@ bool SketchSolver_Storage::update(AttributePtr theAttribute, const GroupID& theG
 
   EntityWrapperPtr aRelated = entity(anAttribute);
   if (!aRelated) { // Attribute is not exist, create it
+    // verify the attribute is a point of arc and add whole arc
+    if (anAttribute->owner()) {
+      FeaturePtr aFeature = ModelAPI_Feature::feature(anAttribute->owner());
+      if (aFeature->getKind() == SketchPlugin_Arc::ID() &&
+          myFeatureMap.find(aFeature) == myFeatureMap.end()) {
+        // Additional checking that all attributes are initialized
+        if (aFeature->attribute(SketchPlugin_Arc::CENTER_ID())->isInitialized() && 
+            aFeature->attribute(SketchPlugin_Arc::START_ID())->isInitialized() && 
+            aFeature->attribute(SketchPlugin_Arc::END_ID())->isInitialized()) {
+////          myFeatureMap[aFeature] = EntityWrapperPtr();
+          return SketchSolver_Storage::update(aFeature);
+        }
+      }
+    }
     BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
-    aRelated = aBuilder->createAttribute(anAttribute, theGroup);
+    GroupID aGroup = theGroup != GID_UNKNOWN ? theGroup : myGroupID;
+    aRelated = aBuilder->createAttribute(anAttribute, aGroup);
     if (!aRelated)
       return false;
     addEntity(anAttribute, aRelated);
@@ -173,6 +240,173 @@ const EntityWrapperPtr& SketchSolver_Storage::entity(const AttributePtr& theAttr
   return aDummy;
 }
 
+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)
+{
+  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
+{
+  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 = 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();
+  }
+
+  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;
+  // 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 false; // 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 (!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 false; // 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 (!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;
+}
+
+
 bool SketchSolver_Storage::isInteract(const FeaturePtr& theFeature) const
 {
   if (!theFeature)
@@ -201,6 +435,19 @@ bool SketchSolver_Storage::isInteract(const AttributePtr& theAttribute) const
   if (!theAttribute)
     return false;
 
+  AttributeRefListPtr aRefList = 
+      std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
+  if (aRefList) {
+    std::list<ObjectPtr> anObjects = aRefList->list();
+    std::list<ObjectPtr>::iterator anObjIt = anObjects.begin();
+    for (; anObjIt != anObjects.end(); ++anObjIt) {
+      FeaturePtr aFeature = ModelAPI_Feature::feature(*anObjIt);
+      if (isInteract(aFeature))
+        return true;
+    }
+    return false;
+  }
+
   AttributeRefAttrPtr aRefAttr =
       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
   if (!aRefAttr)
@@ -228,6 +475,42 @@ bool SketchSolver_Storage::isConsistent() const
   return true;
 }
 
+bool SketchSolver_Storage::isFixed(EntityWrapperPtr theEntity) const
+{
+  if (theEntity->group() != myGroupID)
+    return true;
+  // no need additional checking for entities differ than point
+  if (theEntity->type() != ENTITY_POINT)
+    return false;
+
+  CoincidentPointsMap::const_iterator anIt = myCoincidentPoints.begin();
+  for (; anIt != myCoincidentPoints.end(); ++anIt)
+    if (anIt->first == theEntity || anIt->second.find(theEntity) != anIt->second.end()) {
+      if (anIt->first->group() != myGroupID)
+        return true;
+      std::set<EntityWrapperPtr>::const_iterator anEntIt = anIt->second.begin();
+      for (; anEntIt != anIt->second.end(); ++anEntIt)
+        if ((*anEntIt)->group() != myGroupID)
+          return true;
+    }
+
+  std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator aCIt = myConstraintMap.begin();
+  std::list<ConstraintWrapperPtr>::const_iterator aCWIt;
+  for (; aCIt != myConstraintMap.end(); ++aCIt) {
+    if (aCIt->second.empty())
+      continue;
+    aCWIt = aCIt->second.begin();
+    if ((*aCWIt)->type() != CONSTRAINT_FIXED)
+      continue;
+    for (; aCWIt != aCIt->second.end(); ++aCIt)
+      if ((theEntity->baseAttribute() && (*aCWIt)->isUsed(theEntity->baseAttribute())) ||
+          (theEntity->baseFeature() && (*aCWIt)->isUsed(theEntity->baseFeature())))
+        return true;
+  }
+
+  return false;
+}
+
 void SketchSolver_Storage::removeInvalidEntities()
 {
   // Remove invalid constraints
@@ -272,7 +555,7 @@ const EntityWrapperPtr& SketchSolver_Storage::sketch() const
 
   std::map<FeaturePtr, EntityWrapperPtr>::const_iterator aFIt = myFeatureMap.begin();
   for (; aFIt != myFeatureMap.end(); ++aFIt)
-    if (aFIt->second->type() == ENTITY_SKETCH)
+    if (aFIt->second && aFIt->second->type() == ENTITY_SKETCH)
       break;
   if (aFIt == myFeatureMap.end())
     return aDummySketch;
@@ -286,8 +569,11 @@ void SketchSolver_Storage::setSketch(const EntityWrapperPtr& theSketch)
   addEntity(FeaturePtr(), theSketch);
 }
 
-void SketchSolver_Storage::blockEvents(bool isBlocked) const
+void SketchSolver_Storage::blockEvents(bool isBlocked)
 {
+  if (isBlocked == myEventsBlocked)
+    return;
+
   std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
       aCIter = myConstraintMap.begin();
   for (; aCIter != myConstraintMap.end(); aCIter++)
@@ -304,6 +590,7 @@ void SketchSolver_Storage::blockEvents(bool isBlocked) const
     if (anAtIter->first->owner() && anAtIter->first->owner()->data() &&
         anAtIter->first->owner()->data()->isValid())
       anAtIter->first->owner()->data()->blockSendAttributeUpdated(isBlocked);
+  myEventsBlocked = isBlocked;
 }