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