Salome HOME
updated copyright message
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_MakeShape.cpp
1 // Copyright (C) 2014-2023  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_MakeShape.h"
21
22 #include <BOPAlgo_Builder.hxx>
23 #include <BRep_Tool.hxx>
24 #include <BRepBuilderAPI_MakeShape.hxx>
25 #include <BRepCheck_Analyzer.hxx>
26 #include <BRepGProp.hxx>
27 #include <GProp_GProps.hxx>
28 #include <Precision.hxx>
29 #include <TopExp_Explorer.hxx>
30 #include <TopTools_ListOfShape.hxx>
31 #include <TopTools_ListIteratorOfListOfShape.hxx>
32 #include <GeomAPI_ShapeExplorer.h>
33 #include <GeomAPI_ShapeIterator.h>
34 #include <TopoDS.hxx>
35 #include <TopoDS_Builder.hxx>
36 #include <TopoDS_Edge.hxx>
37
38 // new shape -> old shapes -> index in the old shape
39 typedef NCollection_DataMap<TopoDS_Shape,
40   NCollection_DataMap<TopoDS_Shape, int, TopTools_ShapeMapHasher>, TopTools_ShapeMapHasher>
41   MapNewToOld;
42 typedef
43   NCollection_DataMap<int, NCollection_DataMap<TopoDS_Shape, MapNewToOld, TopTools_ShapeMapHasher> >
44   HistoryMap;
45
46 //==================================================================================================
47 GeomAlgoAPI_MakeShape::GeomAlgoAPI_MakeShape()
48 : myBuilderType(Unknown),
49   myDone(false)
50 {
51   myHist = 0;
52 }
53
54 //==================================================================================================
55 GeomAlgoAPI_MakeShape::~GeomAlgoAPI_MakeShape()
56 {
57   if (myHist) {
58     delete (HistoryMap*)myHist;
59   }
60 }
61
62 //==================================================================================================
63 bool GeomAlgoAPI_MakeShape::isDone() const
64 {
65   return myDone;
66 }
67
68 //==================================================================================================
69 const GeomShapePtr GeomAlgoAPI_MakeShape::shape() const
70 {
71   return myShape;
72 }
73
74 //==================================================================================================
75 bool GeomAlgoAPI_MakeShape::isValid() const
76 {
77   BRepCheck_Analyzer aChecker(myShape->impl<TopoDS_Shape>());
78   return (aChecker.IsValid() == Standard_True);
79 }
80
81 //==================================================================================================
82 std::shared_ptr<GeomAPI_DataMapOfShapeShape> GeomAlgoAPI_MakeShape::mapOfSubShapes() const
83 {
84   return myMap;
85 }
86
87 //==================================================================================================
88 void GeomAlgoAPI_MakeShape::generated(const GeomShapePtr theOldShape,
89                                       ListOfShape& theNewShapes)
90 {
91   TopTools_ListOfShape aList;
92   if(myBuilderType == OCCT_BRepBuilderAPI_MakeShape) {
93     BRepBuilderAPI_MakeShape* aMakeShape = implPtr<BRepBuilderAPI_MakeShape>();
94     aList = aMakeShape->Generated(theOldShape->impl<TopoDS_Shape>());
95   } else if(myBuilderType == OCCT_BOPAlgo_Builder) {
96     BOPAlgo_Builder* aBOPBuilder = implPtr<BOPAlgo_Builder>();
97     aList = aBOPBuilder->Generated(theOldShape->impl<TopoDS_Shape>());
98   }
99   for(TopTools_ListIteratorOfListOfShape anIt(aList); anIt.More(); anIt.Next()) {
100     GeomShapePtr aShape(new GeomAPI_Shape());
101     aShape->setImpl(new TopoDS_Shape(anIt.Value()));
102     if (!isValidForHistory(aShape)) continue;
103     fixOrientation(aShape);
104     theNewShapes.push_back(aShape);
105   }
106 }
107
108 //==================================================================================================
109 void GeomAlgoAPI_MakeShape::modified(const GeomShapePtr theOldShape,
110                                      ListOfShape& theNewShapes)
111 {
112   TopTools_ListOfShape aList;
113   if(myBuilderType == OCCT_BRepBuilderAPI_MakeShape) {
114     BRepBuilderAPI_MakeShape* aMakeShape = implPtr<BRepBuilderAPI_MakeShape>();
115     try {
116       aList = aMakeShape->Modified(theOldShape->impl<TopoDS_Shape>());
117     } catch(Standard_NoSuchObject) {
118     }
119   } else if(myBuilderType == OCCT_BOPAlgo_Builder) {
120     BOPAlgo_Builder* aBOPBuilder = implPtr<BOPAlgo_Builder>();
121     aList = aBOPBuilder->Modified(theOldShape->impl<TopoDS_Shape>());
122   }
123   for(TopTools_ListIteratorOfListOfShape anIt(aList); anIt.More(); anIt.Next()) {
124     GeomShapePtr aShape(new GeomAPI_Shape());
125     aShape->setImpl(new TopoDS_Shape(anIt.Value()));
126     if (!isValidForHistory(aShape)) continue;
127     fixOrientation(aShape);
128     theNewShapes.push_back(aShape);
129   }
130 }
131
132 //==================================================================================================
133 bool GeomAlgoAPI_MakeShape::isDeleted(const GeomShapePtr theOldShape)
134 {
135   bool isDeleted = false;
136   if(myBuilderType == OCCT_BRepBuilderAPI_MakeShape) {
137     BRepBuilderAPI_MakeShape* aMakeShape = implPtr<BRepBuilderAPI_MakeShape>();
138     isDeleted = aMakeShape->IsDeleted(theOldShape->impl<TopoDS_Shape>()) == Standard_True;
139   } else if(myBuilderType == OCCT_BOPAlgo_Builder) {
140     BOPAlgo_Builder* aBOPBuilder = implPtr<BOPAlgo_Builder>();
141     isDeleted = aBOPBuilder->IsDeleted(theOldShape->impl<TopoDS_Shape>()) == Standard_True;
142   }
143
144   return isDeleted;
145 }
146
147 //==================================================================================================
148 void GeomAlgoAPI_MakeShape::setBuilderType(const BuilderType theBuilderType)
149 {
150   myBuilderType = theBuilderType;
151 }
152
153 //==================================================================================================
154 void GeomAlgoAPI_MakeShape::setDone(const bool theFlag)
155 {
156   myDone = theFlag;
157 }
158
159 //==================================================================================================
160 void GeomAlgoAPI_MakeShape::setShape(const GeomShapePtr theShape)
161 {
162   if(myShape.get() && myShape->isEqual(theShape)) {
163     return;
164   }
165
166   myShape = theShape;
167
168   // Filling data map to keep correct orientation of sub-shapes.
169   if(myShape.get()) {
170     if(myMap.get()) {
171       myMap->clear();
172     } else {
173       myMap.reset(new GeomAPI_DataMapOfShapeShape);
174     }
175
176     const TopoDS_Shape& aTopoDSShape = myShape->impl<TopoDS_Shape>();
177     for(TopExp_Explorer anExp(aTopoDSShape,TopAbs_VERTEX); anExp.More(); anExp.Next()) {
178       GeomShapePtr aCurrentShape(new GeomAPI_Shape());
179       aCurrentShape->setImpl(new TopoDS_Shape(anExp.Current()));
180       myMap->bind(aCurrentShape, aCurrentShape);
181     }
182     for(TopExp_Explorer anExp(aTopoDSShape,TopAbs_EDGE); anExp.More(); anExp.Next()) {
183       GeomShapePtr aCurrentShape(new GeomAPI_Shape());
184       aCurrentShape->setImpl(new TopoDS_Shape(anExp.Current()));
185       myMap->bind(aCurrentShape, aCurrentShape);
186     }
187     for(TopExp_Explorer anExp(aTopoDSShape,TopAbs_FACE); anExp.More(); anExp.Next()) {
188       GeomShapePtr aCurrentShape(new GeomAPI_Shape());
189       aCurrentShape->setImpl(new TopoDS_Shape(anExp.Current()));
190       myMap->bind(aCurrentShape, aCurrentShape);
191     }
192   } else {
193     if(myMap.get()) {
194       myMap->clear();
195     }
196   }
197 }
198
199 //==================================================================================================
200 bool GeomAlgoAPI_MakeShape::isValidForHistory(const GeomShapePtr theShape)
201 {
202   if (!theShape.get()) return false;
203
204   const TopoDS_Shape& aShape_ = theShape->impl<TopoDS_Shape>();
205   if (aShape_.IsNull()) return false;
206
207   if (aShape_.ShapeType() == TopAbs_EDGE) {
208     TopoDS_Edge anEdge_ = TopoDS::Edge(aShape_);
209     if (BRep_Tool::Degenerated(anEdge_)) return false;
210   }
211
212   return true;
213 }
214
215 //==================================================================================================
216 void GeomAlgoAPI_MakeShape::fixOrientation(GeomShapePtr& theShape) {
217   if (myMap->isBound(theShape)) theShape = myMap->find(theShape);
218 }
219
220 //==================================================================================================
221 void GeomAlgoAPI_MakeShape::initialize()
222 {
223   switch (myBuilderType) {
224     case OCCT_BRepBuilderAPI_MakeShape: {
225       myDone = implPtr<BRepBuilderAPI_MakeShape>()->IsDone() == Standard_True;
226       myShape.reset(new GeomAPI_Shape());
227       myShape->setImpl(new TopoDS_Shape(implPtr<BRepBuilderAPI_MakeShape>()->Shape()));
228       break;
229     }
230     case OCCT_BOPAlgo_Builder: {
231       myDone = true;
232       myShape.reset(new GeomAPI_Shape());
233       myShape->setImpl(new TopoDS_Shape(implPtr<BOPAlgo_Builder>()->Shape()));
234       break;
235     }
236     default:
237       break;
238   }
239
240   if(myMap.get()) {
241     myMap->clear();
242   } else {
243     myMap.reset(new GeomAPI_DataMapOfShapeShape);
244   }
245
246   const TopoDS_Shape& aTopoDSShape = myShape->impl<TopoDS_Shape>();
247   for(TopExp_Explorer anExp(aTopoDSShape,TopAbs_FACE); anExp.More(); anExp.Next()) {
248     GeomShapePtr aCurrentShape(new GeomAPI_Shape());
249     aCurrentShape->setImpl(new TopoDS_Shape(anExp.Current()));
250     myMap->bind(aCurrentShape, aCurrentShape);
251   }
252   myHist = 0;
253 }
254
255
256 //==================================================================================================
257 void GeomAlgoAPI_MakeShape::prepareNamingFaces()
258 {
259   long long index = 1;
260   for(GeomAPI_ShapeExplorer anExp(shape(), GeomAPI_Shape::FACE); anExp.more(); anExp.next()) {
261     GeomShapePtr aFace = anExp.current();
262     myCreatedFaces["Face_" + std::to_string(index++)] = aFace;
263   }
264 }
265
266
267 //==================================================================================================
268 bool GeomAlgoAPI_MakeShape::checkValid(std::string theMessage)
269 {
270   // isValid() is called from this method
271   if (!isValid()) {
272     myError = theMessage + " :: resulting shape is not valid.";
273     return false;
274   }
275
276   // Check the number of volumes in myShape, make sure there's one and only one.
277   TopoDS_Shape aTopoDSShape = myShape->impl<TopoDS_Shape>();
278   int aNbVolumes = 0;
279   for(TopExp_Explorer anExp(aTopoDSShape,TopAbs_SOLID); anExp.More(); anExp.Next()) {
280     aNbVolumes ++;
281   }
282
283   if (aNbVolumes != 1) {
284     myError = theMessage +
285       " :: connexity error, the resulting shape is made of several separate solids.";
286     return false;
287   }
288
289   return true ;
290 }
291
292 //==================================================================================================
293 bool GeomAlgoAPI_MakeShape::isNewShapesCollected(GeomShapePtr theWholeOld,
294                                                  const int theShapeType)
295 {
296   if (!myHist)
297     return false;
298   HistoryMap* aHist = (HistoryMap*)myHist;
299   if (!aHist->IsBound(theShapeType))
300     return false;
301   return aHist->Find(theShapeType).IsBound(theWholeOld->impl<TopoDS_Shape>());
302 }
303
304 void GeomAlgoAPI_MakeShape::collectNewShapes(
305   GeomShapePtr theWholeOld, const int theShapeType)
306 {
307   if (!myHist)
308     myHist = new HistoryMap;
309   HistoryMap* aHist = (HistoryMap*)myHist;
310   if (!aHist->IsBound(theShapeType))
311     aHist->Bind(
312       theShapeType, NCollection_DataMap<TopoDS_Shape, MapNewToOld, TopTools_ShapeMapHasher>());
313   aHist->ChangeFind(theShapeType). // add a new in anyway
314     Bind(theWholeOld->impl<TopoDS_Shape>(), MapNewToOld());
315   MapNewToOld& aCurrent =
316     aHist->ChangeFind(theShapeType).ChangeFind(theWholeOld->impl<TopoDS_Shape>());
317   ListOfShape aNewList;
318   TopTools_MapOfShape aViewed; //avoid same shapes
319   GeomAPI_ShapeExplorer anExp(theWholeOld, GeomAPI_Shape::ShapeType(theShapeType));
320   for(int anIndexInWholeOld = 0; anExp.more(); anExp.next(), anIndexInWholeOld++) {
321     if (!aViewed.Add(anExp.current()->impl<TopoDS_Shape>()))
322       continue;
323     aNewList.clear();
324     modified(anExp.current(), aNewList);
325     for(ListOfShape::iterator aNew = aNewList.begin(); aNew != aNewList.end(); aNew++) {
326       const TopoDS_Shape& aNewShape = (*aNew)->impl<TopoDS_Shape>();
327       if (!aCurrent.IsBound(aNewShape)) {
328         aCurrent.Bind(
329           aNewShape, NCollection_DataMap<TopoDS_Shape, int, TopTools_ShapeMapHasher>());
330       }
331       aCurrent.ChangeFind(aNewShape).Bind(anExp.current()->impl<TopoDS_Shape>(), anIndexInWholeOld);
332     }
333   }
334 }
335
336 static void addAllSubs(const TopoDS_Shape& theNewShape,
337   MapNewToOld& theCurrent, std::map<int, TopoDS_Shape>& theResMap)
338 {
339   if (theCurrent.IsBound(theNewShape)) {
340     NCollection_DataMap<TopoDS_Shape, int, TopTools_ShapeMapHasher>::Iterator
341       anOldIter(theCurrent.Find(theNewShape));
342     for(; anOldIter.More(); anOldIter.Next()) {
343       theResMap[anOldIter.Value()] = anOldIter.Key();
344     }
345   }
346
347   TopoDS_Iterator anIter(theNewShape);
348   for(; anIter.More(); anIter.Next()) {
349     //if (anIter.Value().ShapeType() != TopAbs_VERTEX)
350     addAllSubs(anIter.Value(), theCurrent, theResMap); // add recursively
351   }
352 }
353
354 //==================================================================================================
355 GeomShapePtr GeomAlgoAPI_MakeShape::oldShapesForNew(GeomShapePtr theWholeOld,
356                                                     GeomShapePtr theNewShape,
357                                                     const int theShapeType)
358 {
359   GeomShapePtr aResult(new GeomAPI_Shape);
360   TopoDS_Compound aResComp;
361   TopoDS_Builder aBuilder;
362   aBuilder.MakeCompound(aResComp);
363   aResult->setImpl<TopoDS_Shape>(new TopoDS_Shape(aResComp));
364
365   HistoryMap* aHist = (HistoryMap*)myHist;
366   if (!aHist->IsBound(theShapeType))
367     return aResult; // not found, empty compound
368   const TopoDS_Shape& aWholeOld = theWholeOld->impl<TopoDS_Shape>();
369   if (!aHist->Find(theShapeType).IsBound(aWholeOld))
370     return aResult; // not found, empty compound
371   std::map<int, TopoDS_Shape> aResMap; // map with all results, ordered by index in whole old
372   MapNewToOld& aCurrent = aHist->ChangeFind(theShapeType).ChangeFind(aWholeOld);
373   // we don't know what type of new shapes were produced by the old theShapeType, so, iterate all
374   addAllSubs(theNewShape->impl<TopoDS_Shape>(), aCurrent, aResMap);
375
376   std::map<int, TopoDS_Shape>::iterator anOldIter = aResMap.begin();
377   for(; anOldIter != aResMap.end(); anOldIter++) {
378     if (anOldIter->second.ShapeType() == (TopAbs_ShapeEnum)theShapeType)
379       aBuilder.Add(aResComp, anOldIter->second);
380   }
381   aResult->setImpl<TopoDS_Shape>(new TopoDS_Shape(aResComp));
382   return aResult;
383 }