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