Salome HOME
1c615ced3505a04201f1ab6bfe933ba95f5e71c9
[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 std::shared_ptr<GeomAlgoAPI_MakeShape> 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 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_MakeShapeList::shape() const
70 {
71   std::shared_ptr<GeomAPI_Shape> 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 std::shared_ptr<GeomAPI_Shape>();
78 }
79
80 //=================================================================================================
81 void GeomAlgoAPI_MakeShapeList::generated(const std::shared_ptr<GeomAPI_Shape> theShape,
82                                           ListOfShape& theHistory)
83 {
84   result(theShape,  GeomAlgoAPI_MakeShapeList::Generated, theHistory);
85 }
86
87 //=================================================================================================
88 void GeomAlgoAPI_MakeShapeList::modified(const std::shared_ptr<GeomAPI_Shape> theShape,
89                                          ListOfShape& theHistory)
90 {
91   result(theShape, GeomAlgoAPI_MakeShapeList::Modified, theHistory);
92 }
93
94 bool GeomAlgoAPI_MakeShapeList::isDeleted(const std::shared_ptr<GeomAPI_Shape> theShape)
95 {
96   for(ListOfMakeShape::iterator aBuilderIt = myListOfMakeShape.begin();
97       aBuilderIt != myListOfMakeShape.end(); aBuilderIt++) {
98     std::shared_ptr<GeomAlgoAPI_MakeShape> aMakeShape = *aBuilderIt;
99     if(aMakeShape->isDeleted(theShape)) {
100       return true;
101     }
102   }
103
104   return false;
105 }
106
107 void GeomAlgoAPI_MakeShapeList::result(const std::shared_ptr<GeomAPI_Shape> theShape,
108                                        OperationType theOperationType,
109                                        ListOfShape& theHistory)
110 {
111   if(myListOfMakeShape.empty()) {
112     return;
113   }
114
115   NCollection_Map<TopoDS_Shape> anAlgoShapes;
116   NCollection_Map<TopoDS_Shape> aResultShapesMap;
117   NCollection_List<TopoDS_Shape> aResultShapesList;
118   anAlgoShapes.Add(theShape->impl<TopoDS_Shape>());
119   aResultShapesMap.Add(theShape->impl<TopoDS_Shape>());
120   aResultShapesList.Append(theShape->impl<TopoDS_Shape>());
121
122   for(ListOfMakeShape::iterator aBuilderIt = myListOfMakeShape.begin();
123       aBuilderIt != myListOfMakeShape.end(); aBuilderIt++) {
124     std::shared_ptr<GeomAlgoAPI_MakeShape> aMakeShape = *aBuilderIt;
125     NCollection_Map<TopoDS_Shape> aTempShapes;
126     for(NCollection_Map<TopoDS_Shape>::Iterator
127         aShapeIt(anAlgoShapes); aShapeIt.More(); aShapeIt.Next()) {
128       bool hasResults = false;
129       bool anArgumentIsInResult = false;
130       std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape);
131       aShape->setImpl(new TopoDS_Shape(aShapeIt.Value()));
132       ListOfShape aGeneratedShapes;
133       aMakeShape->generated(aShape, aGeneratedShapes);
134       for (ListOfShape::const_iterator
135         anIt = aGeneratedShapes.cbegin(); anIt != aGeneratedShapes.cend(); anIt++) {
136         const TopoDS_Shape& anItShape = (*anIt)->impl<TopoDS_Shape>();
137         if (anItShape.IsSame(aShapeIt.Value())) {
138           anArgumentIsInResult = true;
139           continue;
140         }
141         aTempShapes.Add(anItShape);
142         if(aResultShapesMap.Add(anItShape) == Standard_True) {
143           aResultShapesList.Append(anItShape);
144         }
145         hasResults = true;
146       }
147       ListOfShape aModifiedShapes;
148       aMakeShape->modified(aShape, aModifiedShapes);
149       for(ListOfShape::const_iterator
150           anIt = aModifiedShapes.cbegin(); anIt != aModifiedShapes.cend(); anIt++) {
151         const TopoDS_Shape& anItShape = (*anIt)->impl<TopoDS_Shape>();
152         if (anItShape.IsSame(aShapeIt.Value())) {
153           anArgumentIsInResult = true;
154           continue;
155         }
156         aTempShapes.Add(anItShape);
157         if(aResultShapesMap.Add(anItShape) == Standard_True) {
158           aResultShapesList.Append(anItShape);
159         }
160         hasResults = true;
161       }
162       if(hasResults && !anArgumentIsInResult) {
163         const TopoDS_Shape& aTopoDSShape = aShapeIt.Value();
164         if(aResultShapesMap.Remove(aTopoDSShape) == Standard_True) {
165           for(NCollection_List<TopoDS_Shape>::Iterator
166               aResIt(aResultShapesList); aResIt.More(); aResIt.Next()) {
167             if(aTopoDSShape.IsEqual(aResIt.Value())) {
168               aResultShapesList.Remove(aResIt);
169               break;
170             }
171           }
172         }
173       }
174     }
175     anAlgoShapes.Unite(aTempShapes);
176   }
177
178   for(NCollection_List<TopoDS_Shape>::Iterator
179       aShapeIt(aResultShapesList); aShapeIt.More(); aShapeIt.Next()) {
180     std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
181     aShape->setImpl(new TopoDS_Shape(aShapeIt.Value()));
182     theHistory.push_back(aShape);
183   }
184 }