Salome HOME
Issue #1764: The result of partition is not stored in a folder
[modules/shaper.git] / src / Model / Model_AttributeSelectionList.cpp
index a51cfae979f8d7f83a4bebff838092a453d510ab..0dc8d7bd6fda2d74a65c950082795c11004c2b4e 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>
+#include <NCollection_List.hxx>
 
 using namespace std;
 
@@ -34,6 +36,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,6 +91,52 @@ void Model_AttributeSelectionList::removeLast()
   }
 }
 
+// copies named shapes: we need the implementation of this 
+static void CopyNS(const Handle(TNaming_NamedShape)& theFrom,
+  const Handle(TDF_Attribute)& theTo)
+{ 
+  TDF_Label aLab = theTo->Label();
+  TNaming_Builder B(aLab);
+
+  // TNaming_Iterator iterates in reversed way than it was put in Builder, so, keep it in container
+  // and iterate from end to begin
+  NCollection_List<TopoDS_Shape> aOlds;
+  NCollection_List<TopoDS_Shape> aNews;
+  NCollection_List<TNaming_Evolution> aStatuses;
+
+  TNaming_Iterator anIt (theFrom);
+  for ( ;anIt.More() ; anIt.Next()) {
+    aOlds.Prepend(anIt.OldShape());
+    aNews.Prepend(anIt.NewShape());
+    aStatuses.Prepend(anIt.Evolution());
+  }
+
+  NCollection_List<TopoDS_Shape>::Iterator aOldIter(aOlds);
+  NCollection_List<TopoDS_Shape>::Iterator aNewIter(aNews);
+  NCollection_List<TNaming_Evolution>::Iterator aStatIter(aStatuses);
+  for(; aOldIter.More(); aOldIter.Next(), aNewIter.Next(), aStatIter.Next()) {
+    switch (aStatIter.Value()) {
+    case TNaming_PRIMITIVE :
+      B.Generated(aNewIter.Value());
+      break;
+    case TNaming_GENERATED :
+      B.Generated(aOldIter.Value(), aNewIter.Value());
+      break;
+    case TNaming_MODIFY : 
+      B.Modify(aOldIter.Value(), aNewIter.Value());
+      break;
+    case TNaming_DELETE : 
+      B.Delete (aOldIter.Value());
+      break;
+    case TNaming_SELECTED :
+      B.Select(aNewIter.Value(), aOldIter.Value());
+      break;
+    default:
+      break;
+    }
+  }
+}
+
 /// makes copy of all attributes on the given label and all sub-labels
 static void copyAttrs(TDF_Label theSource, TDF_Label theDestination) {
   TDF_AttributeIterator anAttrIter(theSource);
@@ -95,8 +147,14 @@ static void copyAttrs(TDF_Label theSource, TDF_Label theDestination) {
            aTargetAttr = anAttrIter.Value()->NewEmpty();
       theDestination.AddAttribute(aTargetAttr);
     }
-    Handle(TDF_RelocationTable) aRelocTable = new TDF_RelocationTable(); // no relocation, empty map
-    anAttrIter.Value()->Paste(aTargetAttr, aRelocTable);
+    // for named shape copy exact shapes (in NamedShape Paste method the CopyTool is used)
+    Handle(TNaming_NamedShape) aNS = Handle(TNaming_NamedShape)::DownCast(anAttrIter.Value());
+    if (aNS.IsNull()) {
+      Handle(TDF_RelocationTable) aRelocTable = new TDF_RelocationTable(); // no relocation, empty map
+      anAttrIter.Value()->Paste(aTargetAttr, aRelocTable);
+    } else {
+      CopyNS(aNS, aTargetAttr);
+    }
   }
   // copy the sub-labels content
   TDF_ChildIterator aSubLabsIter(theSource);
@@ -145,11 +203,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;
@@ -214,11 +300,33 @@ bool Model_AttributeSelectionList::isInitialized()
 
 Model_AttributeSelectionList::Model_AttributeSelectionList(TDF_Label& theLabel)
 {
-  myIsInitialized = theLabel.FindAttribute(TDataStd_Integer::GetID(), mySize) == Standard_True;
+  myLab = theLabel;
+  reinit();
+}
+
+void Model_AttributeSelectionList::reinit()
+{
+  myIsInitialized = myLab.FindAttribute(TDataStd_Integer::GetID(), mySize) == Standard_True;
   if (!myIsInitialized) {
-    mySize = TDataStd_Integer::Set(theLabel, 0);
-    mySelectionType = TDataStd_Comment::Set(theLabel, "");
+    mySize = TDataStd_Integer::Set(myLab, 0);
+    mySelectionType = TDataStd_Comment::Set(myLab, "");
   } else { // recollect mySubs
-    theLabel.FindAttribute(TDataStd_Comment::GetID(), mySelectionType);
+    myLab.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());
+      }
+    }
   }
 }