Salome HOME
Architecture changes
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_MakeShapeList.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomAlgoAPI_MakeShapeListList.cpp
4 // Created:     27 May 2015
5 // Author:      Dmitry Bobylev
6
7 #include <GeomAlgoAPI_MakeShapeList.h>
8
9 #include <BRepBuilderAPI_MakeShape.hxx>
10 #include <NCollection_Map.hxx>
11 #include <TopTools_ListOfShape.hxx>
12 #include <TopTools_ListIteratorOfListOfShape.hxx>
13
14 //=================================================================================================
15 GeomAlgoAPI_MakeShapeList::GeomAlgoAPI_MakeShapeList()
16 : GeomAlgoAPI_MakeShape()
17 {}
18
19 //=================================================================================================
20 GeomAlgoAPI_MakeShapeList::GeomAlgoAPI_MakeShapeList(const ListOfMakeShape& theMakeShapeList)
21 : GeomAlgoAPI_MakeShape()
22 {
23   init(theMakeShapeList);
24 }
25
26 //=================================================================================================
27 void GeomAlgoAPI_MakeShapeList::init(const ListOfMakeShape& theMakeShapeList)
28 {
29   myListOfMakeShape = theMakeShapeList;
30 }
31
32 //=================================================================================================
33 void GeomAlgoAPI_MakeShapeList::append(const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape)
34 {
35   myListOfMakeShape.push_back(theMakeShape);
36 }
37
38 //=================================================================================================
39 void GeomAlgoAPI_MakeShapeList::append(const GeomAlgoAPI_MakeShapeList& theMakeShapeList)
40 {
41   for(ListOfMakeShape::const_iterator anIt = theMakeShapeList.myListOfMakeShape.cbegin();
42     anIt != theMakeShapeList.myListOfMakeShape.cend(); anIt++) {
43     myListOfMakeShape.push_back(*anIt);
44   }
45 }
46
47 //=================================================================================================
48 const std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_MakeShapeList::shape() const
49 {
50   if(myListOfMakeShape.empty()) {
51     return std::shared_ptr<GeomAPI_Shape>();
52   } else {
53     return myListOfMakeShape.back()->shape();
54   }
55 }
56
57 //=================================================================================================
58 void GeomAlgoAPI_MakeShapeList::generated(const std::shared_ptr<GeomAPI_Shape> theShape,
59                                           ListOfShape& theHistory)
60 {
61   result(theShape,  GeomAlgoAPI_MakeShapeList::Generated, theHistory);
62 }
63
64 //=================================================================================================
65 void GeomAlgoAPI_MakeShapeList::modified(const std::shared_ptr<GeomAPI_Shape> theShape,
66                                          ListOfShape& theHistory)
67 {
68   result(theShape, GeomAlgoAPI_MakeShapeList::Modified, theHistory);
69 }
70
71 bool GeomAlgoAPI_MakeShapeList::isDeleted(const std::shared_ptr<GeomAPI_Shape> theShape)
72 {
73   for(ListOfMakeShape::iterator aBuilderIt = myListOfMakeShape.begin(); aBuilderIt != myListOfMakeShape.end(); aBuilderIt++) {
74     std::shared_ptr<GeomAlgoAPI_MakeShape> aMakeShape = *aBuilderIt;
75     if(aMakeShape->isDeleted(theShape)) {
76       return true;
77     }
78   }
79
80   return false;
81 }
82
83 void GeomAlgoAPI_MakeShapeList::result(const std::shared_ptr<GeomAPI_Shape> theShape,
84                                        OperationType theOperationType,
85                                        ListOfShape& theHistory)
86 {
87   if(myListOfMakeShape.empty()) {
88     return;
89   }
90
91   NCollection_Map<TopoDS_Shape> anAlgoShapes;
92   NCollection_Map<TopoDS_Shape> aResultShapes;
93   anAlgoShapes.Add(theShape->impl<TopoDS_Shape>());
94   aResultShapes.Add(theShape->impl<TopoDS_Shape>());
95
96   for(ListOfMakeShape::iterator aBuilderIt = myListOfMakeShape.begin(); aBuilderIt != myListOfMakeShape.end(); aBuilderIt++) {
97     std::shared_ptr<GeomAlgoAPI_MakeShape> aMakeShape = *aBuilderIt;
98     NCollection_Map<TopoDS_Shape> aTempShapes;
99     bool hasResults = false;
100     for(NCollection_Map<TopoDS_Shape>::Iterator aShapeIt(anAlgoShapes); aShapeIt.More(); aShapeIt.Next()) {
101       std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape);
102       aShape->setImpl(new TopoDS_Shape(aShapeIt.Value()));
103       ListOfShape aGeneratedShapes;
104       aMakeShape->generated(aShape, aGeneratedShapes);
105       for(ListOfShape::const_iterator anIt = aGeneratedShapes.cbegin(); anIt != aGeneratedShapes.cend(); anIt++) {
106         aTempShapes.Add((*anIt)->impl<TopoDS_Shape>());
107         aResultShapes.Add((*anIt)->impl<TopoDS_Shape>());
108         hasResults = true;
109       }
110       ListOfShape aModifiedShapes;
111       aMakeShape->modified(aShape, aModifiedShapes);
112       for(ListOfShape::const_iterator anIt = aModifiedShapes.cbegin(); anIt != aModifiedShapes.cend(); anIt++) {
113         aTempShapes.Add((*anIt)->impl<TopoDS_Shape>());
114         aResultShapes.Add((*anIt)->impl<TopoDS_Shape>());
115         hasResults = true;
116       }
117       if(hasResults) {
118         aResultShapes.Remove(aShape->impl<TopoDS_Shape>());
119       }
120     }
121     anAlgoShapes.Unite(aTempShapes);
122   }
123
124   for(NCollection_Map<TopoDS_Shape>::Iterator aShapeIt(aResultShapes); aShapeIt.More(); aShapeIt.Next()) {
125     std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
126     aShape->setImpl(new TopoDS_Shape(aShapeIt.Value()));
127     theHistory.push_back(aShape);
128   }
129 }