Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Revolution.cpp
1 // Copyright (C) 2014-2017  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "GeomAlgoAPI_Revolution.h"
22
23 #include <GeomAPI_Face.h>
24 #include <GeomAPI_Pln.h>
25 #include <GeomAPI_ShapeExplorer.h>
26 #include <GeomAlgoAPI_DFLoader.h>
27 #include <GeomAlgoAPI_FaceBuilder.h>
28 #include <GeomAlgoAPI_MakeShapeList.h>
29 #include <GeomAlgoAPI_ShapeTools.h>
30
31 #include <BRep_Builder.hxx>
32 #include <BRep_Tool.hxx>
33 #include <BRepAlgoAPI_Cut.hxx>
34 #include <BRepBuilderAPI_FindPlane.hxx>
35 #include <BRepBuilderAPI_MakeFace.hxx>
36 #include <BRepBuilderAPI_Transform.hxx>
37 #include <BRepCheck_Analyzer.hxx>
38 #include <Geom_Curve.hxx>
39 #include <Geom2d_Curve.hxx>
40 #include <BRepLib_CheckCurveOnSurface.hxx>
41 #include <BRepPrimAPI_MakeRevol.hxx>
42 #include <BRepGProp.hxx>
43 #include <GC_MakePlane.hxx>
44 #include <Geom_Plane.hxx>
45 #include <GeomLib_IsPlanarSurface.hxx>
46 #include <gp_Pln.hxx>
47 #include <GProp_GProps.hxx>
48 #include <IntTools_Context.hxx>
49 #include <TopExp_Explorer.hxx>
50 #include <TopoDS.hxx>
51 #include <TopoDS_Edge.hxx>
52 #include <TopTools_ListIteratorOfListOfShape.hxx>
53
54 /// \brief Constructs infinite face from thePlane, and with axis located on the same side
55 /// of the plane as thePoint. Modifies thePlane axis direction.
56 /// \param[in,out] thePlane plane to construct face.
57 /// \param[in] thePoint point to locate plane axis.
58 /// \return constructed face.
59 static TopoDS_Face makeFaceFromPlane(gp_Pln& thePlane, const gp_Pnt& thePoint);
60
61 /// \return solid created from face or shell.
62 static TopoDS_Solid makeSolidFromShape(const TopoDS_Shape& theShape);
63
64 /// \brief return centre of mass for theShape.
65 /// \param[in] theShape shape.
66 static gp_Pnt centreOfMass(const TopoDS_Shape& theShape);
67
68 /// \brief Selects solid from theShape with closest center of mass to thePoint
69 /// \param[in] theShape compound with solids.
70 /// \param[in] thePoint point.
71 /// \return solid.
72 static TopoDS_Shape findClosest(const TopoDS_Shape& theShape, const gp_Pnt& thePoint);
73
74 static void storeGenerationHistory(GeomAlgoAPI_Revolution* theRevolutionAlgo,
75                                    const TopoDS_Shape& theBase,
76                                    const TopAbs_ShapeEnum theType,
77                                    BRepPrimAPI_MakeRevol* theRevolBuilder);
78
79 static void storeGenerationHistory(GeomAlgoAPI_Revolution* theRevolutionAlgo,
80                                    const TopoDS_Shape& theResult,
81                                    const TopAbs_ShapeEnum theType,
82                                    const TopoDS_Shape& theToFace,
83                                    const TopoDS_Shape& theFromFace);
84
85 static void storeGenerationHistory(GeomAlgoAPI_Revolution* theRevolutionAlgo,
86                                    const TopoDS_Shape& theResult,
87                                    const TopAbs_ShapeEnum theType,
88                                    const TopoDS_Shape& theRotatedBoundingFace,
89                                    const TopoDS_Shape& theModifiedBaseShape,
90                                    const bool theIsFromFaceSet);
91
92 //==================================================================================================
93 GeomAlgoAPI_Revolution::GeomAlgoAPI_Revolution(const GeomShapePtr                 theBaseShape,
94                                                const std::shared_ptr<GeomAPI_Ax1> theAxis,
95                                                const double                       theToAngle,
96                                                const double                       theFromAngle)
97 {
98   build(theBaseShape, theAxis, GeomShapePtr(), theToAngle, GeomShapePtr(), theFromAngle);
99 }
100
101 //==================================================================================================
102 GeomAlgoAPI_Revolution::GeomAlgoAPI_Revolution(const GeomShapePtr                 theBaseShape,
103                                                const std::shared_ptr<GeomAPI_Ax1> theAxis,
104                                                const GeomShapePtr                 theToShape,
105                                                const double                       theToAngle,
106                                                const GeomShapePtr                 theFromShape,
107                                                const double                       theFromAngle)
108 {
109   build(theBaseShape, theAxis, theToShape, theToAngle, theFromShape, theFromAngle);
110 }
111
112 //==================================================================================================
113 void GeomAlgoAPI_Revolution::build(const GeomShapePtr&                 theBaseShape,
114                                    const std::shared_ptr<GeomAPI_Ax1>& theAxis,
115                                    const GeomShapePtr&                 theToShape,
116                                    const double                        theToAngle,
117                                    const GeomShapePtr&                 theFromShape,
118                                    const double                        theFromAngle)
119 {
120   if(!theBaseShape || !theAxis ||
121     (((!theFromShape && !theToShape) ||
122     (theFromShape && theToShape && theFromShape->isEqual(theToShape)))
123     && (theFromAngle == -theToAngle))) {
124     return;
125   }
126
127   // Getting base shape.
128   const TopoDS_Shape& aBaseShape = theBaseShape->impl<TopoDS_Shape>();
129   TopAbs_ShapeEnum aShapeTypeToExp;
130   switch(aBaseShape.ShapeType()) {
131     case TopAbs_VERTEX:
132       aShapeTypeToExp = TopAbs_VERTEX;
133       break;
134     case TopAbs_EDGE:
135     case TopAbs_WIRE:
136       aShapeTypeToExp = TopAbs_EDGE;
137       break;
138     case TopAbs_FACE:
139     case TopAbs_SHELL:
140       aShapeTypeToExp = TopAbs_FACE;
141       break;
142     case TopAbs_COMPOUND:
143       aShapeTypeToExp = TopAbs_COMPOUND;
144       break;
145     default:
146       return;
147   }
148
149   // Getting axis.
150   gp_Ax1 anAxis = theAxis->impl<gp_Ax1>();
151
152   // Getting base plane.
153   Handle(Geom_Plane) aBasePlane;
154   BRepBuilderAPI_FindPlane aFindPlane(aBaseShape);
155   if(aShapeTypeToExp == TopAbs_FACE && aFindPlane.Found() == Standard_True) {
156     aBasePlane = aFindPlane.Plane();
157   } else {
158     gp_Pnt aPnt1 = anAxis.Location();
159     gp_Pnt aPnt2 = aPnt1;
160     aPnt2.Translate(anAxis.Direction());
161     gp_Pnt aPnt3;
162
163     for(TopExp_Explorer anExp(aBaseShape, TopAbs_VERTEX); anExp.More(); anExp.Next()) {
164       aPnt3 = BRep_Tool::Pnt(TopoDS::Vertex(anExp.Current()));
165
166       GC_MakePlane aMkPlane(aPnt1, aPnt2, aPnt3);
167       if(aMkPlane.IsDone() != Standard_True) {
168         continue;
169       }
170
171       aBasePlane = aMkPlane.Value();
172       break;
173     }
174
175     if(aBasePlane.IsNull()) {
176       aPnt3 = centreOfMass(aBaseShape);
177
178       GC_MakePlane aMkPlane(aPnt1, aPnt2, aPnt3);
179       if(aMkPlane.IsDone() != Standard_True) {
180         return;
181       }
182
183       aBasePlane = aMkPlane.Value();
184     }
185   }
186
187   if(aShapeTypeToExp == TopAbs_FACE) {
188     if(aBasePlane->Axis().Angle(anAxis) < Precision::Confusion()) {
189       return;
190     }
191   }
192
193   gp_Pnt aBaseCentre = GeomAlgoAPI_ShapeTools::centreOfMass(theBaseShape)->impl<gp_Pnt>();
194
195   TopoDS_Shape aResult;
196   if(!theFromShape && !theToShape) { // Case 1: When only angles was set.
197     // Rotating base face with the negative value of "from angle".
198     gp_Trsf aBaseTrsf;
199     aBaseTrsf.SetRotation(anAxis, -theFromAngle / 180.0 * M_PI);
200     BRepBuilderAPI_Transform* aBaseTransform = new BRepBuilderAPI_Transform(aBaseShape,
201                                                                             aBaseTrsf,
202                                                                             true);
203     if(!aBaseTransform) {
204       return;
205     }
206     this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(
207       new GeomAlgoAPI_MakeShape(aBaseTransform)));
208     if(!aBaseTransform->IsDone()) {
209       return;
210     }
211     TopoDS_Shape aRotatedBase = aBaseTransform->Shape();
212
213     // Making revolution to the angle equal to the sum of "from angle" and "to angle".
214     BRepPrimAPI_MakeRevol* aRevolBuilder = new BRepPrimAPI_MakeRevol(aRotatedBase,
215                                                         anAxis,
216                                                         (theFromAngle + theToAngle) / 180 * M_PI,
217                                                         Standard_True);
218     if(!aRevolBuilder) {
219       return;
220     }
221     this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(
222       new GeomAlgoAPI_MakeShape(aRevolBuilder)));
223     if(!aRevolBuilder->IsDone()) {
224       return;
225     }
226     aResult = aRevolBuilder->Shape();
227
228     // Setting naming.
229     if(aShapeTypeToExp == TopAbs_COMPOUND) {
230       storeGenerationHistory(this, aRotatedBase, TopAbs_EDGE, aRevolBuilder);
231       storeGenerationHistory(this, aRotatedBase, TopAbs_FACE, aRevolBuilder);
232     } else {
233       storeGenerationHistory(this, aRotatedBase, aShapeTypeToExp, aRevolBuilder);
234     }
235   } else if(theFromShape && theToShape) { // Case 2: When both bounding planes were set.
236     // Making revolution to the 360 angle.
237     BRepPrimAPI_MakeRevol* aRevolBuilder =
238       new BRepPrimAPI_MakeRevol(aBaseShape, anAxis, 2 * M_PI, Standard_True);
239     if(!aRevolBuilder) {
240       return;
241     }
242     this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(
243       new GeomAlgoAPI_MakeShape(aRevolBuilder)));
244     if(!aRevolBuilder->IsDone()) {
245       return;
246     }
247     aResult = aRevolBuilder->Shape();
248
249     // Getting bounding faces.
250     TopoDS_Face aFromFace = TopoDS::Face(theFromShape->impl<TopoDS_Shape>());
251     TopoDS_Face aToFace   = TopoDS::Face(theToShape->impl<TopoDS_Shape>());
252
253     // Getting planes from bounding face.
254     GeomLib_IsPlanarSurface isFromPlanar(BRep_Tool::Surface(aFromFace));
255     GeomLib_IsPlanarSurface isToPlanar(BRep_Tool::Surface(aToFace));
256     if(!isFromPlanar.IsPlanar() || !isToPlanar.IsPlanar()) {
257       // non-planar shapes is not supported for revolution bounding
258       return;
259     }
260
261     std::shared_ptr<GeomAPI_Face> aGeomFromFace(new GeomAPI_Face(theFromShape));
262     std::shared_ptr<GeomAPI_Face> aGeomToFace(new GeomAPI_Face(theToShape));
263
264     gp_Pln aFromPln = aGeomFromFace->getPlane()->impl<gp_Pln>();
265     gp_Pln aToPln   = aGeomToFace->getPlane()->impl<gp_Pln>();
266
267     // Orienting bounding planes properly so that the center of mass of the base face stays
268     // on the result shape after cut.
269     aFromFace = makeFaceFromPlane(aFromPln, aBaseCentre);
270     aToFace   = makeFaceFromPlane(aToPln, aBaseCentre);
271
272     // Making solids from bounding planes and putting them in compound.
273     TopoDS_Shape aFromSolid = makeSolidFromShape(aFromFace);
274     TopoDS_Shape aToSolid   = makeSolidFromShape(aToFace);
275
276     // Rotating bounding planes to the specified angle.
277     gp_Trsf aFromTrsf;
278     gp_Trsf aToTrsf;
279     double aFromRotAngle =
280       ((aFromPln.Axis().Direction() * aBasePlane->Axis().Direction()) > 0) ? -theFromAngle :
281                                                                               theFromAngle;
282     double aToRotAngle =
283       ((aToPln.Axis().Direction() * aBasePlane->Axis().Direction()) > 0) ? -theToAngle :
284                                                                             theToAngle;
285     aFromTrsf.SetRotation(anAxis,aFromRotAngle / 180.0 * M_PI);
286     aToTrsf.SetRotation(anAxis, aToRotAngle / 180.0 * M_PI);
287     BRepBuilderAPI_Transform aFromTransform(aFromSolid, aFromTrsf, true);
288     BRepBuilderAPI_Transform aToTransform(aToSolid, aToTrsf, true);
289     TopoDS_Shape aRotatedFromFace = aFromTransform.Modified(aFromFace).First();
290     TopoDS_Shape aRotatedToFace = aToTransform.Modified(aToFace).First();
291     aFromSolid = aFromTransform.Shape();
292     aToSolid = aToTransform.Shape();
293
294     // Cutting revolution with from plane.
295     BRepAlgoAPI_Cut* aFromCutBuilder = new BRepAlgoAPI_Cut(aResult, aFromSolid);
296     aFromCutBuilder->Build();
297     if(!aFromCutBuilder->IsDone()) {
298       return;
299     }
300     this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(
301       new GeomAlgoAPI_MakeShape(aFromCutBuilder)));
302     aResult = aFromCutBuilder->Shape();
303     if(aResult.ShapeType() == TopAbs_COMPOUND) {
304       aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
305     }
306
307     // Cutting revolution with to plane.
308     BRepAlgoAPI_Cut* aToCutBuilder = new BRepAlgoAPI_Cut(aResult, aToSolid);
309     aToCutBuilder->Build();
310     if(!aToCutBuilder->IsDone()) {
311       return;
312     }
313     this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(
314       new GeomAlgoAPI_MakeShape(aToCutBuilder)));
315     aResult = aToCutBuilder->Shape();
316     TopoDS_Iterator aCheckIt(aResult);
317     if(!aCheckIt.More()) {
318       return;
319     }
320     if(aResult.ShapeType() == TopAbs_COMPOUND) {
321       aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
322     }
323     if(aResult.ShapeType() == TopAbs_COMPOUND) {
324       std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
325       aGeomShape->setImpl(new TopoDS_Shape(aResult));
326       ListOfShape aCompSolids, aFreeSolids;
327       aGeomShape = GeomAlgoAPI_ShapeTools::combineShapes(aGeomShape,
328                                                          GeomAPI_Shape::COMPSOLID,
329                                                          aCompSolids,
330                                                          aFreeSolids);
331       aResult = aGeomShape->impl<TopoDS_Shape>();
332     }
333
334     // If after cut we got more than one solids then take closest
335     // to the center of mass of the base face.
336     aResult = findClosest(aResult, aBaseCentre);
337
338     // Setting naming.
339     if(aShapeTypeToExp == TopAbs_COMPOUND) {
340       storeGenerationHistory(this, aResult, TopAbs_EDGE, aRotatedToFace, aRotatedFromFace);
341       storeGenerationHistory(this, aResult, TopAbs_FACE, aRotatedToFace, aRotatedFromFace);
342     } else {
343       storeGenerationHistory(this, aResult, aShapeTypeToExp, aRotatedToFace, aRotatedFromFace);
344     }
345   } else { //Case 3: When only one bounding plane was set.
346     // Making revolution to the 360 angle.
347     BRepPrimAPI_MakeRevol* aRevolBuilder =
348       new BRepPrimAPI_MakeRevol(aBaseShape, anAxis, 2 * M_PI, Standard_True);
349     if(!aRevolBuilder) {
350       return;
351     }
352     this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(
353       new GeomAlgoAPI_MakeShape(aRevolBuilder)));
354     if(!aRevolBuilder->IsDone()) {
355       return;
356     }
357     aResult = aRevolBuilder->Shape();
358
359     // Getting bounding face.
360     bool isFromFaceSet = false;
361     std::shared_ptr<GeomAPI_Face> aGeomBoundingFace;
362     if(theFromShape) {
363       aGeomBoundingFace.reset(new GeomAPI_Face(theFromShape));
364       isFromFaceSet = true;
365     } else if(theToShape) {
366       aGeomBoundingFace.reset(new GeomAPI_Face(theToShape));
367     }
368     TopoDS_Face aBoundingFace = TopoDS::Face(aGeomBoundingFace->impl<TopoDS_Shape>());
369
370     // Getting plane from bounding face.
371     GeomLib_IsPlanarSurface isBoundingPlanar(BRep_Tool::Surface(aBoundingFace));
372     if(!isBoundingPlanar.IsPlanar()) { // non-planar shapes is not supported for revolution bounding
373       return;
374     }
375
376     gp_Pln aBoundingPln = aGeomBoundingFace->getPlane()->impl<gp_Pln>();
377
378     // Orienting bounding plane properly so that the center of mass of the base face stays
379     // on the result shape after cut.
380     aBoundingFace = makeFaceFromPlane(aBoundingPln, aBaseCentre);
381
382     // Making solid from bounding plane.
383     TopoDS_Shape aBoundingSolid = makeSolidFromShape(aBoundingFace);
384
385     // Rotating bounding plane to the specified angle.
386     double aBoundingRotAngle = isFromFaceSet ? theFromAngle : theToAngle;
387     if(aBoundingPln.Axis().IsParallel(aBasePlane->Axis(), Precision::Confusion())) {
388       if(isFromFaceSet) aBoundingRotAngle = -aBoundingRotAngle;
389     } else {
390       double aSign = (aBoundingPln.Axis().Direction() ^ aBasePlane->Axis().Direction()) *
391                      anAxis.Direction();
392       if((aSign <= 0 && !isFromFaceSet) || (aSign > 0 && isFromFaceSet)) {
393         aBoundingRotAngle = -aBoundingRotAngle;
394       }
395     }
396     gp_Trsf aBoundingTrsf;
397     aBoundingTrsf.SetRotation(anAxis, aBoundingRotAngle / 180.0 * M_PI);
398     BRepBuilderAPI_Transform aBoundingTransform(aBoundingSolid, aBoundingTrsf, true);
399     TopoDS_Shape aRotatedBoundingFace = aBoundingTransform.Modified(aBoundingFace).First();
400     aBoundingSolid = aBoundingTransform.Shape();
401
402     // Cutting revolution with bounding plane.
403     BRepAlgoAPI_Cut* aBoundingCutBuilder = new BRepAlgoAPI_Cut(aResult, aBoundingSolid);
404     aBoundingCutBuilder->Build();
405     if(!aBoundingCutBuilder->IsDone()) {
406       return;
407     }
408     this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(
409       new GeomAlgoAPI_MakeShape(aBoundingCutBuilder)));
410     aResult = aBoundingCutBuilder->Shape();
411     if(aResult.ShapeType() == TopAbs_COMPOUND) {
412       aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
413     }
414
415     // Setting naming.
416     if(aShapeTypeToExp == TopAbs_FACE || aShapeTypeToExp == TopAbs_COMPOUND) {
417       const TopTools_ListOfShape& aBndShapes = aBoundingCutBuilder->Modified(aBoundingFace);
418       for(TopTools_ListIteratorOfListOfShape anIt(aBndShapes); anIt.More(); anIt.Next()) {
419         GeomShapePtr aShape(new GeomAPI_Shape());
420         aShape->setImpl(new TopoDS_Shape(anIt.Value()));
421         isFromFaceSet ? this->addFromShape(aShape) : this->addToShape(aShape);
422       }
423     }
424
425     // Try to cut with base face. If it can not be done then keep result of cut with bounding plane.
426     TopoDS_Shape aModifiedBaseShape;
427     if(aShapeTypeToExp != TopAbs_FACE) {
428       ListOfShape aList;
429       GeomShapePtr aSh(new GeomAPI_Shape());
430       aSh->setImpl(new TopoDS_Shape(aBaseShape));
431       std::shared_ptr<GeomAPI_Pnt> theCenter(new GeomAPI_Pnt(aBasePlane->Location().X(),
432                                                              aBasePlane->Location().Y(),
433                                                              aBasePlane->Location().Z()));
434       std::shared_ptr<GeomAPI_Dir> theNormal(new GeomAPI_Dir(aBasePlane->Axis().Direction().X(),
435                                                              aBasePlane->Axis().Direction().Y(),
436                                                              aBasePlane->Axis().Direction().Z()));
437       GeomShapePtr aPln = GeomAlgoAPI_FaceBuilder::planarFace(theCenter, theNormal);
438       aList.push_back(aSh);
439       std::list<std::shared_ptr<GeomAPI_Pnt> >
440         aBoundingPoints = GeomAlgoAPI_ShapeTools::getBoundingBox(aList);
441       aSh = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aPln, aBoundingPoints);
442       aModifiedBaseShape = aSh->impl<TopoDS_Shape>();
443     } else {
444       aModifiedBaseShape = aBaseShape;
445     }
446     if(isFromFaceSet) {
447       if(aModifiedBaseShape.ShapeType() == TopAbs_FACE) {
448         aModifiedBaseShape.Orientation(TopAbs_REVERSED);
449       } else {
450         gp_Trsf aMirrorTrsf;
451         aMirrorTrsf.SetMirror(aBasePlane->Position().Ax2());
452         BRepBuilderAPI_Transform aMirrorTransform(aModifiedBaseShape, aMirrorTrsf, true);
453         aModifiedBaseShape = aMirrorTransform.Shape();
454       }
455     }
456
457     // Making solid from base face.
458     TopoDS_Shape aBaseSolid = makeSolidFromShape(aModifiedBaseShape);
459
460     // Rotating base face to the specified angle.
461     gp_Trsf aBaseTrsf;
462     double aBaseRotAngle = isFromFaceSet ? theToAngle : -theFromAngle;
463     aBaseTrsf.SetRotation(anAxis, aBaseRotAngle / 180.0 * M_PI);
464     BRepBuilderAPI_Transform aBaseTransform(aBaseSolid, aBaseTrsf, true);
465     aBaseSolid = aBaseTransform.Shape();
466
467     // Cutting revolution with base.
468     BRepAlgoAPI_Cut* aBaseCutBuilder = new BRepAlgoAPI_Cut(aResult, aBaseSolid);
469     aBaseCutBuilder->Build();
470     if(aBaseCutBuilder->IsDone()) {
471       TopoDS_Shape aCutResult = aBaseCutBuilder->Shape();
472       TopoDS_Iterator aCheckIt(aCutResult);
473       if(aCheckIt.More()) {
474         this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(
475           new GeomAlgoAPI_MakeShape(aBaseCutBuilder)));
476         aResult = aCutResult;
477         if(aResult.ShapeType() == TopAbs_COMPOUND) {
478           aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
479         }
480         if(aShapeTypeToExp == TopAbs_FACE || aShapeTypeToExp == TopAbs_COMPOUND) {
481           const TopTools_ListOfShape& aBsShapes = aBaseCutBuilder->Modified(aBoundingFace);
482           for(TopTools_ListIteratorOfListOfShape anIt(aBsShapes); anIt.More(); anIt.Next()) {
483             GeomShapePtr aShape(new GeomAPI_Shape());
484             aShape->setImpl(new TopoDS_Shape(anIt.Value()));
485             isFromFaceSet ? this->addToShape(aShape) : this->addFromShape(aShape);
486           }
487         }
488       }
489     }
490
491     if(aResult.ShapeType() == TopAbs_COMPOUND) {
492       std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
493       aGeomShape->setImpl(new TopoDS_Shape(aResult));
494       ListOfShape aCompSolids, aFreeSolids;
495       aGeomShape = GeomAlgoAPI_ShapeTools::combineShapes(aGeomShape,
496                                                          GeomAPI_Shape::COMPSOLID,
497                                                          aCompSolids,
498                                                          aFreeSolids);
499       aResult = aGeomShape->impl<TopoDS_Shape>();
500     }
501
502     // If after cut we got more than one solids then take
503     // closest to the center of mass of the base face.
504     aResult = findClosest(aResult, aBaseCentre);
505
506     // Setting naming.
507     if(aShapeTypeToExp == TopAbs_COMPOUND) {
508       storeGenerationHistory(this, aResult, TopAbs_EDGE,
509         aRotatedBoundingFace, aModifiedBaseShape, isFromFaceSet);
510       storeGenerationHistory(this, aResult, TopAbs_FACE,
511         aRotatedBoundingFace, aModifiedBaseShape, isFromFaceSet);
512     } else {
513       storeGenerationHistory(this, aResult, aShapeTypeToExp,
514         aRotatedBoundingFace, aModifiedBaseShape, isFromFaceSet);
515     }
516   }
517
518   // Setting result.
519   if(aResult.IsNull()) {
520     return;
521   }
522   aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
523   GeomShapePtr aShape(new GeomAPI_Shape());
524   aShape->setImpl(new TopoDS_Shape(aResult));
525   this->setShape(aShape);
526   this->setDone(true);
527 }
528
529 //==================================================================================================
530 TopoDS_Face makeFaceFromPlane(gp_Pln& thePlane, const gp_Pnt& thePoint)
531 {
532   if(!thePlane.Contains(thePoint, Precision::Confusion())) {
533     gp_XYZ aVec = thePoint.XYZ() - thePlane.Location().XYZ();
534     double aSign = aVec * thePlane.Axis().Direction().XYZ();
535     if(aSign < 0) thePlane.SetAxis(thePlane.Axis().Reversed());
536   }
537
538   BRepBuilderAPI_MakeFace aMakeFace(thePlane);
539   TopoDS_Face aResultFace = TopoDS::Face(aMakeFace.Shape());
540
541   return aResultFace;
542 }
543
544 //==================================================================================================
545 TopoDS_Solid makeSolidFromShape(const TopoDS_Shape& theShape)
546 {
547   TopoDS_Shell aShell;
548   TopoDS_Solid aSolid;
549
550   BRep_Builder aBoundingBuilder;
551   if(theShape.ShapeType() == TopAbs_SHELL) {
552     aShell = TopoDS::Shell(theShape);
553   } else {
554     aBoundingBuilder.MakeShell(aShell);
555     aBoundingBuilder.Add(aShell, theShape);
556   }
557   aBoundingBuilder.MakeSolid(aSolid);
558   aBoundingBuilder.Add(aSolid, aShell);
559
560   return aSolid;
561 }
562
563 //================================================================================================
564 gp_Pnt centreOfMass(const TopoDS_Shape& theShape)
565 {
566   TopAbs_ShapeEnum aShType = theShape.ShapeType();
567   GProp_GProps aGProps;
568
569   if(aShType == TopAbs_EDGE || aShType == TopAbs_WIRE) {
570     BRepGProp::LinearProperties(theShape, aGProps);
571   } else if(aShType == TopAbs_FACE || aShType == TopAbs_SHELL) {
572     BRepGProp::SurfaceProperties(theShape, aGProps);
573   } else if(aShType == TopAbs_SOLID || aShType == TopAbs_COMPSOLID) {
574     BRepGProp::VolumeProperties(theShape, aGProps);
575   }
576
577   return aGProps.CentreOfMass();
578 }
579
580 //================================================================================================
581 TopoDS_Shape findClosest(const TopoDS_Shape& theShape, const gp_Pnt& thePoint)
582 {
583   TopoDS_Shape aResult = theShape;
584
585   if(theShape.ShapeType() == TopAbs_COMPOUND) {
586     double aMinDistance = Precision::Infinite();
587     double aCurDistance;
588     gp_Pnt aCentr;
589     for (TopoDS_Iterator anItr(theShape); anItr.More(); anItr.Next()) {
590       TopoDS_Shape aValue = anItr.Value();
591       aCentr = centreOfMass(aValue);
592       aCurDistance = aCentr.Distance(thePoint);
593
594       if(aCurDistance < aMinDistance) {
595         aMinDistance = aCurDistance;
596         aResult = aValue;
597       }
598     }
599   }
600
601   return aResult;
602 }
603
604 //================================================================================================
605 void storeGenerationHistory(GeomAlgoAPI_Revolution* theRevolutionAlgo,
606                             const TopoDS_Shape& theBase,
607                             const TopAbs_ShapeEnum theType,
608                             BRepPrimAPI_MakeRevol* theRevolBuilder)
609 {
610   for(TopExp_Explorer anExp(theBase, theType); anExp.More(); anExp.Next()) {
611     const TopoDS_Shape& aShape = anExp.Current();
612     GeomShapePtr aFromShape(new GeomAPI_Shape), aToShape(new GeomAPI_Shape);
613     aFromShape->setImpl(new TopoDS_Shape(theRevolBuilder->FirstShape(aShape)));
614     aToShape->setImpl(new TopoDS_Shape(theRevolBuilder->LastShape(aShape)));
615     theRevolutionAlgo->addFromShape(aFromShape);
616     theRevolutionAlgo->addToShape(aToShape);
617   }
618 }
619
620 //================================================================================================
621 void storeGenerationHistory(GeomAlgoAPI_Revolution* theRevolutionAlgo,
622                             const TopoDS_Shape& theResult,
623                             const TopAbs_ShapeEnum theType,
624                             const TopoDS_Shape& theToFace,
625                             const TopoDS_Shape& theFromFace)
626 {
627   for(TopExp_Explorer anExp(theResult, theType); anExp.More (); anExp.Next ()) {
628     const TopoDS_Shape& aShape = anExp.Current();
629     GeomShapePtr aGeomSh(new GeomAPI_Shape());
630     if(theType == TopAbs_VERTEX) {
631       gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aShape));
632       IntTools_Context anIntTools;
633       if(anIntTools.IsValidPointForFace(aPnt, TopoDS::Face(theToFace),
634           Precision::Confusion()) == Standard_True) {
635         aGeomSh->setImpl(new TopoDS_Shape(aShape));
636         theRevolutionAlgo->addToShape(aGeomSh);
637       }
638       if(anIntTools.IsValidPointForFace(aPnt, TopoDS::Face(theFromFace),
639           Precision::Confusion()) == Standard_True) {
640         aGeomSh->setImpl(new TopoDS_Shape(aShape));
641         theRevolutionAlgo->addFromShape(aGeomSh);
642       }
643     } else if(theType == TopAbs_EDGE) {
644       TopoDS_Edge anEdge = TopoDS::Edge(aShape);
645       BRepLib_CheckCurveOnSurface anEdgeCheck(anEdge, TopoDS::Face(theToFace));
646       anEdgeCheck.Perform();
647       if(anEdgeCheck.MaxDistance() < Precision::Confusion()) {
648         aGeomSh->setImpl(new TopoDS_Shape(aShape));
649         theRevolutionAlgo->addToShape(aGeomSh);
650       }
651       anEdgeCheck.Init(anEdge, TopoDS::Face(theFromFace));
652       anEdgeCheck.Perform();
653       if(anEdgeCheck.MaxDistance() < Precision::Confusion()) {
654         aGeomSh->setImpl(new TopoDS_Shape(aShape));
655         theRevolutionAlgo->addFromShape(aGeomSh);
656       }
657     } else {
658       Handle(Geom_Surface) aFaceSurface = BRep_Tool::Surface(TopoDS::Face(aShape));
659       Handle(Geom_Surface) aFromSurface = BRep_Tool::Surface(TopoDS::Face(theFromFace));
660       Handle(Geom_Surface) aToSurface = BRep_Tool::Surface(TopoDS::Face(theToFace));
661       if(aFaceSurface == aFromSurface) {
662         aGeomSh->setImpl(new TopoDS_Shape(aShape));
663         theRevolutionAlgo->addFromShape(aGeomSh);
664       }
665       if(aFaceSurface == aToSurface) {
666         aGeomSh->setImpl(new TopoDS_Shape(aShape));
667         theRevolutionAlgo->addToShape(aGeomSh);
668       }
669     }
670   }
671 }
672
673 void storeGenerationHistory(GeomAlgoAPI_Revolution* theRevolutionAlgo,
674                             const TopoDS_Shape& theResult,
675                             const TopAbs_ShapeEnum theType,
676                             const TopoDS_Shape& theRotatedBoundingFace,
677                             const TopoDS_Shape& theModifiedBaseShape,
678                             const bool theIsFromFaceSet)
679 {
680   for(TopExp_Explorer anExp(theResult, theType); anExp.More (); anExp.Next ()) {
681     const TopoDS_Shape& aShape = anExp.Current();
682     GeomShapePtr aGeomSh(new GeomAPI_Shape());
683     if(theType == TopAbs_VERTEX) {
684       gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aShape));
685       IntTools_Context anIntTools;
686       if(anIntTools.IsValidPointForFace(aPnt, TopoDS::Face(theRotatedBoundingFace),
687           Precision::Confusion()) == Standard_True) {
688         aGeomSh->setImpl(new TopoDS_Shape(aShape));
689         theIsFromFaceSet ? theRevolutionAlgo->addFromShape(aGeomSh) :
690                            theRevolutionAlgo->addToShape(aGeomSh);
691       }
692       if(anIntTools.IsValidPointForFace(aPnt, TopoDS::Face(theModifiedBaseShape),
693          Precision::Confusion()) == Standard_True) {
694         aGeomSh->setImpl(new TopoDS_Shape(aShape));
695         theIsFromFaceSet ? theRevolutionAlgo->addToShape(aGeomSh) :
696                            theRevolutionAlgo->addFromShape(aGeomSh);
697       }
698     } else if(theType == TopAbs_EDGE) {
699       TopoDS_Edge anEdge = TopoDS::Edge(aShape);
700       BRepLib_CheckCurveOnSurface anEdgeCheck(anEdge, TopoDS::Face(theRotatedBoundingFace));
701       anEdgeCheck.Perform();
702       if(anEdgeCheck.MaxDistance() < Precision::Confusion()) {
703         aGeomSh->setImpl(new TopoDS_Shape(aShape));
704         theIsFromFaceSet ? theRevolutionAlgo->addFromShape(aGeomSh) :
705                            theRevolutionAlgo->addToShape(aGeomSh);
706       }
707       anEdgeCheck.Init(anEdge, TopoDS::Face(theModifiedBaseShape));
708       anEdgeCheck.Perform();
709       if(anEdgeCheck.MaxDistance() < Precision::Confusion()) {
710         aGeomSh->setImpl(new TopoDS_Shape(aShape));
711         theIsFromFaceSet ? theRevolutionAlgo->addToShape(aGeomSh) :
712                            theRevolutionAlgo->addFromShape(aGeomSh);
713       }
714     } else {
715       Handle(Geom_Surface) aFaceSurface = BRep_Tool::Surface(TopoDS::Face(aShape));
716       Handle(Geom_Surface) aBoundingSurface =
717         BRep_Tool::Surface(TopoDS::Face(theRotatedBoundingFace));
718       Handle(Geom_Surface) aBaseSurface = BRep_Tool::Surface(TopoDS::Face(theModifiedBaseShape));
719       if(aFaceSurface == aBoundingSurface) {
720         aGeomSh->setImpl(new TopoDS_Shape(aShape));
721         theIsFromFaceSet ? theRevolutionAlgo->addFromShape(aGeomSh) :
722                            theRevolutionAlgo->addToShape(aGeomSh);
723       }
724       if(aFaceSurface == aBaseSurface) {
725         aGeomSh->setImpl(new TopoDS_Shape(aShape));
726         theIsFromFaceSet ? theRevolutionAlgo->addToShape(aGeomSh) :
727                            theRevolutionAlgo->addFromShape(aGeomSh);
728       }
729     }
730   }
731 }