Salome HOME
Remove unused entities in sketch solver while changing number of copies in multi...
authorazv <azv@opencascade.com>
Thu, 2 Jul 2015 05:40:38 +0000 (08:40 +0300)
committerazv <azv@opencascade.com>
Thu, 2 Jul 2015 12:40:11 +0000 (15:40 +0300)
src/SketchSolver/SketchSolver_ConstraintMultiRotation.cpp
src/SketchSolver/SketchSolver_ConstraintMultiTranslation.cpp
src/SketchSolver/SketchSolver_Storage.cpp
src/SketchSolver/SketchSolver_Storage.h

index 0e8ec23d6584d6c5efd5bbabdda41437362466c4..0ceaee69066ce7e0f2c335518a61398c8b8fbafa 100644 (file)
@@ -230,6 +230,9 @@ bool SketchSolver_ConstraintMultiRotation::remove(ConstraintPtr theConstraint)
   std::map<FeaturePtr, Slvs_hEntity>::iterator aFeatIt = myFeatureMap.begin();
   for (; aFeatIt != myFeatureMap.end(); aFeatIt++)
     myStorage->removeEntity(aFeatIt->second);
+  myStorage->removeUnusedEntities();
+
+  std::map<FeaturePtr, Slvs_hEntity> aFeatureMapCopy = myFeatureMap;
 
   if (isFullyRemoved) {
     myFeatureMap.clear();
@@ -237,6 +240,18 @@ bool SketchSolver_ConstraintMultiRotation::remove(ConstraintPtr theConstraint)
     myValueMap.clear();
   } else
     cleanRemovedEntities();
+
+  // Restore initial features
+  std::map<FeaturePtr, Slvs_hEntity>::iterator aFIter = aFeatureMapCopy.begin();
+  for (; aFIter != aFeatureMapCopy.end(); ++aFIter)
+  {
+    if (myFeatureMap.find(aFIter->first) != myFeatureMap.end())
+      continue; // the feature was not removed
+    Slvs_hEntity anEntity = myGroup->getFeatureId(aFIter->first);
+    if (anEntity != SLVS_E_UNKNOWN)
+      myFeatureMap[aFIter->first] = anEntity;
+  }
+
   return true;
 }
 
@@ -263,7 +278,9 @@ void SketchSolver_ConstraintMultiRotation::adjustConstraint()
       if (squareDistance(myStorage, aLine.point[0], aLine.point[1]) < tolerance * tolerance) {
         myStorage->removeConstraint(aConstraint.h);
         isFirstRemoved = aConstr == mySlvsConstraints.begin();
-        std::vector<Slvs_hConstraint>::iterator aTmpIter = aConstr--;
+        std::vector<Slvs_hConstraint>::iterator aTmpIter = aConstr;
+        if (!isFirstRemoved)
+          --aConstr;
         mySlvsConstraints.erase(aTmpIter);
       }
       // Store the lines into the map
index a90354a84a5ef072c0cff929c0c918f1d92371cd..8dba381899f1a3bdf2d366825a0061dfdf3f5591 100644 (file)
@@ -199,12 +199,26 @@ bool SketchSolver_ConstraintMultiTranslation::remove(ConstraintPtr theConstraint
   for (; aFeatIt != myFeatureMap.end(); aFeatIt++)
     myStorage->removeEntity(aFeatIt->second);
 
+  std::map<FeaturePtr, Slvs_hEntity> aFeatureMapCopy = myFeatureMap;
+
   if (isFullyRemoved) {
     myFeatureMap.clear();
     myAttributeMap.clear();
     myValueMap.clear();
   } else
     cleanRemovedEntities();
+
+  // Restore initial features
+  std::map<FeaturePtr, Slvs_hEntity>::iterator aFIter = aFeatureMapCopy.begin();
+  for (; aFIter != aFeatureMapCopy.end(); ++aFIter)
+  {
+    if (myFeatureMap.find(aFIter->first) != myFeatureMap.end())
+      continue; // the feature was not removed
+    Slvs_hEntity anEntity = myGroup->getFeatureId(aFIter->first);
+    if (anEntity != SLVS_E_UNKNOWN)
+      myFeatureMap[aFIter->first] = anEntity;
+  }
+
   return true;
 }
 
index 2dca4423378b43407226d1eea5960a22c6d3fd4b..dade8f43fe81c1bd27e133c920076f3425beeec3 100644 (file)
@@ -187,6 +187,65 @@ bool SketchSolver_Storage::removeEntity(const Slvs_hEntity& theEntityID)
   return aResult;
 }
 
+void SketchSolver_Storage::removeUnusedEntities()
+{
+  std::set<Slvs_hEntity> anUnusedEntities;
+  std::vector<Slvs_Entity>::const_iterator aEIt = myEntities.begin();
+  for (; aEIt != myEntities.end(); ++aEIt) {
+    if (aEIt->h == aEIt->wrkpl) {
+      // don't remove workplane
+      anUnusedEntities.erase(aEIt->point[0]);
+      anUnusedEntities.erase(aEIt->normal);
+      continue;
+    }
+    anUnusedEntities.insert(aEIt->h);
+  }
+
+  std::vector<Slvs_Constraint>::const_iterator aCIt = myConstraints.begin();
+  for (; aCIt != myConstraints.end(); ++aCIt) {
+    Slvs_hEntity aSubs[6] = {
+        aCIt->entityA, aCIt->entityB,
+        aCIt->entityC, aCIt->entityD,
+        aCIt->ptA,     aCIt->ptB};
+    for (int i = 0; i < 6; i++) {
+      if (aSubs[i] != SLVS_E_UNKNOWN) {
+        anUnusedEntities.erase(aSubs[i]);
+        int aPos = Search(aSubs[i], myEntities);
+        if (aPos >= 0 && aPos < (int)myEntities.size()) {
+          for (int j = 0; j < 4; j++)
+            if (myEntities[aPos].point[j] != 0)
+              anUnusedEntities.erase(myEntities[aPos].point[j]);
+        }
+      }
+    }
+  }
+
+  std::set<Slvs_hEntity>::const_iterator anEntIt = anUnusedEntities.begin();
+  for (; anEntIt != anUnusedEntities.end(); ++anEntIt) {
+    int aPos = Search(*anEntIt, myEntities);
+    if (aPos >= 0 && aPos < (int)myEntities.size()) {
+      // Remove entity and its parameters
+      Slvs_Entity anEntity = myEntities[aPos];
+      myEntities.erase(myEntities.begin() + aPos);
+      myEntityMaxID = myEntities.empty() ? SLVS_E_UNKNOWN : myEntities.back().h;
+      if (anEntity.distance != SLVS_E_UNKNOWN)
+        removeParameter(anEntity.distance);
+      for (int i = 0; i < 4; i++)
+        if (anEntity.param[i] != SLVS_E_UNKNOWN)
+          removeParameter(anEntity.param[i]);
+      for (int i = 0; i < 4; i++)
+        if (anEntity.point[i] != SLVS_E_UNKNOWN)
+          removeEntity(anEntity.point[i]);
+      myRemovedEntities.insert(*anEntIt);
+      if (anEntity.type == SLVS_E_POINT_IN_2D || anEntity.type == SLVS_E_POINT_IN_3D)
+        removeCoincidentPoint(*anEntIt);
+    }
+  }
+
+  if (!anUnusedEntities.empty())
+    myNeedToResolve = true;
+}
+
 const Slvs_Entity& SketchSolver_Storage::getEntity(const Slvs_hEntity& theEntityID) const
 {
   int aPos = Search(theEntityID, myEntities);
index 3167dd1578cc5dd9cdd242cef2de901ba469bb8d..3f965fca2d30bc1d516381275155a1fb9b18dd35 100644 (file)
@@ -58,6 +58,9 @@ public:
    *  \return \c true if the entity was successfully removed
    */
   bool removeEntity(const Slvs_hEntity& theEntityID);
+  /** \brief Remove all entities, which are not used in constraints
+   */
+  void removeUnusedEntities();
   /// \brief Returns the entity by its ID
   const Slvs_Entity& getEntity(const Slvs_hEntity& theEntityID) const;
   /// \brief Makes a full copy of the given entity