Salome HOME
677d14d78760e552a6ee12fc9c7639fd488bcba8
[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   if (theTemporarily)
47     myTmpAttr = aNewAttr;
48   else 
49     myTmpAttr.reset();
50   owner()->data()->sendAttributeUpdated(this);
51 }
52
53 void Model_AttributeSelectionList::append(
54   const std::string theNamingName, const std::string& theType)
55 {
56   int aNewTag = mySize->Get() + 1;
57   TDF_Label aNewLab = mySize->Label().FindChild(aNewTag);
58
59   std::shared_ptr<Model_AttributeSelection> aNewAttr = 
60     std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aNewLab));
61   if (owner()) {
62     aNewAttr->setObject(owner());
63   }
64   aNewAttr->setID(id());
65   mySize->Set(aNewTag);
66   aNewAttr->selectSubShape(theType.empty() ? selectionType() : theType, theNamingName);
67   owner()->data()->sendAttributeUpdated(this);
68 }
69
70 void Model_AttributeSelectionList::removeLast() 
71 {
72   int anOldSize = mySize->Get();
73   if (anOldSize != 0) {
74     mySize->Set(anOldSize - 1);
75     TDF_Label aLab = mySize->Label().FindChild(anOldSize);
76     std::shared_ptr<Model_AttributeSelection> aOldAttr = 
77       std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLab));
78     aOldAttr->setObject(owner());
79     REMOVE_BACK_REF(aOldAttr->context());
80     aLab.ForgetAllAttributes(Standard_True);
81     myTmpAttr.reset();
82     owner()->data()->sendAttributeUpdated(this);
83   }
84 }
85
86 void Model_AttributeSelectionList::remove(const std::set<int>& theIndices)
87 {
88 }
89
90 int Model_AttributeSelectionList::size()
91 {
92   return mySize->Get();
93 }
94
95 const std::string Model_AttributeSelectionList::selectionType() const
96 {
97   return TCollection_AsciiString(mySelectionType->Get()).ToCString();
98 }
99
100 void Model_AttributeSelectionList::setSelectionType(const std::string& theType)
101 {
102   mySelectionType->Set(theType.c_str());
103 }
104
105 std::shared_ptr<ModelAPI_AttributeSelection> 
106   Model_AttributeSelectionList::value(const int theIndex)
107 {
108   if (myTmpAttr.get() && theIndex == size() - 1) {
109     return myTmpAttr;
110   }
111   TDF_Label aLabel = mySize->Label().FindChild(theIndex + 1);
112   // create a new attribute each time, by demand
113   // supporting of old attributes is too slow (synch each time) and buggy on redo
114   // (if attribute is deleted and created, the abort updates attriute and makes the Attr invalid)
115   std::shared_ptr<Model_AttributeSelection> aNewAttr = 
116     std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLabel));
117   if (owner()) {
118     aNewAttr->setObject(owner());
119   }
120   return aNewAttr;
121 }
122
123 void Model_AttributeSelectionList::clear()
124 {
125   if (mySize->Get() != 0) {
126     mySize->Set(0);
127     myTmpAttr.reset();
128     TDF_ChildIterator aSubIter(mySize->Label());
129     for(; aSubIter.More(); aSubIter.Next()) {
130       TDF_Label aLab = aSubIter.Value();
131       std::shared_ptr<Model_AttributeSelection> aNewAttr = 
132         std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLab));
133       if (owner()) {
134         aNewAttr->setObject(owner());
135       }
136       REMOVE_BACK_REF(aNewAttr->context());
137
138       aLab.ForgetAllAttributes(Standard_True);
139     }
140     owner()->data()->sendAttributeUpdated(this);
141   }
142 }
143
144 bool Model_AttributeSelectionList::isInitialized()
145 {
146   if (size() == 0) { // empty list is not initialized list: sketch will be not valid after add/undo
147     return false;
148   }
149   return ModelAPI_AttributeSelectionList::isInitialized();
150 }
151
152 Model_AttributeSelectionList::Model_AttributeSelectionList(TDF_Label& theLabel)
153 {
154   myIsInitialized = theLabel.FindAttribute(TDataStd_Integer::GetID(), mySize) == Standard_True;
155   if (!myIsInitialized) {
156     mySize = TDataStd_Integer::Set(theLabel, 0);
157     mySelectionType = TDataStd_Comment::Set(theLabel, "");
158   } else { // recollect mySubs
159     theLabel.FindAttribute(TDataStd_Comment::GetID(), mySelectionType);
160   }
161 }