Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom.git into Dev_1.1.0
[modules/shaper.git] / src / Model / Model_AttributeRefList.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModelAPI_AttributeRefList.cxx
4 // Created:     8 May 2014
5 // Author:      Mikhail PONIKAROV
6
7 #include "Model_AttributeRefList.h"
8 #include "Model_Application.h"
9 #include "Model_Data.h"
10 #include <ModelAPI_Feature.h>
11 #include <TDF_ListIteratorOfLabelList.hxx>
12
13 using namespace std;
14
15 void Model_AttributeRefList::append(ObjectPtr theObject)
16 {
17   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theObject->data());
18   myRef->Append(aData->label().Father());  // store label of the object
19   // do it before the transaction finish to make just created/removed objects know dependencies
20   // and reference from composite feature is removed automatically
21   ADD_BACK_REF(theObject);
22
23   owner()->data()->sendAttributeUpdated(this);
24 }
25
26 void Model_AttributeRefList::remove(ObjectPtr theObject)
27 {
28   std::shared_ptr<Model_Data> aData;
29   if (theObject.get() != NULL) {
30     aData = std::dynamic_pointer_cast<Model_Data>(theObject->data());
31     myRef->Remove(aData->label().Father());
32     REMOVE_BACK_REF(theObject);
33   }
34   else { // in case of empty object remove, the first empty object is removed from the list
35     std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
36         owner()->document());
37     if (aDoc) {
38       const TDF_LabelList& aList = myRef->List();
39       for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) {
40         ObjectPtr anObj = aDoc->object(aLIter.Value());
41         if (anObj.get() == NULL) {
42           myRef->Remove(aLIter.Value());
43           REMOVE_BACK_REF(theObject);
44           break;
45         }
46       }
47     }
48   }
49   owner()->data()->sendAttributeUpdated(this);
50 }
51
52 int Model_AttributeRefList::size() const
53 {
54   return myRef->Extent();
55 }
56
57 bool Model_AttributeRefList::isInitialized()
58 {
59   if (size() == 0) { // empty list is not initialized list: sketch will be not valid after add/undo
60     return false;
61   }
62   return ModelAPI_AttributeRefList::isInitialized();
63 }
64
65 list<ObjectPtr> Model_AttributeRefList::list()
66 {
67   std::list<ObjectPtr> aResult;
68   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
69       owner()->document());
70   if (aDoc) {
71     const TDF_LabelList& aList = myRef->List();
72     for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) {
73       ObjectPtr anObj = aDoc->object(aLIter.Value());
74       aResult.push_back(anObj);
75     }
76   }
77   return aResult;
78 }
79
80 ObjectPtr Model_AttributeRefList::object(const int theIndex) const
81 {
82   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
83       owner()->document());
84   if (aDoc) {
85     const TDF_LabelList& aList = myRef->List();
86     int anIndex = 0;
87     for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next(), anIndex++) {
88       if (anIndex == theIndex)
89         return aDoc->object(aLIter.Value());
90     }
91   }
92   return ObjectPtr();
93 }
94
95 Model_AttributeRefList::Model_AttributeRefList(TDF_Label& theLabel)
96 {
97   myIsInitialized = theLabel.FindAttribute(TDataStd_ReferenceList::GetID(), myRef) == Standard_True;
98   if (!myIsInitialized) {
99     myRef = TDataStd_ReferenceList::Set(theLabel);
100   }
101 }