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 owner()->data()->sendAttributeUpdated(this);
116 void Model_AttributeSelectionList::removeTemporaryValues()
118 if (myTmpAttr.get()) {
123 void Model_AttributeSelectionList::removeLast()
125 int anOldSize = mySize->Get();
126 if (anOldSize != 0) {
127 mySize->Set(anOldSize - 1);
128 TDF_Label aLab = mySize->Label().FindChild(anOldSize);
129 std::shared_ptr<Model_AttributeSelection> aOldAttr =
130 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLab));
131 aOldAttr->setObject(owner());
132 REMOVE_BACK_REF(aOldAttr->context());
133 aLab.ForgetAllAttributes(Standard_True);
135 owner()->data()->sendAttributeUpdated(this);
139 // copies named shapes: we need the implementation of this
140 static void CopyNS(const Handle(TNaming_NamedShape)& theFrom,
141 const Handle(TDF_Attribute)& theTo)
143 TDF_Label aLab = theTo->Label();
144 TNaming_Builder B(aLab);
146 // TNaming_Iterator iterates in reversed way than it was put in Builder, so, keep it in container
147 // and iterate from end to begin
148 NCollection_List<TopoDS_Shape> aOlds;
149 NCollection_List<TopoDS_Shape> aNews;
150 NCollection_List<TNaming_Evolution> aStatuses;
152 TNaming_Iterator anIt (theFrom);
153 for ( ;anIt.More() ; anIt.Next()) {
154 aOlds.Prepend(anIt.OldShape());
155 aNews.Prepend(anIt.NewShape());
156 aStatuses.Prepend(anIt.Evolution());
159 NCollection_List<TopoDS_Shape>::Iterator aOldIter(aOlds);
160 NCollection_List<TopoDS_Shape>::Iterator aNewIter(aNews);
161 NCollection_List<TNaming_Evolution>::Iterator aStatIter(aStatuses);
162 for(; aOldIter.More(); aOldIter.Next(), aNewIter.Next(), aStatIter.Next()) {
163 switch (aStatIter.Value()) {
164 case TNaming_PRIMITIVE :
165 B.Generated(aNewIter.Value());
167 case TNaming_GENERATED :
168 B.Generated(aOldIter.Value(), aNewIter.Value());
170 case TNaming_MODIFY :
171 B.Modify(aOldIter.Value(), aNewIter.Value());
173 case TNaming_DELETE :
174 B.Delete (aOldIter.Value());
176 case TNaming_SELECTED :
177 B.Select(aNewIter.Value(), aOldIter.Value());
185 /// makes copy of all attributes on the given label and all sub-labels
186 static void copyAttrs(TDF_Label theSource, TDF_Label theDestination) {
187 TDF_AttributeIterator anAttrIter(theSource);
188 for(; anAttrIter.More(); anAttrIter.Next()) {
189 Handle(TDF_Attribute) aTargetAttr;
190 if (!theDestination.FindAttribute(anAttrIter.Value()->ID(), aTargetAttr)) {
191 // create a new attribute if not yet exists in the destination
192 aTargetAttr = anAttrIter.Value()->NewEmpty();
193 theDestination.AddAttribute(aTargetAttr);
195 // for named shape copy exact shapes (in NamedShape Paste method the CopyTool is used)
196 Handle(TNaming_NamedShape) aNS = Handle(TNaming_NamedShape)::DownCast(anAttrIter.Value());
198 // no special relocation, empty map, but self-relocation is on: copy references w/o changes
199 Handle(TDF_RelocationTable) aRelocTable = new TDF_RelocationTable(Standard_True);
200 anAttrIter.Value()->Paste(aTargetAttr, aRelocTable);
202 CopyNS(aNS, aTargetAttr);
205 // copy the sub-labels content
206 TDF_ChildIterator aSubLabsIter(theSource);
207 for(; aSubLabsIter.More(); aSubLabsIter.Next()) {
208 copyAttrs(aSubLabsIter.Value(), theDestination.FindChild(aSubLabsIter.Value().Tag()));
212 void Model_AttributeSelectionList::remove(const std::set<int>& theIndices)
214 int anOldSize = mySize->Get();
216 // iterate one by one and shifting the removed indicies
217 for(int aCurrent = 0; aCurrent < anOldSize; aCurrent++) {
218 if (theIndices.find(aCurrent) == theIndices.end()) { // not removed
219 if (aRemoved) { // but must be shifted to the removed position
220 TDF_Label aSource = mySize->Label().FindChild(aCurrent + 1);
221 TDF_Label aDest = mySize->Label().FindChild(aCurrent + 1 - aRemoved);
222 copyAttrs(aSource, aDest);
223 // remove the moved source
224 aSource.ForgetAllAttributes(Standard_True);
226 } else { // this is removed, so remove everything from this label
227 TDF_Label aLab = mySize->Label().FindChild(aCurrent + 1);
228 std::shared_ptr<Model_AttributeSelection> aOldAttr =
229 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLab));
230 aOldAttr->setObject(owner());
231 REMOVE_BACK_REF(aOldAttr->context());
232 aLab.ForgetAllAttributes(Standard_True);
237 if (aRemoved) { // remove was performed, so, update the size and this attribute
238 mySize->Set(anOldSize - aRemoved);
239 owner()->data()->sendAttributeUpdated(this);
243 int Model_AttributeSelectionList::size()
245 return mySize->Get();
248 bool Model_AttributeSelectionList::isInList(const ObjectPtr& theContext,
249 const std::shared_ptr<GeomAPI_Shape>& theSubShape,
250 const bool theTemporarily)
252 ResultPtr aResCont = std::dynamic_pointer_cast<ModelAPI_Result>(theContext);
253 if (myIsCashed) { // the cashing is active
254 if (aResCont.get()) {
255 std::map<ResultPtr, std::list<std::shared_ptr<GeomAPI_Shape> > >::iterator aContext =
256 myCash.find(aResCont);
257 if (aContext != myCash.end()) {
258 // iterate shapes because "isSame" method must be called for each shape
259 std::list<std::shared_ptr<GeomAPI_Shape> >::iterator aShapes = aContext->second.begin();
260 for(; aShapes != aContext->second.end(); aShapes++) {
261 if (!theSubShape.get()) {
262 if (!aShapes->get() || (*aShapes)->isSame(aContext->first->shape()))
265 // we need to call here isSame instead of isEqual to do not check shapes orientation
266 if (theSubShape->isSame(*aShapes))
275 for(int anIndex = size() - 1; anIndex >= 0; anIndex--) {
276 AttributeSelectionPtr anAttr = value(anIndex);
278 if (anAttr->context() == theContext) { // contexts are equal, so, check that values are also
279 std::shared_ptr<GeomAPI_Shape> aValue = anAttr->value();
280 if (!theSubShape.get()) {
281 if (!aValue.get() || (aResCont.get() && aValue->isSame(aResCont->shape()))) {// both null
285 // we need to call here isSame instead of isEqual to do not check shapes orientation
286 if (theSubShape->isSame(aValue)) // shapes are equal
295 const std::string Model_AttributeSelectionList::selectionType() const
297 return TCollection_AsciiString(mySelectionType->Get()).ToCString();
300 void Model_AttributeSelectionList::setSelectionType(const std::string& theType)
302 mySelectionType->Set(theType.c_str());
305 std::shared_ptr<ModelAPI_AttributeSelection>
306 Model_AttributeSelectionList::value(const int theIndex)
308 if (myTmpAttr.get() && theIndex == size() - 1) {
311 TDF_Label aLabel = mySize->Label().FindChild(theIndex + 1);
312 // create a new attribute each time, by demand
313 // supporting of old attributes is too slow (synch each time) and buggy on redo
314 // (if attribute is deleted and created, the abort updates attriute and makes the Attr invalid)
315 std::shared_ptr<Model_AttributeSelection> aNewAttr =
316 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLabel));
317 aNewAttr->setID(id());
319 aNewAttr->setObject(owner());
320 aNewAttr->setParent(this);
325 void Model_AttributeSelectionList::clear()
327 if (mySize->Get() != 0) {
330 TDF_ChildIterator aSubIter(mySize->Label());
331 for(; aSubIter.More(); aSubIter.Next()) {
332 TDF_Label aLab = aSubIter.Value();
333 std::shared_ptr<Model_AttributeSelection> aNewAttr =
334 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLab));
336 aNewAttr->setObject(owner());
337 aNewAttr->setParent(this);
339 REMOVE_BACK_REF(aNewAttr->context());
341 aLab.ForgetAllAttributes(Standard_True);
343 owner()->data()->sendAttributeUpdated(this);
347 bool Model_AttributeSelectionList::isInitialized()
349 if (size() == 0) { // empty list is not initialized list: sketch will be not valid after add/undo
352 return ModelAPI_AttributeSelectionList::isInitialized();
355 Model_AttributeSelectionList::Model_AttributeSelectionList(TDF_Label& theLabel)
361 void Model_AttributeSelectionList::reinit()
363 myIsInitialized = myLab.FindAttribute(TDataStd_Integer::GetID(), mySize) == Standard_True;
364 if (!myIsInitialized) {
365 mySize = TDataStd_Integer::Set(myLab, 0);
366 mySelectionType = TDataStd_Comment::Set(myLab, "");
367 } else { // recollect mySubs
368 myLab.FindAttribute(TDataStd_Comment::GetID(), mySelectionType);
374 void Model_AttributeSelectionList::cashValues(const bool theEnabled)
376 myIsCashed = theEnabled;
377 myCash.clear(); // empty list as indicator that cash is not used
379 for(int anIndex = size() - 1; anIndex >= 0; anIndex--) {
380 AttributeSelectionPtr anAttr = value(anIndex);
382 myCash[anAttr->context()].push_back(anAttr->value());