Salome HOME
Make naming name works on bodies with additional prefixes (extrusion solid in the...
[modules/shaper.git] / src / Model / Model_AttributeSelectionList.cpp
index 85f522a6e44ebb1dc3fa019da73c62398a2c9b58..e97a2ee4918331235f52a227afc75931a4e2f9fb 100644 (file)
 #include <TDF_ChildIterator.hxx>
 #include <TDF_RelocationTable.hxx>
 #include <TopAbs_ShapeEnum.hxx>
-
 #include <TopoDS.hxx>
 #include <TopoDS_Shape.hxx>
 #include <TopoDS_Edge.hxx>
 #include <BRep_Tool.hxx>
+#include <TNaming_Builder.hxx>
+#include <TNaming_Iterator.hxx>
 
 using namespace std;
 
@@ -34,6 +35,10 @@ void Model_AttributeSelectionList::append(
     }
   }
 
+  if (myIsCashed && !theTemporarily) {
+    myCash[theContext].push_back(theSubShape);
+  }
+
   int aNewTag = mySize->Get() + 1;
   TDF_Label aNewLab = mySize->Label().FindChild(aNewTag);
 
@@ -85,9 +90,6 @@ void Model_AttributeSelectionList::removeLast()
   }
 }
 
-#include <TNaming_Builder.hxx>
-#include <TNaming_Iterator.hxx>
-
 // copies named shapes: we need the implementation of this 
 static void CopyNS(const Handle(TNaming_NamedShape)& theFrom,
   const Handle(TDF_Attribute)& theTo)
@@ -189,11 +191,39 @@ bool Model_AttributeSelectionList::isInList(const ResultPtr& theContext,
                                             const std::shared_ptr<GeomAPI_Shape>& theSubShape,
                                             const bool theTemporarily)
 {
+  if (myIsCashed) { // the cashing is active
+    std::map<ResultPtr, std::list<std::shared_ptr<GeomAPI_Shape> > >::iterator aContext =
+      myCash.find(theContext);
+    if (aContext != myCash.end()) {
+      // iterate shapes because "isEqual" method must be called for each shape
+      std::list<std::shared_ptr<GeomAPI_Shape> >::iterator aShapes = aContext->second.begin();
+      for(; aShapes != aContext->second.end(); aShapes++) {
+        if (!theSubShape.get()) {
+          if (!aShapes->get())
+            return true;
+        } else {
+          if (theSubShape->isEqual(*aShapes))
+            return true;
+        }
+      }
+    }
+    return false;
+  }
+  // no-cash method
   for(int anIndex = size() - 1; anIndex >= 0; anIndex--) {
     AttributeSelectionPtr anAttr = value(anIndex);
-    if (anAttr.get() && anAttr->value().get()) {
-      if (anAttr->context() == theContext && anAttr->value()->isEqual(theSubShape))
-        return true;
+    if (anAttr.get()) {
+      if (anAttr->context() == theContext) { // contexts are equal, so, check that values are also
+        std::shared_ptr<GeomAPI_Shape> aValue = anAttr->value();
+        if (!aValue.get()) {
+          if (!theSubShape.get()) { // both are null
+            return true;
+          }
+        } else {
+          if (aValue->isEqual(theSubShape)) // shapes are equal
+            return true;
+        }
+      }
     }
   }
   return false;
@@ -265,4 +295,19 @@ Model_AttributeSelectionList::Model_AttributeSelectionList(TDF_Label& theLabel)
   } else { // recollect mySubs
     theLabel.FindAttribute(TDataStd_Comment::GetID(), mySelectionType);
   }
+  myIsCashed = false;
+}
+
+void Model_AttributeSelectionList::cashValues(const bool theEnabled)
+{
+  myIsCashed = theEnabled;
+  myCash.clear(); // empty list as indicator that cash is not used
+  if (theEnabled) {
+    for(int anIndex = size() - 1; anIndex >= 0; anIndex--) {
+      AttributeSelectionPtr anAttr = value(anIndex);
+      if (anAttr.get()) {
+        myCash[anAttr->context()].push_back(anAttr->value());
+      }
+    }
+  }
 }