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