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