Salome HOME
updated copyright message
[modules/shaper.git] / src / Model / Model_AttributeRefList.cpp
1 // Copyright (C) 2014-2023  CEA, EDF
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 email : webmaster.salome@opencascade.com
18 //
19
20 #include "Model_AttributeRefList.h"
21 #include "Model_Application.h"
22 #include "Model_Data.h"
23 #include "Model_Objects.h"
24 #include <ModelAPI_Feature.h>
25 #include <TDF_ListIteratorOfLabelList.hxx>
26 #include <TDF_Tool.hxx>
27 #include <TDataStd_ListIteratorOfListOfExtendedString.hxx>
28
29 void Model_AttributeRefList::append(ObjectPtr theObject)
30 {
31   if (owner()->document() == theObject->document()) {
32     std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theObject->data());
33     myRef->Append(aData->label().Father());  // store label of the object
34   } else if (theObject.get() && theObject->data()->isValid()) { // reference to the other document
35     myRef->Append(myRef->Label());
36     // if these attributes exist, the link is external: keep reference to access the label
37     std::ostringstream anIdString; // string with document Id
38     anIdString<<theObject->document()->id();
39     myExtDocRef->Append(anIdString.str().c_str());
40     std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theObject->data());
41     TCollection_AsciiString anEntry;
42     TDF_Tool::Entry(aData->label().Father(), anEntry);
43     myExtDocRef->Append(anEntry);
44   } else return; // something is wrong
45
46   if (myHashUsed) {
47     myHashObjects.insert(theObject);
48     myHashIndex[myRef->Extent() - 1] = theObject;
49     myHashIndexNoEmpty[int(myHashIndexNoEmpty.size())] = theObject;
50   }
51
52   // do it before the transaction finish to make just created/removed objects know dependencies
53   // and reference from composite feature is removed automatically
54   ADD_BACK_REF(theObject);
55   owner()->data()->sendAttributeUpdated(this);
56 }
57
58 void Model_AttributeRefList::remove(ObjectPtr theObject)
59 {
60   eraseHash();
61   if (theObject.get() != NULL) {
62     if (owner()->document() == theObject->document()) {
63       std::shared_ptr<Model_Data> aData;
64       aData = std::dynamic_pointer_cast<Model_Data>(theObject->data());
65       myRef->Remove(aData->label().Father());
66       REMOVE_BACK_REF(theObject);
67       owner()->data()->sendAttributeUpdated(this);
68     } else {
69       // LCOV_EXCL_START
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       // LCOV_EXCL_STOP
106     }
107   }
108   else { // in case of empty object remove, the first empty object is removed from the list
109     std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
110         owner()->document());
111     if (aDoc) {
112       const TDF_LabelList& aList = myRef->List();
113       for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) {
114         ObjectPtr anObj = aDoc->objects()->object(aLIter.Value());
115         if (anObj.get() == NULL) {
116           myRef->Remove(aLIter.Value());
117           REMOVE_BACK_REF(theObject);
118           owner()->data()->sendAttributeUpdated(this);
119           break;
120         }
121       }
122     }
123   }
124 }
125
126 void Model_AttributeRefList::clear()
127 {
128   std::list<ObjectPtr> anOldList = list();
129   myRef->Clear();
130   std::list<ObjectPtr>::iterator anOldIter = anOldList.begin();
131   for(; anOldIter != anOldList.end(); anOldIter++) {
132     REMOVE_BACK_REF((*anOldIter));
133   }
134   myExtDocRef->Clear();
135   eraseHash();
136   owner()->data()->sendAttributeUpdated(this);
137 }
138
139 int Model_AttributeRefList::size(const bool theWithEmpty) const
140 {
141   if (theWithEmpty)
142     return myRef->Extent();
143
144   if (myHashUsed)
145     return int(myHashIndexNoEmpty.size());
146
147   int aResult = 0;
148   const TDF_LabelList& aList = myRef->List();
149   for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) {
150     if (!aLIter.Value().IsNull() && !aLIter.Value().IsRoot()) aResult++;
151   }
152   return aResult;
153 }
154
155 bool Model_AttributeRefList::isInitialized()
156 {
157   if (size(false) == 0) {
158     // empty list is not initialized list: sketch will be not valid after add/undo
159     return false;
160   }
161   return ModelAPI_AttributeRefList::isInitialized();
162 }
163
164 ObjectPtr Model_AttributeRefList::iteratedObject(TDF_ListIteratorOfLabelList& theLIter,
165     TDataStd_ListIteratorOfListOfExtendedString& theExtIter,
166     std::shared_ptr<Model_Document> theDoc) const
167 {
168   ObjectPtr anObj;
169   if (!theLIter.Value().IsNull() && !theLIter.Value().IsRoot()) {
170     if (theLIter.Value() == myRef->Label()) { // external document object
171       int anID = atoi(TCollection_AsciiString(theExtIter.Value()).ToCString());
172       theExtIter.Next();
173       DocumentPtr aRefDoc = Model_Application::getApplication()->document(anID);
174       if (aRefDoc.get()) {
175         std::shared_ptr<Model_Document> aDR = std::dynamic_pointer_cast<Model_Document>(aRefDoc);
176         TDF_Label aRefLab;
177         TDF_Tool::Label(aDR->objects()->featuresLabel().Data(),
178           TCollection_AsciiString(theExtIter.Value()).ToCString(), aRefLab);
179         if (!aRefLab.IsNull()) {
180           anObj = aDR->objects()->object(aRefLab);
181         }
182       }
183       theExtIter.Next();
184     } else { // internal document object
185       TDF_Label aLab = theLIter.Value();
186       if (!aLab.IsNull())
187         anObj = theDoc->objects()->object(theLIter.Value());
188     }
189   }
190   return anObj;
191 }
192
193 std::list<ObjectPtr> Model_AttributeRefList::list()
194 {
195   createHash();
196   std::list<ObjectPtr> aResult;
197   std::map<int, ObjectPtr>::iterator aHashIter = myHashIndex.begin();
198   for(; aHashIter != myHashIndex.end(); aHashIter++) {
199     aResult.push_back(aHashIter->second);
200   }
201   return aResult;
202 }
203
204 bool Model_AttributeRefList::isInList(const ObjectPtr& theObj)
205 {
206   if(!theObj.get()) {
207     return false;
208   }
209   createHash();
210   return myHashObjects.count(theObj) != 0;
211 }
212
213 ObjectPtr Model_AttributeRefList::object(const int theIndex, const bool theWithEmpty)
214 {
215   createHash();
216   std::map<int, ObjectPtr>::iterator aFind;
217   if (theWithEmpty) {
218     aFind = myHashIndex.find(theIndex);
219     if (aFind == myHashIndex.end())
220       return ObjectPtr();
221   } else {
222     aFind = myHashIndexNoEmpty.find(theIndex);
223     if (aFind == myHashIndexNoEmpty.end())
224       return ObjectPtr();
225   }
226   return aFind->second;
227 }
228
229 void Model_AttributeRefList::substitute(const ObjectPtr& theCurrent, const ObjectPtr& theNew)
230 {
231   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
232       owner()->document());
233   if (aDoc) {
234     std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theCurrent->data());
235     if (aData.get() && aData->isValid()) {
236       TDF_Label aCurrentLab = aData->label().Father();
237       TDF_Label aNewLab;
238       if (theNew.get() && theNew->data()->isValid()) { // the new may be null
239         std::shared_ptr<Model_Data> aNewData =
240           std::dynamic_pointer_cast<Model_Data>(theNew->data());
241         aNewLab = aNewData->label().Father();
242       } else {
243         aNewLab = aCurrentLab.Root(); // root means null object
244       }
245       // do the substitution
246       eraseHash();
247       ADD_BACK_REF(theNew);
248       if (myRef->InsertAfter(aNewLab, aCurrentLab)) {
249         myRef->Remove(aCurrentLab);
250         REMOVE_BACK_REF(theCurrent);
251       }
252       owner()->data()->sendAttributeUpdated(this);
253     }
254   }
255 }
256
257 void Model_AttributeRefList::removeLast()
258 {
259   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
260       owner()->document());
261   if (aDoc && !myRef->IsEmpty()) {
262     ObjectPtr anObj = aDoc->objects()->object(myRef->Last());
263     if (anObj.get()) {
264       eraseHash();
265       myRef->Remove(myRef->Last());
266       REMOVE_BACK_REF(anObj);
267       owner()->data()->sendAttributeUpdated(this);
268     }
269   }
270 }
271
272 void Model_AttributeRefList::remove(const std::set<int>& theIndices)
273 {
274   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
275       owner()->document());
276   if (aDoc && !myRef->IsEmpty()) {
277     // collect labels that will be removed
278     TDF_LabelList aLabelsToRemove;
279     TDF_ListIteratorOfLabelList aLabIter(myRef->List());
280     for(int aCurrent = 0; aLabIter.More(); aLabIter.Next(), aCurrent++) {
281       if (theIndices.find(aCurrent) != theIndices.end())
282         aLabelsToRemove.Append(aLabIter.Value());
283     }
284     // remove labels
285     for(aLabIter.Initialize(aLabelsToRemove); aLabIter.More(); aLabIter.Next()) {
286       ObjectPtr anObj = aDoc->objects()->object(aLabIter.Value());
287       if (anObj.get()) {
288         myRef->Remove(aLabIter.Value());
289         REMOVE_BACK_REF(anObj);
290       }
291     }
292     if (!aLabelsToRemove.IsEmpty()) {
293       eraseHash();
294       owner()->data()->sendAttributeUpdated(this);
295     }
296   }
297 }
298
299 Model_AttributeRefList::Model_AttributeRefList(TDF_Label& theLabel)
300 {
301   myLab = theLabel;
302   reinit();
303 }
304
305 void Model_AttributeRefList::reinit()
306 {
307   myIsInitialized = myLab.FindAttribute(TDataStd_ReferenceList::GetID(), myRef) == Standard_True;
308   if (!myIsInitialized) {
309     myRef = TDataStd_ReferenceList::Set(myLab);
310   }
311   if (!myLab.FindAttribute(TDataStd_ExtStringList::GetID(), myExtDocRef)) {
312     myExtDocRef = TDataStd_ExtStringList::Set(myLab);
313   }
314   eraseHash();
315 }
316
317 void Model_AttributeRefList::createHash()
318 {
319   if (myHashUsed)
320     return;
321   eraseHash();
322   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
323     owner()->document());
324   if (aDoc) {
325     const TDF_LabelList& aList = myRef->List();
326     TDataStd_ListIteratorOfListOfExtendedString anExtIter(myExtDocRef->List());
327     for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) {
328       ObjectPtr anObj = iteratedObject(aLIter, anExtIter, aDoc);
329       myHashIndex[int(myHashIndex.size())] = anObj;
330       if (anObj.get()) {
331         myHashIndexNoEmpty[int(myHashIndexNoEmpty.size())] = anObj;
332         myHashObjects.insert(anObj);
333       }
334     }
335   }
336   if (!myHashObjects.empty()) // on open document with multi-rotation referenced have no results
337     myHashUsed = true;
338 }
339
340 void Model_AttributeRefList::eraseHash()
341 {
342   myHashObjects.clear();
343   myHashIndex.clear();
344   myHashIndexNoEmpty.clear();
345   myHashUsed = false;
346 }