Salome HOME
Added geometrical selection flag to attribute selection list.
[modules/shaper.git] / src / Model / Model_AttributeSelectionList.cpp
index 81ce0984cb9a856808b9439a51fdaee82f571fb5..f32fbcf6396c0b6b8cd01a24cd9a6a819d99c203 100644 (file)
@@ -1,8 +1,22 @@
-// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
-
-// File:        Model_AttributeSelectionList.cpp
-// Created:     22 Oct 2014
-// Author:      Mikhail PONIKAROV
+// Copyright (C) 2014-2017  CEA/DEN, EDF R&D
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+// See http://www.salome-platform.org/ or
+// email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
+//
 
 #include "Model_AttributeSelectionList.h"
 #include "Model_AttributeSelection.h"
 #include "Model_Events.h"
 #include "Model_Data.h"
 
+#include <GeomAPI_Pnt.h>
+#include <GeomAPI_Shape.h>
+
 #include <TDF_AttributeIterator.hxx>
 #include <TDF_ChildIterator.hxx>
 #include <TDF_RelocationTable.hxx>
+#include <TDF_DeltaOnAddition.hxx>
 #include <TopAbs_ShapeEnum.hxx>
 #include <TopoDS.hxx>
 #include <TopoDS_Shape.hxx>
 #include <BRep_Tool.hxx>
 #include <TNaming_Builder.hxx>
 #include <TNaming_Iterator.hxx>
+#include <TNaming_NamedShape.hxx>
 #include <NCollection_List.hxx>
 
-using namespace std;
-
 void Model_AttributeSelectionList::append(
-    const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape,
+    const ObjectPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape,
     const bool theTemporarily)
 {
   // do not use the degenerated edge as a shape, a list is not incremented in this case
@@ -37,37 +54,41 @@ void Model_AttributeSelectionList::append(
   }
 
   if (myIsCashed && !theTemporarily) {
-    myCash[theContext].push_back(theSubShape);
+    ResultPtr aResContext = std::dynamic_pointer_cast<ModelAPI_Result>(theContext);
+    if (aResContext.get())
+      myCash[aResContext].push_back(theSubShape);
   }
 
   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);
 }
 
 void Model_AttributeSelectionList::append(
-  const std::string theNamingName, const std::string& theType)
+  const std::string& theNamingName, const std::string& theType)
 {
   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);
@@ -75,13 +96,55 @@ void Model_AttributeSelectionList::append(
   owner()->data()->sendAttributeUpdated(this);
 }
 
-void Model_AttributeSelectionList::removeLast() 
+void Model_AttributeSelectionList::append(const GeomPointPtr& thePoint, const std::string& theType)
+{
+  int aNewTag = mySize->Get() + 1;
+  TDF_Label aNewLab = mySize->Label().FindChild(aNewTag);
+
+  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->selectSubShape(theType, thePoint);
+  owner()->data()->sendAttributeUpdated(this);
+}
+
+void Model_AttributeSelectionList::append(const std::string& theType,
+  const std::string& theContextName, const int theIndex)
+{
+  int aNewTag = mySize->Get() + 1;
+  TDF_Label aNewLab = mySize->Label().FindChild(aNewTag);
+
+  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->selectSubShape(theType, theContextName, theIndex);
+  owner()->data()->sendAttributeUpdated(this);
+}
+
+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());
@@ -91,10 +154,10 @@ 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);
 
@@ -122,10 +185,10 @@ static void CopyNS(const Handle(TNaming_NamedShape)& theFrom,
     case TNaming_GENERATED :
       B.Generated(aOldIter.Value(), aNewIter.Value());
       break;
-    case TNaming_MODIFY : 
+    case TNaming_MODIFY :
       B.Modify(aOldIter.Value(), aNewIter.Value());
       break;
-    case TNaming_DELETE : 
+    case TNaming_DELETE :
       B.Delete (aOldIter.Value());
       break;
     case TNaming_SELECTED :
@@ -150,7 +213,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);
@@ -179,7 +243,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());
@@ -199,27 +263,31 @@ int Model_AttributeSelectionList::size()
   return mySize->Get();
 }
 
-bool Model_AttributeSelectionList::isInList(const ResultPtr& theContext,
+bool Model_AttributeSelectionList::isInList(const ObjectPtr& theContext,
                                             const std::shared_ptr<GeomAPI_Shape>& theSubShape,
                                             const bool theTemporarily)
 {
+  ResultPtr aResCont = std::dynamic_pointer_cast<ModelAPI_Result>(theContext);
   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;
+    if (aResCont.get()) {
+      std::map<ResultPtr, std::list<std::shared_ptr<GeomAPI_Shape> > >::iterator aContext =
+        myCash.find(aResCont);
+      if (aContext != myCash.end()) {
+        // iterate shapes because "isSame" 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() || (*aShapes)->isSame(aContext->first->shape()))
+              return true;
+          } else {
+            // we need to call here isSame instead of isEqual to do not check shapes orientation
+            if (theSubShape->isSame(*aShapes))
+              return true;
+          }
         }
       }
+      return false;
     }
-    return false;
   }
   // no-cash method
   for(int anIndex = size() - 1; anIndex >= 0; anIndex--) {
@@ -227,12 +295,13 @@ bool Model_AttributeSelectionList::isInList(const ResultPtr& theContext,
     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
+        if (!theSubShape.get()) {
+          if (!aValue.get() || (aResCont.get() && aValue->isSame(aResCont->shape()))) {// both null
             return true;
           }
         } else {
-          if (aValue->isEqual(theSubShape)) // shapes are equal
+          // we need to call here isSame instead of isEqual to do not check shapes orientation
+          if (theSubShape->isSame(aValue)) // shapes are equal
             return true;
         }
       }
@@ -251,7 +320,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) {
@@ -261,12 +330,13 @@ 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);
   }
+  aNewAttr->setID(id());
   return aNewAttr;
 }
 
@@ -278,10 +348,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());
 
@@ -300,8 +371,9 @@ bool Model_AttributeSelectionList::isInitialized()
 }
 
 Model_AttributeSelectionList::Model_AttributeSelectionList(TDF_Label& theLabel)
+: myLab(theLabel),
+  myIsGeometricalSelection(false)
 {
-  myLab = theLabel;
   reinit();
 }
 
@@ -331,3 +403,11 @@ void Model_AttributeSelectionList::cashValues(const bool theEnabled)
     }
   }
 }
+
+void Model_AttributeSelectionList::setGeometricalSelection(const bool theIsGeometricalSelection)
+{
+  myIsGeometricalSelection = theIsGeometricalSelection;
+  // TODO: update list accodring to the flag:
+  // false - all objects with same geometry must be splited in separate.
+  // true - all objets with same geometry must be combined into single.
+}