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