Salome HOME
Merge branch 'SALOME-8.2.0_porting'
[modules/shaper.git] / src / Model / Model_AttributeSelectionList.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        Model_AttributeSelectionList.cpp
4 // Created:     22 Oct 2014
5 // Author:      Mikhail PONIKAROV
6
7 #include "Model_AttributeSelectionList.h"
8 #include "Model_AttributeSelection.h"
9 #include "Model_Application.h"
10 #include "Model_Events.h"
11 #include "Model_Data.h"
12
13 #include <TDF_AttributeIterator.hxx>
14 #include <TDF_ChildIterator.hxx>
15 #include <TDF_RelocationTable.hxx>
16 #include <TDF_DeltaOnAddition.hxx>
17 #include <TopAbs_ShapeEnum.hxx>
18 #include <TopoDS.hxx>
19 #include <TopoDS_Shape.hxx>
20 #include <TopoDS_Edge.hxx>
21 #include <BRep_Tool.hxx>
22 #include <TNaming_Builder.hxx>
23 #include <TNaming_Iterator.hxx>
24 #include <TNaming_NamedShape.hxx>
25 #include <NCollection_List.hxx>
26
27 void Model_AttributeSelectionList::append(
28     const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape,
29     const bool theTemporarily)
30 {
31   // do not use the degenerated edge as a shape, a list is not incremented in this case
32   if (theSubShape.get() && !theSubShape->isNull() && theSubShape->isEdge()) {
33     const TopoDS_Shape& aSubShape = theSubShape->impl<TopoDS_Shape>();
34     if (aSubShape.ShapeType() == TopAbs_EDGE && BRep_Tool::Degenerated(TopoDS::Edge(aSubShape))) {
35       return;
36     }
37   }
38
39   if (myIsCashed && !theTemporarily) {
40     myCash[theContext].push_back(theSubShape);
41   }
42
43   int aNewTag = mySize->Get() + 1;
44   TDF_Label aNewLab = mySize->Label().FindChild(aNewTag);
45
46   std::shared_ptr<Model_AttributeSelection> aNewAttr =
47     std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aNewLab));
48   if (owner()) {
49     aNewAttr->setObject(owner());
50     aNewAttr->setParent(this);
51   }
52   aNewAttr->setID(id());
53   mySize->Set(aNewTag);
54   aNewAttr->setValue(theContext, theSubShape, theTemporarily);
55   if (theTemporarily)
56     myTmpAttr = aNewAttr;
57   else
58     myTmpAttr.reset();
59   owner()->data()->sendAttributeUpdated(this);
60 }
61
62 void Model_AttributeSelectionList::append(
63   const std::string theNamingName, const std::string& theType)
64 {
65   int aNewTag = mySize->Get() + 1;
66   TDF_Label aNewLab = mySize->Label().FindChild(aNewTag);
67
68   std::shared_ptr<Model_AttributeSelection> aNewAttr =
69     std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aNewLab));
70   if (owner()) {
71     aNewAttr->setObject(owner());
72     aNewAttr->setParent(this);
73   }
74   aNewAttr->setID(id());
75   mySize->Set(aNewTag);
76   aNewAttr->selectSubShape(theType.empty() ? selectionType() : theType, theNamingName);
77   owner()->data()->sendAttributeUpdated(this);
78 }
79
80 void Model_AttributeSelectionList::removeTemporaryValues()
81 {
82   if (myTmpAttr.get()) {
83     myTmpAttr.reset();
84   }
85 }
86
87 void Model_AttributeSelectionList::removeLast()
88 {
89   int anOldSize = mySize->Get();
90   if (anOldSize != 0) {
91     mySize->Set(anOldSize - 1);
92     TDF_Label aLab = mySize->Label().FindChild(anOldSize);
93     std::shared_ptr<Model_AttributeSelection> aOldAttr =
94       std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLab));
95     aOldAttr->setObject(owner());
96     REMOVE_BACK_REF(aOldAttr->context());
97     aLab.ForgetAllAttributes(Standard_True);
98     myTmpAttr.reset();
99     owner()->data()->sendAttributeUpdated(this);
100   }
101 }
102
103 // copies named shapes: we need the implementation of this
104 static void CopyNS(const Handle(TNaming_NamedShape)& theFrom,
105   const Handle(TDF_Attribute)& theTo)
106 {
107   TDF_Label aLab = theTo->Label();
108   TNaming_Builder B(aLab);
109
110   // TNaming_Iterator iterates in reversed way than it was put in Builder, so, keep it in container
111   // and iterate from end to begin
112   NCollection_List<TopoDS_Shape> aOlds;
113   NCollection_List<TopoDS_Shape> aNews;
114   NCollection_List<TNaming_Evolution> aStatuses;
115
116   TNaming_Iterator anIt (theFrom);
117   for ( ;anIt.More() ; anIt.Next()) {
118     aOlds.Prepend(anIt.OldShape());
119     aNews.Prepend(anIt.NewShape());
120     aStatuses.Prepend(anIt.Evolution());
121   }
122
123   NCollection_List<TopoDS_Shape>::Iterator aOldIter(aOlds);
124   NCollection_List<TopoDS_Shape>::Iterator aNewIter(aNews);
125   NCollection_List<TNaming_Evolution>::Iterator aStatIter(aStatuses);
126   for(; aOldIter.More(); aOldIter.Next(), aNewIter.Next(), aStatIter.Next()) {
127     switch (aStatIter.Value()) {
128     case TNaming_PRIMITIVE :
129       B.Generated(aNewIter.Value());
130       break;
131     case TNaming_GENERATED :
132       B.Generated(aOldIter.Value(), aNewIter.Value());
133       break;
134     case TNaming_MODIFY :
135       B.Modify(aOldIter.Value(), aNewIter.Value());
136       break;
137     case TNaming_DELETE :
138       B.Delete (aOldIter.Value());
139       break;
140     case TNaming_SELECTED :
141       B.Select(aNewIter.Value(), aOldIter.Value());
142       break;
143     default:
144       break;
145     }
146   }
147 }
148
149 /// makes copy of all attributes on the given label and all sub-labels
150 static void copyAttrs(TDF_Label theSource, TDF_Label theDestination) {
151   TDF_AttributeIterator anAttrIter(theSource);
152   for(; anAttrIter.More(); anAttrIter.Next()) {
153     Handle(TDF_Attribute) aTargetAttr;
154     if (!theDestination.FindAttribute(anAttrIter.Value()->ID(), aTargetAttr)) {
155       // create a new attribute if not yet exists in the destination
156             aTargetAttr = anAttrIter.Value()->NewEmpty();
157       theDestination.AddAttribute(aTargetAttr);
158     }
159     // for named shape copy exact shapes (in NamedShape Paste method the CopyTool is used)
160     Handle(TNaming_NamedShape) aNS = Handle(TNaming_NamedShape)::DownCast(anAttrIter.Value());
161     if (aNS.IsNull()) {
162       // no special relocation, empty map, but self-relocation is on: copy references w/o changes
163       Handle(TDF_RelocationTable) aRelocTable = new TDF_RelocationTable(Standard_True);
164       anAttrIter.Value()->Paste(aTargetAttr, aRelocTable);
165     } else {
166       CopyNS(aNS, aTargetAttr);
167     }
168   }
169   // copy the sub-labels content
170   TDF_ChildIterator aSubLabsIter(theSource);
171   for(; aSubLabsIter.More(); aSubLabsIter.Next()) {
172     copyAttrs(aSubLabsIter.Value(), theDestination.FindChild(aSubLabsIter.Value().Tag()));
173   }
174 }
175
176 void Model_AttributeSelectionList::remove(const std::set<int>& theIndices)
177 {
178   int anOldSize = mySize->Get();
179   int aRemoved = 0;
180   // iterate one by one and shifting the removed indicies
181   for(int aCurrent = 0; aCurrent < anOldSize; aCurrent++) {
182     if (theIndices.find(aCurrent) == theIndices.end()) { // not removed
183       if (aRemoved) { // but must be shifted to the removed position
184         TDF_Label aSource = mySize->Label().FindChild(aCurrent + 1);
185         TDF_Label aDest = mySize->Label().FindChild(aCurrent + 1 - aRemoved);
186         copyAttrs(aSource, aDest);
187         // remove the moved source
188         aSource.ForgetAllAttributes(Standard_True);
189       }
190     } else { // this is removed, so remove everything from this label
191       TDF_Label aLab = mySize->Label().FindChild(aCurrent + 1);
192       std::shared_ptr<Model_AttributeSelection> aOldAttr =
193         std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLab));
194       aOldAttr->setObject(owner());
195       REMOVE_BACK_REF(aOldAttr->context());
196       aLab.ForgetAllAttributes(Standard_True);
197       myTmpAttr.reset();
198       aRemoved++;
199     }
200   }
201   if (aRemoved) { // remove was performed, so, update the size and this attribute
202     mySize->Set(anOldSize - aRemoved);
203     owner()->data()->sendAttributeUpdated(this);
204   }
205 }
206
207 int Model_AttributeSelectionList::size()
208 {
209   return mySize->Get();
210 }
211
212 bool Model_AttributeSelectionList::isInList(const ResultPtr& theContext,
213                                             const std::shared_ptr<GeomAPI_Shape>& theSubShape,
214                                             const bool theTemporarily)
215 {
216   if (myIsCashed) { // the cashing is active
217     std::map<ResultPtr, std::list<std::shared_ptr<GeomAPI_Shape> > >::iterator aContext =
218       myCash.find(theContext);
219     if (aContext != myCash.end()) {
220       // iterate shapes because "isEqual" method must be called for each shape
221       std::list<std::shared_ptr<GeomAPI_Shape> >::iterator aShapes = aContext->second.begin();
222       for(; aShapes != aContext->second.end(); aShapes++) {
223         if (!theSubShape.get()) {
224           if (!aShapes->get())
225             return true;
226         } else {
227           if (theSubShape->isEqual(*aShapes))
228             return true;
229         }
230       }
231     }
232     return false;
233   }
234   // no-cash method
235   for(int anIndex = size() - 1; anIndex >= 0; anIndex--) {
236     AttributeSelectionPtr anAttr = value(anIndex);
237     if (anAttr.get()) {
238       if (anAttr->context() == theContext) { // contexts are equal, so, check that values are also
239         std::shared_ptr<GeomAPI_Shape> aValue = anAttr->value();
240         if (!aValue.get()) {
241           if (!theSubShape.get()) { // both are null
242             return true;
243           }
244         } else {
245           if (aValue->isEqual(theSubShape)) // shapes are equal
246             return true;
247         }
248       }
249     }
250   }
251   return false;
252 }
253
254 const std::string Model_AttributeSelectionList::selectionType() const
255 {
256   return TCollection_AsciiString(mySelectionType->Get()).ToCString();
257 }
258
259 void Model_AttributeSelectionList::setSelectionType(const std::string& theType)
260 {
261   mySelectionType->Set(theType.c_str());
262 }
263
264 std::shared_ptr<ModelAPI_AttributeSelection>
265   Model_AttributeSelectionList::value(const int theIndex)
266 {
267   if (myTmpAttr.get() && theIndex == size() - 1) {
268     return myTmpAttr;
269   }
270   TDF_Label aLabel = mySize->Label().FindChild(theIndex + 1);
271   // create a new attribute each time, by demand
272   // supporting of old attributes is too slow (synch each time) and buggy on redo
273   // (if attribute is deleted and created, the abort updates attriute and makes the Attr invalid)
274   std::shared_ptr<Model_AttributeSelection> aNewAttr =
275     std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLabel));
276   aNewAttr->setID(id());
277   if (owner()) {
278     aNewAttr->setObject(owner());
279     aNewAttr->setParent(this);
280   }
281   return aNewAttr;
282 }
283
284 void Model_AttributeSelectionList::clear()
285 {
286   if (mySize->Get() != 0) {
287     mySize->Set(0);
288     myTmpAttr.reset();
289     TDF_ChildIterator aSubIter(mySize->Label());
290     for(; aSubIter.More(); aSubIter.Next()) {
291       TDF_Label aLab = aSubIter.Value();
292       std::shared_ptr<Model_AttributeSelection> aNewAttr =
293         std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLab));
294       if (owner()) {
295         aNewAttr->setObject(owner());
296         aNewAttr->setParent(this);
297       }
298       REMOVE_BACK_REF(aNewAttr->context());
299
300       aLab.ForgetAllAttributes(Standard_True);
301     }
302     owner()->data()->sendAttributeUpdated(this);
303   }
304 }
305
306 bool Model_AttributeSelectionList::isInitialized()
307 {
308   if (size() == 0) { // empty list is not initialized list: sketch will be not valid after add/undo
309     return false;
310   }
311   return ModelAPI_AttributeSelectionList::isInitialized();
312 }
313
314 Model_AttributeSelectionList::Model_AttributeSelectionList(TDF_Label& theLabel)
315 {
316   myLab = theLabel;
317   reinit();
318 }
319
320 void Model_AttributeSelectionList::reinit()
321 {
322   myIsInitialized = myLab.FindAttribute(TDataStd_Integer::GetID(), mySize) == Standard_True;
323   if (!myIsInitialized) {
324     mySize = TDataStd_Integer::Set(myLab, 0);
325     mySelectionType = TDataStd_Comment::Set(myLab, "");
326   } else { // recollect mySubs
327     myLab.FindAttribute(TDataStd_Comment::GetID(), mySelectionType);
328   }
329   myIsCashed = false;
330 }
331
332
333 void Model_AttributeSelectionList::cashValues(const bool theEnabled)
334 {
335   myIsCashed = theEnabled;
336   myCash.clear(); // empty list as indicator that cash is not used
337   if (theEnabled) {
338     for(int anIndex = size() - 1; anIndex >= 0; anIndex--) {
339       AttributeSelectionPtr anAttr = value(anIndex);
340       if (anAttr.get()) {
341         myCash[anAttr->context()].push_back(anAttr->value());
342       }
343     }
344   }
345 }