Salome HOME
24a9efca5308d5684d3030a0b55c6ac5d4fd8f54
[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   mySize->Set(aNewTag);
43   aNewAttr->setValue(theContext, theSubShape);
44   owner()->data()->sendAttributeUpdated(this);
45 }
46
47 void Model_AttributeSelectionList::append(std::string theNamingName)
48 {
49   int aNewTag = mySize->Get() + 1;
50   TDF_Label aNewLab = mySize->Label().FindChild(aNewTag);
51
52   std::shared_ptr<Model_AttributeSelection> aNewAttr = 
53     std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aNewLab));
54   if (owner()) {
55     aNewAttr->setObject(owner());
56   }
57   mySize->Set(aNewTag);
58   aNewAttr->selectSubShape(selectionType(), theNamingName);
59   owner()->data()->sendAttributeUpdated(this);
60 }
61
62 int Model_AttributeSelectionList::size()
63 {
64   return mySize->Get();
65 }
66
67 const std::string Model_AttributeSelectionList::selectionType() const
68 {
69   return TCollection_AsciiString(mySelectionType->Get()).ToCString();
70 }
71
72 void Model_AttributeSelectionList::setSelectionType(const std::string& theType)
73 {
74   mySelectionType->Set(theType.c_str());
75 }
76
77 std::shared_ptr<ModelAPI_AttributeSelection> 
78   Model_AttributeSelectionList::value(const int theIndex)
79 {
80   TDF_Label aLabel = mySize->Label().FindChild(theIndex + 1);
81   // create a new attribute each time, by demand
82   // supporting of old attributes is too slow (synch each time) and buggy on redo
83   // (if attribute is deleted and created, the abort updates attriute and makes the Attr invalid)
84   std::shared_ptr<Model_AttributeSelection> aNewAttr = 
85     std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLabel));
86   if (owner()) {
87     aNewAttr->setObject(owner());
88   }
89   return aNewAttr;
90 }
91
92 void Model_AttributeSelectionList::clear()
93 {
94   if (mySize->Get() != 0) {
95     mySize->Set(0);
96     TDF_ChildIterator aSubIter(mySize->Label());
97     for(; aSubIter.More(); aSubIter.Next()) {
98       aSubIter.Value().ForgetAllAttributes(Standard_True);
99     }
100     owner()->data()->sendAttributeUpdated(this);
101   }
102 }
103
104 Model_AttributeSelectionList::Model_AttributeSelectionList(TDF_Label& theLabel)
105 {
106   myIsInitialized = theLabel.FindAttribute(TDataStd_Integer::GetID(), mySize) == Standard_True;
107   if (!myIsInitialized) {
108     mySize = TDataStd_Integer::Set(theLabel, 0);
109     mySelectionType = TDataStd_Comment::Set(theLabel, "");
110   } else { // recollect mySubs
111     theLabel.FindAttribute(TDataStd_Comment::GetID(), mySelectionType);
112   }
113 }