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