Salome HOME
Merge branch 'Dev_1.2.0' of newgeom:newgeom into Dev_1.2.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 "Model_Objects.h"
11 #include <ModelAPI_Feature.h>
12 #include <TDF_ListIteratorOfLabelList.hxx>
13
14 using namespace std;
15
16 void Model_AttributeRefList::append(ObjectPtr theObject)
17 {
18   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theObject->data());
19   myRef->Append(aData->label().Father());  // store label of the object
20   // do it before the transaction finish to make just created/removed objects know dependencies
21   // and reference from composite feature is removed automatically
22   ADD_BACK_REF(theObject);
23
24   owner()->data()->sendAttributeUpdated(this);
25 }
26
27 void Model_AttributeRefList::remove(ObjectPtr theObject)
28 {
29   std::shared_ptr<Model_Data> aData;
30   if (theObject.get() != NULL) {
31     aData = std::dynamic_pointer_cast<Model_Data>(theObject->data());
32     myRef->Remove(aData->label().Father());
33     REMOVE_BACK_REF(theObject);
34   }
35   else { // in case of empty object remove, the first empty object is removed from the list
36     std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
37         owner()->document());
38     if (aDoc) {
39       const TDF_LabelList& aList = myRef->List();
40       for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) {
41         ObjectPtr anObj = aDoc->objects()->object(aLIter.Value());
42         if (anObj.get() == NULL) {
43           myRef->Remove(aLIter.Value());
44           REMOVE_BACK_REF(theObject);
45           break;
46         }
47       }
48     }
49   }
50   owner()->data()->sendAttributeUpdated(this);
51 }
52
53 void Model_AttributeRefList::clear()
54 {
55   std::list<ObjectPtr> anOldList = list();
56   myRef->Clear();
57   std::list<ObjectPtr>::iterator anOldIter = anOldList.begin();
58   for(; anOldIter != anOldList.end(); anOldIter++) {
59     REMOVE_BACK_REF((*anOldIter));
60   }
61 }
62
63 int Model_AttributeRefList::size() const
64 {
65   return myRef->Extent();
66 }
67
68 bool Model_AttributeRefList::isInitialized()
69 {
70   if (size() == 0) { // empty list is not initialized list: sketch will be not valid after add/undo
71     return false;
72   }
73   return ModelAPI_AttributeRefList::isInitialized();
74 }
75
76 list<ObjectPtr> Model_AttributeRefList::list()
77 {
78   std::list<ObjectPtr> aResult;
79   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
80       owner()->document());
81   if (aDoc) {
82     const TDF_LabelList& aList = myRef->List();
83     for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) {
84       ObjectPtr anObj = aDoc->objects()->object(aLIter.Value());
85       aResult.push_back(anObj);
86     }
87   }
88   return aResult;
89 }
90
91 ObjectPtr Model_AttributeRefList::object(const int theIndex) const
92 {
93   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
94       owner()->document());
95   if (aDoc) {
96     const TDF_LabelList& aList = myRef->List();
97     int anIndex = 0;
98     for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next(), anIndex++) {
99       if (anIndex == theIndex)
100         return aDoc->objects()->object(aLIter.Value());
101     }
102   }
103   return ObjectPtr();
104 }
105
106 Model_AttributeRefList::Model_AttributeRefList(TDF_Label& theLabel)
107 {
108   myIsInitialized = theLabel.FindAttribute(TDataStd_ReferenceList::GetID(), myRef) == Standard_True;
109   if (!myIsInitialized) {
110     myRef = TDataStd_ReferenceList::Set(theLabel);
111   }
112 }