Salome HOME
Make extrusion-cut store from-to faces in the data tree.
[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 const ListOfMakeShape& GeomAlgoAPI_MakeShapeList::list() const
58 {
59   return myListOfMakeShape;
60 }
61
62
63 //==================================================================================================
64 void GeomAlgoAPI_MakeShapeList::appendAlgo(
65   const GeomMakeShapePtr theMakeShape)
66 {
67   myListOfMakeShape.push_back(theMakeShape);
68   if(!myMap.get()) {
69     myMap.reset(new GeomAPI_DataMapOfShapeShape());
70   }
71   myMap->merge(theMakeShape->mapOfSubShapes());
72 }
73
74 //==================================================================================================
75 const GeomShapePtr GeomAlgoAPI_MakeShapeList::shape() const
76 {
77   GeomShapePtr aShape = GeomAlgoAPI_MakeShape::shape();
78   if(aShape.get() && !aShape->impl<TopoDS_Shape>().IsNull()) {
79     return aShape;
80   } else if(!myListOfMakeShape.empty()) {
81     return myListOfMakeShape.back()->shape();
82   }
83   return GeomShapePtr();
84 }
85
86 //==================================================================================================
87 void GeomAlgoAPI_MakeShapeList::generated(const GeomShapePtr theOldShape,
88                                           ListOfShape& theNewShapes)
89 {
90   result(theOldShape,  GeomAlgoAPI_MakeShapeList::Generated, theNewShapes);
91 }
92
93 //==================================================================================================
94 void GeomAlgoAPI_MakeShapeList::modified(const GeomShapePtr theOldShape,
95                                          ListOfShape& theNewShapes)
96 {
97   result(theOldShape, GeomAlgoAPI_MakeShapeList::Modified, theNewShapes);
98 }
99
100 //==================================================================================================
101 bool GeomAlgoAPI_MakeShapeList::isDeleted(const GeomShapePtr theOldShape)
102 {
103   for (ListOfMakeShape::iterator aBuilderIt = myListOfMakeShape.begin();
104        aBuilderIt != myListOfMakeShape.end();
105        ++aBuilderIt)
106   {
107     GeomMakeShapePtr aMakeShape = *aBuilderIt;
108     if(aMakeShape->isDeleted(theOldShape)) {
109       return true;
110     }
111   }
112
113   return false;
114 }
115
116 //==================================================================================================
117 void GeomAlgoAPI_MakeShapeList::result(const GeomShapePtr theOldShape,
118                                        OperationType theOperationType,
119                                        ListOfShape& theNewShapes)
120 {
121   if(myListOfMakeShape.empty()) {
122     return;
123   }
124
125   NCollection_Map<TopoDS_Shape> anAlgoShapes;
126   NCollection_Map<TopoDS_Shape> aResultShapesMap;
127   NCollection_List<TopoDS_Shape> aResultShapesList;
128   anAlgoShapes.Add(theOldShape->impl<TopoDS_Shape>());
129   aResultShapesMap.Add(theOldShape->impl<TopoDS_Shape>());
130   aResultShapesList.Append(theOldShape->impl<TopoDS_Shape>());
131
132   for(ListOfMakeShape::iterator aBuilderIt = myListOfMakeShape.begin();
133       aBuilderIt != myListOfMakeShape.end();
134       ++aBuilderIt)
135   {
136     GeomMakeShapePtr aMakeShape = *aBuilderIt;
137     NCollection_Map<TopoDS_Shape> aTempShapes;
138     for (NCollection_Map<TopoDS_Shape>::Iterator aShapeIt(anAlgoShapes);
139          aShapeIt.More();
140          aShapeIt.Next())
141     {
142       bool hasResults = false;
143       bool anArgumentIsInResult = false;
144       GeomShapePtr aShape(new GeomAPI_Shape);
145       aShape->setImpl(new TopoDS_Shape(aShapeIt.Value()));
146       ListOfShape aGeneratedShapes;
147       aMakeShape->generated(aShape, aGeneratedShapes);
148       for (ListOfShape::const_iterator anIt = aGeneratedShapes.cbegin();
149            anIt != aGeneratedShapes.cend();
150            ++anIt)
151       {
152         const TopoDS_Shape& anItShape = (*anIt)->impl<TopoDS_Shape>();
153         if (anItShape.IsSame(aShapeIt.Value())) {
154           anArgumentIsInResult = true;
155           continue;
156         }
157         aTempShapes.Add(anItShape);
158         if(aResultShapesMap.Add(anItShape) == Standard_True) {
159           aResultShapesList.Append(anItShape);
160         }
161         hasResults = true;
162       }
163       ListOfShape aModifiedShapes;
164       aMakeShape->modified(aShape, aModifiedShapes);
165       for (ListOfShape::const_iterator anIt = aModifiedShapes.cbegin();
166            anIt != aModifiedShapes.cend();
167            ++anIt)
168       {
169         const TopoDS_Shape& anItShape = (*anIt)->impl<TopoDS_Shape>();
170         if (anItShape.IsSame(aShapeIt.Value())) {
171           anArgumentIsInResult = true;
172           continue;
173         }
174         aTempShapes.Add(anItShape);
175         if(aResultShapesMap.Add(anItShape) == Standard_True) {
176           aResultShapesList.Append(anItShape);
177         }
178         hasResults = true;
179       }
180       if(hasResults && !anArgumentIsInResult) {
181         const TopoDS_Shape& aTopoDSShape = aShapeIt.Value();
182         if(aResultShapesMap.Remove(aTopoDSShape) == Standard_True) {
183           for(NCollection_List<TopoDS_Shape>::Iterator
184               aResIt(aResultShapesList); aResIt.More(); aResIt.Next()) {
185             if(aTopoDSShape.IsEqual(aResIt.Value())) {
186               aResultShapesList.Remove(aResIt);
187               break;
188             }
189           }
190         }
191       }
192     }
193     anAlgoShapes.Unite(aTempShapes);
194   }
195
196   for (NCollection_List<TopoDS_Shape>::Iterator aShapeIt(aResultShapesList);
197        aShapeIt.More();
198        aShapeIt.Next())
199   {
200     GeomShapePtr aShape(new GeomAPI_Shape());
201     aShape->setImpl(new TopoDS_Shape(aShapeIt.Value()));
202     if (!isValidForHistory(aShape)) continue;
203     fixOrientation(aShape);
204     theNewShapes.push_back(aShape);
205   }
206 }