1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: Model_AttributeSelectionList.cpp
4 // Created: 22 Oct 2014
5 // Author: Mikhail PONIKAROV
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"
13 #include <TDF_ChildIterator.hxx>
14 #include <TopAbs_ShapeEnum.hxx>
17 #include <TopoDS_Shape.hxx>
18 #include <TopoDS_Edge.hxx>
19 #include <BRep_Tool.hxx>
23 void Model_AttributeSelectionList::append(
24 const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape)
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))) {
34 int aNewTag = mySize->Get() + 1;
35 TDF_Label aNewLab = mySize->Label().FindChild(aNewTag);
37 std::shared_ptr<Model_AttributeSelection> aNewAttr =
38 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aNewLab));
40 aNewAttr->setObject(owner());
42 aNewAttr->setID(id());
44 aNewAttr->setValue(theContext, theSubShape);
45 owner()->data()->sendAttributeUpdated(this);
48 void Model_AttributeSelectionList::append(std::string theNamingName)
50 int aNewTag = mySize->Get() + 1;
51 TDF_Label aNewLab = mySize->Label().FindChild(aNewTag);
53 std::shared_ptr<Model_AttributeSelection> aNewAttr =
54 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aNewLab));
56 aNewAttr->setObject(owner());
59 aNewAttr->selectSubShape(selectionType(), theNamingName);
60 owner()->data()->sendAttributeUpdated(this);
63 void Model_AttributeSelectionList::removeLast()
65 int anOldSize = mySize->Get();
67 mySize->Set(anOldSize - 1);
68 TDF_Label aLab = mySize->Label().FindChild(anOldSize);
69 std::shared_ptr<Model_AttributeSelection> aOldAttr =
70 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLab));
71 aOldAttr->setObject(owner());
72 REMOVE_BACK_REF(aOldAttr->context());
73 aLab.ForgetAllAttributes(Standard_True);
74 owner()->data()->sendAttributeUpdated(this);
78 int Model_AttributeSelectionList::size()
83 const std::string Model_AttributeSelectionList::selectionType() const
85 return TCollection_AsciiString(mySelectionType->Get()).ToCString();
88 void Model_AttributeSelectionList::setSelectionType(const std::string& theType)
90 mySelectionType->Set(theType.c_str());
93 std::shared_ptr<ModelAPI_AttributeSelection>
94 Model_AttributeSelectionList::value(const int theIndex)
96 TDF_Label aLabel = mySize->Label().FindChild(theIndex + 1);
97 // create a new attribute each time, by demand
98 // supporting of old attributes is too slow (synch each time) and buggy on redo
99 // (if attribute is deleted and created, the abort updates attriute and makes the Attr invalid)
100 std::shared_ptr<Model_AttributeSelection> aNewAttr =
101 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLabel));
103 aNewAttr->setObject(owner());
108 void Model_AttributeSelectionList::clear()
110 if (mySize->Get() != 0) {
112 TDF_ChildIterator aSubIter(mySize->Label());
113 for(; aSubIter.More(); aSubIter.Next()) {
114 TDF_Label aLab = aSubIter.Value();
115 std::shared_ptr<Model_AttributeSelection> aNewAttr =
116 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLab));
118 aNewAttr->setObject(owner());
120 REMOVE_BACK_REF(aNewAttr->context());
122 aLab.ForgetAllAttributes(Standard_True);
124 owner()->data()->sendAttributeUpdated(this);
128 bool Model_AttributeSelectionList::isInitialized()
130 if (size() == 0) { // empty list is not initialized list: sketch will be not valid after add/undo
133 return ModelAPI_AttributeSelectionList::isInitialized();
136 Model_AttributeSelectionList::Model_AttributeSelectionList(TDF_Label& theLabel)
138 myIsInitialized = theLabel.FindAttribute(TDataStd_Integer::GetID(), mySize) == Standard_True;
139 if (!myIsInitialized) {
140 mySize = TDataStd_Integer::Set(theLabel, 0);
141 mySelectionType = TDataStd_Comment::Set(theLabel, "");
142 } else { // recollect mySubs
143 theLabel.FindAttribute(TDataStd_Comment::GetID(), mySelectionType);