Salome HOME
Merge branch 'Dev_0.6.1' of newgeom:newgeom into Dev_0.6.1
[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
20   owner()->data()->sendAttributeUpdated(this);
21 }
22
23 void Model_AttributeRefList::remove(ObjectPtr theObject)
24 {
25   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theObject->data());
26   myRef->Remove(aData->label().Father());
27
28   owner()->data()->sendAttributeUpdated(this);
29 }
30
31 int Model_AttributeRefList::size() const
32 {
33   return myRef->Extent();
34 }
35
36 list<ObjectPtr> Model_AttributeRefList::list()
37 {
38   std::list<ObjectPtr> aResult;
39   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
40       owner()->document());
41   if (aDoc) {
42     const TDF_LabelList& aList = myRef->List();
43     for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) {
44       ObjectPtr anObj = aDoc->object(aLIter.Value());
45       aResult.push_back(anObj);
46     }
47   }
48   return aResult;
49 }
50
51 ObjectPtr Model_AttributeRefList::object(const int theIndex) const
52 {
53   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
54       owner()->document());
55   if (aDoc) {
56     const TDF_LabelList& aList = myRef->List();
57     int anIndex = 0;
58     for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next(), anIndex++) {
59       if (anIndex == theIndex)
60         return aDoc->object(aLIter.Value());
61     }
62   }
63   return ObjectPtr();
64 }
65
66 Model_AttributeRefList::Model_AttributeRefList(TDF_Label& theLabel)
67 {
68   myIsInitialized = theLabel.FindAttribute(TDataStd_ReferenceList::GetID(), myRef) == Standard_True;
69   if (!myIsInitialized) {
70     myRef = TDataStd_ReferenceList::Set(theLabel);
71   }
72 }