1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: ModelAPI_AttributeRefAttrList.cxx
5 // Author: Mikhail PONIKAROV
7 #include "Model_AttributeRefAttrList.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 #include <TDataStd_ListIteratorOfListOfExtendedString.hxx>
17 void Model_AttributeRefAttrList::append(ObjectPtr theObject)
19 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theObject->data());
20 myRef->Append(aData->label().Father()); // store label of the object
21 myIDs->Append(""); // for the object store an empty string
22 // do it before the transaction finish to make just created/removed objects know dependencies
23 // and reference from composite feature is removed automatically
24 ADD_BACK_REF(theObject);
26 owner()->data()->sendAttributeUpdated(this);
29 void Model_AttributeRefAttrList::append(AttributePtr theAttr)
31 std::shared_ptr<Model_Data> aData =
32 std::dynamic_pointer_cast<Model_Data>(theAttr->owner()->data());
33 myRef->Append(aData->label().Father()); // store label of the object
34 myIDs->Append(theAttr->id().c_str()); // store the ID of the referenced attribute
35 // do it before the transaction finish to make just created/removed objects know dependencies
36 // and reference from composite feature is removed automatically
37 ADD_BACK_REF(theAttr->owner());
39 owner()->data()->sendAttributeUpdated(this);
42 void Model_AttributeRefAttrList::remove(ObjectPtr theObject)
45 if (theObject.get() != NULL) {
46 aTheObjLab = std::dynamic_pointer_cast<Model_Data>(theObject->data())->label().Father();
48 std::shared_ptr<Model_Document> aDoc =
49 std::dynamic_pointer_cast<Model_Document>(owner()->document());
50 // remove from the both lists by clearing the list and then appending one by one
51 // TODO: in OCCT 7.0 there are methods for removing by index, seems will be more optimal
52 TDF_LabelList aRefList = myRef->List();
54 TDataStd_ListOfExtendedString anIDList = myIDs->List();
56 bool aOneisDeleted = false;
57 TDF_ListIteratorOfLabelList aRefIter(aRefList);
58 TDataStd_ListIteratorOfListOfExtendedString anIDIter(anIDList);
59 for (; aRefIter.More(); aRefIter.Next(), anIDIter.Next()) {
60 // append now only not removed
61 if (aOneisDeleted || aRefIter.Value() != aTheObjLab || !anIDIter.Value().IsEmpty() ||
62 (aTheObjLab.IsNull() && aDoc->objects()->object(aRefIter.Value()) != NULL)) {
63 myRef->Append(aRefIter.Value());
64 myIDs->Append(anIDIter.Value());
65 } else if (aTheObjLab.IsNull() && aDoc->objects()->object(aRefIter.Value()) != NULL) {
70 REMOVE_BACK_REF(theObject);
71 owner()->data()->sendAttributeUpdated(this);
75 void Model_AttributeRefAttrList::remove(AttributePtr theAttr)
78 if (theAttr->owner().get() != NULL) {
79 aTheObjLab = std::dynamic_pointer_cast<Model_Data>(theAttr->owner()->data())->label().Father();
81 std::shared_ptr<Model_Document> aDoc =
82 std::dynamic_pointer_cast<Model_Document>(owner()->document());
83 // remove from the both lists by clearing the list and then appending one by one
84 // TODO: in OCCT 7.0 there are methods for removing by index, seems will be more optimal
85 TDF_LabelList aRefList = myRef->List();
87 TDataStd_ListOfExtendedString anIDList = myIDs->List();
89 bool aOneisDeleted = false;
90 TDF_ListIteratorOfLabelList aRefIter(aRefList);
91 TDataStd_ListIteratorOfListOfExtendedString anIDIter(anIDList);
92 for (; aRefIter.More(); aRefIter.Next(), anIDIter.Next()) {
93 if (aOneisDeleted || anIDIter.Value() != theAttr->id().c_str() || // append now only not removed
94 aRefIter.Value() != aTheObjLab || // append now only not removed
95 (aTheObjLab.IsNull() && aDoc->objects()->object(aRefIter.Value()) != NULL)) {
96 myRef->Append(aRefIter.Value());
97 myIDs->Append(anIDIter.Value());
103 REMOVE_BACK_REF(theAttr->owner());
104 owner()->data()->sendAttributeUpdated(this);
108 void Model_AttributeRefAttrList::clear()
110 std::list<std::pair<ObjectPtr, AttributePtr> > anOldList = list();
113 std::list<std::pair<ObjectPtr, AttributePtr> >::iterator anOldIter = anOldList.begin();
114 for(; anOldIter != anOldList.end(); anOldIter++) {
115 REMOVE_BACK_REF((anOldIter->first));
117 owner()->data()->sendAttributeUpdated(this);
120 int Model_AttributeRefAttrList::size() const
122 return myRef->Extent();
125 bool Model_AttributeRefAttrList::isInitialized()
127 if (size() == 0) { // empty list is not initialized list: sketch will be not valid after add/undo
130 return ModelAPI_AttributeRefAttrList::isInitialized();
133 std::list<std::pair<ObjectPtr, AttributePtr> > Model_AttributeRefAttrList::list()
135 std::list<std::pair<ObjectPtr, AttributePtr> > aResult;
136 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
137 owner()->document());
139 const TDF_LabelList& aList = myRef->List();
140 const TDataStd_ListOfExtendedString& anIDList = myIDs->List();
141 TDF_ListIteratorOfLabelList aLIter(aList);
142 TDataStd_ListIteratorOfListOfExtendedString anIDIter(anIDList);
143 for (; aLIter.More(); aLIter.Next(), anIDIter.Next()) {
145 if (!aLIter.Value().IsNull())
146 anObj = aDoc->objects()->object(aLIter.Value());
147 aResult.push_back(std::pair<ObjectPtr, AttributePtr>(anObj,
148 anObj->data()->attribute(TCollection_AsciiString(anIDIter.Value()).ToCString())));
154 bool Model_AttributeRefAttrList::isInList(const ObjectPtr& theObj)
159 std::list<ObjectPtr> aResult;
160 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
161 owner()->document());
163 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theObj->data());
164 if (aData.get() && aData->isValid()) {
165 TDF_Label anObjLab = aData->label().Father();
166 const TDF_LabelList& aList = myRef->List();
167 const TDataStd_ListOfExtendedString& anIDList = myIDs->List();
168 TDF_ListIteratorOfLabelList aLIter(aList);
169 TDataStd_ListIteratorOfListOfExtendedString anIDIter(anIDList);
170 for (; aLIter.More(); aLIter.Next(), anIDIter.Next()) {
171 if (anIDIter.Value().IsEmpty() && aLIter.Value().IsEqual(anObjLab)) {
180 bool Model_AttributeRefAttrList::isInList(const AttributePtr& theAttr)
182 if (!theAttr->owner().get()) {
185 std::list<ObjectPtr> aResult;
186 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
187 owner()->document());
189 std::shared_ptr<Model_Data> aData =
190 std::dynamic_pointer_cast<Model_Data>(theAttr->owner()->data());
191 if (aData.get() && aData->isValid()) {
192 TDF_Label anObjLab = aData->label().Father();
193 const TDF_LabelList& aList = myRef->List();
194 const TDataStd_ListOfExtendedString& anIDList = myIDs->List();
195 TDF_ListIteratorOfLabelList aLIter(aList);
196 TDataStd_ListIteratorOfListOfExtendedString anIDIter(anIDList);
197 for (; aLIter.More(); aLIter.Next(), anIDIter.Next()) {
198 if (anIDIter.Value() == theAttr->id().c_str() && aLIter.Value().IsEqual(anObjLab)) {
207 bool Model_AttributeRefAttrList::isAttribute(const int theIndex) const
209 const TDataStd_ListOfExtendedString& anIDList = myIDs->List();
210 TDataStd_ListIteratorOfListOfExtendedString anIDIter(anIDList);
212 for (; anIDIter.More(); anIDIter.Next()) {
214 if (anIndex == theIndex) {
215 return !anIDIter.Value().IsEmpty();
221 ObjectPtr Model_AttributeRefAttrList::object(const int theIndex) const
223 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
224 owner()->document());
226 const TDF_LabelList& aList = myRef->List();
228 for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) {
230 if (anIndex == theIndex) {
231 if (aLIter.Value().IsNull()) { // null label => null sub
234 return aDoc->objects()->object(aLIter.Value());
241 AttributePtr Model_AttributeRefAttrList::attribute(const int theIndex) const
243 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
244 owner()->document());
247 const TDF_LabelList& aList = myRef->List();
248 const TDataStd_ListOfExtendedString& anIDList = myIDs->List();
249 TDF_ListIteratorOfLabelList aLIter(aList);
250 TDataStd_ListIteratorOfListOfExtendedString anIDIter(anIDList);
251 for (; aLIter.More(); aLIter.Next(), anIDIter.Next()) {
253 if (anIndex == theIndex) {
254 if (aLIter.Value().IsNull()) { // null label => null sub
255 return AttributePtr();
257 return aDoc->objects()->object(aLIter.Value())->data()->
258 attribute(TCollection_AsciiString(anIDIter.Value().ToExtString()).ToCString());
262 return AttributePtr();
265 void Model_AttributeRefAttrList::removeLast()
267 // remove from the both lists by clearing the list and then appending one by one
268 // TODO: in OCCT 7.0 there are methods for removing by index, seems will be more optimal
269 std::set<int> aLastSet;
270 aLastSet.insert(size() - 1);
274 void Model_AttributeRefAttrList::remove(const std::set<int>& theIndices)
276 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
277 owner()->document());
278 if (aDoc && !myRef->IsEmpty()) {
279 // remove from the both lists by clearing the list and then appending one by one
280 // TODO: in OCCT 7.0 there are methods for removing by index, seems will be more optimal
281 TDF_LabelList aRefList = myRef->List();
283 TDataStd_ListOfExtendedString anIDList = myIDs->List();
285 bool aOneisDeleted = false;
286 TDF_ListIteratorOfLabelList aRefIter(aRefList);
287 TDataStd_ListIteratorOfListOfExtendedString anIDIter(anIDList);
288 for (int anIndex = 0; aRefIter.More(); aRefIter.Next(), anIDIter.Next(), anIndex++) {
289 if (theIndices.find(anIndex) == theIndices.end()) { // not found
290 myRef->Append(aRefIter.Value());
291 myIDs->Append(anIDIter.Value());
292 } else { // found, so need to update the dependencies
293 aOneisDeleted = true;
294 ObjectPtr anObj = aDoc->objects()->object(aRefIter.Value());
296 REMOVE_BACK_REF(anObj);
301 owner()->data()->sendAttributeUpdated(this);
306 Model_AttributeRefAttrList::Model_AttributeRefAttrList(TDF_Label& theLabel)
308 myIsInitialized = theLabel.FindAttribute(TDataStd_ReferenceList::GetID(), myRef) == Standard_True;
309 if (!myIsInitialized) {
310 myRef = TDataStd_ReferenceList::Set(theLabel);
311 myIDs = TDataStd_ExtStringList::Set(theLabel);
313 theLabel.FindAttribute(TDataStd_ExtStringList::GetID(), myIDs);