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