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_Pnt.h>
28 #include <GeomAPI_Shape.h>
30 #include <TDF_AttributeIterator.hxx>
31 #include <TDF_ChildIterator.hxx>
32 #include <TDF_RelocationTable.hxx>
33 #include <TDF_DeltaOnAddition.hxx>
34 #include <TopAbs_ShapeEnum.hxx>
36 #include <TopoDS_Shape.hxx>
37 #include <TopoDS_Edge.hxx>
38 #include <BRep_Tool.hxx>
39 #include <TNaming_Builder.hxx>
40 #include <TNaming_Iterator.hxx>
41 #include <TNaming_NamedShape.hxx>
42 #include <NCollection_List.hxx>
44 void Model_AttributeSelectionList::append(
45 const ObjectPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape,
46 const bool theTemporarily)
48 // do not use the degenerated edge as a shape, a list is not incremented in this case
49 if (theSubShape.get() && !theSubShape->isNull() && theSubShape->isEdge()) {
50 const TopoDS_Shape& aSubShape = theSubShape->impl<TopoDS_Shape>();
51 if (aSubShape.ShapeType() == TopAbs_EDGE && BRep_Tool::Degenerated(TopoDS::Edge(aSubShape))) {
56 if (myIsCashed && !theTemporarily) {
57 ResultPtr aResContext = std::dynamic_pointer_cast<ModelAPI_Result>(theContext);
58 if (aResContext.get())
59 myCash[aResContext].push_back(theSubShape);
62 int aNewTag = mySize->Get() + 1;
63 TDF_Label aNewLab = mySize->Label().FindChild(aNewTag);
65 std::shared_ptr<Model_AttributeSelection> aNewAttr =
66 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aNewLab));
68 aNewAttr->setObject(owner());
69 aNewAttr->setParent(this);
71 aNewAttr->setID(id());
73 aNewAttr->setValue(theContext, theSubShape, theTemporarily);
78 owner()->data()->sendAttributeUpdated(this);
81 void Model_AttributeSelectionList::append(
82 const std::string& theNamingName, const std::string& theType)
84 int aNewTag = mySize->Get() + 1;
85 TDF_Label aNewLab = mySize->Label().FindChild(aNewTag);
87 std::shared_ptr<Model_AttributeSelection> aNewAttr =
88 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aNewLab));
90 aNewAttr->setObject(owner());
91 aNewAttr->setParent(this);
93 aNewAttr->setID(id());
95 aNewAttr->selectSubShape(theType.empty() ? selectionType() : theType, theNamingName);
96 owner()->data()->sendAttributeUpdated(this);
99 void Model_AttributeSelectionList::append(const GeomPointPtr& thePoint, const std::string& theType)
101 int aNewTag = mySize->Get() + 1;
102 TDF_Label aNewLab = mySize->Label().FindChild(aNewTag);
104 std::shared_ptr<Model_AttributeSelection> aNewAttr =
105 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aNewLab));
107 aNewAttr->setObject(owner());
108 aNewAttr->setParent(this);
110 aNewAttr->setID(id());
111 mySize->Set(aNewTag);
112 aNewAttr->selectSubShape(theType, thePoint);
113 if (selectionType().empty())
114 setSelectionType(aNewAttr->value()->shapeTypeStr());
115 owner()->data()->sendAttributeUpdated(this);
118 void Model_AttributeSelectionList::removeTemporaryValues()
120 if (myTmpAttr.get()) {
125 void Model_AttributeSelectionList::removeLast()
127 int anOldSize = mySize->Get();
128 if (anOldSize != 0) {
129 mySize->Set(anOldSize - 1);
130 TDF_Label aLab = mySize->Label().FindChild(anOldSize);
131 std::shared_ptr<Model_AttributeSelection> aOldAttr =
132 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLab));
133 aOldAttr->setObject(owner());
134 REMOVE_BACK_REF(aOldAttr->context());
135 aLab.ForgetAllAttributes(Standard_True);
137 owner()->data()->sendAttributeUpdated(this);
141 // copies named shapes: we need the implementation of this
142 static void CopyNS(const Handle(TNaming_NamedShape)& theFrom,
143 const Handle(TDF_Attribute)& theTo)
145 TDF_Label aLab = theTo->Label();
146 TNaming_Builder B(aLab);
148 // TNaming_Iterator iterates in reversed way than it was put in Builder, so, keep it in container
149 // and iterate from end to begin
150 NCollection_List<TopoDS_Shape> aOlds;
151 NCollection_List<TopoDS_Shape> aNews;
152 NCollection_List<TNaming_Evolution> aStatuses;
154 TNaming_Iterator anIt (theFrom);
155 for ( ;anIt.More() ; anIt.Next()) {
156 aOlds.Prepend(anIt.OldShape());
157 aNews.Prepend(anIt.NewShape());
158 aStatuses.Prepend(anIt.Evolution());
161 NCollection_List<TopoDS_Shape>::Iterator aOldIter(aOlds);
162 NCollection_List<TopoDS_Shape>::Iterator aNewIter(aNews);
163 NCollection_List<TNaming_Evolution>::Iterator aStatIter(aStatuses);
164 for(; aOldIter.More(); aOldIter.Next(), aNewIter.Next(), aStatIter.Next()) {
165 switch (aStatIter.Value()) {
166 case TNaming_PRIMITIVE :
167 B.Generated(aNewIter.Value());
169 case TNaming_GENERATED :
170 B.Generated(aOldIter.Value(), aNewIter.Value());
172 case TNaming_MODIFY :
173 B.Modify(aOldIter.Value(), aNewIter.Value());
175 case TNaming_DELETE :
176 B.Delete (aOldIter.Value());
178 case TNaming_SELECTED :
179 B.Select(aNewIter.Value(), aOldIter.Value());
187 /// makes copy of all attributes on the given label and all sub-labels
188 static void copyAttrs(TDF_Label theSource, TDF_Label theDestination) {
189 TDF_AttributeIterator anAttrIter(theSource);
190 for(; anAttrIter.More(); anAttrIter.Next()) {
191 Handle(TDF_Attribute) aTargetAttr;
192 if (!theDestination.FindAttribute(anAttrIter.Value()->ID(), aTargetAttr)) {
193 // create a new attribute if not yet exists in the destination
194 aTargetAttr = anAttrIter.Value()->NewEmpty();
195 theDestination.AddAttribute(aTargetAttr);
197 // for named shape copy exact shapes (in NamedShape Paste method the CopyTool is used)
198 Handle(TNaming_NamedShape) aNS = Handle(TNaming_NamedShape)::DownCast(anAttrIter.Value());
200 // no special relocation, empty map, but self-relocation is on: copy references w/o changes
201 Handle(TDF_RelocationTable) aRelocTable = new TDF_RelocationTable(Standard_True);
202 anAttrIter.Value()->Paste(aTargetAttr, aRelocTable);
204 CopyNS(aNS, aTargetAttr);
207 // copy the sub-labels content
208 TDF_ChildIterator aSubLabsIter(theSource);
209 for(; aSubLabsIter.More(); aSubLabsIter.Next()) {
210 copyAttrs(aSubLabsIter.Value(), theDestination.FindChild(aSubLabsIter.Value().Tag()));
214 void Model_AttributeSelectionList::remove(const std::set<int>& theIndices)
216 int anOldSize = mySize->Get();
218 // iterate one by one and shifting the removed indicies
219 for(int aCurrent = 0; aCurrent < anOldSize; aCurrent++) {
220 if (theIndices.find(aCurrent) == theIndices.end()) { // not removed
221 if (aRemoved) { // but must be shifted to the removed position
222 TDF_Label aSource = mySize->Label().FindChild(aCurrent + 1);
223 TDF_Label aDest = mySize->Label().FindChild(aCurrent + 1 - aRemoved);
224 copyAttrs(aSource, aDest);
225 // remove the moved source
226 aSource.ForgetAllAttributes(Standard_True);
228 } else { // this is removed, so remove everything from this label
229 TDF_Label aLab = mySize->Label().FindChild(aCurrent + 1);
230 std::shared_ptr<Model_AttributeSelection> aOldAttr =
231 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLab));
232 aOldAttr->setObject(owner());
233 REMOVE_BACK_REF(aOldAttr->context());
234 aLab.ForgetAllAttributes(Standard_True);
239 if (aRemoved) { // remove was performed, so, update the size and this attribute
240 mySize->Set(anOldSize - aRemoved);
241 owner()->data()->sendAttributeUpdated(this);
245 int Model_AttributeSelectionList::size()
247 return mySize->Get();
250 bool Model_AttributeSelectionList::isInList(const ObjectPtr& theContext,
251 const std::shared_ptr<GeomAPI_Shape>& theSubShape,
252 const bool theTemporarily)
254 ResultPtr aResCont = std::dynamic_pointer_cast<ModelAPI_Result>(theContext);
255 if (myIsCashed) { // the cashing is active
256 if (aResCont.get()) {
257 std::map<ResultPtr, std::list<std::shared_ptr<GeomAPI_Shape> > >::iterator aContext =
258 myCash.find(aResCont);
259 if (aContext != myCash.end()) {
260 // iterate shapes because "isSame" method must be called for each shape
261 std::list<std::shared_ptr<GeomAPI_Shape> >::iterator aShapes = aContext->second.begin();
262 for(; aShapes != aContext->second.end(); aShapes++) {
263 if (!theSubShape.get()) {
264 if (!aShapes->get() || (*aShapes)->isSame(aContext->first->shape()))
267 // we need to call here isSame instead of isEqual to do not check shapes orientation
268 if (theSubShape->isSame(*aShapes))
277 for(int anIndex = size() - 1; anIndex >= 0; anIndex--) {
278 AttributeSelectionPtr anAttr = value(anIndex);
280 if (anAttr->context() == theContext) { // contexts are equal, so, check that values are also
281 std::shared_ptr<GeomAPI_Shape> aValue = anAttr->value();
282 if (!theSubShape.get()) {
283 if (!aValue.get() || (aResCont.get() && aValue->isSame(aResCont->shape()))) {// both null
287 // we need to call here isSame instead of isEqual to do not check shapes orientation
288 if (theSubShape->isSame(aValue)) // shapes are equal
297 const std::string Model_AttributeSelectionList::selectionType() const
299 return TCollection_AsciiString(mySelectionType->Get()).ToCString();
302 void Model_AttributeSelectionList::setSelectionType(const std::string& theType)
304 mySelectionType->Set(theType.c_str());
307 std::shared_ptr<ModelAPI_AttributeSelection>
308 Model_AttributeSelectionList::value(const int theIndex)
310 if (myTmpAttr.get() && theIndex == size() - 1) {
313 TDF_Label aLabel = mySize->Label().FindChild(theIndex + 1);
314 // create a new attribute each time, by demand
315 // supporting of old attributes is too slow (synch each time) and buggy on redo
316 // (if attribute is deleted and created, the abort updates attriute and makes the Attr invalid)
317 std::shared_ptr<Model_AttributeSelection> aNewAttr =
318 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLabel));
319 aNewAttr->setID(id());
321 aNewAttr->setObject(owner());
322 aNewAttr->setParent(this);
327 void Model_AttributeSelectionList::clear()
329 if (mySize->Get() != 0) {
332 TDF_ChildIterator aSubIter(mySize->Label());
333 for(; aSubIter.More(); aSubIter.Next()) {
334 TDF_Label aLab = aSubIter.Value();
335 std::shared_ptr<Model_AttributeSelection> aNewAttr =
336 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLab));
338 aNewAttr->setObject(owner());
339 aNewAttr->setParent(this);
341 REMOVE_BACK_REF(aNewAttr->context());
343 aLab.ForgetAllAttributes(Standard_True);
345 owner()->data()->sendAttributeUpdated(this);
349 bool Model_AttributeSelectionList::isInitialized()
351 if (size() == 0) { // empty list is not initialized list: sketch will be not valid after add/undo
354 return ModelAPI_AttributeSelectionList::isInitialized();
357 Model_AttributeSelectionList::Model_AttributeSelectionList(TDF_Label& theLabel)
363 void Model_AttributeSelectionList::reinit()
365 myIsInitialized = myLab.FindAttribute(TDataStd_Integer::GetID(), mySize) == Standard_True;
366 if (!myIsInitialized) {
367 mySize = TDataStd_Integer::Set(myLab, 0);
368 mySelectionType = TDataStd_Comment::Set(myLab, "");
369 } else { // recollect mySubs
370 myLab.FindAttribute(TDataStd_Comment::GetID(), mySelectionType);
376 void Model_AttributeSelectionList::cashValues(const bool theEnabled)
378 myIsCashed = theEnabled;
379 myCash.clear(); // empty list as indicator that cash is not used
381 for(int anIndex = size() - 1; anIndex >= 0; anIndex--) {
382 AttributeSelectionPtr anAttr = value(anIndex);
384 myCash[anAttr->context()].push_back(anAttr->value());