Salome HOME
Merge branch 'master' of newgeom:newgeom.git into BR_PYTHON_PLUGIN
[modules/shaper.git] / src / Model / Model_AttributeRefList.cpp
1 // File:        ModelAPI_AttributeRefList.cxx
2 // Created:     8 May 2014
3 // Author:      Mikhail PONIKAROV
4
5 #include "Model_AttributeRefList.h"
6 #include "Model_Application.h"
7 #include "Model_Data.h"
8 #include <ModelAPI_Feature.h>
9 #include <TDF_ListIteratorOfLabelList.hxx>
10
11 using namespace std;
12
13 void Model_AttributeRefList::append(ObjectPtr theObject)
14 {
15   boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(theObject->data());
16   myRef->Append(aData->label().Father());  // store label of the object
17
18   owner()->data()->sendAttributeUpdated(this);
19 }
20
21 void Model_AttributeRefList::remove(ObjectPtr theObject)
22 {
23   boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(theObject->data());
24   myRef->Remove(aData->label().Father());
25
26   owner()->data()->sendAttributeUpdated(this);
27 }
28
29 int Model_AttributeRefList::size() const
30 {
31   return myRef->Extent();
32 }
33
34 list<ObjectPtr> Model_AttributeRefList::list()
35 {
36   std::list<ObjectPtr> aResult;
37   boost::shared_ptr<Model_Document> aDoc = boost::dynamic_pointer_cast<Model_Document>(
38       owner()->document());
39   if (aDoc) {
40     const TDF_LabelList& aList = myRef->List();
41     for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) {
42       ObjectPtr anObj = aDoc->object(aLIter.Value());
43       aResult.push_back(anObj);
44     }
45   }
46   return aResult;
47 }
48
49 ObjectPtr Model_AttributeRefList::object(const int theIndex) const
50 {
51   boost::shared_ptr<Model_Document> aDoc = boost::dynamic_pointer_cast<Model_Document>(
52       owner()->document());
53   if (aDoc) {
54     const TDF_LabelList& aList = myRef->List();
55     int anIndex = 0;
56     for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next(), anIndex++) {
57       if (anIndex == theIndex)
58         return aDoc->object(aLIter.Value());
59     }
60   }
61   return ObjectPtr();
62 }
63
64 Model_AttributeRefList::Model_AttributeRefList(TDF_Label& theLabel)
65 {
66   myIsInitialized = theLabel.FindAttribute(TDataStd_ReferenceList::GetID(), myRef) == Standard_True;
67   if (!myIsInitialized) {
68     myRef = TDataStd_ReferenceList::Set(theLabel);
69   }
70 }