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 <TDF_DeltaOnAddition.hxx>
17 #include <TopAbs_ShapeEnum.hxx>
19 #include <TopoDS_Shape.hxx>
20 #include <TopoDS_Edge.hxx>
21 #include <BRep_Tool.hxx>
22 #include <TNaming_Builder.hxx>
23 #include <TNaming_Iterator.hxx>
24 #include <TNaming_NamedShape.hxx>
25 #include <NCollection_List.hxx>
27 void Model_AttributeSelectionList::append(
28 const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape,
29 const bool theTemporarily)
31 // do not use the degenerated edge as a shape, a list is not incremented in this case
32 if (theSubShape.get() && !theSubShape->isNull() && theSubShape->isEdge()) {
33 const TopoDS_Shape& aSubShape = theSubShape->impl<TopoDS_Shape>();
34 if (aSubShape.ShapeType() == TopAbs_EDGE && BRep_Tool::Degenerated(TopoDS::Edge(aSubShape))) {
39 if (myIsCashed && !theTemporarily) {
40 myCash[theContext].push_back(theSubShape);
43 int aNewTag = mySize->Get() + 1;
44 TDF_Label aNewLab = mySize->Label().FindChild(aNewTag);
46 std::shared_ptr<Model_AttributeSelection> aNewAttr =
47 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aNewLab));
49 aNewAttr->setObject(owner());
50 aNewAttr->setParent(this);
52 aNewAttr->setID(id());
54 aNewAttr->setValue(theContext, theSubShape, theTemporarily);
59 owner()->data()->sendAttributeUpdated(this);
62 void Model_AttributeSelectionList::append(
63 const std::string theNamingName, const std::string& theType)
65 int aNewTag = mySize->Get() + 1;
66 TDF_Label aNewLab = mySize->Label().FindChild(aNewTag);
68 std::shared_ptr<Model_AttributeSelection> aNewAttr =
69 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aNewLab));
71 aNewAttr->setObject(owner());
72 aNewAttr->setParent(this);
74 aNewAttr->setID(id());
76 aNewAttr->selectSubShape(theType.empty() ? selectionType() : theType, theNamingName);
77 owner()->data()->sendAttributeUpdated(this);
80 void Model_AttributeSelectionList::removeTemporaryValues()
82 if (myTmpAttr.get()) {
87 void Model_AttributeSelectionList::removeLast()
89 int anOldSize = mySize->Get();
91 mySize->Set(anOldSize - 1);
92 TDF_Label aLab = mySize->Label().FindChild(anOldSize);
93 std::shared_ptr<Model_AttributeSelection> aOldAttr =
94 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLab));
95 aOldAttr->setObject(owner());
96 REMOVE_BACK_REF(aOldAttr->context());
97 aLab.ForgetAllAttributes(Standard_True);
99 owner()->data()->sendAttributeUpdated(this);
103 // copies named shapes: we need the implementation of this
104 static void CopyNS(const Handle(TNaming_NamedShape)& theFrom,
105 const Handle(TDF_Attribute)& theTo)
107 TDF_Label aLab = theTo->Label();
108 TNaming_Builder B(aLab);
110 // TNaming_Iterator iterates in reversed way than it was put in Builder, so, keep it in container
111 // and iterate from end to begin
112 NCollection_List<TopoDS_Shape> aOlds;
113 NCollection_List<TopoDS_Shape> aNews;
114 NCollection_List<TNaming_Evolution> aStatuses;
116 TNaming_Iterator anIt (theFrom);
117 for ( ;anIt.More() ; anIt.Next()) {
118 aOlds.Prepend(anIt.OldShape());
119 aNews.Prepend(anIt.NewShape());
120 aStatuses.Prepend(anIt.Evolution());
123 NCollection_List<TopoDS_Shape>::Iterator aOldIter(aOlds);
124 NCollection_List<TopoDS_Shape>::Iterator aNewIter(aNews);
125 NCollection_List<TNaming_Evolution>::Iterator aStatIter(aStatuses);
126 for(; aOldIter.More(); aOldIter.Next(), aNewIter.Next(), aStatIter.Next()) {
127 switch (aStatIter.Value()) {
128 case TNaming_PRIMITIVE :
129 B.Generated(aNewIter.Value());
131 case TNaming_GENERATED :
132 B.Generated(aOldIter.Value(), aNewIter.Value());
134 case TNaming_MODIFY :
135 B.Modify(aOldIter.Value(), aNewIter.Value());
137 case TNaming_DELETE :
138 B.Delete (aOldIter.Value());
140 case TNaming_SELECTED :
141 B.Select(aNewIter.Value(), aOldIter.Value());
149 /// makes copy of all attributes on the given label and all sub-labels
150 static void copyAttrs(TDF_Label theSource, TDF_Label theDestination) {
151 TDF_AttributeIterator anAttrIter(theSource);
152 for(; anAttrIter.More(); anAttrIter.Next()) {
153 Handle(TDF_Attribute) aTargetAttr;
154 if (!theDestination.FindAttribute(anAttrIter.Value()->ID(), aTargetAttr)) {
155 // create a new attribute if not yet exists in the destination
156 aTargetAttr = anAttrIter.Value()->NewEmpty();
157 theDestination.AddAttribute(aTargetAttr);
159 // for named shape copy exact shapes (in NamedShape Paste method the CopyTool is used)
160 Handle(TNaming_NamedShape) aNS = Handle(TNaming_NamedShape)::DownCast(anAttrIter.Value());
162 // no special relocation, empty map, but self-relocation is on: copy references w/o changes
163 Handle(TDF_RelocationTable) aRelocTable = new TDF_RelocationTable(Standard_True);
164 anAttrIter.Value()->Paste(aTargetAttr, aRelocTable);
166 CopyNS(aNS, aTargetAttr);
169 // copy the sub-labels content
170 TDF_ChildIterator aSubLabsIter(theSource);
171 for(; aSubLabsIter.More(); aSubLabsIter.Next()) {
172 copyAttrs(aSubLabsIter.Value(), theDestination.FindChild(aSubLabsIter.Value().Tag()));
176 void Model_AttributeSelectionList::remove(const std::set<int>& theIndices)
178 int anOldSize = mySize->Get();
180 // iterate one by one and shifting the removed indicies
181 for(int aCurrent = 0; aCurrent < anOldSize; aCurrent++) {
182 if (theIndices.find(aCurrent) == theIndices.end()) { // not removed
183 if (aRemoved) { // but must be shifted to the removed position
184 TDF_Label aSource = mySize->Label().FindChild(aCurrent + 1);
185 TDF_Label aDest = mySize->Label().FindChild(aCurrent + 1 - aRemoved);
186 copyAttrs(aSource, aDest);
187 // remove the moved source
188 aSource.ForgetAllAttributes(Standard_True);
190 } else { // this is removed, so remove everything from this label
191 TDF_Label aLab = mySize->Label().FindChild(aCurrent + 1);
192 std::shared_ptr<Model_AttributeSelection> aOldAttr =
193 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLab));
194 aOldAttr->setObject(owner());
195 REMOVE_BACK_REF(aOldAttr->context());
196 aLab.ForgetAllAttributes(Standard_True);
201 if (aRemoved) { // remove was performed, so, update the size and this attribute
202 mySize->Set(anOldSize - aRemoved);
203 owner()->data()->sendAttributeUpdated(this);
207 int Model_AttributeSelectionList::size()
209 return mySize->Get();
212 bool Model_AttributeSelectionList::isInList(const ResultPtr& theContext,
213 const std::shared_ptr<GeomAPI_Shape>& theSubShape,
214 const bool theTemporarily)
216 if (myIsCashed) { // the cashing is active
217 std::map<ResultPtr, std::list<std::shared_ptr<GeomAPI_Shape> > >::iterator aContext =
218 myCash.find(theContext);
219 if (aContext != myCash.end()) {
220 // iterate shapes because "isSame" method must be called for each shape
221 std::list<std::shared_ptr<GeomAPI_Shape> >::iterator aShapes = aContext->second.begin();
222 for(; aShapes != aContext->second.end(); aShapes++) {
223 if (!theSubShape.get()) {
227 // we need to call here isSame instead of isEqual to do not check shapes orientation
228 if (theSubShape->isSame(*aShapes))
236 for(int anIndex = size() - 1; anIndex >= 0; anIndex--) {
237 AttributeSelectionPtr anAttr = value(anIndex);
239 if (anAttr->context() == theContext) { // contexts are equal, so, check that values are also
240 std::shared_ptr<GeomAPI_Shape> aValue = anAttr->value();
242 if (!theSubShape.get()) { // both are null
246 // we need to call here isSame instead of isEqual to do not check shapes orientation
247 if (aValue->isSame(theSubShape)) // shapes are equal
256 const std::string Model_AttributeSelectionList::selectionType() const
258 return TCollection_AsciiString(mySelectionType->Get()).ToCString();
261 void Model_AttributeSelectionList::setSelectionType(const std::string& theType)
263 mySelectionType->Set(theType.c_str());
266 std::shared_ptr<ModelAPI_AttributeSelection>
267 Model_AttributeSelectionList::value(const int theIndex)
269 if (myTmpAttr.get() && theIndex == size() - 1) {
272 TDF_Label aLabel = mySize->Label().FindChild(theIndex + 1);
273 // create a new attribute each time, by demand
274 // supporting of old attributes is too slow (synch each time) and buggy on redo
275 // (if attribute is deleted and created, the abort updates attriute and makes the Attr invalid)
276 std::shared_ptr<Model_AttributeSelection> aNewAttr =
277 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLabel));
278 aNewAttr->setID(id());
280 aNewAttr->setObject(owner());
281 aNewAttr->setParent(this);
286 void Model_AttributeSelectionList::clear()
288 if (mySize->Get() != 0) {
291 TDF_ChildIterator aSubIter(mySize->Label());
292 for(; aSubIter.More(); aSubIter.Next()) {
293 TDF_Label aLab = aSubIter.Value();
294 std::shared_ptr<Model_AttributeSelection> aNewAttr =
295 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLab));
297 aNewAttr->setObject(owner());
298 aNewAttr->setParent(this);
300 REMOVE_BACK_REF(aNewAttr->context());
302 aLab.ForgetAllAttributes(Standard_True);
304 owner()->data()->sendAttributeUpdated(this);
308 bool Model_AttributeSelectionList::isInitialized()
310 if (size() == 0) { // empty list is not initialized list: sketch will be not valid after add/undo
313 return ModelAPI_AttributeSelectionList::isInitialized();
316 Model_AttributeSelectionList::Model_AttributeSelectionList(TDF_Label& theLabel)
322 void Model_AttributeSelectionList::reinit()
324 myIsInitialized = myLab.FindAttribute(TDataStd_Integer::GetID(), mySize) == Standard_True;
325 if (!myIsInitialized) {
326 mySize = TDataStd_Integer::Set(myLab, 0);
327 mySelectionType = TDataStd_Comment::Set(myLab, "");
328 } else { // recollect mySubs
329 myLab.FindAttribute(TDataStd_Comment::GetID(), mySelectionType);
335 void Model_AttributeSelectionList::cashValues(const bool theEnabled)
337 myIsCashed = theEnabled;
338 myCash.clear(); // empty list as indicator that cash is not used
340 for(int anIndex = size() - 1; anIndex >= 0; anIndex--) {
341 AttributeSelectionPtr anAttr = value(anIndex);
343 myCash[anAttr->context()].push_back(anAttr->value());