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_AttributeIterator.hxx>
14 #include <TDF_ChildIterator.hxx>
15 #include <TDF_RelocationTable.hxx>
16 #include <TopAbs_ShapeEnum.hxx>
18 #include <TopoDS_Shape.hxx>
19 #include <TopoDS_Edge.hxx>
20 #include <BRep_Tool.hxx>
21 #include <TNaming_Builder.hxx>
22 #include <TNaming_Iterator.hxx>
26 void Model_AttributeSelectionList::append(
27 const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape,
28 const bool theTemporarily)
30 // do not use the degenerated edge as a shape, a list is not incremented in this case
31 if (theSubShape.get() && !theSubShape->isNull() && theSubShape->isEdge()) {
32 const TopoDS_Shape& aSubShape = theSubShape->impl<TopoDS_Shape>();
33 if (aSubShape.ShapeType() == TopAbs_EDGE && BRep_Tool::Degenerated(TopoDS::Edge(aSubShape))) {
38 if (myIsCashed && !theTemporarily) {
39 myCash[theContext].push_back(theSubShape);
42 int aNewTag = mySize->Get() + 1;
43 TDF_Label aNewLab = mySize->Label().FindChild(aNewTag);
45 std::shared_ptr<Model_AttributeSelection> aNewAttr =
46 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aNewLab));
48 aNewAttr->setObject(owner());
50 aNewAttr->setID(id());
52 aNewAttr->setValue(theContext, theSubShape, theTemporarily);
57 owner()->data()->sendAttributeUpdated(this);
60 void Model_AttributeSelectionList::append(
61 const std::string theNamingName, const std::string& theType)
63 int aNewTag = mySize->Get() + 1;
64 TDF_Label aNewLab = mySize->Label().FindChild(aNewTag);
66 std::shared_ptr<Model_AttributeSelection> aNewAttr =
67 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aNewLab));
69 aNewAttr->setObject(owner());
71 aNewAttr->setID(id());
73 aNewAttr->selectSubShape(theType.empty() ? selectionType() : theType, theNamingName);
74 owner()->data()->sendAttributeUpdated(this);
77 void Model_AttributeSelectionList::removeLast()
79 int anOldSize = mySize->Get();
81 mySize->Set(anOldSize - 1);
82 TDF_Label aLab = mySize->Label().FindChild(anOldSize);
83 std::shared_ptr<Model_AttributeSelection> aOldAttr =
84 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLab));
85 aOldAttr->setObject(owner());
86 REMOVE_BACK_REF(aOldAttr->context());
87 aLab.ForgetAllAttributes(Standard_True);
89 owner()->data()->sendAttributeUpdated(this);
93 // copies named shapes: we need the implementation of this
94 static void CopyNS(const Handle(TNaming_NamedShape)& theFrom,
95 const Handle(TDF_Attribute)& theTo)
97 TDF_Label aLab = theTo->Label();
98 TNaming_Builder B(aLab);
100 TNaming_Iterator It (theFrom);
101 for ( ;It.More() ; It.Next()) {
102 const TopoDS_Shape& OS = It.OldShape();
103 const TopoDS_Shape& NS = It.NewShape();
104 TNaming_Evolution Status = It.Evolution();
107 case TNaming_PRIMITIVE :
110 case TNaming_GENERATED :
113 case TNaming_MODIFY :
116 case TNaming_DELETE :
119 case TNaming_SELECTED :
128 /// makes copy of all attributes on the given label and all sub-labels
129 static void copyAttrs(TDF_Label theSource, TDF_Label theDestination) {
130 TDF_AttributeIterator anAttrIter(theSource);
131 for(; anAttrIter.More(); anAttrIter.Next()) {
132 Handle(TDF_Attribute) aTargetAttr;
133 if (!theDestination.FindAttribute(anAttrIter.Value()->ID(), aTargetAttr)) {
134 // create a new attribute if not yet exists in the destination
135 aTargetAttr = anAttrIter.Value()->NewEmpty();
136 theDestination.AddAttribute(aTargetAttr);
138 // for named shape copy exact shapes (in NamedShape Paste method the CopyTool is used)
139 Handle(TNaming_NamedShape) aNS = Handle(TNaming_NamedShape)::DownCast(anAttrIter.Value());
141 Handle(TDF_RelocationTable) aRelocTable = new TDF_RelocationTable(); // no relocation, empty map
142 anAttrIter.Value()->Paste(aTargetAttr, aRelocTable);
144 CopyNS(aNS, aTargetAttr);
147 // copy the sub-labels content
148 TDF_ChildIterator aSubLabsIter(theSource);
149 for(; aSubLabsIter.More(); aSubLabsIter.Next()) {
150 copyAttrs(aSubLabsIter.Value(), theDestination.FindChild(aSubLabsIter.Value().Tag()));
154 void Model_AttributeSelectionList::remove(const std::set<int>& theIndices)
156 int anOldSize = mySize->Get();
158 // iterate one by one and shifting the removed indicies
159 for(int aCurrent = 0; aCurrent < anOldSize; aCurrent++) {
160 if (theIndices.find(aCurrent) == theIndices.end()) { // not removed
161 if (aRemoved) { // but must be shifted to the removed position
162 TDF_Label aSource = mySize->Label().FindChild(aCurrent + 1);
163 TDF_Label aDest = mySize->Label().FindChild(aCurrent + 1 - aRemoved);
164 copyAttrs(aSource, aDest);
165 // remove the moved source
166 aSource.ForgetAllAttributes(Standard_True);
168 } else { // this is removed, so remove everything from this label
169 TDF_Label aLab = mySize->Label().FindChild(aCurrent + 1);
170 std::shared_ptr<Model_AttributeSelection> aOldAttr =
171 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLab));
172 aOldAttr->setObject(owner());
173 REMOVE_BACK_REF(aOldAttr->context());
174 aLab.ForgetAllAttributes(Standard_True);
179 if (aRemoved) { // remove was performed, so, update the size and this attribute
180 mySize->Set(anOldSize - aRemoved);
181 owner()->data()->sendAttributeUpdated(this);
185 int Model_AttributeSelectionList::size()
187 return mySize->Get();
190 bool Model_AttributeSelectionList::isInList(const ResultPtr& theContext,
191 const std::shared_ptr<GeomAPI_Shape>& theSubShape,
192 const bool theTemporarily)
194 if (myIsCashed) { // the cashing is active
195 std::map<ResultPtr, std::list<std::shared_ptr<GeomAPI_Shape> > >::iterator aContext =
196 myCash.find(theContext);
197 if (aContext != myCash.end()) {
198 // iterate shapes because "isEqual" method must be called for each shape
199 std::list<std::shared_ptr<GeomAPI_Shape> >::iterator aShapes = aContext->second.begin();
200 for(; aShapes != aContext->second.end(); aShapes++) {
201 if (!theSubShape.get()) {
205 if (theSubShape->isEqual(*aShapes))
213 for(int anIndex = size() - 1; anIndex >= 0; anIndex--) {
214 AttributeSelectionPtr anAttr = value(anIndex);
216 if (anAttr->context() == theContext) { // contexts are equal, so, check that values are also
217 std::shared_ptr<GeomAPI_Shape> aValue = anAttr->value();
219 if (!theSubShape.get()) { // both are null
223 if (aValue->isEqual(theSubShape)) // shapes are equal
232 const std::string Model_AttributeSelectionList::selectionType() const
234 return TCollection_AsciiString(mySelectionType->Get()).ToCString();
237 void Model_AttributeSelectionList::setSelectionType(const std::string& theType)
239 mySelectionType->Set(theType.c_str());
242 std::shared_ptr<ModelAPI_AttributeSelection>
243 Model_AttributeSelectionList::value(const int theIndex)
245 if (myTmpAttr.get() && theIndex == size() - 1) {
248 TDF_Label aLabel = mySize->Label().FindChild(theIndex + 1);
249 // create a new attribute each time, by demand
250 // supporting of old attributes is too slow (synch each time) and buggy on redo
251 // (if attribute is deleted and created, the abort updates attriute and makes the Attr invalid)
252 std::shared_ptr<Model_AttributeSelection> aNewAttr =
253 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLabel));
255 aNewAttr->setObject(owner());
260 void Model_AttributeSelectionList::clear()
262 if (mySize->Get() != 0) {
265 TDF_ChildIterator aSubIter(mySize->Label());
266 for(; aSubIter.More(); aSubIter.Next()) {
267 TDF_Label aLab = aSubIter.Value();
268 std::shared_ptr<Model_AttributeSelection> aNewAttr =
269 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLab));
271 aNewAttr->setObject(owner());
273 REMOVE_BACK_REF(aNewAttr->context());
275 aLab.ForgetAllAttributes(Standard_True);
277 owner()->data()->sendAttributeUpdated(this);
281 bool Model_AttributeSelectionList::isInitialized()
283 if (size() == 0) { // empty list is not initialized list: sketch will be not valid after add/undo
286 return ModelAPI_AttributeSelectionList::isInitialized();
289 Model_AttributeSelectionList::Model_AttributeSelectionList(TDF_Label& theLabel)
291 myIsInitialized = theLabel.FindAttribute(TDataStd_Integer::GetID(), mySize) == Standard_True;
292 if (!myIsInitialized) {
293 mySize = TDataStd_Integer::Set(theLabel, 0);
294 mySelectionType = TDataStd_Comment::Set(theLabel, "");
295 } else { // recollect mySubs
296 theLabel.FindAttribute(TDataStd_Comment::GetID(), mySelectionType);
301 void Model_AttributeSelectionList::cashValues(const bool theEnabled)
303 myIsCashed = theEnabled;
304 myCash.clear(); // empty list as indicator that cash is not used
306 for(int anIndex = size() - 1; anIndex >= 0; anIndex--) {
307 AttributeSelectionPtr anAttr = value(anIndex);
309 myCash[anAttr->context()].push_back(anAttr->value());