Salome HOME
Issue #653 - Double and triple click edges -- Fix Debian dynamic_pointer_cast problem...
[modules/shaper.git] / src / Model / Model_AttributeSelectionList.cpp
index 5aca20aee34c8822d9ab946131976e70c7f13827..f547a797fdcdbbc80bc10a844ed082a652a68d15 100644 (file)
@@ -1,4 +1,6 @@
-// File:        Model_AttributeSelectionList.h
+// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
+
+// File:        Model_AttributeSelectionList.cpp
 // Created:     22 Oct 2014
 // Author:      Mikhail PONIKAROV
 
 #include "Model_Data.h"
 
 #include <TDF_ChildIterator.hxx>
+#include <TopAbs_ShapeEnum.hxx>
+
+#include <TopoDS.hxx>
+#include <TopoDS_Shape.hxx>
+#include <TopoDS_Edge.hxx>
+#include <BRep_Tool.hxx>
 
 using namespace std;
 
 void Model_AttributeSelectionList::append(
-    const ResultPtr& theContext, const boost::shared_ptr<GeomAPI_Shape>& theSubShape)
+    const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape)
 {
+  // do not use the degenerated edge as a shape, a list is not incremented in this case
+  if (theSubShape.get() && !theSubShape->isNull() && theSubShape->isEdge()) {
+    const TopoDS_Shape& aSubShape = theSubShape->impl<TopoDS_Shape>();
+    if (aSubShape.ShapeType() == TopAbs_EDGE && BRep_Tool::Degenerated(TopoDS::Edge(aSubShape))) {
+      return;
+    }
+  }
+
   int aNewTag = mySize->Get() + 1;
   TDF_Label aNewLab = mySize->Label().FindChild(aNewTag);
 
-  boost::shared_ptr<Model_AttributeSelection> aNewAttr = 
-    boost::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aNewLab));
+  std::shared_ptr<Model_AttributeSelection> aNewAttr = 
+    std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aNewLab));
   if (owner()) {
     aNewAttr->setObject(owner());
   }
-  mySubs.push_back(aNewAttr);
+  aNewAttr->setID(id());
   mySize->Set(aNewTag);
   aNewAttr->setValue(theContext, theSubShape);
   owner()->data()->sendAttributeUpdated(this);
 }
 
+void Model_AttributeSelectionList::append(std::string theNamingName)
+{
+  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());
+  }
+  mySize->Set(aNewTag);
+  aNewAttr->selectSubShape(selectionType(), theNamingName);
+  owner()->data()->sendAttributeUpdated(this);
+}
+
 int Model_AttributeSelectionList::size()
 {
   return mySize->Get();
 }
 
-boost::shared_ptr<ModelAPI_AttributeSelection> 
+const std::string Model_AttributeSelectionList::selectionType() const
+{
+  return TCollection_AsciiString(mySelectionType->Get()).ToCString();
+}
+
+void Model_AttributeSelectionList::setSelectionType(const std::string& theType)
+{
+  mySelectionType->Set(theType.c_str());
+}
+
+std::shared_ptr<ModelAPI_AttributeSelection> 
   Model_AttributeSelectionList::value(const int theIndex)
 {
-  return mySubs[theIndex];
+  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)
+  std::shared_ptr<Model_AttributeSelection> aNewAttr = 
+    std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLabel));
+  if (owner()) {
+    aNewAttr->setObject(owner());
+  }
+  return aNewAttr;
 }
 
 void Model_AttributeSelectionList::clear()
 {
-  mySubs.clear();
-  TDF_ChildIterator aSubIter(mySize->Label());
-  for(; aSubIter.More(); aSubIter.Next()) {
-    aSubIter.Value().ForgetAllAttributes(Standard_True);
+  if (mySize->Get() != 0) {
+    mySize->Set(0);
+    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>(new Model_AttributeSelection(aLab));
+      if (owner()) {
+        aNewAttr->setObject(owner());
+      }
+      REMOVE_BACK_REF(aNewAttr->context());
+
+      aLab.ForgetAllAttributes(Standard_True);
+    }
+    owner()->data()->sendAttributeUpdated(this);
   }
-  owner()->data()->sendAttributeUpdated(this);
+}
+
+bool Model_AttributeSelectionList::isInitialized()
+{
+  if (size() == 0) { // empty list is not initialized list: sketch will be not valid after add/undo
+    return false;
+  }
+  return ModelAPI_AttributeSelectionList::isInitialized();
 }
 
 Model_AttributeSelectionList::Model_AttributeSelectionList(TDF_Label& theLabel)
@@ -55,25 +123,8 @@ Model_AttributeSelectionList::Model_AttributeSelectionList(TDF_Label& theLabel)
   myIsInitialized = theLabel.FindAttribute(TDataStd_Integer::GetID(), mySize) == Standard_True;
   if (!myIsInitialized) {
     mySize = TDataStd_Integer::Set(theLabel, 0);
+    mySelectionType = TDataStd_Comment::Set(theLabel, "");
   } else { // recollect mySubs
-    int aNum = mySize->Get();
-    TDF_ChildIterator aSubIter(theLabel);
-    for(; aSubIter.More(), aNum != 0; aSubIter.Next(), aNum--) {
-      TDF_Label aChildLab = aSubIter.Value();
-      boost::shared_ptr<Model_AttributeSelection> aNewAttr = 
-        boost::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aChildLab));
-      if (owner())
-        aNewAttr->setObject(owner());
-      mySubs.push_back(aNewAttr);
-    }
-  }
-}
-
-void Model_AttributeSelectionList::setObject(const boost::shared_ptr<ModelAPI_Object>& theObject)
-{
-  ModelAPI_AttributeSelectionList::setObject(theObject);
-  std::vector<boost::shared_ptr<Model_AttributeSelection> >::iterator aSubIter = mySubs.begin();
-  for(; aSubIter != mySubs.end(); aSubIter++) {
-    (*aSubIter)->setObject(theObject);
+    theLabel.FindAttribute(TDataStd_Comment::GetID(), mySelectionType);
   }
 }