]> SALOME platform Git repositories - modules/shaper.git/blob - src/Model/Model_AttributeRefList.cpp
Salome HOME
c09190f3326da11cd0057ac0236d4397780762c9
[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   }
33   else { // in case of empty object remove, the first empty object is removed from the list
34     std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
35         owner()->document());
36     if (aDoc) {
37       const TDF_LabelList& aList = myRef->List();
38       for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) {
39         ObjectPtr anObj = aDoc->object(aLIter.Value());
40         if (anObj.get() == NULL) {
41           myRef->Remove(aLIter.Value());
42           REMOVE_BACK_REF(theObject);
43           break;
44         }
45       }
46     }
47   }
48   owner()->data()->sendAttributeUpdated(this);
49 }
50
51 int Model_AttributeRefList::size() const
52 {
53   return myRef->Extent();
54 }
55
56 list<ObjectPtr> Model_AttributeRefList::list()
57 {
58   std::list<ObjectPtr> aResult;
59   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
60       owner()->document());
61   if (aDoc) {
62     const TDF_LabelList& aList = myRef->List();
63     for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) {
64       ObjectPtr anObj = aDoc->object(aLIter.Value());
65       aResult.push_back(anObj);
66     }
67   }
68   return aResult;
69 }
70
71 ObjectPtr Model_AttributeRefList::object(const int theIndex) const
72 {
73   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
74       owner()->document());
75   if (aDoc) {
76     const TDF_LabelList& aList = myRef->List();
77     int anIndex = 0;
78     for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next(), anIndex++) {
79       if (anIndex == theIndex)
80         return aDoc->object(aLIter.Value());
81     }
82   }
83   return ObjectPtr();
84 }
85
86 Model_AttributeRefList::Model_AttributeRefList(TDF_Label& theLabel)
87 {
88   myIsInitialized = theLabel.FindAttribute(TDataStd_ReferenceList::GetID(), myRef) == Standard_True;
89   if (!myIsInitialized) {
90     myRef = TDataStd_ReferenceList::Set(theLabel);
91   }
92 }