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 ResultPtr& 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 myCash[theContext].push_back(theSubShape);
59 int aNewTag = mySize->Get() + 1;
60 TDF_Label aNewLab = mySize->Label().FindChild(aNewTag);
62 std::shared_ptr<Model_AttributeSelection> aNewAttr =
63 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aNewLab));
65 aNewAttr->setObject(owner());
66 aNewAttr->setParent(this);
68 aNewAttr->setID(id());
70 aNewAttr->setValue(theContext, theSubShape, theTemporarily);
75 owner()->data()->sendAttributeUpdated(this);
78 void Model_AttributeSelectionList::append(
79 const std::string theNamingName, const std::string& theType)
81 int aNewTag = mySize->Get() + 1;
82 TDF_Label aNewLab = mySize->Label().FindChild(aNewTag);
84 std::shared_ptr<Model_AttributeSelection> aNewAttr =
85 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aNewLab));
87 aNewAttr->setObject(owner());
88 aNewAttr->setParent(this);
90 aNewAttr->setID(id());
92 aNewAttr->selectSubShape(theType.empty() ? selectionType() : theType, theNamingName);
93 owner()->data()->sendAttributeUpdated(this);
96 void Model_AttributeSelectionList::removeTemporaryValues()
98 if (myTmpAttr.get()) {
103 void Model_AttributeSelectionList::removeLast()
105 int anOldSize = mySize->Get();
106 if (anOldSize != 0) {
107 mySize->Set(anOldSize - 1);
108 TDF_Label aLab = mySize->Label().FindChild(anOldSize);
109 std::shared_ptr<Model_AttributeSelection> aOldAttr =
110 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLab));
111 aOldAttr->setObject(owner());
112 REMOVE_BACK_REF(aOldAttr->context());
113 aLab.ForgetAllAttributes(Standard_True);
115 owner()->data()->sendAttributeUpdated(this);
119 // copies named shapes: we need the implementation of this
120 static void CopyNS(const Handle(TNaming_NamedShape)& theFrom,
121 const Handle(TDF_Attribute)& theTo)
123 TDF_Label aLab = theTo->Label();
124 TNaming_Builder B(aLab);
126 // TNaming_Iterator iterates in reversed way than it was put in Builder, so, keep it in container
127 // and iterate from end to begin
128 NCollection_List<TopoDS_Shape> aOlds;
129 NCollection_List<TopoDS_Shape> aNews;
130 NCollection_List<TNaming_Evolution> aStatuses;
132 TNaming_Iterator anIt (theFrom);
133 for ( ;anIt.More() ; anIt.Next()) {
134 aOlds.Prepend(anIt.OldShape());
135 aNews.Prepend(anIt.NewShape());
136 aStatuses.Prepend(anIt.Evolution());
139 NCollection_List<TopoDS_Shape>::Iterator aOldIter(aOlds);
140 NCollection_List<TopoDS_Shape>::Iterator aNewIter(aNews);
141 NCollection_List<TNaming_Evolution>::Iterator aStatIter(aStatuses);
142 for(; aOldIter.More(); aOldIter.Next(), aNewIter.Next(), aStatIter.Next()) {
143 switch (aStatIter.Value()) {
144 case TNaming_PRIMITIVE :
145 B.Generated(aNewIter.Value());
147 case TNaming_GENERATED :
148 B.Generated(aOldIter.Value(), aNewIter.Value());
150 case TNaming_MODIFY :
151 B.Modify(aOldIter.Value(), aNewIter.Value());
153 case TNaming_DELETE :
154 B.Delete (aOldIter.Value());
156 case TNaming_SELECTED :
157 B.Select(aNewIter.Value(), aOldIter.Value());
165 /// makes copy of all attributes on the given label and all sub-labels
166 static void copyAttrs(TDF_Label theSource, TDF_Label theDestination) {
167 TDF_AttributeIterator anAttrIter(theSource);
168 for(; anAttrIter.More(); anAttrIter.Next()) {
169 Handle(TDF_Attribute) aTargetAttr;
170 if (!theDestination.FindAttribute(anAttrIter.Value()->ID(), aTargetAttr)) {
171 // create a new attribute if not yet exists in the destination
172 aTargetAttr = anAttrIter.Value()->NewEmpty();
173 theDestination.AddAttribute(aTargetAttr);
175 // for named shape copy exact shapes (in NamedShape Paste method the CopyTool is used)
176 Handle(TNaming_NamedShape) aNS = Handle(TNaming_NamedShape)::DownCast(anAttrIter.Value());
178 // no special relocation, empty map, but self-relocation is on: copy references w/o changes
179 Handle(TDF_RelocationTable) aRelocTable = new TDF_RelocationTable(Standard_True);
180 anAttrIter.Value()->Paste(aTargetAttr, aRelocTable);
182 CopyNS(aNS, aTargetAttr);
185 // copy the sub-labels content
186 TDF_ChildIterator aSubLabsIter(theSource);
187 for(; aSubLabsIter.More(); aSubLabsIter.Next()) {
188 copyAttrs(aSubLabsIter.Value(), theDestination.FindChild(aSubLabsIter.Value().Tag()));
192 void Model_AttributeSelectionList::remove(const std::set<int>& theIndices)
194 int anOldSize = mySize->Get();
196 // iterate one by one and shifting the removed indicies
197 for(int aCurrent = 0; aCurrent < anOldSize; aCurrent++) {
198 if (theIndices.find(aCurrent) == theIndices.end()) { // not removed
199 if (aRemoved) { // but must be shifted to the removed position
200 TDF_Label aSource = mySize->Label().FindChild(aCurrent + 1);
201 TDF_Label aDest = mySize->Label().FindChild(aCurrent + 1 - aRemoved);
202 copyAttrs(aSource, aDest);
203 // remove the moved source
204 aSource.ForgetAllAttributes(Standard_True);
206 } else { // this is removed, so remove everything from this label
207 TDF_Label aLab = mySize->Label().FindChild(aCurrent + 1);
208 std::shared_ptr<Model_AttributeSelection> aOldAttr =
209 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLab));
210 aOldAttr->setObject(owner());
211 REMOVE_BACK_REF(aOldAttr->context());
212 aLab.ForgetAllAttributes(Standard_True);
217 if (aRemoved) { // remove was performed, so, update the size and this attribute
218 mySize->Set(anOldSize - aRemoved);
219 owner()->data()->sendAttributeUpdated(this);
223 int Model_AttributeSelectionList::size()
225 return mySize->Get();
228 bool Model_AttributeSelectionList::isInList(const ResultPtr& theContext,
229 const std::shared_ptr<GeomAPI_Shape>& theSubShape,
230 const bool theTemporarily)
232 if (myIsCashed) { // the cashing is active
233 std::map<ResultPtr, std::list<std::shared_ptr<GeomAPI_Shape> > >::iterator aContext =
234 myCash.find(theContext);
235 if (aContext != myCash.end()) {
236 // iterate shapes because "isSame" method must be called for each shape
237 std::list<std::shared_ptr<GeomAPI_Shape> >::iterator aShapes = aContext->second.begin();
238 for(; aShapes != aContext->second.end(); aShapes++) {
239 if (!theSubShape.get()) {
240 if (!aShapes->get() || (*aShapes)->isSame(aContext->first->shape()))
243 // we need to call here isSame instead of isEqual to do not check shapes orientation
244 if (theSubShape->isSame(*aShapes))
252 for(int anIndex = size() - 1; anIndex >= 0; anIndex--) {
253 AttributeSelectionPtr anAttr = value(anIndex);
255 if (anAttr->context() == theContext) { // contexts are equal, so, check that values are also
256 std::shared_ptr<GeomAPI_Shape> aValue = anAttr->value();
257 if (!theSubShape.get()) {
258 if (!aValue.get() || aValue->isSame(theContext->shape())) { // both are null
262 // we need to call here isSame instead of isEqual to do not check shapes orientation
263 if (theSubShape->isSame(aValue)) // shapes are equal
272 const std::string Model_AttributeSelectionList::selectionType() const
274 return TCollection_AsciiString(mySelectionType->Get()).ToCString();
277 void Model_AttributeSelectionList::setSelectionType(const std::string& theType)
279 mySelectionType->Set(theType.c_str());
282 std::shared_ptr<ModelAPI_AttributeSelection>
283 Model_AttributeSelectionList::value(const int theIndex)
285 if (myTmpAttr.get() && theIndex == size() - 1) {
288 TDF_Label aLabel = mySize->Label().FindChild(theIndex + 1);
289 // create a new attribute each time, by demand
290 // supporting of old attributes is too slow (synch each time) and buggy on redo
291 // (if attribute is deleted and created, the abort updates attriute and makes the Attr invalid)
292 std::shared_ptr<Model_AttributeSelection> aNewAttr =
293 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLabel));
294 aNewAttr->setID(id());
296 aNewAttr->setObject(owner());
297 aNewAttr->setParent(this);
302 void Model_AttributeSelectionList::clear()
304 if (mySize->Get() != 0) {
307 TDF_ChildIterator aSubIter(mySize->Label());
308 for(; aSubIter.More(); aSubIter.Next()) {
309 TDF_Label aLab = aSubIter.Value();
310 std::shared_ptr<Model_AttributeSelection> aNewAttr =
311 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLab));
313 aNewAttr->setObject(owner());
314 aNewAttr->setParent(this);
316 REMOVE_BACK_REF(aNewAttr->context());
318 aLab.ForgetAllAttributes(Standard_True);
320 owner()->data()->sendAttributeUpdated(this);
324 bool Model_AttributeSelectionList::isInitialized()
326 if (size() == 0) { // empty list is not initialized list: sketch will be not valid after add/undo
329 return ModelAPI_AttributeSelectionList::isInitialized();
332 Model_AttributeSelectionList::Model_AttributeSelectionList(TDF_Label& theLabel)
338 void Model_AttributeSelectionList::reinit()
340 myIsInitialized = myLab.FindAttribute(TDataStd_Integer::GetID(), mySize) == Standard_True;
341 if (!myIsInitialized) {
342 mySize = TDataStd_Integer::Set(myLab, 0);
343 mySelectionType = TDataStd_Comment::Set(myLab, "");
344 } else { // recollect mySubs
345 myLab.FindAttribute(TDataStd_Comment::GetID(), mySelectionType);
351 void Model_AttributeSelectionList::cashValues(const bool theEnabled)
353 myIsCashed = theEnabled;
354 myCash.clear(); // empty list as indicator that cash is not used
356 for(int anIndex = size() - 1; anIndex >= 0; anIndex--) {
357 AttributeSelectionPtr anAttr = value(anIndex);
359 myCash[anAttr->context()].push_back(anAttr->value());