Salome HOME
Make compilable on Linux
[modules/shaper.git] / src / Model / Model_AttributeRefList.cpp
index 83e6b9c44624c74fb8d39f3c9085dcf3750f12e4..b1fcda04632516822a6a4ae3c9abdbd4d95320dd 100644 (file)
@@ -1,3 +1,5 @@
+// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
+
 // File:        ModelAPI_AttributeRefList.cxx
 // Created:     8 May 2014
 // Author:      Mikhail PONIKAROV
@@ -12,45 +14,84 @@ using namespace std;
 
 void Model_AttributeRefList::append(ObjectPtr theObject)
 {
-  boost::shared_ptr<Model_Data> aData = 
-    boost::dynamic_pointer_cast<Model_Data>(theObject->data());
-  myRef->Append(aData->label());
+  std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theObject->data());
+  myRef->Append(aData->label().Father());  // store label of the object
+  // do it before the transaction finish to make just created/removed objects know dependencies
+  // and reference from composite feature is removed automatically
+  ADD_BACK_REF(theObject);
 
   owner()->data()->sendAttributeUpdated(this);
 }
 
 void Model_AttributeRefList::remove(ObjectPtr theObject)
 {
-  boost::shared_ptr<Model_Data> aData = 
-    boost::dynamic_pointer_cast<Model_Data>(theObject->data());
-  myRef->Remove(aData->label());
-
+  std::shared_ptr<Model_Data> aData;
+  if (theObject.get() != NULL) {
+    aData = std::dynamic_pointer_cast<Model_Data>(theObject->data());
+    myRef->Remove(aData->label().Father());
+    REMOVE_BACK_REF(theObject);
+  }
+  else { // in case of empty object remove, the first empty object is removed from the list
+    std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
+        owner()->document());
+    if (aDoc) {
+      const TDF_LabelList& aList = myRef->List();
+      for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) {
+        ObjectPtr anObj = aDoc->object(aLIter.Value());
+        if (anObj.get() == NULL) {
+          myRef->Remove(aLIter.Value());
+          REMOVE_BACK_REF(theObject);
+          break;
+        }
+      }
+    }
+  }
   owner()->data()->sendAttributeUpdated(this);
 }
 
-int Model_AttributeRefList::size()
+int Model_AttributeRefList::size() const
 {
   return myRef->Extent();
 }
 
+bool Model_AttributeRefList::isInitialized()
+{
+  if (size() == 0) { // empty list is not initialized list: sketch will be not valid after add/undo
+    return false;
+  }
+  return ModelAPI_AttributeRefList::isInitialized();
+}
+
 list<ObjectPtr> Model_AttributeRefList::list()
 {
-  std::list< ObjectPtr > aResult;
-  boost::shared_ptr<Model_Document> aDoc = 
-    boost::dynamic_pointer_cast<Model_Document>(owner()->document());
+  std::list<ObjectPtr> aResult;
+  std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
+      owner()->document());
   if (aDoc) {
     const TDF_LabelList& aList = myRef->List();
-    for(TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) {
+    for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) {
       ObjectPtr anObj = aDoc->object(aLIter.Value());
-      if (!anObj) { // try to use father (for feature)
-        anObj = aDoc->object(aLIter.Value().Father());
-      }
       aResult.push_back(anObj);
     }
   }
   return aResult;
 }
 
+ObjectPtr Model_AttributeRefList::object(const int theIndex) const
+{
+  std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
+      owner()->document());
+  if (aDoc) {
+    const TDF_LabelList& aList = myRef->List();
+    int anIndex = 0;
+    for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next(), anIndex++) {
+      if (anIndex == theIndex)
+        return aDoc->object(aLIter.Value());
+    }
+  }
+  return ObjectPtr();
+}
+
 Model_AttributeRefList::Model_AttributeRefList(TDF_Label& theLabel)
 {
   myIsInitialized = theLabel.FindAttribute(TDataStd_ReferenceList::GetID(), myRef) == Standard_True;