Salome HOME
Fix for the tests SketchPlugin_TestMirror and SketchPlugin_TestMultiRotation in RefLi...
[modules/shaper.git] / src / Model / Model_AttributeRefList.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "Model_AttributeRefList.h"
22 #include "Model_Application.h"
23 #include "Model_Data.h"
24 #include "Model_Objects.h"
25 #include <ModelAPI_Feature.h>
26 #include <TDF_ListIteratorOfLabelList.hxx>
27 #include <TDF_Tool.hxx>
28 #include <TDataStd_ListIteratorOfListOfExtendedString.hxx>
29
30 void Model_AttributeRefList::append(ObjectPtr theObject)
31 {
32   if (owner()->document() == theObject->document()) {
33     std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theObject->data());
34     myRef->Append(aData->label().Father());  // store label of the object
35   } else if (theObject.get() && theObject->data()->isValid()) { // reference to the other document
36     myRef->Append(myRef->Label());
37     // if these attributes exist, the link is external: keep reference to access the label
38     std::ostringstream anIdString; // string with document Id
39     anIdString<<theObject->document()->id();
40     myExtDocRef->Append(anIdString.str().c_str());
41     std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theObject->data());
42     TCollection_AsciiString anEntry;
43     TDF_Tool::Entry(aData->label().Father(), anEntry);
44     myExtDocRef->Append(anEntry);
45   } else return; // something is wrong
46
47   if (myHashUsed) {
48     myHashObjects.insert(theObject);
49     myHashIndex[myRef->Extent() - 1] = theObject;
50     myHashIndexNoEmpty[int(myHashIndexNoEmpty.size())] = theObject;
51   }
52
53   // do it before the transaction finish to make just created/removed objects know dependencies
54   // and reference from composite feature is removed automatically
55   ADD_BACK_REF(theObject);
56   owner()->data()->sendAttributeUpdated(this);
57 }
58
59 void Model_AttributeRefList::remove(ObjectPtr theObject)
60 {
61   eraseHash();
62   if (theObject.get() != NULL) {
63     if (owner()->document() == theObject->document()) {
64       std::shared_ptr<Model_Data> aData;
65       aData = std::dynamic_pointer_cast<Model_Data>(theObject->data());
66       myRef->Remove(aData->label().Father());
67       REMOVE_BACK_REF(theObject);
68       owner()->data()->sendAttributeUpdated(this);
69     } else {
70       // create new lists because for the current moment remove one of the duplicated elements
71       // from the list is buggy
72       TDF_LabelList anOldList = myRef->List();
73       myRef->Clear();
74       TDataStd_ListOfExtendedString anOldExts = myExtDocRef->List();
75       myExtDocRef->Clear();
76
77       std::ostringstream anIdString; // string with document Id
78       anIdString<<theObject->document()->id();
79       std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theObject->data());
80       TCollection_AsciiString anEntry;
81       TDF_Tool::Entry(aData->label().Father(), anEntry);
82       bool aFound = false;
83       TDataStd_ListIteratorOfListOfExtendedString anExtIter(anOldExts);
84       for (TDF_ListIteratorOfLabelList aLIter(anOldList); aLIter.More(); aLIter.Next()) {
85         if (aLIter.Value() == myRef->Label()) {
86           if (anExtIter.Value() == anIdString.str().c_str()) {
87             TDataStd_ListIteratorOfListOfExtendedString anExtIter2 = anExtIter;
88             anExtIter2.Next();
89             if (anExtIter2.Value() == anEntry) { // fully matches, so, remove(don't copy)
90               aFound = true;
91               continue;
92             }
93           }
94           myExtDocRef->Append(anExtIter.Value());
95           anExtIter.Next();
96           myExtDocRef->Append(anExtIter.Value());
97           anExtIter.Next();
98         }
99         myRef->Append(aLIter.Value());
100       }
101       if (aFound) {
102         REMOVE_BACK_REF(theObject);
103         owner()->data()->sendAttributeUpdated(this);
104       }
105     }
106   }
107   else { // in case of empty object remove, the first empty object is removed from the list
108     std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
109         owner()->document());
110     if (aDoc) {
111       const TDF_LabelList& aList = myRef->List();
112       for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) {
113         ObjectPtr anObj = aDoc->objects()->object(aLIter.Value());
114         if (anObj.get() == NULL) {
115           myRef->Remove(aLIter.Value());
116           REMOVE_BACK_REF(theObject);
117           owner()->data()->sendAttributeUpdated(this);
118           break;
119         }
120       }
121     }
122   }
123 }
124
125 void Model_AttributeRefList::clear()
126 {
127   std::list<ObjectPtr> anOldList = list();
128   myRef->Clear();
129   std::list<ObjectPtr>::iterator anOldIter = anOldList.begin();
130   for(; anOldIter != anOldList.end(); anOldIter++) {
131     REMOVE_BACK_REF((*anOldIter));
132   }
133   myExtDocRef->Clear();
134   eraseHash();
135   owner()->data()->sendAttributeUpdated(this);
136 }
137
138 int Model_AttributeRefList::size(const bool theWithEmpty) const
139 {
140   if (theWithEmpty)
141     return myRef->Extent();
142
143   if (myHashUsed)
144     return int(myHashIndexNoEmpty.size());
145
146   int aResult = 0;
147   const TDF_LabelList& aList = myRef->List();
148   for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) {
149     if (!aLIter.Value().IsNull() && !aLIter.Value().IsRoot()) aResult++;
150   }
151   return aResult;
152 }
153
154 bool Model_AttributeRefList::isInitialized()
155 {
156   if (size(false) == 0) {
157     // empty list is not initialized list: sketch will be not valid after add/undo
158     return false;
159   }
160   return ModelAPI_AttributeRefList::isInitialized();
161 }
162
163 ObjectPtr Model_AttributeRefList::iteratedObject(TDF_ListIteratorOfLabelList& theLIter,
164     TDataStd_ListIteratorOfListOfExtendedString& theExtIter,
165     std::shared_ptr<Model_Document> theDoc) const
166 {
167   ObjectPtr anObj;
168   if (!theLIter.Value().IsNull() && !theLIter.Value().IsRoot()) {
169     if (theLIter.Value() == myRef->Label()) { // external document object
170       int anID = atoi(TCollection_AsciiString(theExtIter.Value()).ToCString());
171       theExtIter.Next();
172       DocumentPtr aRefDoc = Model_Application::getApplication()->document(anID);
173       if (aRefDoc.get()) {
174         std::shared_ptr<Model_Document> aDR = std::dynamic_pointer_cast<Model_Document>(aRefDoc);
175         TDF_Label aRefLab;
176         TDF_Tool::Label(aDR->objects()->featuresLabel().Data(),
177           TCollection_AsciiString(theExtIter.Value()).ToCString(), aRefLab);
178         if (!aRefLab.IsNull()) {
179           anObj = aDR->objects()->object(aRefLab);
180         }
181       }
182       theExtIter.Next();
183     } else { // internal document object
184       anObj = theDoc->objects()->object(theLIter.Value());
185     }
186   }
187   return anObj;
188 }
189
190 std::list<ObjectPtr> Model_AttributeRefList::list()
191 {
192   createHash();
193   std::list<ObjectPtr> aResult;
194   std::map<int, ObjectPtr>::iterator aHashIter = myHashIndex.begin();
195   for(; aHashIter != myHashIndex.end(); aHashIter++) {
196     aResult.push_back(aHashIter->second);
197   }
198   return aResult;
199 }
200
201 bool Model_AttributeRefList::isInList(const ObjectPtr& theObj)
202 {
203   if(!theObj.get()) {
204     return false;
205   }
206   createHash();
207   return myHashObjects.count(theObj) != 0;
208 }
209
210 ObjectPtr Model_AttributeRefList::object(const int theIndex, const bool theWithEmpty)
211 {
212   createHash();
213   std::map<int, ObjectPtr>::iterator aFind;
214   if (theWithEmpty) {
215     aFind = myHashIndex.find(theIndex);
216     if (aFind == myHashIndex.end())
217       return ObjectPtr();
218   } else {
219     aFind = myHashIndexNoEmpty.find(theIndex);
220     if (aFind == myHashIndexNoEmpty.end())
221       return ObjectPtr();
222   }
223   return aFind->second;
224 }
225
226 void Model_AttributeRefList::substitute(const ObjectPtr& theCurrent, const ObjectPtr& theNew)
227 {
228   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
229       owner()->document());
230   if (aDoc) {
231     std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theCurrent->data());
232     if (aData.get() && aData->isValid()) {
233       TDF_Label aCurrentLab = aData->label().Father();
234       TDF_Label aNewLab;
235       if (theNew.get() && theNew->data()->isValid()) { // the new may be null
236         std::shared_ptr<Model_Data> aNewData =
237           std::dynamic_pointer_cast<Model_Data>(theNew->data());
238         aNewLab = aNewData->label().Father();
239       } else {
240         aNewLab = aCurrentLab.Root(); // root means null object
241       }
242       // do the substitution
243       eraseHash();
244       ADD_BACK_REF(theNew);
245       if (myRef->InsertAfter(aNewLab, aCurrentLab)) {
246         myRef->Remove(aCurrentLab);
247         REMOVE_BACK_REF(theCurrent);
248       }
249       owner()->data()->sendAttributeUpdated(this);
250     }
251   }
252 }
253
254 void Model_AttributeRefList::exchange(const ObjectPtr& theObject1, const ObjectPtr& theObject2)
255 {
256   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
257       owner()->document());
258   if (aDoc) {
259     std::shared_ptr<Model_Data> aData1 = std::dynamic_pointer_cast<Model_Data>(theObject1->data());
260     if (aData1.get() && aData1->isValid()) {
261       TDF_Label aLab1 = aData1->label().Father();
262       if (theObject2.get() && theObject2->data()->isValid()) { // the new may be null
263         std::shared_ptr<Model_Data> aData2 =
264           std::dynamic_pointer_cast<Model_Data>(theObject2->data());
265         if (aData2.get() && aData2->isValid()) {
266           eraseHash();
267           TDF_Label aLab2 = aData2->label().Father();
268           // do the substitution: use the temporary label, as usually in exchange
269           TDF_Label aTmpLab = aLab1.Root();
270           if (myRef->InsertAfter(aTmpLab, aLab1)) {
271             myRef->Remove(aLab1);
272           }
273           if (myRef->InsertAfter(aLab1, aLab2)) {
274             myRef->Remove(aLab2);
275           }
276           if (myRef->InsertAfter(aLab2, aTmpLab)) {
277             myRef->Remove(aTmpLab);
278           }
279           owner()->data()->sendAttributeUpdated(this);
280         }
281       }
282     }
283   }
284 }
285
286 void Model_AttributeRefList::removeLast()
287 {
288   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
289       owner()->document());
290   if (aDoc && !myRef->IsEmpty()) {
291     ObjectPtr anObj = aDoc->objects()->object(myRef->Last());
292     if (anObj.get()) {
293       eraseHash();
294       myRef->Remove(myRef->Last());
295       REMOVE_BACK_REF(anObj);
296       owner()->data()->sendAttributeUpdated(this);
297     }
298   }
299 }
300
301 void Model_AttributeRefList::remove(const std::set<int>& theIndices)
302 {
303   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
304       owner()->document());
305   if (aDoc && !myRef->IsEmpty()) {
306     // collect labels that will be removed
307     TDF_LabelList aLabelsToRemove;
308     TDF_ListIteratorOfLabelList aLabIter(myRef->List());
309     for(int aCurrent = 0; aLabIter.More(); aLabIter.Next(), aCurrent++) {
310       if (theIndices.find(aCurrent) != theIndices.end())
311         aLabelsToRemove.Append(aLabIter.Value());
312     }
313     // remove labels
314     for(aLabIter.Initialize(aLabelsToRemove); aLabIter.More(); aLabIter.Next()) {
315       ObjectPtr anObj = aDoc->objects()->object(aLabIter.Value());
316       if (anObj.get()) {
317         myRef->Remove(aLabIter.Value());
318         REMOVE_BACK_REF(anObj);
319       }
320     }
321     if (!aLabelsToRemove.IsEmpty()) {
322       eraseHash();
323       owner()->data()->sendAttributeUpdated(this);
324     }
325   }
326 }
327
328 Model_AttributeRefList::Model_AttributeRefList(TDF_Label& theLabel)
329 {
330   myLab = theLabel;
331   reinit();
332 }
333
334 void Model_AttributeRefList::reinit()
335 {
336   myIsInitialized = myLab.FindAttribute(TDataStd_ReferenceList::GetID(), myRef) == Standard_True;
337   if (!myIsInitialized) {
338     myRef = TDataStd_ReferenceList::Set(myLab);
339   }
340   if (!myLab.FindAttribute(TDataStd_ExtStringList::GetID(), myExtDocRef)) {
341     myExtDocRef = TDataStd_ExtStringList::Set(myLab);
342   }
343   eraseHash();
344 }
345
346 void Model_AttributeRefList::createHash()
347 {
348   if (myHashUsed)
349     return;
350   eraseHash();
351   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
352     owner()->document());
353   if (aDoc) {
354     const TDF_LabelList& aList = myRef->List();
355     TDataStd_ListIteratorOfListOfExtendedString anExtIter(myExtDocRef->List());
356     for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) {
357       ObjectPtr anObj = iteratedObject(aLIter, anExtIter, aDoc);
358       myHashIndex[int(myHashIndex.size())] = anObj;
359       if (anObj.get()) {
360         myHashIndexNoEmpty[int(myHashIndexNoEmpty.size())] = anObj;
361         myHashObjects.insert(anObj);
362       }
363     }
364     myHashUsed = true;
365   }
366 }
367
368
369 void Model_AttributeRefList::eraseHash()
370 {
371   myHashObjects.clear();
372   myHashIndex.clear();
373   myHashIndexNoEmpty.clear();
374   myHashUsed = false;
375 }