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