Salome HOME
updated copyright message
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_MakeShapeList.cpp
1 // Copyright (C) 2014-2023  CEA, EDF
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, theNewShapes);
90 }
91
92 //==================================================================================================
93 void GeomAlgoAPI_MakeShapeList::modified(const GeomShapePtr theOldShape,
94                                          ListOfShape& theNewShapes)
95 {
96   result(theOldShape, 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                                        ListOfShape& theNewShapes)
118 {
119   if(myListOfMakeShape.empty()) {
120     return;
121   }
122
123   NCollection_Map<TopoDS_Shape> anAlgoShapes;
124   NCollection_Map<TopoDS_Shape> aResultShapesMap;
125   NCollection_List<TopoDS_Shape> aResultShapesList;
126   anAlgoShapes.Add(theOldShape->impl<TopoDS_Shape>());
127   aResultShapesMap.Add(theOldShape->impl<TopoDS_Shape>());
128   aResultShapesList.Append(theOldShape->impl<TopoDS_Shape>());
129
130   for(ListOfMakeShape::iterator aBuilderIt = myListOfMakeShape.begin();
131       aBuilderIt != myListOfMakeShape.end();
132       ++aBuilderIt)
133   {
134     GeomMakeShapePtr aMakeShape = *aBuilderIt;
135     NCollection_Map<TopoDS_Shape> aTempShapes;
136     for (NCollection_Map<TopoDS_Shape>::Iterator aShapeIt(anAlgoShapes);
137          aShapeIt.More();
138          aShapeIt.Next())
139     {
140       bool hasResults = false;
141       bool anArgumentIsInResult = false;
142       GeomShapePtr aShape(new GeomAPI_Shape);
143       aShape->setImpl(new TopoDS_Shape(aShapeIt.Value()));
144       ListOfShape aGeneratedShapes;
145       aMakeShape->generated(aShape, aGeneratedShapes);
146       for (ListOfShape::const_iterator anIt = aGeneratedShapes.cbegin();
147            anIt != aGeneratedShapes.cend();
148            ++anIt)
149       {
150         const TopoDS_Shape& anItShape = (*anIt)->impl<TopoDS_Shape>();
151         if (anItShape.IsSame(aShapeIt.Value())) {
152           anArgumentIsInResult = true;
153           continue;
154         }
155         aTempShapes.Add(anItShape);
156         if(aResultShapesMap.Add(anItShape) == Standard_True) {
157           aResultShapesList.Append(anItShape);
158         }
159         hasResults = true;
160       }
161       ListOfShape aModifiedShapes;
162       aMakeShape->modified(aShape, aModifiedShapes);
163       for (ListOfShape::const_iterator anIt = aModifiedShapes.cbegin();
164            anIt != aModifiedShapes.cend();
165            ++anIt)
166       {
167         const TopoDS_Shape& anItShape = (*anIt)->impl<TopoDS_Shape>();
168         if (anItShape.IsSame(aShapeIt.Value())) {
169           anArgumentIsInResult = true;
170           continue;
171         }
172         aTempShapes.Add(anItShape);
173         if(aResultShapesMap.Add(anItShape) == Standard_True) {
174           aResultShapesList.Append(anItShape);
175         }
176         hasResults = true;
177       }
178       if(hasResults && !anArgumentIsInResult) {
179         const TopoDS_Shape& aTopoDSShape = aShapeIt.Value();
180         if(aResultShapesMap.Remove(aTopoDSShape) == Standard_True) {
181           for(NCollection_List<TopoDS_Shape>::Iterator
182               aResIt(aResultShapesList); aResIt.More(); aResIt.Next()) {
183             if(aTopoDSShape.IsEqual(aResIt.Value())) {
184               aResultShapesList.Remove(aResIt);
185               break;
186             }
187           }
188         }
189       }
190     }
191     anAlgoShapes.Unite(aTempShapes);
192   }
193
194   for (NCollection_List<TopoDS_Shape>::Iterator aShapeIt(aResultShapesList);
195        aShapeIt.More();
196        aShapeIt.Next())
197   {
198     GeomShapePtr aShape(new GeomAPI_Shape());
199     aShape->setImpl(new TopoDS_Shape(aShapeIt.Value()));
200     if (!isValidForHistory(aShape)) continue;
201     fixOrientation(aShape);
202     theNewShapes.push_back(aShape);
203   }
204 }