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 email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
20 #include "Model_AttributeSelectionList.h"
21 #include "Model_AttributeSelection.h"
22 #include "Model_Application.h"
23 #include "Model_Events.h"
24 #include "Model_Data.h"
26 #include <GeomAPI_Shape.h>
28 #include <TDF_AttributeIterator.hxx>
29 #include <TDF_ChildIterator.hxx>
30 #include <TDF_RelocationTable.hxx>
31 #include <TDF_DeltaOnAddition.hxx>
32 #include <TopAbs_ShapeEnum.hxx>
34 #include <TopoDS_Shape.hxx>
35 #include <TopoDS_Edge.hxx>
36 #include <BRep_Tool.hxx>
37 #include <TNaming_Builder.hxx>
38 #include <TNaming_Iterator.hxx>
39 #include <TNaming_NamedShape.hxx>
40 #include <NCollection_List.hxx>
42 void Model_AttributeSelectionList::append(
43 const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape,
44 const bool theTemporarily)
46 // do not use the degenerated edge as a shape, a list is not incremented in this case
47 if (theSubShape.get() && !theSubShape->isNull() && theSubShape->isEdge()) {
48 const TopoDS_Shape& aSubShape = theSubShape->impl<TopoDS_Shape>();
49 if (aSubShape.ShapeType() == TopAbs_EDGE && BRep_Tool::Degenerated(TopoDS::Edge(aSubShape))) {
54 if (myIsCashed && !theTemporarily) {
55 myCash[theContext].push_back(theSubShape);
58 int aNewTag = mySize->Get() + 1;
59 TDF_Label aNewLab = mySize->Label().FindChild(aNewTag);
61 std::shared_ptr<Model_AttributeSelection> aNewAttr =
62 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aNewLab));
64 aNewAttr->setObject(owner());
65 aNewAttr->setParent(this);
67 aNewAttr->setID(id());
69 aNewAttr->setValue(theContext, theSubShape, theTemporarily);
74 owner()->data()->sendAttributeUpdated(this);
77 void Model_AttributeSelectionList::append(
78 const std::string theNamingName, const std::string& theType)
80 int aNewTag = mySize->Get() + 1;
81 TDF_Label aNewLab = mySize->Label().FindChild(aNewTag);
83 std::shared_ptr<Model_AttributeSelection> aNewAttr =
84 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aNewLab));
86 aNewAttr->setObject(owner());
87 aNewAttr->setParent(this);
89 aNewAttr->setID(id());
91 aNewAttr->selectSubShape(theType.empty() ? selectionType() : theType, theNamingName);
92 owner()->data()->sendAttributeUpdated(this);
95 void Model_AttributeSelectionList::removeTemporaryValues()
97 if (myTmpAttr.get()) {
102 void Model_AttributeSelectionList::removeLast()
104 int anOldSize = mySize->Get();
105 if (anOldSize != 0) {
106 mySize->Set(anOldSize - 1);
107 TDF_Label aLab = mySize->Label().FindChild(anOldSize);
108 std::shared_ptr<Model_AttributeSelection> aOldAttr =
109 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLab));
110 aOldAttr->setObject(owner());
111 REMOVE_BACK_REF(aOldAttr->context());
112 aLab.ForgetAllAttributes(Standard_True);
114 owner()->data()->sendAttributeUpdated(this);
118 // copies named shapes: we need the implementation of this
119 static void CopyNS(const Handle(TNaming_NamedShape)& theFrom,
120 const Handle(TDF_Attribute)& theTo)
122 TDF_Label aLab = theTo->Label();
123 TNaming_Builder B(aLab);
125 // TNaming_Iterator iterates in reversed way than it was put in Builder, so, keep it in container
126 // and iterate from end to begin
127 NCollection_List<TopoDS_Shape> aOlds;
128 NCollection_List<TopoDS_Shape> aNews;
129 NCollection_List<TNaming_Evolution> aStatuses;
131 TNaming_Iterator anIt (theFrom);
132 for ( ;anIt.More() ; anIt.Next()) {
133 aOlds.Prepend(anIt.OldShape());
134 aNews.Prepend(anIt.NewShape());
135 aStatuses.Prepend(anIt.Evolution());
138 NCollection_List<TopoDS_Shape>::Iterator aOldIter(aOlds);
139 NCollection_List<TopoDS_Shape>::Iterator aNewIter(aNews);
140 NCollection_List<TNaming_Evolution>::Iterator aStatIter(aStatuses);
141 for(; aOldIter.More(); aOldIter.Next(), aNewIter.Next(), aStatIter.Next()) {
142 switch (aStatIter.Value()) {
143 case TNaming_PRIMITIVE :
144 B.Generated(aNewIter.Value());
146 case TNaming_GENERATED :
147 B.Generated(aOldIter.Value(), aNewIter.Value());
149 case TNaming_MODIFY :
150 B.Modify(aOldIter.Value(), aNewIter.Value());
152 case TNaming_DELETE :
153 B.Delete (aOldIter.Value());
155 case TNaming_SELECTED :
156 B.Select(aNewIter.Value(), aOldIter.Value());
164 /// makes copy of all attributes on the given label and all sub-labels
165 static void copyAttrs(TDF_Label theSource, TDF_Label theDestination) {
166 TDF_AttributeIterator anAttrIter(theSource);
167 for(; anAttrIter.More(); anAttrIter.Next()) {
168 Handle(TDF_Attribute) aTargetAttr;
169 if (!theDestination.FindAttribute(anAttrIter.Value()->ID(), aTargetAttr)) {
170 // create a new attribute if not yet exists in the destination
171 aTargetAttr = anAttrIter.Value()->NewEmpty();
172 theDestination.AddAttribute(aTargetAttr);
174 // for named shape copy exact shapes (in NamedShape Paste method the CopyTool is used)
175 Handle(TNaming_NamedShape) aNS = Handle(TNaming_NamedShape)::DownCast(anAttrIter.Value());
177 // no special relocation, empty map, but self-relocation is on: copy references w/o changes
178 Handle(TDF_RelocationTable) aRelocTable = new TDF_RelocationTable(Standard_True);
179 anAttrIter.Value()->Paste(aTargetAttr, aRelocTable);
181 CopyNS(aNS, aTargetAttr);
184 // copy the sub-labels content
185 TDF_ChildIterator aSubLabsIter(theSource);
186 for(; aSubLabsIter.More(); aSubLabsIter.Next()) {
187 copyAttrs(aSubLabsIter.Value(), theDestination.FindChild(aSubLabsIter.Value().Tag()));
191 void Model_AttributeSelectionList::remove(const std::set<int>& theIndices)
193 int anOldSize = mySize->Get();
195 // iterate one by one and shifting the removed indicies
196 for(int aCurrent = 0; aCurrent < anOldSize; aCurrent++) {
197 if (theIndices.find(aCurrent) == theIndices.end()) { // not removed
198 if (aRemoved) { // but must be shifted to the removed position
199 TDF_Label aSource = mySize->Label().FindChild(aCurrent + 1);
200 TDF_Label aDest = mySize->Label().FindChild(aCurrent + 1 - aRemoved);
201 copyAttrs(aSource, aDest);
202 // remove the moved source
203 aSource.ForgetAllAttributes(Standard_True);
205 } else { // this is removed, so remove everything from this label
206 TDF_Label aLab = mySize->Label().FindChild(aCurrent + 1);
207 std::shared_ptr<Model_AttributeSelection> aOldAttr =
208 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLab));
209 aOldAttr->setObject(owner());
210 REMOVE_BACK_REF(aOldAttr->context());
211 aLab.ForgetAllAttributes(Standard_True);
216 if (aRemoved) { // remove was performed, so, update the size and this attribute
217 mySize->Set(anOldSize - aRemoved);
218 owner()->data()->sendAttributeUpdated(this);
222 int Model_AttributeSelectionList::size()
224 return mySize->Get();
227 bool Model_AttributeSelectionList::isInList(const ResultPtr& theContext,
228 const std::shared_ptr<GeomAPI_Shape>& theSubShape,
229 const bool theTemporarily)
231 if (myIsCashed) { // the cashing is active
232 std::map<ResultPtr, std::list<std::shared_ptr<GeomAPI_Shape> > >::iterator aContext =
233 myCash.find(theContext);
234 if (aContext != myCash.end()) {
235 // iterate shapes because "isSame" method must be called for each shape
236 std::list<std::shared_ptr<GeomAPI_Shape> >::iterator aShapes = aContext->second.begin();
237 for(; aShapes != aContext->second.end(); aShapes++) {
238 if (!theSubShape.get()) {
242 // we need to call here isSame instead of isEqual to do not check shapes orientation
243 if (theSubShape->isSame(*aShapes))
251 for(int anIndex = size() - 1; anIndex >= 0; anIndex--) {
252 AttributeSelectionPtr anAttr = value(anIndex);
254 if (anAttr->context() == theContext) { // contexts are equal, so, check that values are also
255 std::shared_ptr<GeomAPI_Shape> aValue = anAttr->value();
257 if (!theSubShape.get()) { // both are null
261 // we need to call here isSame instead of isEqual to do not check shapes orientation
262 if (aValue->isSame(theSubShape)) // shapes are equal
271 const std::string Model_AttributeSelectionList::selectionType() const
273 return TCollection_AsciiString(mySelectionType->Get()).ToCString();
276 void Model_AttributeSelectionList::setSelectionType(const std::string& theType)
278 mySelectionType->Set(theType.c_str());
281 std::shared_ptr<ModelAPI_AttributeSelection>
282 Model_AttributeSelectionList::value(const int theIndex)
284 if (myTmpAttr.get() && theIndex == size() - 1) {
287 TDF_Label aLabel = mySize->Label().FindChild(theIndex + 1);
288 // create a new attribute each time, by demand
289 // supporting of old attributes is too slow (synch each time) and buggy on redo
290 // (if attribute is deleted and created, the abort updates attriute and makes the Attr invalid)
291 std::shared_ptr<Model_AttributeSelection> aNewAttr =
292 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLabel));
293 aNewAttr->setID(id());
295 aNewAttr->setObject(owner());
296 aNewAttr->setParent(this);
301 void Model_AttributeSelectionList::clear()
303 if (mySize->Get() != 0) {
306 TDF_ChildIterator aSubIter(mySize->Label());
307 for(; aSubIter.More(); aSubIter.Next()) {
308 TDF_Label aLab = aSubIter.Value();
309 std::shared_ptr<Model_AttributeSelection> aNewAttr =
310 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLab));
312 aNewAttr->setObject(owner());
313 aNewAttr->setParent(this);
315 REMOVE_BACK_REF(aNewAttr->context());
317 aLab.ForgetAllAttributes(Standard_True);
319 owner()->data()->sendAttributeUpdated(this);
323 bool Model_AttributeSelectionList::isInitialized()
325 if (size() == 0) { // empty list is not initialized list: sketch will be not valid after add/undo
328 return ModelAPI_AttributeSelectionList::isInitialized();
331 Model_AttributeSelectionList::Model_AttributeSelectionList(TDF_Label& theLabel)
337 void Model_AttributeSelectionList::reinit()
339 myIsInitialized = myLab.FindAttribute(TDataStd_Integer::GetID(), mySize) == Standard_True;
340 if (!myIsInitialized) {
341 mySize = TDataStd_Integer::Set(myLab, 0);
342 mySelectionType = TDataStd_Comment::Set(myLab, "");
343 } else { // recollect mySubs
344 myLab.FindAttribute(TDataStd_Comment::GetID(), mySelectionType);
350 void Model_AttributeSelectionList::cashValues(const bool theEnabled)
352 myIsCashed = theEnabled;
353 myCash.clear(); // empty list as indicator that cash is not used
355 for(int anIndex = size() - 1; anIndex >= 0; anIndex--) {
356 AttributeSelectionPtr anAttr = value(anIndex);
358 myCash[anAttr->context()].push_back(anAttr->value());