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::append(const std::string& theType,
117 const std::string& theContextName, const int theIndex)
119 int aNewTag = mySize->Get() + 1;
120 TDF_Label aNewLab = mySize->Label().FindChild(aNewTag);
122 std::shared_ptr<Model_AttributeSelection> aNewAttr =
123 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aNewLab));
125 aNewAttr->setObject(owner());
126 aNewAttr->setParent(this);
128 aNewAttr->setID(id());
129 mySize->Set(aNewTag);
130 aNewAttr->selectSubShape(theType, theContextName, theIndex);
131 owner()->data()->sendAttributeUpdated(this);
134 void Model_AttributeSelectionList::removeTemporaryValues()
136 if (myTmpAttr.get()) {
141 void Model_AttributeSelectionList::removeLast()
143 int anOldSize = mySize->Get();
144 if (anOldSize != 0) {
145 mySize->Set(anOldSize - 1);
146 TDF_Label aLab = mySize->Label().FindChild(anOldSize);
147 std::shared_ptr<Model_AttributeSelection> aOldAttr =
148 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLab));
149 aOldAttr->setObject(owner());
150 REMOVE_BACK_REF(aOldAttr->context());
151 aLab.ForgetAllAttributes(Standard_True);
153 owner()->data()->sendAttributeUpdated(this);
157 // copies named shapes: we need the implementation of this
158 static void CopyNS(const Handle(TNaming_NamedShape)& theFrom,
159 const Handle(TDF_Attribute)& theTo)
161 TDF_Label aLab = theTo->Label();
162 TNaming_Builder B(aLab);
164 // TNaming_Iterator iterates in reversed way than it was put in Builder, so, keep it in container
165 // and iterate from end to begin
166 NCollection_List<TopoDS_Shape> aOlds;
167 NCollection_List<TopoDS_Shape> aNews;
168 NCollection_List<TNaming_Evolution> aStatuses;
170 TNaming_Iterator anIt (theFrom);
171 for ( ;anIt.More() ; anIt.Next()) {
172 aOlds.Prepend(anIt.OldShape());
173 aNews.Prepend(anIt.NewShape());
174 aStatuses.Prepend(anIt.Evolution());
177 NCollection_List<TopoDS_Shape>::Iterator aOldIter(aOlds);
178 NCollection_List<TopoDS_Shape>::Iterator aNewIter(aNews);
179 NCollection_List<TNaming_Evolution>::Iterator aStatIter(aStatuses);
180 for(; aOldIter.More(); aOldIter.Next(), aNewIter.Next(), aStatIter.Next()) {
181 switch (aStatIter.Value()) {
182 case TNaming_PRIMITIVE :
183 B.Generated(aNewIter.Value());
185 case TNaming_GENERATED :
186 B.Generated(aOldIter.Value(), aNewIter.Value());
188 case TNaming_MODIFY :
189 B.Modify(aOldIter.Value(), aNewIter.Value());
191 case TNaming_DELETE :
192 B.Delete (aOldIter.Value());
194 case TNaming_SELECTED :
195 B.Select(aNewIter.Value(), aOldIter.Value());
203 /// makes copy of all attributes on the given label and all sub-labels
204 static void copyAttrs(TDF_Label theSource, TDF_Label theDestination) {
205 TDF_AttributeIterator anAttrIter(theSource);
206 for(; anAttrIter.More(); anAttrIter.Next()) {
207 Handle(TDF_Attribute) aTargetAttr;
208 if (!theDestination.FindAttribute(anAttrIter.Value()->ID(), aTargetAttr)) {
209 // create a new attribute if not yet exists in the destination
210 aTargetAttr = anAttrIter.Value()->NewEmpty();
211 theDestination.AddAttribute(aTargetAttr);
213 // for named shape copy exact shapes (in NamedShape Paste method the CopyTool is used)
214 Handle(TNaming_NamedShape) aNS = Handle(TNaming_NamedShape)::DownCast(anAttrIter.Value());
216 // no special relocation, empty map, but self-relocation is on: copy references w/o changes
217 Handle(TDF_RelocationTable) aRelocTable = new TDF_RelocationTable(Standard_True);
218 anAttrIter.Value()->Paste(aTargetAttr, aRelocTable);
220 CopyNS(aNS, aTargetAttr);
223 // copy the sub-labels content
224 TDF_ChildIterator aSubLabsIter(theSource);
225 for(; aSubLabsIter.More(); aSubLabsIter.Next()) {
226 copyAttrs(aSubLabsIter.Value(), theDestination.FindChild(aSubLabsIter.Value().Tag()));
230 void Model_AttributeSelectionList::remove(const std::set<int>& theIndices)
232 int anOldSize = mySize->Get();
234 // iterate one by one and shifting the removed indicies
235 for(int aCurrent = 0; aCurrent < anOldSize; aCurrent++) {
236 if (theIndices.find(aCurrent) == theIndices.end()) { // not removed
237 if (aRemoved) { // but must be shifted to the removed position
238 TDF_Label aSource = mySize->Label().FindChild(aCurrent + 1);
239 TDF_Label aDest = mySize->Label().FindChild(aCurrent + 1 - aRemoved);
240 copyAttrs(aSource, aDest);
241 // remove the moved source
242 aSource.ForgetAllAttributes(Standard_True);
244 } else { // this is removed, so remove everything from this label
245 TDF_Label aLab = mySize->Label().FindChild(aCurrent + 1);
246 std::shared_ptr<Model_AttributeSelection> aOldAttr =
247 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLab));
248 aOldAttr->setObject(owner());
249 REMOVE_BACK_REF(aOldAttr->context());
250 aLab.ForgetAllAttributes(Standard_True);
255 if (aRemoved) { // remove was performed, so, update the size and this attribute
256 mySize->Set(anOldSize - aRemoved);
257 owner()->data()->sendAttributeUpdated(this);
261 int Model_AttributeSelectionList::size()
263 return mySize->Get();
266 bool Model_AttributeSelectionList::isInList(const ObjectPtr& theContext,
267 const std::shared_ptr<GeomAPI_Shape>& theSubShape,
268 const bool theTemporarily)
270 ResultPtr aResCont = std::dynamic_pointer_cast<ModelAPI_Result>(theContext);
271 if (myIsCashed) { // the cashing is active
272 if (aResCont.get()) {
273 std::map<ResultPtr, std::list<std::shared_ptr<GeomAPI_Shape> > >::iterator aContext =
274 myCash.find(aResCont);
275 if (aContext != myCash.end()) {
276 // iterate shapes because "isSame" method must be called for each shape
277 std::list<std::shared_ptr<GeomAPI_Shape> >::iterator aShapes = aContext->second.begin();
278 for(; aShapes != aContext->second.end(); aShapes++) {
279 if (!theSubShape.get()) {
280 if (!aShapes->get() || (*aShapes)->isSame(aContext->first->shape()))
283 // we need to call here isSame instead of isEqual to do not check shapes orientation
284 if (theSubShape->isSame(*aShapes))
293 for(int anIndex = size() - 1; anIndex >= 0; anIndex--) {
294 AttributeSelectionPtr anAttr = value(anIndex);
296 if (anAttr->context() == theContext) { // contexts are equal, so, check that values are also
297 std::shared_ptr<GeomAPI_Shape> aValue = anAttr->value();
298 if (!theSubShape.get()) {
299 if (!aValue.get() || (aResCont.get() && aValue->isSame(aResCont->shape()))) {// both null
303 // we need to call here isSame instead of isEqual to do not check shapes orientation
304 if (theSubShape->isSame(aValue)) // shapes are equal
313 const std::string Model_AttributeSelectionList::selectionType() const
315 return TCollection_AsciiString(mySelectionType->Get()).ToCString();
318 void Model_AttributeSelectionList::setSelectionType(const std::string& theType)
320 mySelectionType->Set(theType.c_str());
323 std::shared_ptr<ModelAPI_AttributeSelection>
324 Model_AttributeSelectionList::value(const int theIndex)
326 if (myTmpAttr.get() && theIndex == size() - 1) {
329 TDF_Label aLabel = mySize->Label().FindChild(theIndex + 1);
330 // create a new attribute each time, by demand
331 // supporting of old attributes is too slow (synch each time) and buggy on redo
332 // (if attribute is deleted and created, the abort updates attriute and makes the Attr invalid)
333 std::shared_ptr<Model_AttributeSelection> aNewAttr =
334 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLabel));
336 aNewAttr->setObject(owner());
337 aNewAttr->setParent(this);
339 aNewAttr->setID(id());
343 void Model_AttributeSelectionList::clear()
345 if (mySize->Get() != 0) {
348 TDF_ChildIterator aSubIter(mySize->Label());
349 for(; aSubIter.More(); aSubIter.Next()) {
350 TDF_Label aLab = aSubIter.Value();
351 std::shared_ptr<Model_AttributeSelection> aNewAttr =
352 std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLab));
354 aNewAttr->setObject(owner());
355 aNewAttr->setParent(this);
357 REMOVE_BACK_REF(aNewAttr->context());
359 aLab.ForgetAllAttributes(Standard_True);
361 owner()->data()->sendAttributeUpdated(this);
365 bool Model_AttributeSelectionList::isInitialized()
367 if (size() == 0) { // empty list is not initialized list: sketch will be not valid after add/undo
370 return ModelAPI_AttributeSelectionList::isInitialized();
373 Model_AttributeSelectionList::Model_AttributeSelectionList(TDF_Label& theLabel)
375 myIsGeometricalSelection(false)
380 void Model_AttributeSelectionList::reinit()
382 myIsInitialized = myLab.FindAttribute(TDataStd_Integer::GetID(), mySize) == Standard_True;
383 if (!myIsInitialized) {
384 mySize = TDataStd_Integer::Set(myLab, 0);
385 mySelectionType = TDataStd_Comment::Set(myLab, "");
386 } else { // recollect mySubs
387 myLab.FindAttribute(TDataStd_Comment::GetID(), mySelectionType);
393 void Model_AttributeSelectionList::cashValues(const bool theEnabled)
395 myIsCashed = theEnabled;
396 myCash.clear(); // empty list as indicator that cash is not used
398 for(int anIndex = size() - 1; anIndex >= 0; anIndex--) {
399 AttributeSelectionPtr anAttr = value(anIndex);
401 myCash[anAttr->context()].push_back(anAttr->value());
407 void Model_AttributeSelectionList::setGeometricalSelection(const bool theIsGeometricalSelection)
409 myIsGeometricalSelection = theIsGeometricalSelection;
410 // TODO: update list accodring to the flag:
411 // false - all objects with same geometry must be splited in separate.
412 // true - all objets with same geometry must be combined into single.