Salome HOME
Implementation of the issue #1307. Also make connected parameters from different...
[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 "Model_Objects.h"
11 #include <ModelAPI_Feature.h>
12 #include <TDF_ListIteratorOfLabelList.hxx>
13 #include <TDF_Tool.hxx>
14 #include <TDataStd_ListIteratorOfListOfExtendedString.hxx>
15
16 using namespace std;
17
18 void Model_AttributeRefList::append(ObjectPtr theObject)
19 {
20   if (owner()->document() == theObject->document()) {
21     std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theObject->data());
22     myRef->Append(aData->label().Father());  // store label of the object
23   } else if (theObject.get() && theObject->data()->isValid()) { // reference to the other document
24     myRef->Append(myRef->Label());
25     // if these attributes exist, the link is external: keep reference to access the label
26     std::ostringstream anIdString; // string with document Id
27     anIdString<<theObject->document()->id();
28     myExtDocRef->Append(anIdString.str().c_str());
29     std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theObject->data());
30     TCollection_AsciiString anEntry;
31     TDF_Tool::Entry(aData->label().Father(), anEntry);
32     myExtDocRef->Append(anEntry);
33   } else return; // something is wrong
34
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(theObject);
38   owner()->data()->sendAttributeUpdated(this);
39 }
40
41 void Model_AttributeRefList::remove(ObjectPtr theObject)
42 {
43   if (theObject.get() != NULL) {
44     if (owner()->document() == theObject->document()) {
45       std::shared_ptr<Model_Data> aData;
46       aData = std::dynamic_pointer_cast<Model_Data>(theObject->data());
47       myRef->Remove(aData->label().Father());
48       REMOVE_BACK_REF(theObject);
49       owner()->data()->sendAttributeUpdated(this);
50     } else {
51       // create new lists because for the current moment remove one of the duplicated elements
52       // from the list is buggy
53       TDF_LabelList anOldList = myRef->List();
54       myRef->Clear();
55       TDataStd_ListOfExtendedString anOldExts = myExtDocRef->List();
56       myExtDocRef->Clear();
57
58       std::ostringstream anIdString; // string with document Id
59       anIdString<<theObject->document()->id();
60       std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theObject->data());
61       TCollection_AsciiString anEntry;
62       TDF_Tool::Entry(aData->label().Father(), anEntry);
63       bool aFound = false;
64       TDataStd_ListIteratorOfListOfExtendedString anExtIter(anOldExts);
65       for (TDF_ListIteratorOfLabelList aLIter(anOldList); aLIter.More(); aLIter.Next()) {
66         if (aLIter.Value() == myRef->Label()) {
67           if (anExtIter.Value() == anIdString.str().c_str()) {
68             TDataStd_ListIteratorOfListOfExtendedString anExtIter2 = anExtIter;
69             anExtIter2.Next();
70             if (anExtIter2.Value() == anEntry) { // fully maches, so, remove(don't copy)
71               aFound = true;
72               continue;
73             }
74           }
75           myExtDocRef->Append(anExtIter.Value());
76           anExtIter.Next();
77           myExtDocRef->Append(anExtIter.Value());
78           anExtIter.Next();
79         }
80         myRef->Append(aLIter.Value());
81       }
82       if (aFound) {
83         REMOVE_BACK_REF(theObject);
84         owner()->data()->sendAttributeUpdated(this);
85       }
86     }
87   }
88   else { // in case of empty object remove, the first empty object is removed from the list
89     std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
90         owner()->document());
91     if (aDoc) {
92       const TDF_LabelList& aList = myRef->List();
93       for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) {
94         ObjectPtr anObj = aDoc->objects()->object(aLIter.Value());
95         if (anObj.get() == NULL) {
96           myRef->Remove(aLIter.Value());
97           REMOVE_BACK_REF(theObject);
98           owner()->data()->sendAttributeUpdated(this);
99           break;
100         }
101       }
102     }
103   }
104 }
105
106 void Model_AttributeRefList::clear()
107 {
108   std::list<ObjectPtr> anOldList = list();
109   myRef->Clear();
110   std::list<ObjectPtr>::iterator anOldIter = anOldList.begin();
111   for(; anOldIter != anOldList.end(); anOldIter++) {
112     REMOVE_BACK_REF((*anOldIter));
113   }
114   myExtDocRef->Clear();
115   owner()->data()->sendAttributeUpdated(this);
116 }
117
118 int Model_AttributeRefList::size(const bool theWithEmpty) const
119 {
120   if (theWithEmpty)
121     return myRef->Extent();
122   int aResult = 0;
123   const TDF_LabelList& aList = myRef->List();
124   for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) {
125     if (!aLIter.Value().IsNull() && !aLIter.Value().IsRoot()) aResult++;
126   }
127   return aResult;
128 }
129
130 bool Model_AttributeRefList::isInitialized()
131 {
132   if (size(false) == 0) { // empty list is not initialized list: sketch will be not valid after add/undo
133     return false;
134   }
135   return ModelAPI_AttributeRefList::isInitialized();
136 }
137
138 ObjectPtr Model_AttributeRefList::iteratedObject(TDF_ListIteratorOfLabelList& theLIter,
139     TDataStd_ListIteratorOfListOfExtendedString& theExtIter, 
140     std::shared_ptr<Model_Document> theDoc) const
141 {
142   ObjectPtr anObj;
143   if (!theLIter.Value().IsNull() && !theLIter.Value().IsRoot()) {
144     if (theLIter.Value() == myRef->Label()) { // external document object
145       int anID = atoi(TCollection_AsciiString(theExtIter.Value()).ToCString());
146       theExtIter.Next();
147       DocumentPtr aRefDoc = Model_Application::getApplication()->document(anID);
148       if (aRefDoc.get()) {
149         std::shared_ptr<Model_Document> aDR = std::dynamic_pointer_cast<Model_Document>(aRefDoc);
150         TDF_Label aRefLab;
151         TDF_Tool::Label(aDR->objects()->featuresLabel().Data(),
152           TCollection_AsciiString(theExtIter.Value()).ToCString(), aRefLab);
153         if (!aRefLab.IsNull()) {
154           anObj = aDR->objects()->object(aRefLab);
155         }
156       }
157       theExtIter.Next();
158     } else { // internal document object
159       anObj = theDoc->objects()->object(theLIter.Value());
160     }
161   }
162   return anObj;
163 }
164
165 list<ObjectPtr> Model_AttributeRefList::list()
166 {
167   std::list<ObjectPtr> aResult;
168   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
169       owner()->document());
170   if (aDoc) {
171     const TDF_LabelList& aList = myRef->List();
172     TDataStd_ListIteratorOfListOfExtendedString anExtIter(myExtDocRef->List());
173     for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) {
174       aResult.push_back(iteratedObject(aLIter, anExtIter, aDoc));
175     }
176   }
177   return aResult;
178 }
179
180 bool Model_AttributeRefList::isInList(const ObjectPtr& theObj)
181 {
182   if(!theObj.get()) {
183     return false;
184   }
185   if (theObj->document() == owner()->document()) { // this document object
186     std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
187         owner()->document());
188     if (aDoc) {
189       std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theObj->data());
190       if (aData.get() && aData->isValid()) {
191         TDF_Label anObjLab = aData->label().Father();
192         const TDF_LabelList& aList = myRef->List();
193         for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) {
194           if (aLIter.Value().IsEqual(anObjLab)) {
195             return true;
196           }
197         }
198       }
199     }
200   } else { // external document object
201     // create new lists because for the current moment remove one of the duplicated elements
202     // from the list is buggy
203     std::ostringstream anIdString; // string with document Id
204     anIdString<<theObj->document()->id();
205     std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theObj->data());
206     TCollection_AsciiString anEntry;
207     TDF_Tool::Entry(aData->label().Father(), anEntry);
208     bool aFound = false;
209     TDataStd_ListIteratorOfListOfExtendedString anExtIter(myExtDocRef->List());
210     for (; anExtIter.More(); anExtIter.Next()) {
211       if (anExtIter.Value() == anIdString.str().c_str()) {
212         anExtIter.Next();
213         if (anExtIter.Value() == anEntry) { // fully maches
214           return true;
215         }
216       } else {
217         anExtIter.Next();
218       }
219     }
220   }
221   return false;
222 }
223
224 ObjectPtr Model_AttributeRefList::object(const int theIndex, const bool theWithEmpty) const
225 {
226   std::shared_ptr<Model_Document> aDoc = 
227     std::dynamic_pointer_cast<Model_Document>(owner()->document());
228   if (aDoc) {
229     int anIndex = -1;
230     TDataStd_ListIteratorOfListOfExtendedString anExtIter(myExtDocRef->List());
231     for (TDF_ListIteratorOfLabelList aLIter(myRef->List()); aLIter.More(); aLIter.Next()) {
232       if (theWithEmpty || (!aLIter.Value().IsNull() && !aLIter.Value().IsRoot()))
233         anIndex++;
234       if (anIndex == theIndex) {
235         return iteratedObject(aLIter, anExtIter, aDoc);
236       }
237       if (aLIter.Value() == myRef->Label()) {
238         anExtIter.Next();
239         anExtIter.Next();
240       }
241     }
242   }
243   return ObjectPtr();
244 }
245
246 void Model_AttributeRefList::substitute(const ObjectPtr& theCurrent, const ObjectPtr& theNew)
247 {
248   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
249       owner()->document());
250   if (aDoc) {
251     std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theCurrent->data());
252     if (aData.get() && aData->isValid()) {
253       TDF_Label aCurrentLab = aData->label().Father();
254       TDF_Label aNewLab;
255       if (theNew.get() && theNew->data()->isValid()) { // the new may be null
256         std::shared_ptr<Model_Data> aNewData = 
257           std::dynamic_pointer_cast<Model_Data>(theNew->data());
258         aNewLab = aNewData->label().Father();
259       } else {
260         aNewLab = aCurrentLab.Root(); // root means null object
261       }
262       // do the substitution
263       ADD_BACK_REF(theNew);
264       if (myRef->InsertAfter(aNewLab, aCurrentLab)) {
265         myRef->Remove(aCurrentLab);
266         REMOVE_BACK_REF(theCurrent);
267       }
268       owner()->data()->sendAttributeUpdated(this);
269     }
270   }
271 }
272
273 void Model_AttributeRefList::exchange(const ObjectPtr& theObject1, const ObjectPtr& theObject2)
274 {
275   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
276       owner()->document());
277   if (aDoc) {
278     std::shared_ptr<Model_Data> aData1 = std::dynamic_pointer_cast<Model_Data>(theObject1->data());
279     if (aData1.get() && aData1->isValid()) {
280       TDF_Label aLab1 = aData1->label().Father();
281       if (theObject2.get() && theObject2->data()->isValid()) { // the new may be null
282         std::shared_ptr<Model_Data> aData2 = 
283           std::dynamic_pointer_cast<Model_Data>(theObject2->data());
284         if (aData2.get() && aData2->isValid()) {
285           TDF_Label aLab2 = aData2->label().Father();
286           // do the substitution: use the temporary label, as usually in exchange
287           TDF_Label aTmpLab = aLab1.Root();
288           if (myRef->InsertAfter(aTmpLab, aLab1)) {
289             myRef->Remove(aLab1);
290           }
291           if (myRef->InsertAfter(aLab1, aLab2)) {
292             myRef->Remove(aLab2);
293           }
294           if (myRef->InsertAfter(aLab2, aTmpLab)) {
295             myRef->Remove(aTmpLab);
296           }
297           owner()->data()->sendAttributeUpdated(this);
298         }
299       }
300     }
301   }
302 }
303
304 void Model_AttributeRefList::removeLast()
305 {
306   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
307       owner()->document());
308   if (aDoc && !myRef->IsEmpty()) {
309     ObjectPtr anObj = aDoc->objects()->object(myRef->Last());
310     if (anObj.get()) {
311       myRef->Remove(myRef->Last());
312       REMOVE_BACK_REF(anObj);
313       owner()->data()->sendAttributeUpdated(this);
314     }
315   }
316 }
317
318 void Model_AttributeRefList::remove(const std::set<int>& theIndices)
319 {
320   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
321       owner()->document());
322   if (aDoc && !myRef->IsEmpty()) {
323     // collet labels that will be removed
324     TDF_LabelList aLabelsToRemove;
325     TDF_ListIteratorOfLabelList aLabIter(myRef->List());
326     for(int aCurrent = 0; aLabIter.More(); aLabIter.Next(), aCurrent++) {
327       if (theIndices.find(aCurrent) != theIndices.end())
328         aLabelsToRemove.Append(aLabIter.Value());
329     }
330     // remove labels
331     for(aLabIter.Initialize(aLabelsToRemove); aLabIter.More(); aLabIter.Next()) {
332       ObjectPtr anObj = aDoc->objects()->object(aLabIter.Value());
333       if (anObj.get()) {
334         myRef->Remove(aLabIter.Value());
335         REMOVE_BACK_REF(anObj);
336       }
337     }
338     if (!aLabelsToRemove.IsEmpty()) {
339       owner()->data()->sendAttributeUpdated(this);
340     }
341   }
342 }
343
344 Model_AttributeRefList::Model_AttributeRefList(TDF_Label& theLabel)
345 {
346   myIsInitialized = theLabel.FindAttribute(TDataStd_ReferenceList::GetID(), myRef) == Standard_True;
347   if (!myIsInitialized) {
348     myRef = TDataStd_ReferenceList::Set(theLabel);
349   }
350   if (!theLabel.FindAttribute(TDataStd_ExtStringList::GetID(), myExtDocRef)) {
351     myExtDocRef = TDataStd_ExtStringList::Set(theLabel);
352   }
353 }