Salome HOME
Bug #1341: selected point on circle
[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   myListOfMakeShape = theMakeShapeList;
28 }
29
30 //=================================================================================================
31 void GeomAlgoAPI_MakeShapeList::appendAlgo(const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape)
32 {
33   myListOfMakeShape.push_back(theMakeShape);
34 }
35
36 //=================================================================================================
37 const std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_MakeShapeList::shape() const
38 {
39   std::shared_ptr<GeomAPI_Shape> aShape = GeomAlgoAPI_MakeShape::shape();
40   if(aShape.get() && !aShape->impl<TopoDS_Shape>().IsNull()) {
41     return aShape;
42   } else if(!myListOfMakeShape.empty()) {
43     return myListOfMakeShape.back()->shape();
44   }
45   return std::shared_ptr<GeomAPI_Shape>();
46 }
47
48 //=================================================================================================
49 void GeomAlgoAPI_MakeShapeList::generated(const std::shared_ptr<GeomAPI_Shape> theShape,
50                                           ListOfShape& theHistory)
51 {
52   result(theShape,  GeomAlgoAPI_MakeShapeList::Generated, theHistory);
53 }
54
55 //=================================================================================================
56 void GeomAlgoAPI_MakeShapeList::modified(const std::shared_ptr<GeomAPI_Shape> theShape,
57                                          ListOfShape& theHistory)
58 {
59   result(theShape, GeomAlgoAPI_MakeShapeList::Modified, theHistory);
60 }
61
62 bool GeomAlgoAPI_MakeShapeList::isDeleted(const std::shared_ptr<GeomAPI_Shape> theShape)
63 {
64   for(ListOfMakeShape::iterator aBuilderIt = myListOfMakeShape.begin(); aBuilderIt != myListOfMakeShape.end(); aBuilderIt++) {
65     std::shared_ptr<GeomAlgoAPI_MakeShape> aMakeShape = *aBuilderIt;
66     if(aMakeShape->isDeleted(theShape)) {
67       return true;
68     }
69   }
70
71   return false;
72 }
73
74 void GeomAlgoAPI_MakeShapeList::result(const std::shared_ptr<GeomAPI_Shape> theShape,
75                                        OperationType theOperationType,
76                                        ListOfShape& theHistory)
77 {
78   if(myListOfMakeShape.empty()) {
79     return;
80   }
81
82   NCollection_Map<TopoDS_Shape> anAlgoShapes;
83   NCollection_Map<TopoDS_Shape> aResultShapes;
84   anAlgoShapes.Add(theShape->impl<TopoDS_Shape>());
85   aResultShapes.Add(theShape->impl<TopoDS_Shape>());
86
87   for(ListOfMakeShape::iterator aBuilderIt = myListOfMakeShape.begin(); aBuilderIt != myListOfMakeShape.end(); aBuilderIt++) {
88     std::shared_ptr<GeomAlgoAPI_MakeShape> aMakeShape = *aBuilderIt;
89     NCollection_Map<TopoDS_Shape> aTempShapes;
90     bool hasResults = false;
91     for(NCollection_Map<TopoDS_Shape>::Iterator aShapeIt(anAlgoShapes); aShapeIt.More(); aShapeIt.Next()) {
92       std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape);
93       aShape->setImpl(new TopoDS_Shape(aShapeIt.Value()));
94       ListOfShape aGeneratedShapes;
95       aMakeShape->generated(aShape, aGeneratedShapes);
96       for(ListOfShape::const_iterator anIt = aGeneratedShapes.cbegin(); anIt != aGeneratedShapes.cend(); anIt++) {
97         aTempShapes.Add((*anIt)->impl<TopoDS_Shape>());
98         aResultShapes.Add((*anIt)->impl<TopoDS_Shape>());
99         hasResults = true;
100       }
101       ListOfShape aModifiedShapes;
102       aMakeShape->modified(aShape, aModifiedShapes);
103       for(ListOfShape::const_iterator anIt = aModifiedShapes.cbegin(); anIt != aModifiedShapes.cend(); anIt++) {
104         aTempShapes.Add((*anIt)->impl<TopoDS_Shape>());
105         aResultShapes.Add((*anIt)->impl<TopoDS_Shape>());
106         hasResults = true;
107       }
108       if(hasResults) {
109         aResultShapes.Remove(aShape->impl<TopoDS_Shape>());
110       }
111     }
112     anAlgoShapes.Unite(aTempShapes);
113   }
114
115   for(NCollection_Map<TopoDS_Shape>::Iterator aShapeIt(aResultShapes); aShapeIt.More(); aShapeIt.Next()) {
116     std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
117     aShape->setImpl(new TopoDS_Shape(aShapeIt.Value()));
118     theHistory.push_back(aShape);
119   }
120 }