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