Salome HOME
updated copyright message
[modules/shaper.git] / src / Model / Model_AttributeSelectionList.cpp
index e091a6e9e6fa09505864ebea062e9e3bc702d11e..3c3fb2a1b7eff5eda6f1b1acd92d89dc3b539821 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2017  CEA/DEN, EDF R&D
+// Copyright (C) 2014-2023  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
 //
 // 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
+// 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>
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
 
 #include "Model_AttributeSelectionList.h"
 #include "Model_Application.h"
 #include "Model_Events.h"
 #include "Model_Data.h"
+#include "Model_Objects.h"
 
+#include <GeomAPI_Pnt.h>
 #include <GeomAPI_Shape.h>
+#include <GeomAPI_ShapeIterator.h>
 
 #include <TDF_AttributeIterator.hxx>
 #include <TDF_ChildIterator.hxx>
 #include <TDF_RelocationTable.hxx>
 #include <TDF_DeltaOnAddition.hxx>
+#include <TDataStd_UAttribute.hxx>
+#include <TDataStd_ReferenceList.hxx>
 #include <TopAbs_ShapeEnum.hxx>
 #include <TopoDS.hxx>
 #include <TopoDS_Shape.hxx>
 #include <TNaming_NamedShape.hxx>
 #include <NCollection_List.hxx>
 
+/// GUID for UAttribute that indicates the list has "To add all elements that share the same
+/// topology" flag enabled
+static const Standard_GUID kIS_GEOMETRICAL_SELECTION("f16987b6-e6c8-435c-99fa-03a7e0b06e83");
+
+/// GUID for TDataStd_ReferenceList attribute that refers the selection filters feature
+static const Standard_GUID kSELECTION_FILTERS_REF("ea5b1dbf-a740-4a0b-a1b2-3c3c756e691a");
+
+
 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
@@ -76,7 +88,7 @@ void Model_AttributeSelectionList::append(
 }
 
 void Model_AttributeSelectionList::append(
-  const std::string theNamingName, const std::string& theType)
+  const std::wstring& theNamingName, const std::string& theType)
 {
   int aNewTag = mySize->Get() + 1;
   TDF_Label aNewLab = mySize->Label().FindChild(aNewTag);
@@ -93,6 +105,41 @@ void Model_AttributeSelectionList::append(
   owner()->data()->sendAttributeUpdated(this);
 }
 
+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::wstring& 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()) {
@@ -170,6 +217,8 @@ static void copyAttrs(TDF_Label theSource, TDF_Label theDestination) {
     if (!theDestination.FindAttribute(anAttrIter.Value()->ID(), aTargetAttr)) {
       // create a new attribute if not yet exists in the destination
            aTargetAttr = anAttrIter.Value()->NewEmpty();
+      if (aTargetAttr->ID() != anAttrIter.Value()->ID())
+        aTargetAttr->SetID(anAttrIter.Value()->ID());
       theDestination.AddAttribute(aTargetAttr);
     }
     // for named shape copy exact shapes (in NamedShape Paste method the CopyTool is used)
@@ -193,7 +242,7 @@ void Model_AttributeSelectionList::remove(const std::set<int>& theIndices)
 {
   int anOldSize = mySize->Get();
   int aRemoved = 0;
-  // iterate one by one and shifting the removed indicies
+  // iterate one by one and shifting the removed indices
   for(int aCurrent = 0; aCurrent < anOldSize; aCurrent++) {
     if (theIndices.find(aCurrent) == theIndices.end()) { // not removed
       if (aRemoved) { // but must be shifted to the removed position
@@ -220,47 +269,84 @@ void Model_AttributeSelectionList::remove(const std::set<int>& theIndices)
   }
 }
 
+void Model_AttributeSelectionList::copyTo(AttributeSelectionListPtr theTarget) const
+{
+  std::shared_ptr<Model_AttributeSelectionList> aTarget =
+    std::dynamic_pointer_cast<Model_AttributeSelectionList>(theTarget);
+  if (aTarget) {
+    copyAttrs(myLab, aTarget->myLab);
+    aTarget->reinit();
+  }
+}
+
 int Model_AttributeSelectionList::size()
 {
   return mySize->Get();
 }
 
-bool Model_AttributeSelectionList::isInList(const ResultPtr& theContext,
+// LCOV_EXCL_START
+
+// returns true if theShape is same with theInList or is contained in it (a compound)
+static bool isIn(GeomShapePtr theInList, GeomShapePtr theShape) {
+  if (theShape->isSame(theInList))
+    return true;
+  if (theInList.get() && theInList->shapeType() == GeomAPI_Shape::COMPOUND) {
+    for(GeomAPI_ShapeIterator anIter(theInList); anIter.more(); anIter.next()) {
+      if (!anIter.current()->isNull() && anIter.current()->isSame(theShape))
+        return true;
+    }
+  }
+  return false;
+}
+
+bool Model_AttributeSelectionList::isInList(const ObjectPtr& theContext,
                                             const std::shared_ptr<GeomAPI_Shape>& theSubShape,
-                                            const bool theTemporarily)
+                                            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 "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())
-            return true;
-        } else {
-          // we need to call here isSame instead of isEqual to do not check shapes orientation
-          if (theSubShape->isSame(*aShapes))
-            return true;
+    if (theContext.get()) {
+      std::map<ObjectPtr, std::list<std::shared_ptr<GeomAPI_Shape> > >::iterator aContext =
+        myCash.find(theContext);
+      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())
+              return true;
+            ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aContext->first);
+            FeaturePtr aFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(aContext->first);
+            if ((aRes.get() && (*aShapes)->isSame(aRes->shape())) || aFeat.get())
+              return true;
+          } else {
+            // we need to call here isSame instead of isEqual to do not check shapes orientation
+            if (isIn(*aShapes, theSubShape))
+              return true;
+          }
         }
       }
+      return false;
     }
-    return false;
   }
   // no-cash method
+  bool isFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theContext).get() != NULL;
+  ResultPtr aRes;
+  if (!isFeature)
+    aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theContext);
   for(int anIndex = size() - 1; anIndex >= 0; anIndex--) {
     AttributeSelectionPtr anAttr = value(anIndex);
     if (anAttr.get()) {
-      if (anAttr->context() == theContext) { // contexts are equal, so, check that values are also
+      if (isFeature && anAttr->contextFeature() == theContext)
+        return true; // for the feature value should not be compared
+      if (!isFeature && 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() || (aRes.get() && aValue->isSame(aRes->shape())))
             return true;
-          }
         } else {
           // we need to call here isSame instead of isEqual to do not check shapes orientation
-          if (aValue->isSame(theSubShape)) // shapes are equal
+          if (isIn(aValue, theSubShape)) // shapes are equal
             return true;
         }
       }
@@ -268,6 +354,7 @@ bool Model_AttributeSelectionList::isInList(const ResultPtr& theContext,
   }
   return false;
 }
+// LCOV_EXCL_STOP
 
 const std::string Model_AttributeSelectionList::selectionType() const
 {
@@ -287,15 +374,15 @@ std::shared_ptr<ModelAPI_AttributeSelection>
   }
   TDF_Label aLabel = mySize->Label().FindChild(theIndex + 1);
   // 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)
+  // supporting of old attributes is too slow (sync each time) and buggy on redo
+  // (if attribute is deleted and created, the abort updates attribute and makes the Attr invalid)
   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;
 }
 
@@ -330,8 +417,8 @@ bool Model_AttributeSelectionList::isInitialized()
 }
 
 Model_AttributeSelectionList::Model_AttributeSelectionList(TDF_Label& theLabel)
+: ModelAPI_AttributeSelectionList(), myLab(theLabel)
 {
-  myLab = theLabel;
   reinit();
 }
 
@@ -347,7 +434,7 @@ void Model_AttributeSelectionList::reinit()
   myIsCashed = false;
 }
 
-
+// LCOV_EXCL_START
 void Model_AttributeSelectionList::cashValues(const bool theEnabled)
 {
   myIsCashed = theEnabled;
@@ -356,8 +443,106 @@ void Model_AttributeSelectionList::cashValues(const bool theEnabled)
     for(int anIndex = size() - 1; anIndex >= 0; anIndex--) {
       AttributeSelectionPtr anAttr = value(anIndex);
       if (anAttr.get()) {
-        myCash[anAttr->context()].push_back(anAttr->value());
+        myCash[anAttr->contextObject()].push_back(anAttr->value());
       }
     }
   }
 }
+// LCOV_EXCL_STOP
+
+bool Model_AttributeSelectionList::isGeometricalSelection() const
+{
+  return myLab.IsAttribute(kIS_GEOMETRICAL_SELECTION);
+}
+
+void Model_AttributeSelectionList::setGeometricalSelection(const bool theIsGeometricalSelection)
+{
+  if (isGeometricalSelection() == theIsGeometricalSelection)
+    return; // nothing to do
+  if (theIsGeometricalSelection) // store the state
+    TDataStd_UAttribute::Set(myLab, kIS_GEOMETRICAL_SELECTION);
+  else
+    myLab.ForgetAttribute(kIS_GEOMETRICAL_SELECTION);
+  std::set<int> anIndiciesToRemove;  // Update list according to the flag
+  if (theIsGeometricalSelection) { // all objects with same geometry must be combined into single
+    std::list<AttributeSelectionPtr> anAttributes; // collect attributes with geometrical compounds
+    for(int anIndex = 0; anIndex < size(); anIndex++) {
+      AttributeSelectionPtr anAttr = value(anIndex);
+      if (!anAttr.get() || !anAttr->context().get())
+        continue;
+      anAttr->combineGeometrical();
+      if (!anAttr->value().get() || anAttr->value()->shapeType() != GeomAPI_Shape::COMPOUND)
+        continue;
+      // check it is equal to some other attribute already presented in the list
+      std::list<AttributeSelectionPtr>::iterator anAttrIter = anAttributes.begin();
+      for(; anAttrIter != anAttributes.end(); anAttrIter++) {
+        if (anAttr->context() == (*anAttrIter)->context() &&
+            anAttr->namingName() == (*anAttrIter)->namingName()) {
+          anIndiciesToRemove.insert(anIndex);
+          break;
+        }
+      }
+      if (anAttrIter == anAttributes.end()) // not removed, so, add to the compare-list
+        anAttributes.push_back(anAttr);
+    }
+  } else { // all objects with same geometry must be divided into separated sub-attributes
+    int anInitialSize = size();
+    for(int anIndex = 0; anIndex < anInitialSize; anIndex++) {
+      AttributeSelectionPtr anAttr = value(anIndex);
+      if (!anAttr.get() || !anAttr->context().get())
+        continue;
+      GeomShapePtr aValue = anAttr->value();
+      if (!aValue.get() || aValue->shapeType() != GeomAPI_Shape::COMPOUND)
+        continue;
+      for(GeomAPI_ShapeIterator anIter(aValue); anIter.more(); anIter.next()) {
+        append(anAttr->context(), anIter.current());
+      }
+      anIndiciesToRemove.insert(anIndex);
+    }
+  }
+  remove(anIndiciesToRemove);
+  myIsCashed = false;
+  myCash.clear(); // empty list as indicator that cash is not used
+  owner()->data()->sendAttributeUpdated(this);
+}
+
+FiltersFeaturePtr Model_AttributeSelectionList::filters() const
+{
+  Handle(TDataStd_ReferenceList) aRef;
+  if (myLab.FindAttribute(kSELECTION_FILTERS_REF, aRef) && !aRef->IsEmpty()) {
+    if (owner().get()) {
+      std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
+        owner()->document());
+      if (aDoc) {
+        const TDF_Label& aRefLab = aRef->First();
+        if (!aRefLab.IsNull()) {  // it may happen with old document, issue #285
+          ObjectPtr anObj = aDoc->objects()->object(aRefLab);
+          FiltersFeaturePtr aFeat = std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(anObj);
+          if (aFeat.get()) {
+            aFeat->setAttribute(owner()->data()->attribute(id()));
+            return aFeat;
+          }
+        }
+      }
+    }
+  }
+  return FiltersFeaturePtr(); // null pointer if nothing is defined
+}
+
+void Model_AttributeSelectionList::setFilters(FiltersFeaturePtr theFeature)
+{
+  Handle(TDataStd_ReferenceList) aRef = TDataStd_ReferenceList::Set(myLab, kSELECTION_FILTERS_REF);
+  if (theFeature.get()) {
+    std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theFeature->data());
+    if (aData->isValid())  {
+      TDF_Label anObjLab = aData->label().Father(); // object label
+      if (!aRef->IsEmpty())
+        aRef->Clear();
+      aRef->Append(anObjLab);
+      theFeature->setAttribute(owner()->data()->attribute(id()));
+      return;
+    }
+  }
+  // remove attribute if something is wrong
+  myLab.ForgetAttribute(TDataStd_ReferenceList::GetID());
+}