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 <GeomAPI_Shape.h>
15 #include <TDF_AttributeIterator.hxx>
16 #include <TDF_ChildIterator.hxx>
17 #include <TDF_RelocationTable.hxx>
18 #include <TDF_DeltaOnAddition.hxx>
19 #include <TopAbs_ShapeEnum.hxx>
21 #include <TopoDS_Shape.hxx>
22 #include <TopoDS_Edge.hxx>
23 #include <BRep_Tool.hxx>
24 #include <TNaming_Builder.hxx>
25 #include <TNaming_Iterator.hxx>
26 #include <TNaming_NamedShape.hxx>
27 #include <NCollection_List.hxx>
29 void Model_AttributeSelectionList::append(
30 const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape,
31 const bool theTemporarily)
33 // do not use the degenerated edge as a shape, a list is not incremented in this case
34 if (theSubShape.get() && !theSubShape->isNull() && theSubShape->isEdge()) {
35 const TopoDS_Shape& aSubShape = theSubShape->impl<TopoDS_Shape>();
36 if (aSubShape.ShapeType() == TopAbs_EDGE && BRep_Tool::Degenerated(TopoDS::Edge(aSubShape))) {
41 if (myIsCashed && !theTemporarily) {
42 myCash[theContext].push_back(theSubShape);
45 int aNewTag = mySize->Get() + 1;
46 TDF_Label aNewLab = mySize->Label().FindChild(aNewTag);
48 std::shared_ptr<Model_AttributeSelection> aNewAttr =
49 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aNewLab));
51 aNewAttr->setObject(owner());
52 aNewAttr->setParent(this);
54 aNewAttr->setID(id());
56 aNewAttr->setValue(theContext, theSubShape, theTemporarily);
61 owner()->data()->sendAttributeUpdated(this);
64 void Model_AttributeSelectionList::append(
65 const std::string theNamingName, const std::string& theType)
67 int aNewTag = mySize->Get() + 1;
68 TDF_Label aNewLab = mySize->Label().FindChild(aNewTag);
70 std::shared_ptr<Model_AttributeSelection> aNewAttr =
71 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aNewLab));
73 aNewAttr->setObject(owner());
74 aNewAttr->setParent(this);
76 aNewAttr->setID(id());
78 aNewAttr->selectSubShape(theType.empty() ? selectionType() : theType, theNamingName);
79 owner()->data()->sendAttributeUpdated(this);
82 void Model_AttributeSelectionList::removeTemporaryValues()
84 if (myTmpAttr.get()) {
89 void Model_AttributeSelectionList::removeLast()
91 int anOldSize = mySize->Get();
93 mySize->Set(anOldSize - 1);
94 TDF_Label aLab = mySize->Label().FindChild(anOldSize);
95 std::shared_ptr<Model_AttributeSelection> aOldAttr =
96 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLab));
97 aOldAttr->setObject(owner());
98 REMOVE_BACK_REF(aOldAttr->context());
99 aLab.ForgetAllAttributes(Standard_True);
101 owner()->data()->sendAttributeUpdated(this);
105 // copies named shapes: we need the implementation of this
106 static void CopyNS(const Handle(TNaming_NamedShape)& theFrom,
107 const Handle(TDF_Attribute)& theTo)
109 TDF_Label aLab = theTo->Label();
110 TNaming_Builder B(aLab);
112 // TNaming_Iterator iterates in reversed way than it was put in Builder, so, keep it in container
113 // and iterate from end to begin
114 NCollection_List<TopoDS_Shape> aOlds;
115 NCollection_List<TopoDS_Shape> aNews;
116 NCollection_List<TNaming_Evolution> aStatuses;
118 TNaming_Iterator anIt (theFrom);
119 for ( ;anIt.More() ; anIt.Next()) {
120 aOlds.Prepend(anIt.OldShape());
121 aNews.Prepend(anIt.NewShape());
122 aStatuses.Prepend(anIt.Evolution());
125 NCollection_List<TopoDS_Shape>::Iterator aOldIter(aOlds);
126 NCollection_List<TopoDS_Shape>::Iterator aNewIter(aNews);
127 NCollection_List<TNaming_Evolution>::Iterator aStatIter(aStatuses);
128 for(; aOldIter.More(); aOldIter.Next(), aNewIter.Next(), aStatIter.Next()) {
129 switch (aStatIter.Value()) {
130 case TNaming_PRIMITIVE :
131 B.Generated(aNewIter.Value());
133 case TNaming_GENERATED :
134 B.Generated(aOldIter.Value(), aNewIter.Value());
136 case TNaming_MODIFY :
137 B.Modify(aOldIter.Value(), aNewIter.Value());
139 case TNaming_DELETE :
140 B.Delete (aOldIter.Value());
142 case TNaming_SELECTED :
143 B.Select(aNewIter.Value(), aOldIter.Value());
151 /// makes copy of all attributes on the given label and all sub-labels
152 static void copyAttrs(TDF_Label theSource, TDF_Label theDestination) {
153 TDF_AttributeIterator anAttrIter(theSource);
154 for(; anAttrIter.More(); anAttrIter.Next()) {
155 Handle(TDF_Attribute) aTargetAttr;
156 if (!theDestination.FindAttribute(anAttrIter.Value()->ID(), aTargetAttr)) {
157 // create a new attribute if not yet exists in the destination
158 aTargetAttr = anAttrIter.Value()->NewEmpty();
159 theDestination.AddAttribute(aTargetAttr);
161 // for named shape copy exact shapes (in NamedShape Paste method the CopyTool is used)
162 Handle(TNaming_NamedShape) aNS = Handle(TNaming_NamedShape)::DownCast(anAttrIter.Value());
164 // no special relocation, empty map, but self-relocation is on: copy references w/o changes
165 Handle(TDF_RelocationTable) aRelocTable = new TDF_RelocationTable(Standard_True);
166 anAttrIter.Value()->Paste(aTargetAttr, aRelocTable);
168 CopyNS(aNS, aTargetAttr);
171 // copy the sub-labels content
172 TDF_ChildIterator aSubLabsIter(theSource);
173 for(; aSubLabsIter.More(); aSubLabsIter.Next()) {
174 copyAttrs(aSubLabsIter.Value(), theDestination.FindChild(aSubLabsIter.Value().Tag()));
178 void Model_AttributeSelectionList::remove(const std::set<int>& theIndices)
180 int anOldSize = mySize->Get();
182 // iterate one by one and shifting the removed indicies
183 for(int aCurrent = 0; aCurrent < anOldSize; aCurrent++) {
184 if (theIndices.find(aCurrent) == theIndices.end()) { // not removed
185 if (aRemoved) { // but must be shifted to the removed position
186 TDF_Label aSource = mySize->Label().FindChild(aCurrent + 1);
187 TDF_Label aDest = mySize->Label().FindChild(aCurrent + 1 - aRemoved);
188 copyAttrs(aSource, aDest);
189 // remove the moved source
190 aSource.ForgetAllAttributes(Standard_True);
192 } else { // this is removed, so remove everything from this label
193 TDF_Label aLab = mySize->Label().FindChild(aCurrent + 1);
194 std::shared_ptr<Model_AttributeSelection> aOldAttr =
195 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLab));
196 aOldAttr->setObject(owner());
197 REMOVE_BACK_REF(aOldAttr->context());
198 aLab.ForgetAllAttributes(Standard_True);
203 if (aRemoved) { // remove was performed, so, update the size and this attribute
204 mySize->Set(anOldSize - aRemoved);
205 owner()->data()->sendAttributeUpdated(this);
209 int Model_AttributeSelectionList::size()
211 return mySize->Get();
214 bool Model_AttributeSelectionList::isInList(const ResultPtr& theContext,
215 const std::shared_ptr<GeomAPI_Shape>& theSubShape,
216 const bool theTemporarily)
218 if (myIsCashed) { // the cashing is active
219 std::map<ResultPtr, std::list<std::shared_ptr<GeomAPI_Shape> > >::iterator aContext =
220 myCash.find(theContext);
221 if (aContext != myCash.end()) {
222 // iterate shapes because "isSame" method must be called for each shape
223 std::list<std::shared_ptr<GeomAPI_Shape> >::iterator aShapes = aContext->second.begin();
224 for(; aShapes != aContext->second.end(); aShapes++) {
225 if (!theSubShape.get()) {
229 // we need to call here isSame instead of isEqual to do not check shapes orientation
230 if (theSubShape->isSame(*aShapes))
238 for(int anIndex = size() - 1; anIndex >= 0; anIndex--) {
239 AttributeSelectionPtr anAttr = value(anIndex);
241 if (anAttr->context() == theContext) { // contexts are equal, so, check that values are also
242 std::shared_ptr<GeomAPI_Shape> aValue = anAttr->value();
244 if (!theSubShape.get()) { // both are null
248 // we need to call here isSame instead of isEqual to do not check shapes orientation
249 if (aValue->isSame(theSubShape)) // shapes are equal
258 const std::string Model_AttributeSelectionList::selectionType() const
260 return TCollection_AsciiString(mySelectionType->Get()).ToCString();
263 void Model_AttributeSelectionList::setSelectionType(const std::string& theType)
265 mySelectionType->Set(theType.c_str());
268 std::shared_ptr<ModelAPI_AttributeSelection>
269 Model_AttributeSelectionList::value(const int theIndex)
271 if (myTmpAttr.get() && theIndex == size() - 1) {
274 TDF_Label aLabel = mySize->Label().FindChild(theIndex + 1);
275 // create a new attribute each time, by demand
276 // supporting of old attributes is too slow (synch each time) and buggy on redo
277 // (if attribute is deleted and created, the abort updates attriute and makes the Attr invalid)
278 std::shared_ptr<Model_AttributeSelection> aNewAttr =
279 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLabel));
280 aNewAttr->setID(id());
282 aNewAttr->setObject(owner());
283 aNewAttr->setParent(this);
288 void Model_AttributeSelectionList::clear()
290 if (mySize->Get() != 0) {
293 TDF_ChildIterator aSubIter(mySize->Label());
294 for(; aSubIter.More(); aSubIter.Next()) {
295 TDF_Label aLab = aSubIter.Value();
296 std::shared_ptr<Model_AttributeSelection> aNewAttr =
297 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLab));
299 aNewAttr->setObject(owner());
300 aNewAttr->setParent(this);
302 REMOVE_BACK_REF(aNewAttr->context());
304 aLab.ForgetAllAttributes(Standard_True);
306 owner()->data()->sendAttributeUpdated(this);
310 bool Model_AttributeSelectionList::isInitialized()
312 if (size() == 0) { // empty list is not initialized list: sketch will be not valid after add/undo
315 return ModelAPI_AttributeSelectionList::isInitialized();
318 Model_AttributeSelectionList::Model_AttributeSelectionList(TDF_Label& theLabel)
324 void Model_AttributeSelectionList::reinit()
326 myIsInitialized = myLab.FindAttribute(TDataStd_Integer::GetID(), mySize) == Standard_True;
327 if (!myIsInitialized) {
328 mySize = TDataStd_Integer::Set(myLab, 0);
329 mySelectionType = TDataStd_Comment::Set(myLab, "");
330 } else { // recollect mySubs
331 myLab.FindAttribute(TDataStd_Comment::GetID(), mySelectionType);
337 void Model_AttributeSelectionList::cashValues(const bool theEnabled)
339 myIsCashed = theEnabled;
340 myCash.clear(); // empty list as indicator that cash is not used
342 for(int anIndex = size() - 1; anIndex >= 0; anIndex--) {
343 AttributeSelectionPtr anAttr = value(anIndex);
345 myCash[anAttr->context()].push_back(anAttr->value());