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