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