1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "Model_AttributeSelectionList.h"
22 #include "Model_AttributeSelection.h"
23 #include "Model_Application.h"
24 #include "Model_Events.h"
25 #include "Model_Data.h"
27 #include <GeomAPI_Shape.h>
29 #include <TDF_AttributeIterator.hxx>
30 #include <TDF_ChildIterator.hxx>
31 #include <TDF_RelocationTable.hxx>
32 #include <TDF_DeltaOnAddition.hxx>
33 #include <TopAbs_ShapeEnum.hxx>
35 #include <TopoDS_Shape.hxx>
36 #include <TopoDS_Edge.hxx>
37 #include <BRep_Tool.hxx>
38 #include <TNaming_Builder.hxx>
39 #include <TNaming_Iterator.hxx>
40 #include <TNaming_NamedShape.hxx>
41 #include <NCollection_List.hxx>
43 void Model_AttributeSelectionList::append(
44 const ObjectPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape,
45 const bool theTemporarily)
47 // do not use the degenerated edge as a shape, a list is not incremented in this case
48 if (theSubShape.get() && !theSubShape->isNull() && theSubShape->isEdge()) {
49 const TopoDS_Shape& aSubShape = theSubShape->impl<TopoDS_Shape>();
50 if (aSubShape.ShapeType() == TopAbs_EDGE && BRep_Tool::Degenerated(TopoDS::Edge(aSubShape))) {
55 if (myIsCashed && !theTemporarily) {
56 ResultPtr aResContext = std::dynamic_pointer_cast<ModelAPI_Result>(theContext);
57 if (aResContext.get())
58 myCash[aResContext].push_back(theSubShape);
61 int aNewTag = mySize->Get() + 1;
62 TDF_Label aNewLab = mySize->Label().FindChild(aNewTag);
64 std::shared_ptr<Model_AttributeSelection> aNewAttr =
65 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aNewLab));
67 aNewAttr->setObject(owner());
68 aNewAttr->setParent(this);
70 aNewAttr->setID(id());
72 aNewAttr->setValue(theContext, theSubShape, theTemporarily);
77 owner()->data()->sendAttributeUpdated(this);
80 void Model_AttributeSelectionList::append(
81 const std::string theNamingName, const std::string& theType)
83 int aNewTag = mySize->Get() + 1;
84 TDF_Label aNewLab = mySize->Label().FindChild(aNewTag);
86 std::shared_ptr<Model_AttributeSelection> aNewAttr =
87 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aNewLab));
89 aNewAttr->setObject(owner());
90 aNewAttr->setParent(this);
92 aNewAttr->setID(id());
94 aNewAttr->selectSubShape(theType.empty() ? selectionType() : theType, theNamingName);
95 owner()->data()->sendAttributeUpdated(this);
98 void Model_AttributeSelectionList::removeTemporaryValues()
100 if (myTmpAttr.get()) {
105 void Model_AttributeSelectionList::removeLast()
107 int anOldSize = mySize->Get();
108 if (anOldSize != 0) {
109 mySize->Set(anOldSize - 1);
110 TDF_Label aLab = mySize->Label().FindChild(anOldSize);
111 std::shared_ptr<Model_AttributeSelection> aOldAttr =
112 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLab));
113 aOldAttr->setObject(owner());
114 REMOVE_BACK_REF(aOldAttr->context());
115 aLab.ForgetAllAttributes(Standard_True);
117 owner()->data()->sendAttributeUpdated(this);
121 // copies named shapes: we need the implementation of this
122 static void CopyNS(const Handle(TNaming_NamedShape)& theFrom,
123 const Handle(TDF_Attribute)& theTo)
125 TDF_Label aLab = theTo->Label();
126 TNaming_Builder B(aLab);
128 // TNaming_Iterator iterates in reversed way than it was put in Builder, so, keep it in container
129 // and iterate from end to begin
130 NCollection_List<TopoDS_Shape> aOlds;
131 NCollection_List<TopoDS_Shape> aNews;
132 NCollection_List<TNaming_Evolution> aStatuses;
134 TNaming_Iterator anIt (theFrom);
135 for ( ;anIt.More() ; anIt.Next()) {
136 aOlds.Prepend(anIt.OldShape());
137 aNews.Prepend(anIt.NewShape());
138 aStatuses.Prepend(anIt.Evolution());
141 NCollection_List<TopoDS_Shape>::Iterator aOldIter(aOlds);
142 NCollection_List<TopoDS_Shape>::Iterator aNewIter(aNews);
143 NCollection_List<TNaming_Evolution>::Iterator aStatIter(aStatuses);
144 for(; aOldIter.More(); aOldIter.Next(), aNewIter.Next(), aStatIter.Next()) {
145 switch (aStatIter.Value()) {
146 case TNaming_PRIMITIVE :
147 B.Generated(aNewIter.Value());
149 case TNaming_GENERATED :
150 B.Generated(aOldIter.Value(), aNewIter.Value());
152 case TNaming_MODIFY :
153 B.Modify(aOldIter.Value(), aNewIter.Value());
155 case TNaming_DELETE :
156 B.Delete (aOldIter.Value());
158 case TNaming_SELECTED :
159 B.Select(aNewIter.Value(), aOldIter.Value());
167 /// makes copy of all attributes on the given label and all sub-labels
168 static void copyAttrs(TDF_Label theSource, TDF_Label theDestination) {
169 TDF_AttributeIterator anAttrIter(theSource);
170 for(; anAttrIter.More(); anAttrIter.Next()) {
171 Handle(TDF_Attribute) aTargetAttr;
172 if (!theDestination.FindAttribute(anAttrIter.Value()->ID(), aTargetAttr)) {
173 // create a new attribute if not yet exists in the destination
174 aTargetAttr = anAttrIter.Value()->NewEmpty();
175 theDestination.AddAttribute(aTargetAttr);
177 // for named shape copy exact shapes (in NamedShape Paste method the CopyTool is used)
178 Handle(TNaming_NamedShape) aNS = Handle(TNaming_NamedShape)::DownCast(anAttrIter.Value());
180 // no special relocation, empty map, but self-relocation is on: copy references w/o changes
181 Handle(TDF_RelocationTable) aRelocTable = new TDF_RelocationTable(Standard_True);
182 anAttrIter.Value()->Paste(aTargetAttr, aRelocTable);
184 CopyNS(aNS, aTargetAttr);
187 // copy the sub-labels content
188 TDF_ChildIterator aSubLabsIter(theSource);
189 for(; aSubLabsIter.More(); aSubLabsIter.Next()) {
190 copyAttrs(aSubLabsIter.Value(), theDestination.FindChild(aSubLabsIter.Value().Tag()));
194 void Model_AttributeSelectionList::remove(const std::set<int>& theIndices)
196 int anOldSize = mySize->Get();
198 // iterate one by one and shifting the removed indicies
199 for(int aCurrent = 0; aCurrent < anOldSize; aCurrent++) {
200 if (theIndices.find(aCurrent) == theIndices.end()) { // not removed
201 if (aRemoved) { // but must be shifted to the removed position
202 TDF_Label aSource = mySize->Label().FindChild(aCurrent + 1);
203 TDF_Label aDest = mySize->Label().FindChild(aCurrent + 1 - aRemoved);
204 copyAttrs(aSource, aDest);
205 // remove the moved source
206 aSource.ForgetAllAttributes(Standard_True);
208 } else { // this is removed, so remove everything from this label
209 TDF_Label aLab = mySize->Label().FindChild(aCurrent + 1);
210 std::shared_ptr<Model_AttributeSelection> aOldAttr =
211 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLab));
212 aOldAttr->setObject(owner());
213 REMOVE_BACK_REF(aOldAttr->context());
214 aLab.ForgetAllAttributes(Standard_True);
219 if (aRemoved) { // remove was performed, so, update the size and this attribute
220 mySize->Set(anOldSize - aRemoved);
221 owner()->data()->sendAttributeUpdated(this);
225 int Model_AttributeSelectionList::size()
227 return mySize->Get();
230 bool Model_AttributeSelectionList::isInList(const ObjectPtr& theContext,
231 const std::shared_ptr<GeomAPI_Shape>& theSubShape,
232 const bool theTemporarily)
234 ResultPtr aResCont = std::dynamic_pointer_cast<ModelAPI_Result>(theContext);
235 if (myIsCashed) { // the cashing is active
236 if (aResCont.get()) {
237 std::map<ResultPtr, std::list<std::shared_ptr<GeomAPI_Shape> > >::iterator aContext =
238 myCash.find(aResCont);
239 if (aContext != myCash.end()) {
240 // iterate shapes because "isSame" method must be called for each shape
241 std::list<std::shared_ptr<GeomAPI_Shape> >::iterator aShapes = aContext->second.begin();
242 for(; aShapes != aContext->second.end(); aShapes++) {
243 if (!theSubShape.get()) {
244 if (!aShapes->get() || (*aShapes)->isSame(aContext->first->shape()))
247 // we need to call here isSame instead of isEqual to do not check shapes orientation
248 if (theSubShape->isSame(*aShapes))
257 for(int anIndex = size() - 1; anIndex >= 0; anIndex--) {
258 AttributeSelectionPtr anAttr = value(anIndex);
260 if (anAttr->context() == theContext) { // contexts are equal, so, check that values are also
261 std::shared_ptr<GeomAPI_Shape> aValue = anAttr->value();
262 if (!theSubShape.get()) {
263 if (!aValue.get() || (aResCont.get() && aValue->isSame(aResCont->shape()))) {// both null
267 // we need to call here isSame instead of isEqual to do not check shapes orientation
268 if (theSubShape->isSame(aValue)) // shapes are equal
277 const std::string Model_AttributeSelectionList::selectionType() const
279 return TCollection_AsciiString(mySelectionType->Get()).ToCString();
282 void Model_AttributeSelectionList::setSelectionType(const std::string& theType)
284 mySelectionType->Set(theType.c_str());
287 std::shared_ptr<ModelAPI_AttributeSelection>
288 Model_AttributeSelectionList::value(const int theIndex)
290 if (myTmpAttr.get() && theIndex == size() - 1) {
293 TDF_Label aLabel = mySize->Label().FindChild(theIndex + 1);
294 // create a new attribute each time, by demand
295 // supporting of old attributes is too slow (synch each time) and buggy on redo
296 // (if attribute is deleted and created, the abort updates attriute and makes the Attr invalid)
297 std::shared_ptr<Model_AttributeSelection> aNewAttr =
298 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLabel));
299 aNewAttr->setID(id());
301 aNewAttr->setObject(owner());
302 aNewAttr->setParent(this);
307 void Model_AttributeSelectionList::clear()
309 if (mySize->Get() != 0) {
312 TDF_ChildIterator aSubIter(mySize->Label());
313 for(; aSubIter.More(); aSubIter.Next()) {
314 TDF_Label aLab = aSubIter.Value();
315 std::shared_ptr<Model_AttributeSelection> aNewAttr =
316 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLab));
318 aNewAttr->setObject(owner());
319 aNewAttr->setParent(this);
321 REMOVE_BACK_REF(aNewAttr->context());
323 aLab.ForgetAllAttributes(Standard_True);
325 owner()->data()->sendAttributeUpdated(this);
329 bool Model_AttributeSelectionList::isInitialized()
331 if (size() == 0) { // empty list is not initialized list: sketch will be not valid after add/undo
334 return ModelAPI_AttributeSelectionList::isInitialized();
337 Model_AttributeSelectionList::Model_AttributeSelectionList(TDF_Label& theLabel)
343 void Model_AttributeSelectionList::reinit()
345 myIsInitialized = myLab.FindAttribute(TDataStd_Integer::GetID(), mySize) == Standard_True;
346 if (!myIsInitialized) {
347 mySize = TDataStd_Integer::Set(myLab, 0);
348 mySelectionType = TDataStd_Comment::Set(myLab, "");
349 } else { // recollect mySubs
350 myLab.FindAttribute(TDataStd_Comment::GetID(), mySelectionType);
356 void Model_AttributeSelectionList::cashValues(const bool theEnabled)
358 myIsCashed = theEnabled;
359 myCash.clear(); // empty list as indicator that cash is not used
361 for(int anIndex = size() - 1; anIndex >= 0; anIndex--) {
362 AttributeSelectionPtr anAttr = value(anIndex);
364 myCash[anAttr->context()].push_back(anAttr->value());