Salome HOME
Issue #1343: Architecture changes. Composite features now derived from extrusion...
[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 <NCollection_Map.hxx>
10 #include <TopoDS_Shape.hxx>
11
12 //=================================================================================================
13 GeomAlgoAPI_MakeShapeList::GeomAlgoAPI_MakeShapeList()
14 : GeomAlgoAPI_MakeShape()
15 {}
16
17 //=================================================================================================
18 GeomAlgoAPI_MakeShapeList::GeomAlgoAPI_MakeShapeList(const ListOfMakeShape& theMakeShapeList)
19 : GeomAlgoAPI_MakeShape()
20 {
21   init(theMakeShapeList);
22 }
23
24 //=================================================================================================
25 void GeomAlgoAPI_MakeShapeList::init(const ListOfMakeShape& theMakeShapeList)
26 {
27   if(myMap.get()) {
28     myMap->clear();
29   } else {
30     myMap.reset(new GeomAPI_DataMapOfShapeShape);
31   }
32
33   myListOfMakeShape = theMakeShapeList;
34
35   for(ListOfMakeShape::const_iterator anIt = theMakeShapeList.cbegin();
36       anIt != theMakeShapeList.cend(); ++anIt) {
37     myMap->merge((*anIt)->mapOfSubShapes());
38   }
39 }
40
41 //=================================================================================================
42 void GeomAlgoAPI_MakeShapeList::appendAlgo(const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape)
43 {
44   myListOfMakeShape.push_back(theMakeShape);
45   if(!myMap.get()) {
46     myMap.reset(new GeomAPI_DataMapOfShapeShape());
47   }
48   myMap->merge(theMakeShape->mapOfSubShapes());
49 }
50
51 //=================================================================================================
52 const std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_MakeShapeList::shape() const
53 {
54   std::shared_ptr<GeomAPI_Shape> aShape = GeomAlgoAPI_MakeShape::shape();
55   if(aShape.get() && !aShape->impl<TopoDS_Shape>().IsNull()) {
56     return aShape;
57   } else if(!myListOfMakeShape.empty()) {
58     return myListOfMakeShape.back()->shape();
59   }
60   return std::shared_ptr<GeomAPI_Shape>();
61 }
62
63 //=================================================================================================
64 void GeomAlgoAPI_MakeShapeList::generated(const std::shared_ptr<GeomAPI_Shape> theShape,
65                                           ListOfShape& theHistory)
66 {
67   result(theShape,  GeomAlgoAPI_MakeShapeList::Generated, theHistory);
68 }
69
70 //=================================================================================================
71 void GeomAlgoAPI_MakeShapeList::modified(const std::shared_ptr<GeomAPI_Shape> theShape,
72                                          ListOfShape& theHistory)
73 {
74   result(theShape, GeomAlgoAPI_MakeShapeList::Modified, theHistory);
75 }
76
77 bool GeomAlgoAPI_MakeShapeList::isDeleted(const std::shared_ptr<GeomAPI_Shape> theShape)
78 {
79   for(ListOfMakeShape::iterator aBuilderIt = myListOfMakeShape.begin(); aBuilderIt != myListOfMakeShape.end(); aBuilderIt++) {
80     std::shared_ptr<GeomAlgoAPI_MakeShape> aMakeShape = *aBuilderIt;
81     if(aMakeShape->isDeleted(theShape)) {
82       return true;
83     }
84   }
85
86   return false;
87 }
88
89 void GeomAlgoAPI_MakeShapeList::result(const std::shared_ptr<GeomAPI_Shape> theShape,
90                                        OperationType theOperationType,
91                                        ListOfShape& theHistory)
92 {
93   if(myListOfMakeShape.empty()) {
94     return;
95   }
96
97   NCollection_Map<TopoDS_Shape> anAlgoShapes;
98   NCollection_Map<TopoDS_Shape> aResultShapes;
99   anAlgoShapes.Add(theShape->impl<TopoDS_Shape>());
100   aResultShapes.Add(theShape->impl<TopoDS_Shape>());
101
102   for(ListOfMakeShape::iterator aBuilderIt = myListOfMakeShape.begin(); aBuilderIt != myListOfMakeShape.end(); aBuilderIt++) {
103     std::shared_ptr<GeomAlgoAPI_MakeShape> aMakeShape = *aBuilderIt;
104     NCollection_Map<TopoDS_Shape> aTempShapes;
105     bool hasResults = false;
106     for(NCollection_Map<TopoDS_Shape>::Iterator aShapeIt(anAlgoShapes); aShapeIt.More(); aShapeIt.Next()) {
107       std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape);
108       aShape->setImpl(new TopoDS_Shape(aShapeIt.Value()));
109       ListOfShape aGeneratedShapes;
110       aMakeShape->generated(aShape, aGeneratedShapes);
111       for(ListOfShape::const_iterator anIt = aGeneratedShapes.cbegin(); anIt != aGeneratedShapes.cend(); anIt++) {
112         aTempShapes.Add((*anIt)->impl<TopoDS_Shape>());
113         aResultShapes.Add((*anIt)->impl<TopoDS_Shape>());
114         hasResults = true;
115       }
116       ListOfShape aModifiedShapes;
117       aMakeShape->modified(aShape, aModifiedShapes);
118       for(ListOfShape::const_iterator anIt = aModifiedShapes.cbegin(); anIt != aModifiedShapes.cend(); anIt++) {
119         aTempShapes.Add((*anIt)->impl<TopoDS_Shape>());
120         aResultShapes.Add((*anIt)->impl<TopoDS_Shape>());
121         hasResults = true;
122       }
123       if(hasResults) {
124         aResultShapes.Remove(aShape->impl<TopoDS_Shape>());
125       }
126     }
127     anAlgoShapes.Unite(aTempShapes);
128   }
129
130   for(NCollection_Map<TopoDS_Shape>::Iterator aShapeIt(aResultShapes); aShapeIt.More(); aShapeIt.Next()) {
131     std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
132     aShape->setImpl(new TopoDS_Shape(aShapeIt.Value()));
133     theHistory.push_back(aShape);
134   }
135 }