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