]> SALOME platform Git repositories - modules/shaper.git/blob - src/GeomAlgoAPI/GeomAlgoAPI_MakeShapeList.cpp
Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[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 email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
18 //
19
20 #include "GeomAlgoAPI_MakeShapeList.h"
21
22
23 #include <NCollection_List.hxx>
24 #include <NCollection_Map.hxx>
25 #include <TopoDS_Shape.hxx>
26
27 //=================================================================================================
28 GeomAlgoAPI_MakeShapeList::GeomAlgoAPI_MakeShapeList()
29 : GeomAlgoAPI_MakeShape()
30 {}
31
32 //=================================================================================================
33 GeomAlgoAPI_MakeShapeList::GeomAlgoAPI_MakeShapeList(const ListOfMakeShape& theMakeShapeList)
34 : GeomAlgoAPI_MakeShape()
35 {
36   init(theMakeShapeList);
37 }
38
39 //=================================================================================================
40 void GeomAlgoAPI_MakeShapeList::init(const ListOfMakeShape& theMakeShapeList)
41 {
42   if(myMap.get()) {
43     myMap->clear();
44   } else {
45     myMap.reset(new GeomAPI_DataMapOfShapeShape);
46   }
47
48   myListOfMakeShape = theMakeShapeList;
49
50   for(ListOfMakeShape::const_iterator anIt = theMakeShapeList.cbegin();
51       anIt != theMakeShapeList.cend(); ++anIt) {
52     myMap->merge((*anIt)->mapOfSubShapes());
53   }
54 }
55
56 //=================================================================================================
57 void GeomAlgoAPI_MakeShapeList::appendAlgo(
58   const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape)
59 {
60   myListOfMakeShape.push_back(theMakeShape);
61   if(!myMap.get()) {
62     myMap.reset(new GeomAPI_DataMapOfShapeShape());
63   }
64   myMap->merge(theMakeShape->mapOfSubShapes());
65 }
66
67 //=================================================================================================
68 const std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_MakeShapeList::shape() const
69 {
70   std::shared_ptr<GeomAPI_Shape> aShape = GeomAlgoAPI_MakeShape::shape();
71   if(aShape.get() && !aShape->impl<TopoDS_Shape>().IsNull()) {
72     return aShape;
73   } else if(!myListOfMakeShape.empty()) {
74     return myListOfMakeShape.back()->shape();
75   }
76   return std::shared_ptr<GeomAPI_Shape>();
77 }
78
79 //=================================================================================================
80 void GeomAlgoAPI_MakeShapeList::generated(const std::shared_ptr<GeomAPI_Shape> theShape,
81                                           ListOfShape& theHistory)
82 {
83   result(theShape,  GeomAlgoAPI_MakeShapeList::Generated, theHistory);
84 }
85
86 //=================================================================================================
87 void GeomAlgoAPI_MakeShapeList::modified(const std::shared_ptr<GeomAPI_Shape> theShape,
88                                          ListOfShape& theHistory)
89 {
90   result(theShape, GeomAlgoAPI_MakeShapeList::Modified, theHistory);
91 }
92
93 bool GeomAlgoAPI_MakeShapeList::isDeleted(const std::shared_ptr<GeomAPI_Shape> theShape)
94 {
95   for(ListOfMakeShape::iterator aBuilderIt = myListOfMakeShape.begin();
96       aBuilderIt != myListOfMakeShape.end(); aBuilderIt++) {
97     std::shared_ptr<GeomAlgoAPI_MakeShape> aMakeShape = *aBuilderIt;
98     if(aMakeShape->isDeleted(theShape)) {
99       return true;
100     }
101   }
102
103   return false;
104 }
105
106 void GeomAlgoAPI_MakeShapeList::result(const std::shared_ptr<GeomAPI_Shape> theShape,
107                                        OperationType theOperationType,
108                                        ListOfShape& theHistory)
109 {
110   if(myListOfMakeShape.empty()) {
111     return;
112   }
113
114   NCollection_Map<TopoDS_Shape> anAlgoShapes;
115   NCollection_Map<TopoDS_Shape> aResultShapesMap;
116   NCollection_List<TopoDS_Shape> aResultShapesList;
117   anAlgoShapes.Add(theShape->impl<TopoDS_Shape>());
118   aResultShapesMap.Add(theShape->impl<TopoDS_Shape>());
119   aResultShapesList.Append(theShape->impl<TopoDS_Shape>());
120
121   for(ListOfMakeShape::iterator aBuilderIt = myListOfMakeShape.begin();
122       aBuilderIt != myListOfMakeShape.end(); aBuilderIt++) {
123     std::shared_ptr<GeomAlgoAPI_MakeShape> aMakeShape = *aBuilderIt;
124     NCollection_Map<TopoDS_Shape> aTempShapes;
125     bool hasResults = false;
126     for(NCollection_Map<TopoDS_Shape>::Iterator
127         aShapeIt(anAlgoShapes); aShapeIt.More(); aShapeIt.Next()) {
128       std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape);
129       aShape->setImpl(new TopoDS_Shape(aShapeIt.Value()));
130       ListOfShape aGeneratedShapes;
131       aMakeShape->generated(aShape, aGeneratedShapes);
132       for(ListOfShape::const_iterator
133           anIt = aGeneratedShapes.cbegin(); anIt != aGeneratedShapes.cend(); anIt++) {
134         const TopoDS_Shape& anItShape = (*anIt)->impl<TopoDS_Shape>();
135         aTempShapes.Add(anItShape);
136         if(aResultShapesMap.Add(anItShape) == Standard_True) {
137           aResultShapesList.Append(anItShape);
138         }
139         hasResults = true;
140       }
141       ListOfShape aModifiedShapes;
142       aMakeShape->modified(aShape, aModifiedShapes);
143       for(ListOfShape::const_iterator
144           anIt = aModifiedShapes.cbegin(); anIt != aModifiedShapes.cend(); anIt++) {
145         const TopoDS_Shape& anItShape = (*anIt)->impl<TopoDS_Shape>();
146         aTempShapes.Add(anItShape);
147         if(aResultShapesMap.Add(anItShape) == Standard_True) {
148           aResultShapesList.Append(anItShape);
149         }
150         hasResults = true;
151       }
152       if(hasResults) {
153         const TopoDS_Shape& aTopoDSShape = aShapeIt.Value();
154         if(aResultShapesMap.Remove(aTopoDSShape) == Standard_True) {
155           for(NCollection_List<TopoDS_Shape>::Iterator
156               aResIt(aResultShapesList); aResIt.More(); aResIt.Next()) {
157             if(aTopoDSShape.IsEqual(aResIt.Value())) {
158               aResultShapesList.Remove(aResIt);
159               break;
160             }
161           }
162         }
163       }
164     }
165     anAlgoShapes.Unite(aTempShapes);
166   }
167
168   for(NCollection_List<TopoDS_Shape>::Iterator
169       aShapeIt(aResultShapesList); aShapeIt.More(); aShapeIt.Next()) {
170     std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
171     aShape->setImpl(new TopoDS_Shape(aShapeIt.Value()));
172     theHistory.push_back(aShape);
173   }
174 }