Salome HOME
Copyright update 2020
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_MakeShapeList.cpp
1 // Copyright (C) 2014-2020  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "GeomAlgoAPI_MakeShapeList.h"
21
22
23 #include <NCollection_List.hxx>
24 #include <NCollection_Map.hxx>
25 #include <TopoDS_Shape.hxx>
26
27 //==================================================================================================
28 GeomAlgoAPI_MakeShapeList::GeomAlgoAPI_MakeShapeList()
29 : GeomAlgoAPI_MakeShape()
30 {}
31
32 //==================================================================================================
33 GeomAlgoAPI_MakeShapeList::GeomAlgoAPI_MakeShapeList(const ListOfMakeShape& theMakeShapeList)
34 : GeomAlgoAPI_MakeShape()
35 {
36   init(theMakeShapeList);
37 }
38
39 //==================================================================================================
40 void GeomAlgoAPI_MakeShapeList::init(const ListOfMakeShape& theMakeShapeList)
41 {
42   if(myMap.get()) {
43     myMap->clear();
44   } else {
45     myMap.reset(new GeomAPI_DataMapOfShapeShape);
46   }
47
48   myListOfMakeShape = theMakeShapeList;
49
50   for(ListOfMakeShape::const_iterator anIt = theMakeShapeList.cbegin();
51       anIt != theMakeShapeList.cend(); ++anIt) {
52     myMap->merge((*anIt)->mapOfSubShapes());
53   }
54 }
55
56 const ListOfMakeShape& GeomAlgoAPI_MakeShapeList::list() const
57 {
58   return myListOfMakeShape;
59 }
60
61
62 //==================================================================================================
63 void GeomAlgoAPI_MakeShapeList::appendAlgo(
64   const GeomMakeShapePtr theMakeShape)
65 {
66   myListOfMakeShape.push_back(theMakeShape);
67   if(!myMap.get()) {
68     myMap.reset(new GeomAPI_DataMapOfShapeShape());
69   }
70   myMap->merge(theMakeShape->mapOfSubShapes());
71 }
72
73 //==================================================================================================
74 const GeomShapePtr GeomAlgoAPI_MakeShapeList::shape() const
75 {
76   GeomShapePtr aShape = GeomAlgoAPI_MakeShape::shape();
77   if(aShape.get() && !aShape->impl<TopoDS_Shape>().IsNull()) {
78     return aShape;
79   } else if(!myListOfMakeShape.empty()) {
80     return myListOfMakeShape.back()->shape();
81   }
82   return GeomShapePtr();
83 }
84
85 //==================================================================================================
86 void GeomAlgoAPI_MakeShapeList::generated(const GeomShapePtr theOldShape,
87                                           ListOfShape& theNewShapes)
88 {
89   result(theOldShape,  GeomAlgoAPI_MakeShapeList::Generated, theNewShapes);
90 }
91
92 //==================================================================================================
93 void GeomAlgoAPI_MakeShapeList::modified(const GeomShapePtr theOldShape,
94                                          ListOfShape& theNewShapes)
95 {
96   result(theOldShape, GeomAlgoAPI_MakeShapeList::Modified, theNewShapes);
97 }
98
99 //==================================================================================================
100 bool GeomAlgoAPI_MakeShapeList::isDeleted(const GeomShapePtr theOldShape)
101 {
102   for (ListOfMakeShape::iterator aBuilderIt = myListOfMakeShape.begin();
103        aBuilderIt != myListOfMakeShape.end();
104        ++aBuilderIt)
105   {
106     GeomMakeShapePtr aMakeShape = *aBuilderIt;
107     if(aMakeShape->isDeleted(theOldShape)) {
108       return true;
109     }
110   }
111
112   return false;
113 }
114
115 //==================================================================================================
116 void GeomAlgoAPI_MakeShapeList::result(const GeomShapePtr theOldShape,
117                                        OperationType theOperationType,
118                                        ListOfShape& theNewShapes)
119 {
120   if(myListOfMakeShape.empty()) {
121     return;
122   }
123
124   NCollection_Map<TopoDS_Shape> anAlgoShapes;
125   NCollection_Map<TopoDS_Shape> aResultShapesMap;
126   NCollection_List<TopoDS_Shape> aResultShapesList;
127   anAlgoShapes.Add(theOldShape->impl<TopoDS_Shape>());
128   aResultShapesMap.Add(theOldShape->impl<TopoDS_Shape>());
129   aResultShapesList.Append(theOldShape->impl<TopoDS_Shape>());
130
131   for(ListOfMakeShape::iterator aBuilderIt = myListOfMakeShape.begin();
132       aBuilderIt != myListOfMakeShape.end();
133       ++aBuilderIt)
134   {
135     GeomMakeShapePtr aMakeShape = *aBuilderIt;
136     NCollection_Map<TopoDS_Shape> aTempShapes;
137     for (NCollection_Map<TopoDS_Shape>::Iterator aShapeIt(anAlgoShapes);
138          aShapeIt.More();
139          aShapeIt.Next())
140     {
141       bool hasResults = false;
142       bool anArgumentIsInResult = false;
143       GeomShapePtr aShape(new GeomAPI_Shape);
144       aShape->setImpl(new TopoDS_Shape(aShapeIt.Value()));
145       ListOfShape aGeneratedShapes;
146       aMakeShape->generated(aShape, aGeneratedShapes);
147       for (ListOfShape::const_iterator anIt = aGeneratedShapes.cbegin();
148            anIt != aGeneratedShapes.cend();
149            ++anIt)
150       {
151         const TopoDS_Shape& anItShape = (*anIt)->impl<TopoDS_Shape>();
152         if (anItShape.IsSame(aShapeIt.Value())) {
153           anArgumentIsInResult = true;
154           continue;
155         }
156         aTempShapes.Add(anItShape);
157         if(aResultShapesMap.Add(anItShape) == Standard_True) {
158           aResultShapesList.Append(anItShape);
159         }
160         hasResults = true;
161       }
162       ListOfShape aModifiedShapes;
163       aMakeShape->modified(aShape, aModifiedShapes);
164       for (ListOfShape::const_iterator anIt = aModifiedShapes.cbegin();
165            anIt != aModifiedShapes.cend();
166            ++anIt)
167       {
168         const TopoDS_Shape& anItShape = (*anIt)->impl<TopoDS_Shape>();
169         if (anItShape.IsSame(aShapeIt.Value())) {
170           anArgumentIsInResult = true;
171           continue;
172         }
173         aTempShapes.Add(anItShape);
174         if(aResultShapesMap.Add(anItShape) == Standard_True) {
175           aResultShapesList.Append(anItShape);
176         }
177         hasResults = true;
178       }
179       if(hasResults && !anArgumentIsInResult) {
180         const TopoDS_Shape& aTopoDSShape = aShapeIt.Value();
181         if(aResultShapesMap.Remove(aTopoDSShape) == Standard_True) {
182           for(NCollection_List<TopoDS_Shape>::Iterator
183               aResIt(aResultShapesList); aResIt.More(); aResIt.Next()) {
184             if(aTopoDSShape.IsEqual(aResIt.Value())) {
185               aResultShapesList.Remove(aResIt);
186               break;
187             }
188           }
189         }
190       }
191     }
192     anAlgoShapes.Unite(aTempShapes);
193   }
194
195   for (NCollection_List<TopoDS_Shape>::Iterator aShapeIt(aResultShapesList);
196        aShapeIt.More();
197        aShapeIt.Next())
198   {
199     GeomShapePtr aShape(new GeomAPI_Shape());
200     aShape->setImpl(new TopoDS_Shape(aShapeIt.Value()));
201     if (!isValidForHistory(aShape)) continue;
202     fixOrientation(aShape);
203     theNewShapes.push_back(aShape);
204   }
205 }