Salome HOME
A unit test for the issue #1799
[modules/shaper.git] / src / Model / Model_AttributeSelectionList.cpp
index 856db04473f8860fe7d6c5f7152ecba4d4a0ba10..72e830852a424de3f5f87461d1b33b22f8b0e95b 100644 (file)
@@ -20,8 +20,7 @@
 #include <BRep_Tool.hxx>
 #include <TNaming_Builder.hxx>
 #include <TNaming_Iterator.hxx>
-
-using namespace std;
+#include <NCollection_List.hxx>
 
 void Model_AttributeSelectionList::append(
     const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape,
@@ -42,17 +41,18 @@ void Model_AttributeSelectionList::append(
   int aNewTag = mySize->Get() + 1;
   TDF_Label aNewLab = mySize->Label().FindChild(aNewTag);
 
-  std::shared_ptr<Model_AttributeSelection> aNewAttr = 
+  std::shared_ptr<Model_AttributeSelection> aNewAttr =
     std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aNewLab));
   if (owner()) {
     aNewAttr->setObject(owner());
+    aNewAttr->setParent(this);
   }
   aNewAttr->setID(id());
   mySize->Set(aNewTag);
   aNewAttr->setValue(theContext, theSubShape, theTemporarily);
   if (theTemporarily)
     myTmpAttr = aNewAttr;
-  else 
+  else
     myTmpAttr.reset();
   owner()->data()->sendAttributeUpdated(this);
 }
@@ -63,10 +63,11 @@ void Model_AttributeSelectionList::append(
   int aNewTag = mySize->Get() + 1;
   TDF_Label aNewLab = mySize->Label().FindChild(aNewTag);
 
-  std::shared_ptr<Model_AttributeSelection> aNewAttr = 
+  std::shared_ptr<Model_AttributeSelection> aNewAttr =
     std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aNewLab));
   if (owner()) {
     aNewAttr->setObject(owner());
+    aNewAttr->setParent(this);
   }
   aNewAttr->setID(id());
   mySize->Set(aNewTag);
@@ -74,13 +75,20 @@ void Model_AttributeSelectionList::append(
   owner()->data()->sendAttributeUpdated(this);
 }
 
-void Model_AttributeSelectionList::removeLast() 
+void Model_AttributeSelectionList::removeTemporaryValues()
+{
+  if (myTmpAttr.get()) {
+    myTmpAttr.reset();
+  }
+}
+
+void Model_AttributeSelectionList::removeLast()
 {
   int anOldSize = mySize->Get();
   if (anOldSize != 0) {
     mySize->Set(anOldSize - 1);
     TDF_Label aLab = mySize->Label().FindChild(anOldSize);
-    std::shared_ptr<Model_AttributeSelection> aOldAttr = 
+    std::shared_ptr<Model_AttributeSelection> aOldAttr =
       std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLab));
     aOldAttr->setObject(owner());
     REMOVE_BACK_REF(aOldAttr->context());
@@ -90,34 +98,45 @@ void Model_AttributeSelectionList::removeLast()
   }
 }
 
-// copies named shapes: we need the implementation of this 
+// 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 It (theFrom);
-  for ( ;It.More() ; It.Next()) {
-    const TopoDS_Shape& OS     = It.OldShape();
-    const TopoDS_Shape& NS     = It.NewShape();
-    TNaming_Evolution   Status = It.Evolution();
+  // 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;
 
-    switch (Status) {
+  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(NS);
+      B.Generated(aNewIter.Value());
       break;
     case TNaming_GENERATED :
-      B.Generated(OS, NS);
+      B.Generated(aOldIter.Value(), aNewIter.Value());
       break;
-    case TNaming_MODIFY : 
-      B.Modify(OS, NS);
+    case TNaming_MODIFY :
+      B.Modify(aOldIter.Value(), aNewIter.Value());
       break;
-    case TNaming_DELETE : 
-      B.Delete (OS);
+    case TNaming_DELETE :
+      B.Delete (aOldIter.Value());
       break;
     case TNaming_SELECTED :
-      B.Select(NS, OS);
+      B.Select(aNewIter.Value(), aOldIter.Value());
       break;
     default:
       break;
@@ -138,7 +157,8 @@ static void copyAttrs(TDF_Label theSource, TDF_Label theDestination) {
     // 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
+      // no special relocation, empty map, but self-relocation is on: copy references w/o changes
+      Handle(TDF_RelocationTable) aRelocTable = new TDF_RelocationTable(Standard_True);
       anAttrIter.Value()->Paste(aTargetAttr, aRelocTable);
     } else {
       CopyNS(aNS, aTargetAttr);
@@ -167,7 +187,7 @@ void Model_AttributeSelectionList::remove(const std::set<int>& theIndices)
       }
     } else { // this is removed, so remove everything from this label
       TDF_Label aLab = mySize->Label().FindChild(aCurrent + 1);
-      std::shared_ptr<Model_AttributeSelection> aOldAttr = 
+      std::shared_ptr<Model_AttributeSelection> aOldAttr =
         std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLab));
       aOldAttr->setObject(owner());
       REMOVE_BACK_REF(aOldAttr->context());
@@ -239,7 +259,7 @@ void Model_AttributeSelectionList::setSelectionType(const std::string& theType)
   mySelectionType->Set(theType.c_str());
 }
 
-std::shared_ptr<ModelAPI_AttributeSelection> 
+std::shared_ptr<ModelAPI_AttributeSelection>
   Model_AttributeSelectionList::value(const int theIndex)
 {
   if (myTmpAttr.get() && theIndex == size() - 1) {
@@ -249,10 +269,12 @@ std::shared_ptr<ModelAPI_AttributeSelection>
   // create a new attribute each time, by demand
   // supporting of old attributes is too slow (synch each time) and buggy on redo
   // (if attribute is deleted and created, the abort updates attriute and makes the Attr invalid)
-  std::shared_ptr<Model_AttributeSelection> aNewAttr = 
+  std::shared_ptr<Model_AttributeSelection> aNewAttr =
     std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLabel));
+  aNewAttr->setID(id());
   if (owner()) {
     aNewAttr->setObject(owner());
+    aNewAttr->setParent(this);
   }
   return aNewAttr;
 }
@@ -265,10 +287,11 @@ void Model_AttributeSelectionList::clear()
     TDF_ChildIterator aSubIter(mySize->Label());
     for(; aSubIter.More(); aSubIter.Next()) {
       TDF_Label aLab = aSubIter.Value();
-      std::shared_ptr<Model_AttributeSelection> aNewAttr = 
+      std::shared_ptr<Model_AttributeSelection> aNewAttr =
         std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLab));
       if (owner()) {
         aNewAttr->setObject(owner());
+        aNewAttr->setParent(this);
       }
       REMOVE_BACK_REF(aNewAttr->context());