Salome HOME
Updated modified shapes storing.
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_MakeShapeList.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "GeomAlgoAPI_MakeShapeList.h"
22
23
24 #include <NCollection_List.hxx>
25 #include <NCollection_Map.hxx>
26 #include <TopoDS_Shape.hxx>
27
28 //==================================================================================================
29 GeomAlgoAPI_MakeShapeList::GeomAlgoAPI_MakeShapeList()
30 : GeomAlgoAPI_MakeShape()
31 {}
32
33 //==================================================================================================
34 GeomAlgoAPI_MakeShapeList::GeomAlgoAPI_MakeShapeList(const ListOfMakeShape& theMakeShapeList)
35 : GeomAlgoAPI_MakeShape()
36 {
37   init(theMakeShapeList);
38 }
39
40 //==================================================================================================
41 void GeomAlgoAPI_MakeShapeList::init(const ListOfMakeShape& theMakeShapeList)
42 {
43   if(myMap.get()) {
44     myMap->clear();
45   } else {
46     myMap.reset(new GeomAPI_DataMapOfShapeShape);
47   }
48
49   myListOfMakeShape = theMakeShapeList;
50
51   for(ListOfMakeShape::const_iterator anIt = theMakeShapeList.cbegin();
52       anIt != theMakeShapeList.cend(); ++anIt) {
53     myMap->merge((*anIt)->mapOfSubShapes());
54   }
55 }
56
57 //==================================================================================================
58 void GeomAlgoAPI_MakeShapeList::appendAlgo(
59   const GeomMakeShapePtr theMakeShape)
60 {
61   myListOfMakeShape.push_back(theMakeShape);
62   if(!myMap.get()) {
63     myMap.reset(new GeomAPI_DataMapOfShapeShape());
64   }
65   myMap->merge(theMakeShape->mapOfSubShapes());
66 }
67
68 //==================================================================================================
69 const GeomShapePtr GeomAlgoAPI_MakeShapeList::shape() const
70 {
71   GeomShapePtr aShape = GeomAlgoAPI_MakeShape::shape();
72   if(aShape.get() && !aShape->impl<TopoDS_Shape>().IsNull()) {
73     return aShape;
74   } else if(!myListOfMakeShape.empty()) {
75     return myListOfMakeShape.back()->shape();
76   }
77   return GeomShapePtr();
78 }
79
80 //==================================================================================================
81 void GeomAlgoAPI_MakeShapeList::generated(const GeomShapePtr theOldShape,
82                                           ListOfShape& theNewShapes)
83 {
84   result(theOldShape,  GeomAlgoAPI_MakeShapeList::Generated, theNewShapes);
85 }
86
87 //==================================================================================================
88 void GeomAlgoAPI_MakeShapeList::modified(const GeomShapePtr theOldShape,
89                                          ListOfShape& theNewShapes)
90 {
91   result(theOldShape, GeomAlgoAPI_MakeShapeList::Modified, theNewShapes);
92 }
93
94 //==================================================================================================
95 bool GeomAlgoAPI_MakeShapeList::isDeleted(const GeomShapePtr theOldShape)
96 {
97   for (ListOfMakeShape::iterator aBuilderIt = myListOfMakeShape.begin();
98        aBuilderIt != myListOfMakeShape.end();
99        ++aBuilderIt)
100   {
101     GeomMakeShapePtr aMakeShape = *aBuilderIt;
102     if(aMakeShape->isDeleted(theOldShape)) {
103       return true;
104     }
105   }
106
107   return false;
108 }
109
110 //==================================================================================================
111 void GeomAlgoAPI_MakeShapeList::result(const GeomShapePtr theOldShape,
112                                        OperationType theOperationType,
113                                        ListOfShape& theNewShapes)
114 {
115   if(myListOfMakeShape.empty()) {
116     return;
117   }
118
119   NCollection_Map<TopoDS_Shape> anAlgoShapes;
120   NCollection_Map<TopoDS_Shape> aResultShapesMap;
121   NCollection_List<TopoDS_Shape> aResultShapesList;
122   anAlgoShapes.Add(theOldShape->impl<TopoDS_Shape>());
123   aResultShapesMap.Add(theOldShape->impl<TopoDS_Shape>());
124   aResultShapesList.Append(theOldShape->impl<TopoDS_Shape>());
125
126   for(ListOfMakeShape::iterator aBuilderIt = myListOfMakeShape.begin();
127       aBuilderIt != myListOfMakeShape.end();
128       ++aBuilderIt)
129   {
130     GeomMakeShapePtr aMakeShape = *aBuilderIt;
131     NCollection_Map<TopoDS_Shape> aTempShapes;
132     for (NCollection_Map<TopoDS_Shape>::Iterator aShapeIt(anAlgoShapes);
133          aShapeIt.More();
134          aShapeIt.Next())
135     {
136       bool hasResults = false;
137       bool anArgumentIsInResult = false;
138       GeomShapePtr aShape(new GeomAPI_Shape);
139       aShape->setImpl(new TopoDS_Shape(aShapeIt.Value()));
140       ListOfShape aGeneratedShapes;
141       aMakeShape->generated(aShape, aGeneratedShapes);
142       for (ListOfShape::const_iterator anIt = aGeneratedShapes.cbegin();
143            anIt != aGeneratedShapes.cend();
144            ++anIt)
145       {
146         const TopoDS_Shape& anItShape = (*anIt)->impl<TopoDS_Shape>();
147         if (anItShape.IsSame(aShapeIt.Value())) {
148           anArgumentIsInResult = true;
149           continue;
150         }
151         aTempShapes.Add(anItShape);
152         if(aResultShapesMap.Add(anItShape) == Standard_True) {
153           aResultShapesList.Append(anItShape);
154         }
155         hasResults = true;
156       }
157       ListOfShape aModifiedShapes;
158       aMakeShape->modified(aShape, aModifiedShapes);
159       for (ListOfShape::const_iterator anIt = aModifiedShapes.cbegin();
160            anIt != aModifiedShapes.cend();
161            ++anIt)
162       {
163         const TopoDS_Shape& anItShape = (*anIt)->impl<TopoDS_Shape>();
164         if (anItShape.IsSame(aShapeIt.Value())) {
165           anArgumentIsInResult = true;
166           continue;
167         }
168         aTempShapes.Add(anItShape);
169         if(aResultShapesMap.Add(anItShape) == Standard_True) {
170           aResultShapesList.Append(anItShape);
171         }
172         hasResults = true;
173       }
174       if(hasResults && !anArgumentIsInResult) {
175         const TopoDS_Shape& aTopoDSShape = aShapeIt.Value();
176         if(aResultShapesMap.Remove(aTopoDSShape) == Standard_True) {
177           for(NCollection_List<TopoDS_Shape>::Iterator
178               aResIt(aResultShapesList); aResIt.More(); aResIt.Next()) {
179             if(aTopoDSShape.IsEqual(aResIt.Value())) {
180               aResultShapesList.Remove(aResIt);
181               break;
182             }
183           }
184         }
185       }
186     }
187     anAlgoShapes.Unite(aTempShapes);
188   }
189
190   for (NCollection_List<TopoDS_Shape>::Iterator aShapeIt(aResultShapesList);
191        aShapeIt.More();
192        aShapeIt.Next())
193   {
194     GeomShapePtr aShape(new GeomAPI_Shape());
195     aShape->setImpl(new TopoDS_Shape(aShapeIt.Value()));
196     if (!isValidForHistory(aShape)) continue;
197     fixOrientation(aShape);
198     theNewShapes.push_back(aShape);
199   }
200 }