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>
23 #include <NCollection_List.hxx>
25 void Model_AttributeSelectionList::append(
26 const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape,
27 const bool theTemporarily)
29 // do not use the degenerated edge as a shape, a list is not incremented in this case
30 if (theSubShape.get() && !theSubShape->isNull() && theSubShape->isEdge()) {
31 const TopoDS_Shape& aSubShape = theSubShape->impl<TopoDS_Shape>();
32 if (aSubShape.ShapeType() == TopAbs_EDGE && BRep_Tool::Degenerated(TopoDS::Edge(aSubShape))) {
37 if (myIsCashed && !theTemporarily) {
38 myCash[theContext].push_back(theSubShape);
41 int aNewTag = mySize->Get() + 1;
42 TDF_Label aNewLab = mySize->Label().FindChild(aNewTag);
44 std::shared_ptr<Model_AttributeSelection> aNewAttr =
45 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aNewLab));
47 aNewAttr->setObject(owner());
49 aNewAttr->setID(id());
51 aNewAttr->setValue(theContext, theSubShape, theTemporarily);
56 owner()->data()->sendAttributeUpdated(this);
59 void Model_AttributeSelectionList::append(
60 const std::string theNamingName, const std::string& theType)
62 int aNewTag = mySize->Get() + 1;
63 TDF_Label aNewLab = mySize->Label().FindChild(aNewTag);
65 std::shared_ptr<Model_AttributeSelection> aNewAttr =
66 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aNewLab));
68 aNewAttr->setObject(owner());
70 aNewAttr->setID(id());
72 aNewAttr->selectSubShape(theType.empty() ? selectionType() : theType, theNamingName);
73 owner()->data()->sendAttributeUpdated(this);
76 void Model_AttributeSelectionList::removeTemporaryValues()
78 if (myTmpAttr.get()) {
83 void Model_AttributeSelectionList::removeLast()
85 int anOldSize = mySize->Get();
87 mySize->Set(anOldSize - 1);
88 TDF_Label aLab = mySize->Label().FindChild(anOldSize);
89 std::shared_ptr<Model_AttributeSelection> aOldAttr =
90 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLab));
91 aOldAttr->setObject(owner());
92 REMOVE_BACK_REF(aOldAttr->context());
93 aLab.ForgetAllAttributes(Standard_True);
95 owner()->data()->sendAttributeUpdated(this);
99 // copies named shapes: we need the implementation of this
100 static void CopyNS(const Handle(TNaming_NamedShape)& theFrom,
101 const Handle(TDF_Attribute)& theTo)
103 TDF_Label aLab = theTo->Label();
104 TNaming_Builder B(aLab);
106 // TNaming_Iterator iterates in reversed way than it was put in Builder, so, keep it in container
107 // and iterate from end to begin
108 NCollection_List<TopoDS_Shape> aOlds;
109 NCollection_List<TopoDS_Shape> aNews;
110 NCollection_List<TNaming_Evolution> aStatuses;
112 TNaming_Iterator anIt (theFrom);
113 for ( ;anIt.More() ; anIt.Next()) {
114 aOlds.Prepend(anIt.OldShape());
115 aNews.Prepend(anIt.NewShape());
116 aStatuses.Prepend(anIt.Evolution());
119 NCollection_List<TopoDS_Shape>::Iterator aOldIter(aOlds);
120 NCollection_List<TopoDS_Shape>::Iterator aNewIter(aNews);
121 NCollection_List<TNaming_Evolution>::Iterator aStatIter(aStatuses);
122 for(; aOldIter.More(); aOldIter.Next(), aNewIter.Next(), aStatIter.Next()) {
123 switch (aStatIter.Value()) {
124 case TNaming_PRIMITIVE :
125 B.Generated(aNewIter.Value());
127 case TNaming_GENERATED :
128 B.Generated(aOldIter.Value(), aNewIter.Value());
130 case TNaming_MODIFY :
131 B.Modify(aOldIter.Value(), aNewIter.Value());
133 case TNaming_DELETE :
134 B.Delete (aOldIter.Value());
136 case TNaming_SELECTED :
137 B.Select(aNewIter.Value(), aOldIter.Value());
145 /// makes copy of all attributes on the given label and all sub-labels
146 static void copyAttrs(TDF_Label theSource, TDF_Label theDestination) {
147 TDF_AttributeIterator anAttrIter(theSource);
148 for(; anAttrIter.More(); anAttrIter.Next()) {
149 Handle(TDF_Attribute) aTargetAttr;
150 if (!theDestination.FindAttribute(anAttrIter.Value()->ID(), aTargetAttr)) {
151 // create a new attribute if not yet exists in the destination
152 aTargetAttr = anAttrIter.Value()->NewEmpty();
153 theDestination.AddAttribute(aTargetAttr);
155 // for named shape copy exact shapes (in NamedShape Paste method the CopyTool is used)
156 Handle(TNaming_NamedShape) aNS = Handle(TNaming_NamedShape)::DownCast(anAttrIter.Value());
158 // no special relocation, empty map, but self-relocation is on: copy references w/o changes
159 Handle(TDF_RelocationTable) aRelocTable = new TDF_RelocationTable(Standard_True);
160 anAttrIter.Value()->Paste(aTargetAttr, aRelocTable);
162 CopyNS(aNS, aTargetAttr);
165 // copy the sub-labels content
166 TDF_ChildIterator aSubLabsIter(theSource);
167 for(; aSubLabsIter.More(); aSubLabsIter.Next()) {
168 copyAttrs(aSubLabsIter.Value(), theDestination.FindChild(aSubLabsIter.Value().Tag()));
172 void Model_AttributeSelectionList::remove(const std::set<int>& theIndices)
174 int anOldSize = mySize->Get();
176 // iterate one by one and shifting the removed indicies
177 for(int aCurrent = 0; aCurrent < anOldSize; aCurrent++) {
178 if (theIndices.find(aCurrent) == theIndices.end()) { // not removed
179 if (aRemoved) { // but must be shifted to the removed position
180 TDF_Label aSource = mySize->Label().FindChild(aCurrent + 1);
181 TDF_Label aDest = mySize->Label().FindChild(aCurrent + 1 - aRemoved);
182 copyAttrs(aSource, aDest);
183 // remove the moved source
184 aSource.ForgetAllAttributes(Standard_True);
186 } else { // this is removed, so remove everything from this label
187 TDF_Label aLab = mySize->Label().FindChild(aCurrent + 1);
188 std::shared_ptr<Model_AttributeSelection> aOldAttr =
189 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLab));
190 aOldAttr->setObject(owner());
191 REMOVE_BACK_REF(aOldAttr->context());
192 aLab.ForgetAllAttributes(Standard_True);
197 if (aRemoved) { // remove was performed, so, update the size and this attribute
198 mySize->Set(anOldSize - aRemoved);
199 owner()->data()->sendAttributeUpdated(this);
203 int Model_AttributeSelectionList::size()
205 return mySize->Get();
208 bool Model_AttributeSelectionList::isInList(const ResultPtr& theContext,
209 const std::shared_ptr<GeomAPI_Shape>& theSubShape,
210 const bool theTemporarily)
212 if (myIsCashed) { // the cashing is active
213 std::map<ResultPtr, std::list<std::shared_ptr<GeomAPI_Shape> > >::iterator aContext =
214 myCash.find(theContext);
215 if (aContext != myCash.end()) {
216 // iterate shapes because "isEqual" method must be called for each shape
217 std::list<std::shared_ptr<GeomAPI_Shape> >::iterator aShapes = aContext->second.begin();
218 for(; aShapes != aContext->second.end(); aShapes++) {
219 if (!theSubShape.get()) {
223 if (theSubShape->isEqual(*aShapes))
231 for(int anIndex = size() - 1; anIndex >= 0; anIndex--) {
232 AttributeSelectionPtr anAttr = value(anIndex);
234 if (anAttr->context() == theContext) { // contexts are equal, so, check that values are also
235 std::shared_ptr<GeomAPI_Shape> aValue = anAttr->value();
237 if (!theSubShape.get()) { // both are null
241 if (aValue->isEqual(theSubShape)) // shapes are equal
250 const std::string Model_AttributeSelectionList::selectionType() const
252 return TCollection_AsciiString(mySelectionType->Get()).ToCString();
255 void Model_AttributeSelectionList::setSelectionType(const std::string& theType)
257 mySelectionType->Set(theType.c_str());
260 std::shared_ptr<ModelAPI_AttributeSelection>
261 Model_AttributeSelectionList::value(const int theIndex)
263 if (myTmpAttr.get() && theIndex == size() - 1) {
266 TDF_Label aLabel = mySize->Label().FindChild(theIndex + 1);
267 // create a new attribute each time, by demand
268 // supporting of old attributes is too slow (synch each time) and buggy on redo
269 // (if attribute is deleted and created, the abort updates attriute and makes the Attr invalid)
270 std::shared_ptr<Model_AttributeSelection> aNewAttr =
271 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLabel));
272 aNewAttr->setID(id());
274 aNewAttr->setObject(owner());
279 void Model_AttributeSelectionList::clear()
281 if (mySize->Get() != 0) {
284 TDF_ChildIterator aSubIter(mySize->Label());
285 for(; aSubIter.More(); aSubIter.Next()) {
286 TDF_Label aLab = aSubIter.Value();
287 std::shared_ptr<Model_AttributeSelection> aNewAttr =
288 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLab));
290 aNewAttr->setObject(owner());
292 REMOVE_BACK_REF(aNewAttr->context());
294 aLab.ForgetAllAttributes(Standard_True);
296 owner()->data()->sendAttributeUpdated(this);
300 bool Model_AttributeSelectionList::isInitialized()
302 if (size() == 0) { // empty list is not initialized list: sketch will be not valid after add/undo
305 return ModelAPI_AttributeSelectionList::isInitialized();
308 Model_AttributeSelectionList::Model_AttributeSelectionList(TDF_Label& theLabel)
314 void Model_AttributeSelectionList::reinit()
316 myIsInitialized = myLab.FindAttribute(TDataStd_Integer::GetID(), mySize) == Standard_True;
317 if (!myIsInitialized) {
318 mySize = TDataStd_Integer::Set(myLab, 0);
319 mySelectionType = TDataStd_Comment::Set(myLab, "");
320 } else { // recollect mySubs
321 myLab.FindAttribute(TDataStd_Comment::GetID(), mySelectionType);
327 void Model_AttributeSelectionList::cashValues(const bool theEnabled)
329 myIsCashed = theEnabled;
330 myCash.clear(); // empty list as indicator that cash is not used
332 for(int anIndex = size() - 1; anIndex >= 0; anIndex--) {
333 AttributeSelectionPtr anAttr = value(anIndex);
335 myCash[anAttr->context()].push_back(anAttr->value());