1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: ModelAPI_AttributeRefList.cxx
5 // Author: Mikhail PONIKAROV
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>
16 void Model_AttributeRefList::append(ObjectPtr theObject)
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);
24 owner()->data()->sendAttributeUpdated(this);
27 void Model_AttributeRefList::remove(ObjectPtr theObject)
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);
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>(
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);
50 owner()->data()->sendAttributeUpdated(this);
53 void Model_AttributeRefList::clear()
55 std::list<ObjectPtr> anOldList = list();
57 std::list<ObjectPtr>::iterator anOldIter = anOldList.begin();
58 for(; anOldIter != anOldList.end(); anOldIter++) {
59 REMOVE_BACK_REF((*anOldIter));
61 owner()->data()->sendAttributeUpdated(this);
64 int Model_AttributeRefList::size(const bool theWithEmpty) const
67 return myRef->Extent();
69 const TDF_LabelList& aList = myRef->List();
70 for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) {
71 if (!aLIter.Value().IsNull()) aResult++;
76 bool Model_AttributeRefList::isInitialized()
78 if (size(false) == 0) { // empty list is not initialized list: sketch will be not valid after add/undo
81 return ModelAPI_AttributeRefList::isInitialized();
84 list<ObjectPtr> Model_AttributeRefList::list()
86 std::list<ObjectPtr> aResult;
87 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
90 const TDF_LabelList& aList = myRef->List();
91 for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) {
93 if (!aLIter.Value().IsNull())
94 anObj = aDoc->objects()->object(aLIter.Value());
95 aResult.push_back(anObj);
101 bool Model_AttributeRefList::isInList(const ObjectPtr& theObj)
103 std::list<ObjectPtr> aResult;
104 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
105 owner()->document());
107 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theObj->data());
108 if (aData.get() && aData->isValid()) {
109 TDF_Label anObjLab = aData->label().Father();
110 const TDF_LabelList& aList = myRef->List();
111 for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) {
112 if (aLIter.Value().IsEqual(anObjLab)) {
121 ObjectPtr Model_AttributeRefList::object(const int theIndex, const bool theWithEmpty) const
123 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
124 owner()->document());
126 const TDF_LabelList& aList = myRef->List();
128 for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) {
129 if (theWithEmpty || !aLIter.Value().IsNull())
131 if (anIndex == theIndex) {
132 if (aLIter.Value().IsNull()) { // null label => null sub
135 return aDoc->objects()->object(aLIter.Value());
142 void Model_AttributeRefList::substitute(const ObjectPtr& theCurrent, const ObjectPtr& theNew)
144 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
145 owner()->document());
147 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theCurrent->data());
148 if (aData.get() && aData->isValid()) {
149 TDF_Label aCurrentLab = aData->label().Father();
151 if (theNew.get() && theNew->data()->isValid()) { // the new may be null
152 std::shared_ptr<Model_Data> aNewData =
153 std::dynamic_pointer_cast<Model_Data>(theNew->data());
154 aNewLab = aNewData->label().Father();
156 // do the substitution
157 ADD_BACK_REF(theNew);
158 if (myRef->InsertAfter(aNewLab, aCurrentLab)) {
159 myRef->Remove(aCurrentLab);
160 REMOVE_BACK_REF(theCurrent);
162 owner()->data()->sendAttributeUpdated(this);
167 void Model_AttributeRefList::exchange(const ObjectPtr& theObject1, const ObjectPtr& theObject2)
169 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
170 owner()->document());
172 std::shared_ptr<Model_Data> aData1 = std::dynamic_pointer_cast<Model_Data>(theObject1->data());
173 if (aData1.get() && aData1->isValid()) {
174 TDF_Label aLab1 = aData1->label().Father();
175 if (theObject2.get() && theObject2->data()->isValid()) { // the new may be null
176 std::shared_ptr<Model_Data> aData2 =
177 std::dynamic_pointer_cast<Model_Data>(theObject2->data());
178 if (aData2.get() && aData2->isValid()) {
179 TDF_Label aLab2 = aData2->label().Father();
180 // do the substitution: use the temporary label, as usually in exchange
181 TDF_Label aTmpLab = aLab1.Root();
182 if (myRef->InsertAfter(aTmpLab, aLab1)) {
183 myRef->Remove(aLab1);
185 if (myRef->InsertAfter(aLab1, aLab2)) {
186 myRef->Remove(aLab2);
188 if (myRef->InsertAfter(aLab2, aTmpLab)) {
189 myRef->Remove(aTmpLab);
191 owner()->data()->sendAttributeUpdated(this);
198 void Model_AttributeRefList::removeLast()
200 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
201 owner()->document());
202 if (aDoc && !myRef->IsEmpty()) {
203 ObjectPtr anObj = aDoc->objects()->object(myRef->Last());
205 myRef->Remove(myRef->Last());
206 REMOVE_BACK_REF(anObj);
207 owner()->data()->sendAttributeUpdated(this);
212 Model_AttributeRefList::Model_AttributeRefList(TDF_Label& theLabel)
214 myIsInitialized = theLabel.FindAttribute(TDataStd_ReferenceList::GetID(), myRef) == Standard_True;
215 if (!myIsInitialized) {
216 myRef = TDataStd_ReferenceList::Set(theLabel);