Salome HOME
Performance optimization: allow to put temporary objects into selection attributes...
[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_ChildIterator.hxx>
14 #include <TopAbs_ShapeEnum.hxx>
15
16 #include <TopoDS.hxx>
17 #include <TopoDS_Shape.hxx>
18 #include <TopoDS_Edge.hxx>
19 #include <BRep_Tool.hxx>
20
21 using namespace std;
22
23 void Model_AttributeSelectionList::append(
24     const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape,
25     const bool theTemporarily)
26 {
27   // do not use the degenerated edge as a shape, a list is not incremented in this case
28   if (theSubShape.get() && !theSubShape->isNull() && theSubShape->isEdge()) {
29     const TopoDS_Shape& aSubShape = theSubShape->impl<TopoDS_Shape>();
30     if (aSubShape.ShapeType() == TopAbs_EDGE && BRep_Tool::Degenerated(TopoDS::Edge(aSubShape))) {
31       return;
32     }
33   }
34
35   int aNewTag = mySize->Get() + 1;
36   TDF_Label aNewLab = mySize->Label().FindChild(aNewTag);
37
38   std::shared_ptr<Model_AttributeSelection> aNewAttr = 
39     std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aNewLab));
40   if (owner()) {
41     aNewAttr->setObject(owner());
42   }
43   aNewAttr->setID(id());
44   mySize->Set(aNewTag);
45   aNewAttr->setValue(theContext, theSubShape, theTemporarily);
46   owner()->data()->sendAttributeUpdated(this);
47 }
48
49 void Model_AttributeSelectionList::append(std::string theNamingName)
50 {
51   int aNewTag = mySize->Get() + 1;
52   TDF_Label aNewLab = mySize->Label().FindChild(aNewTag);
53
54   std::shared_ptr<Model_AttributeSelection> aNewAttr = 
55     std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aNewLab));
56   if (owner()) {
57     aNewAttr->setObject(owner());
58   }
59   mySize->Set(aNewTag);
60   aNewAttr->selectSubShape(selectionType(), theNamingName);
61   owner()->data()->sendAttributeUpdated(this);
62 }
63
64 void Model_AttributeSelectionList::removeLast() 
65 {
66   int anOldSize = mySize->Get();
67   if (anOldSize != 0) {
68     mySize->Set(anOldSize - 1);
69     TDF_Label aLab = mySize->Label().FindChild(anOldSize);
70     std::shared_ptr<Model_AttributeSelection> aOldAttr = 
71       std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLab));
72     aOldAttr->setObject(owner());
73     REMOVE_BACK_REF(aOldAttr->context());
74     aLab.ForgetAllAttributes(Standard_True);
75     owner()->data()->sendAttributeUpdated(this);
76   }
77 }
78
79 int Model_AttributeSelectionList::size()
80 {
81   return mySize->Get();
82 }
83
84 const std::string Model_AttributeSelectionList::selectionType() const
85 {
86   return TCollection_AsciiString(mySelectionType->Get()).ToCString();
87 }
88
89 void Model_AttributeSelectionList::setSelectionType(const std::string& theType)
90 {
91   mySelectionType->Set(theType.c_str());
92 }
93
94 std::shared_ptr<ModelAPI_AttributeSelection> 
95   Model_AttributeSelectionList::value(const int theIndex)
96 {
97   TDF_Label aLabel = mySize->Label().FindChild(theIndex + 1);
98   // create a new attribute each time, by demand
99   // supporting of old attributes is too slow (synch each time) and buggy on redo
100   // (if attribute is deleted and created, the abort updates attriute and makes the Attr invalid)
101   std::shared_ptr<Model_AttributeSelection> aNewAttr = 
102     std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLabel));
103   if (owner()) {
104     aNewAttr->setObject(owner());
105   }
106   return aNewAttr;
107 }
108
109 void Model_AttributeSelectionList::clear()
110 {
111   if (mySize->Get() != 0) {
112     mySize->Set(0);
113     TDF_ChildIterator aSubIter(mySize->Label());
114     for(; aSubIter.More(); aSubIter.Next()) {
115       TDF_Label aLab = aSubIter.Value();
116       std::shared_ptr<Model_AttributeSelection> aNewAttr = 
117         std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLab));
118       if (owner()) {
119         aNewAttr->setObject(owner());
120       }
121       REMOVE_BACK_REF(aNewAttr->context());
122
123       aLab.ForgetAllAttributes(Standard_True);
124     }
125     owner()->data()->sendAttributeUpdated(this);
126   }
127 }
128
129 bool Model_AttributeSelectionList::isInitialized()
130 {
131   if (size() == 0) { // empty list is not initialized list: sketch will be not valid after add/undo
132     return false;
133   }
134   return ModelAPI_AttributeSelectionList::isInitialized();
135 }
136
137 Model_AttributeSelectionList::Model_AttributeSelectionList(TDF_Label& theLabel)
138 {
139   myIsInitialized = theLabel.FindAttribute(TDataStd_Integer::GetID(), mySize) == Standard_True;
140   if (!myIsInitialized) {
141     mySize = TDataStd_Integer::Set(theLabel, 0);
142     mySelectionType = TDataStd_Comment::Set(theLabel, "");
143   } else { // recollect mySubs
144     theLabel.FindAttribute(TDataStd_Comment::GetID(), mySelectionType);
145   }
146 }