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