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