Salome HOME
ae079d7a1673e8158816beeb0416511202124aae
[modules/shaper.git] / src / Model / Model_AttributeSelectionList.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        Model_AttributeSelectionList.h
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 {
26   // do not use the degenerated edge as a shape, a list is not incremented in this case
27   if (theSubShape.get() && !theSubShape->isNull() && theSubShape->isEdge()) {
28     const TopoDS_Shape& aSubShape = theSubShape->impl<TopoDS_Shape>();
29     if (aSubShape.ShapeType() == TopAbs_EDGE && BRep_Tool::Degenerated(TopoDS::Edge(aSubShape))) {
30       return;
31     }
32   }
33
34   int aNewTag = mySize->Get() + 1;
35   TDF_Label aNewLab = mySize->Label().FindChild(aNewTag);
36
37   std::shared_ptr<Model_AttributeSelection> aNewAttr = 
38     std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aNewLab));
39   if (owner()) {
40     aNewAttr->setObject(owner());
41   }
42   aNewAttr->setID(id());
43   mySize->Set(aNewTag);
44   aNewAttr->setValue(theContext, theSubShape);
45   owner()->data()->sendAttributeUpdated(this);
46 }
47
48 void Model_AttributeSelectionList::append(std::string theNamingName)
49 {
50   int aNewTag = mySize->Get() + 1;
51   TDF_Label aNewLab = mySize->Label().FindChild(aNewTag);
52
53   std::shared_ptr<Model_AttributeSelection> aNewAttr = 
54     std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aNewLab));
55   if (owner()) {
56     aNewAttr->setObject(owner());
57   }
58   mySize->Set(aNewTag);
59   aNewAttr->selectSubShape(selectionType(), theNamingName);
60   owner()->data()->sendAttributeUpdated(this);
61 }
62
63 int Model_AttributeSelectionList::size()
64 {
65   return mySize->Get();
66 }
67
68 const std::string Model_AttributeSelectionList::selectionType() const
69 {
70   return TCollection_AsciiString(mySelectionType->Get()).ToCString();
71 }
72
73 void Model_AttributeSelectionList::setSelectionType(const std::string& theType)
74 {
75   mySelectionType->Set(theType.c_str());
76 }
77
78 std::shared_ptr<ModelAPI_AttributeSelection> 
79   Model_AttributeSelectionList::value(const int theIndex)
80 {
81   TDF_Label aLabel = mySize->Label().FindChild(theIndex + 1);
82   // create a new attribute each time, by demand
83   // supporting of old attributes is too slow (synch each time) and buggy on redo
84   // (if attribute is deleted and created, the abort updates attriute and makes the Attr invalid)
85   std::shared_ptr<Model_AttributeSelection> aNewAttr = 
86     std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLabel));
87   if (owner()) {
88     aNewAttr->setObject(owner());
89   }
90   return aNewAttr;
91 }
92
93 void Model_AttributeSelectionList::clear()
94 {
95   if (mySize->Get() != 0) {
96     mySize->Set(0);
97     TDF_ChildIterator aSubIter(mySize->Label());
98     for(; aSubIter.More(); aSubIter.Next()) {
99       TDF_Label aLab = aSubIter.Value();
100       std::shared_ptr<Model_AttributeSelection> aNewAttr = 
101         std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLab));
102       if (owner()) {
103         aNewAttr->setObject(owner());
104       }
105       REMOVE_BACK_REF(aNewAttr->context());
106
107       aLab.ForgetAllAttributes(Standard_True);
108     }
109     owner()->data()->sendAttributeUpdated(this);
110   }
111 }
112
113 bool Model_AttributeSelectionList::isInitialized()
114 {
115   if (size() == 0) { // empty list is not initialized list: sketch will be not valid after add/undo
116     return false;
117   }
118   return ModelAPI_AttributeSelectionList::isInitialized();
119 }
120
121 Model_AttributeSelectionList::Model_AttributeSelectionList(TDF_Label& theLabel)
122 {
123   myIsInitialized = theLabel.FindAttribute(TDataStd_Integer::GetID(), mySize) == Standard_True;
124   if (!myIsInitialized) {
125     mySize = TDataStd_Integer::Set(theLabel, 0);
126     mySelectionType = TDataStd_Comment::Set(theLabel, "");
127   } else { // recollect mySubs
128     theLabel.FindAttribute(TDataStd_Comment::GetID(), mySelectionType);
129   }
130 }