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())));
150 aResult.push_back(std::pair<ObjectPtr, AttributePtr>(ObjectPtr(), AttributePtr()));
157 bool Model_AttributeRefAttrList::isInList(const ObjectPtr& theObj)
162 std::list<ObjectPtr> aResult;
163 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
164 owner()->document());
166 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theObj->data());
167 if (aData.get() && aData->isValid()) {
168 TDF_Label anObjLab = aData->label().Father();
169 const TDF_LabelList& aList = myRef->List();
170 const TDataStd_ListOfExtendedString& anIDList = myIDs->List();
171 TDF_ListIteratorOfLabelList aLIter(aList);
172 TDataStd_ListIteratorOfListOfExtendedString anIDIter(anIDList);
173 for (; aLIter.More(); aLIter.Next(), anIDIter.Next()) {
174 if (anIDIter.Value().IsEmpty() && aLIter.Value().IsEqual(anObjLab)) {
183 bool Model_AttributeRefAttrList::isInList(const AttributePtr& theAttr)
185 if (!theAttr->owner().get()) {
188 std::list<ObjectPtr> aResult;
189 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
190 owner()->document());
192 std::shared_ptr<Model_Data> aData =
193 std::dynamic_pointer_cast<Model_Data>(theAttr->owner()->data());
194 if (aData.get() && aData->isValid()) {
195 TDF_Label anObjLab = aData->label().Father();
196 const TDF_LabelList& aList = myRef->List();
197 const TDataStd_ListOfExtendedString& anIDList = myIDs->List();
198 TDF_ListIteratorOfLabelList aLIter(aList);
199 TDataStd_ListIteratorOfListOfExtendedString anIDIter(anIDList);
200 for (; aLIter.More(); aLIter.Next(), anIDIter.Next()) {
201 if (anIDIter.Value() == theAttr->id().c_str() && aLIter.Value().IsEqual(anObjLab)) {
210 bool Model_AttributeRefAttrList::isAttribute(const int theIndex) const
212 const TDataStd_ListOfExtendedString& anIDList = myIDs->List();
213 TDataStd_ListIteratorOfListOfExtendedString anIDIter(anIDList);
215 for (; anIDIter.More(); anIDIter.Next()) {
217 if (anIndex == theIndex) {
218 return !anIDIter.Value().IsEmpty();
224 ObjectPtr Model_AttributeRefAttrList::object(const int theIndex) const
226 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
227 owner()->document());
229 const TDF_LabelList& aList = myRef->List();
231 for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) {
233 if (anIndex == theIndex) {
234 if (aLIter.Value().IsNull()) { // null label => null sub
237 return aDoc->objects()->object(aLIter.Value());
244 AttributePtr Model_AttributeRefAttrList::attribute(const int theIndex) const
246 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
247 owner()->document());
250 const TDF_LabelList& aList = myRef->List();
251 const TDataStd_ListOfExtendedString& anIDList = myIDs->List();
252 TDF_ListIteratorOfLabelList aLIter(aList);
253 TDataStd_ListIteratorOfListOfExtendedString anIDIter(anIDList);
254 for (; aLIter.More(); aLIter.Next(), anIDIter.Next()) {
256 if (anIndex == theIndex) {
257 if (aLIter.Value().IsNull()) { // null label => null sub
258 return AttributePtr();
260 return aDoc->objects()->object(aLIter.Value())->data()->
261 attribute(TCollection_AsciiString(anIDIter.Value().ToExtString()).ToCString());
265 return AttributePtr();
268 void Model_AttributeRefAttrList::removeLast()
270 // remove from the both lists by clearing the list and then appending one by one
271 // TODO: in OCCT 7.0 there are methods for removing by index, seems will be more optimal
272 std::set<int> aLastSet;
273 aLastSet.insert(size() - 1);
277 void Model_AttributeRefAttrList::remove(const std::set<int>& theIndices)
279 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
280 owner()->document());
281 if (aDoc && !myRef->IsEmpty()) {
282 // remove from the both lists by clearing the list and then appending one by one
283 // TODO: in OCCT 7.0 there are methods for removing by index, seems will be more optimal
284 TDF_LabelList aRefList = myRef->List();
286 TDataStd_ListOfExtendedString anIDList = myIDs->List();
288 bool aOneisDeleted = false;
289 TDF_ListIteratorOfLabelList aRefIter(aRefList);
290 TDataStd_ListIteratorOfListOfExtendedString anIDIter(anIDList);
291 for (int anIndex = 0; aRefIter.More(); aRefIter.Next(), anIDIter.Next(), anIndex++) {
292 if (theIndices.find(anIndex) == theIndices.end()) { // not found
293 myRef->Append(aRefIter.Value());
294 myIDs->Append(anIDIter.Value());
295 } else { // found, so need to update the dependencies
296 aOneisDeleted = true;
297 ObjectPtr anObj = aDoc->objects()->object(aRefIter.Value());
299 REMOVE_BACK_REF(anObj);
304 owner()->data()->sendAttributeUpdated(this);
309 Model_AttributeRefAttrList::Model_AttributeRefAttrList(TDF_Label& theLabel)
311 myIsInitialized = theLabel.FindAttribute(TDataStd_ReferenceList::GetID(), myRef) == Standard_True;
312 if (!myIsInitialized) {
313 myRef = TDataStd_ReferenceList::Set(theLabel);
314 myIDs = TDataStd_ExtStringList::Set(theLabel);
316 theLabel.FindAttribute(TDataStd_ExtStringList::GetID(), myIDs);