Salome HOME
Test update
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Revolution.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomAlgoAPI_Revolution.cpp
4 // Created:     12 May 2015
5 // Author:      Dmitry Bobylev
6
7 #include <GeomAlgoAPI_Revolution.h>
8
9 #include <GeomAPI_Face.h>
10 #include <GeomAPI_ShapeExplorer.h>
11 #include <GeomAlgoAPI_DFLoader.h>
12 #include <GeomAlgoAPI_MakeShapeList.h>
13 #include <GeomAlgoAPI_Rotation.h>
14 #include <GeomAlgoAPI_ShapeTools.h>
15
16 #include <BRep_Builder.hxx>
17 #include <BRep_Tool.hxx>
18 #include <BRepAlgoAPI_Cut.hxx>
19 #include <BRepBuilderAPI_MakeEdge.hxx>
20 #include <BRepBuilderAPI_MakeFace.hxx>
21 #include <BRepBuilderAPI_Transform.hxx>
22 #include <BRepBuilderAPI_MakeWire.hxx>
23 #include <BRepCheck_Analyzer.hxx>
24 #include <BRepPrimAPI_MakeRevol.hxx>
25 #include <BRepGProp.hxx>
26 #include <BRepOffsetAPI_MakePipe.hxx>
27 #include <BRepTools.hxx>
28 #include <Geom_Circle.hxx>
29 #include <Geom_Line.hxx>
30 #include <Geom_Plane.hxx>
31 #include <GeomAPI_ProjectPointOnCurve.hxx>
32 #include <GeomLib_IsPlanarSurface.hxx>
33 #include <gp_Circ.hxx>
34 #include <gp_Pln.hxx>
35 #include <GProp_GProps.hxx>
36 #include <TopExp_Explorer.hxx>
37 #include <TopoDS.hxx>
38 #include <TopTools_ListIteratorOfListOfShape.hxx>
39
40 //=================================================================================================
41 GeomAlgoAPI_Revolution::GeomAlgoAPI_Revolution(std::shared_ptr<GeomAPI_Shape> theBasis,
42                                                std::shared_ptr<GeomAPI_Ax1>   theAxis,
43                                                double                         theToAngle,
44                                                double                         theFromAngle)
45 : myDone(false)
46 {
47   build(theBasis, theAxis, std::shared_ptr<GeomAPI_Shape>(), theToAngle, std::shared_ptr<GeomAPI_Shape>(), theFromAngle);
48 }
49
50 //=================================================================================================
51 GeomAlgoAPI_Revolution::GeomAlgoAPI_Revolution(std::shared_ptr<GeomAPI_Shape> theBasis,
52                                                std::shared_ptr<GeomAPI_Ax1>   theAxis,
53                                                std::shared_ptr<GeomAPI_Shape> theToShape,
54                                                double                         theToAngle,
55                                                std::shared_ptr<GeomAPI_Shape> theFromShape,
56                                                double                         theFromAngle)
57 : myDone(false)
58 {
59   build(theBasis, theAxis, theToShape, theToAngle, theFromShape, theFromAngle);
60 }
61
62 //=================================================================================================
63 TopoDS_Face GeomAlgoAPI_Revolution::makeFaceFromPlane(gp_Pln& thePlane, const gp_Pnt& thePoint)
64 {
65   gp_XYZ aVec = thePoint.XYZ() - thePlane.Location().XYZ();
66   double aSign = aVec * thePlane.Axis().Direction().XYZ();
67   if(aSign < 0) thePlane.SetAxis(thePlane.Axis().Reversed());
68
69   BRepBuilderAPI_MakeFace aMakeFace(thePlane);
70   TopoDS_Face aResultFace = TopoDS::Face(aMakeFace.Shape());
71
72   return aResultFace;
73 }
74
75 //=================================================================================================
76 TopoDS_Solid GeomAlgoAPI_Revolution::makeSolidFromShape(const TopoDS_Shape& theShape)
77 {
78   TopoDS_Shell aShell;
79   TopoDS_Solid aSolid;
80
81   BRep_Builder aBoundingBuilder;
82   if(theShape.ShapeType() == TopAbs_SHELL) {
83     aShell = TopoDS::Shell(theShape);
84   } else {
85     aBoundingBuilder.MakeShell(aShell);
86     aBoundingBuilder.Add(aShell, theShape);
87   }
88   aBoundingBuilder.MakeSolid(aSolid);
89   aBoundingBuilder.Add(aSolid, aShell);
90
91   return aSolid;
92 }
93
94 //=================================================================================================
95 TopoDS_Shape GeomAlgoAPI_Revolution::findClosest(const TopoDS_Shape& theShape, const gp_Pnt& thePoint)
96 {
97   TopoDS_Shape aResult = theShape;
98
99   if(theShape.ShapeType() == TopAbs_COMPOUND) {
100     double aMinDistance = Precision::Infinite();
101     double aCurDistance;
102     GProp_GProps aGProps;
103     gp_Pnt aCentr;
104
105     for (TopoDS_Iterator anItr(theShape); anItr.More(); anItr.Next()) {
106       TopoDS_Shape aValue = anItr.Value();
107       BRepGProp::VolumeProperties(aValue, aGProps);
108       aCentr = aGProps.CentreOfMass();
109       aCurDistance = aCentr.Distance(thePoint);
110
111       if(aCurDistance < aMinDistance) {
112         aMinDistance = aCurDistance;
113         aResult = aValue;
114       }
115     }
116   }
117
118   return aResult;
119 }
120
121 //=================================================================================================
122 void GeomAlgoAPI_Revolution::build(const std::shared_ptr<GeomAPI_Shape>& theBasis,
123                                    const std::shared_ptr<GeomAPI_Ax1>&   theAxis,
124                                    const std::shared_ptr<GeomAPI_Shape>& theToShape,
125                                    double                                theToAngle,
126                                    const std::shared_ptr<GeomAPI_Shape>& theFromShape,
127                                    double                                theFromAngle)
128 {
129   if(!theBasis || !theAxis ||
130     (((!theFromShape && !theToShape) || (theFromShape && theToShape && theFromShape->isEqual(theToShape)))
131     && (theFromAngle == -theToAngle))) {
132     return;
133   }
134
135   // Checking that shell is planar.
136   TopoDS_Shape aBasis = theBasis->impl<TopoDS_Shape>();
137   // TODO: fix planar checking
138   //TopExp_Explorer aBasisExp(aBasis, TopAbs_FACE);
139   //for(; aBasisExp.More(); aBasisExp.Next()) {
140   //  const TopoDS_Shape& aCurSh = aBasisExp.Current();
141   //}
142
143   // Geting base plane.
144   std::shared_ptr<GeomAPI_Face> aBaseFace;
145   if(theBasis->shapeType() == GeomAPI_Shape::FACE) {
146     aBaseFace = std::shared_ptr<GeomAPI_Face>(new GeomAPI_Face(theBasis));
147   } else if(theBasis->shapeType() == GeomAPI_Shape::SHELL) {
148     GeomAPI_ShapeExplorer anExp(theBasis, GeomAPI_Shape::FACE);
149     if(anExp.more()) {
150       std::shared_ptr<GeomAPI_Shape> aFaceOnShell = anExp.current();
151       aBaseFace = std::shared_ptr<GeomAPI_Face>(new GeomAPI_Face(aFaceOnShell));
152     }
153   }
154   if(!aBaseFace.get()) {
155     return;
156   }
157   TopoDS_Face aBasisFace = TopoDS::Face(aBaseFace->impl<TopoDS_Shape>());
158   GeomLib_IsPlanarSurface isBasisPlanar(BRep_Tool::Surface(aBasisFace));
159   gp_Pln aBasisPln = isBasisPlanar.Plan();
160   Geom_Plane aBasisPlane(aBasisPln);
161   gp_Ax1 anAxis = theAxis->impl<gp_Ax1>();
162   if(aBasisPlane.Axis().Angle(anAxis) < Precision::Confusion()) {
163     return;
164   }
165   gp_Lin anAxisLin(anAxis);
166
167   // Creating circle for pipe.
168   gp_Pnt aBasisCentre = GeomAlgoAPI_ShapeTools::centreOfMass(theBasis)->impl<gp_Pnt>();
169   gp_Pnt aStartPnt = aBasisCentre;
170   const TopoDS_Shape& aBasisShape = theBasis->impl<TopoDS_Shape>();
171   Handle(Geom_Line) anAxisLine = new Geom_Line(anAxis);
172   if(anAxisLin.Contains(aStartPnt, Precision::Confusion())) {
173     aStartPnt.Translate(anAxis.Direction() ^ aBasisPln.Axis().Direction());
174   }
175   GeomAPI_ProjectPointOnCurve aProjection(aStartPnt, anAxisLine);
176   if(aProjection.NbPoints() != 1) {
177     return;
178   }
179   Standard_Real aRadius = aProjection.Distance(1);
180   gp_Circ aCircle(gp_Ax2(aProjection.NearestPoint(), anAxis.Direction()), aRadius);
181
182   TopoDS_Shape aResult;
183   ListOfMakeShape aListOfMakeShape;
184   if(!theFromShape && !theToShape) { // Case 1: When only angles was set.
185     // Rotating base face with the negative value of "from angle".
186     gp_Trsf aBaseTrsf;
187     aBaseTrsf.SetRotation(anAxis, -theFromAngle / 180.0 * M_PI);
188     BRepBuilderAPI_Transform* aBaseTransform = new BRepBuilderAPI_Transform(aBasisShape,
189                                                                             aBaseTrsf,
190                                                                             true);
191     if(!aBaseTransform || !aBaseTransform->IsDone()) {
192       return;
193     }
194     aListOfMakeShape.push_back(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aBaseTransform)));
195     TopoDS_Shape aRotatedBaseShape = aBaseTransform->Shape();
196
197     if(theBasis->shapeType() == GeomAPI_Shape::FACE) {
198       // Making revolution to the angle equal to the sum of "from angle" and "to angle".
199       double anAngle = theFromAngle + theToAngle;
200       BRepPrimAPI_MakeRevol* aRevolBuilder = new BRepPrimAPI_MakeRevol(aRotatedBaseShape,
201                                                                        anAxis,
202                                                                        anAngle / 180 * M_PI,
203                                                                        Standard_True);
204       aRevolBuilder->Build();
205       if(!aRevolBuilder->IsDone()) {
206         return;
207       }
208       aListOfMakeShape.push_back(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aRevolBuilder)));
209       aResult = aRevolBuilder->Shape();
210
211       // Setting naming.
212       std::shared_ptr<GeomAPI_Shape> aFSHape(new GeomAPI_Shape);
213       aFSHape->setImpl(new TopoDS_Shape(aRevolBuilder->FirstShape()));
214       myFromFaces.push_back(aFSHape);
215       std::shared_ptr<GeomAPI_Shape> aTSHape(new GeomAPI_Shape);
216       aTSHape->setImpl(new TopoDS_Shape(aRevolBuilder->LastShape()));
217       myToFaces.push_back(aTSHape);
218     } else {
219       gp_Pnt aFromPnt = aStartPnt.Transformed(aBaseTrsf);
220       aCircle = gp_Circ(gp_Ax2(aProjection.NearestPoint(), anAxis.Direction(), gp_Vec(aProjection.NearestPoint(), aFromPnt)),
221                         aRadius);
222
223       // Making wire for pipe.
224       TopoDS_Edge aPipeEdge = BRepBuilderAPI_MakeEdge(aCircle, 0, (theFromAngle + theToAngle) / 180.0 * M_PI);
225       TopoDS_Wire aPipeWire = BRepBuilderAPI_MakeWire(aPipeEdge).Wire();
226
227       // Making pipe.
228       BRepOffsetAPI_MakePipe* aPipeBuilder = new BRepOffsetAPI_MakePipe(aPipeWire, aRotatedBaseShape);
229       if(!aPipeBuilder || !aPipeBuilder->IsDone()) {
230         return;
231       }
232       std::shared_ptr<GeomAPI_Shape> aWire(new GeomAPI_Shape);
233       std::shared_ptr<GeomAPI_Shape> aBShape(new GeomAPI_Shape);
234       aWire->setImpl(new TopoDS_Shape(aPipeWire));
235       aBShape->setImpl(new TopoDS_Shape(aRotatedBaseShape));
236       aListOfMakeShape.push_back(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aPipeBuilder, aWire, aBShape)));
237       aResult = aPipeBuilder->Shape();
238       TopoDS_Shape aToShape = aPipeBuilder->LastShape();
239       TopoDS_Shape aFromShape = aPipeBuilder->FirstShape();
240
241       // Setting naming.
242       TopExp_Explorer anExp(aToShape, TopAbs_FACE);
243       for(; anExp.More(); anExp.Next()) {
244         std::shared_ptr<GeomAPI_Shape> aTSHape(new GeomAPI_Shape);
245         aTSHape->setImpl(new TopoDS_Shape(anExp.Current()));
246         myToFaces.push_back(aTSHape);
247       }
248       anExp.Init(aFromShape, TopAbs_FACE);
249       for(; anExp.More(); anExp.Next()) {
250         std::shared_ptr<GeomAPI_Shape> aFSHape(new GeomAPI_Shape);
251         aFSHape->setImpl(new TopoDS_Shape(anExp.Current()));
252         myFromFaces.push_back(aFSHape);
253       }
254     }
255   } else if(theFromShape && theToShape) { // Case 2: When both bounding planes were set.
256     // Getting bounding faces.
257     TopoDS_Face aFromFace = TopoDS::Face(theFromShape->impl<TopoDS_Shape>());
258     TopoDS_Face aToFace   = TopoDS::Face(theToShape->impl<TopoDS_Shape>());
259
260     // Getting planes from bounding face.
261     GeomLib_IsPlanarSurface isFromPlanar(BRep_Tool::Surface(aFromFace));
262     GeomLib_IsPlanarSurface isToPlanar(BRep_Tool::Surface(aToFace));
263     if(!isFromPlanar.IsPlanar() || !isToPlanar.IsPlanar()) {// non-planar shapes is not supported for revolution bounding
264       return;
265     }
266     gp_Pln aFromPln = isFromPlanar.Plan();
267     gp_Pln aToPln   = isToPlanar.Plan();
268
269     // Orienting bounding planes properly so that the center of mass of the base face stays
270     // on the result shape after cut.
271     aFromFace = makeFaceFromPlane(aFromPln, aBasisCentre);
272     aToFace   = makeFaceFromPlane(aToPln, aBasisCentre);
273
274     // Making solids from bounding planes and putting them in compound.
275     TopoDS_Shape aFromSolid = makeSolidFromShape(aFromFace);
276     TopoDS_Shape aToSolid   = makeSolidFromShape(aToFace);
277
278     // Rotating bounding planes to the specified angle.
279     gp_Trsf aFromTrsf;
280     gp_Trsf aToTrsf;
281     double aFromRotAngle = ((aFromPln.Axis().Direction() * aBasisPln.Axis().Direction()) > 0) ? -theFromAngle : theFromAngle;
282     double aToRotAngle = ((aToPln.Axis().Direction() * aBasisPln.Axis().Direction()) > 0) ? -theToAngle : theToAngle;
283     aFromTrsf.SetRotation(anAxis,aFromRotAngle / 180.0 * M_PI);
284     aToTrsf.SetRotation(anAxis, aToRotAngle / 180.0 * M_PI);
285     BRepBuilderAPI_Transform aFromTransform(aFromSolid, aFromTrsf, true);
286     BRepBuilderAPI_Transform aToTransform(aToSolid, aToTrsf, true);
287     TopoDS_Shape aRotatedFromFace = aFromTransform.Modified(aFromFace).First();
288     TopoDS_Shape aRotatedToFace = aToTransform.Modified(aToFace).First();
289     aFromSolid = aFromTransform.Shape();
290     aToSolid = aToTransform.Shape();
291
292     // Making revolution to the 360 angle.
293     if(theBasis->shapeType() == GeomAPI_Shape::FACE) {
294       BRepPrimAPI_MakeRevol* aRevolBuilder = new BRepPrimAPI_MakeRevol(aBasisShape, anAxis, 2 * M_PI, Standard_True);
295       aRevolBuilder->Build();
296       aListOfMakeShape.push_back(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aRevolBuilder)));
297       aResult = aRevolBuilder->Shape();
298     } else {
299       // Making wire for pipe.
300       TopoDS_Edge aPipeEdge = BRepBuilderAPI_MakeEdge(aCircle, 0, 2 * M_PI);
301       TopoDS_Wire aPipeWire = BRepBuilderAPI_MakeWire(aPipeEdge).Wire();
302
303       // Making pipe.
304       BRepOffsetAPI_MakePipe* aPipeBuilder = new BRepOffsetAPI_MakePipe(aPipeWire, aBasisShape);
305       if(!aPipeBuilder || !aPipeBuilder->IsDone()) {
306         return;
307       }
308       std::shared_ptr<GeomAPI_Shape> aWire(new GeomAPI_Shape);
309       std::shared_ptr<GeomAPI_Shape> aBShape(new GeomAPI_Shape);
310       aWire->setImpl(new TopoDS_Shape(aPipeWire));
311       aBShape->setImpl(new TopoDS_Shape(aBasisShape));
312       aListOfMakeShape.push_back(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aPipeBuilder, aWire, aBShape)));
313       aResult = aPipeBuilder->Shape();
314     }
315
316     // Cutting revolution with from plane.
317     BRepAlgoAPI_Cut* aFromCutBuilder = new BRepAlgoAPI_Cut(aResult, aFromSolid);
318     aFromCutBuilder->Build();
319     if(!aFromCutBuilder->IsDone()) {
320       return;
321     }
322     aListOfMakeShape.push_back(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aFromCutBuilder)));
323     aResult = aFromCutBuilder->Shape();
324
325     // Cutting revolution with to plane.
326     BRepAlgoAPI_Cut* aToCutBuilder = new BRepAlgoAPI_Cut(aResult, aToSolid);
327     aToCutBuilder->Build();
328     if(!aToCutBuilder->IsDone()) {
329       return;
330     }
331     aListOfMakeShape.push_back(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aToCutBuilder)));
332     aResult = aToCutBuilder->Shape();
333
334     // If after cut we got more than one solids then take closest to the center of mass of the base face.
335     aResult = findClosest(aResult, aBasisCentre);
336
337     // Setting naming.
338     for(TopExp_Explorer anExp(aResult, TopAbs_FACE); anExp.More (); anExp.Next ()) {
339       const TopoDS_Shape& aFaceOnResult = anExp.Current();
340       Handle(Geom_Surface) aFaceSurface = BRep_Tool::Surface(TopoDS::Face(aFaceOnResult));
341       Handle(Geom_Surface) aFromSurface = BRep_Tool::Surface(TopoDS::Face(aRotatedFromFace));
342       Handle(Geom_Surface) aToSurface = BRep_Tool::Surface(TopoDS::Face(aRotatedToFace));
343       if(aFaceSurface == aFromSurface) {
344         std::shared_ptr<GeomAPI_Shape> aFSHape(new GeomAPI_Shape);
345         aFSHape->setImpl(new TopoDS_Shape(aFaceOnResult));
346         myFromFaces.push_back(aFSHape);
347       }
348       if(aFaceSurface == aToSurface) {
349         std::shared_ptr<GeomAPI_Shape> aTSHape(new GeomAPI_Shape);
350         aTSHape->setImpl(new TopoDS_Shape(aFaceOnResult));
351         myToFaces.push_back(aTSHape);
352       }
353     }
354   } else { //Case 3: When only one bounding plane was set.
355     // Getting bounding face.
356     TopoDS_Face aBoundingFace;
357     bool isFromFaceSet = false;
358     if(theFromShape) {
359       aBoundingFace = TopoDS::Face(theFromShape->impl<TopoDS_Shape>());
360       isFromFaceSet = true;
361     } else if(theToShape) {
362       aBoundingFace = TopoDS::Face(theToShape->impl<TopoDS_Shape>());
363     }
364
365     // Getting plane from bounding face.
366     GeomLib_IsPlanarSurface isBoundingPlanar(BRep_Tool::Surface(aBoundingFace));
367     if(!isBoundingPlanar.IsPlanar()) { // non-planar shapes is not supported for revolution bounding
368       return;
369     }
370     gp_Pln aBoundingPln = isBoundingPlanar.Plan();
371
372     // Orienting bounding plane properly so that the center of mass of the base face stays
373     // on the result shape after cut.
374     gp_Pnt aBasisCentr = GeomAlgoAPI_ShapeTools::centreOfMass(theBasis)->impl<gp_Pnt>();
375     aBoundingFace = makeFaceFromPlane(aBoundingPln, aBasisCentr);
376
377     // Making solid from bounding plane.
378     TopoDS_Shape aBoundingSolid = makeSolidFromShape(aBoundingFace);
379
380     // Rotating bounding plane to the specified angle.
381     double aBoundingRotAngle = isFromFaceSet ? theFromAngle : theToAngle;
382     if(aBoundingPln.Axis().IsParallel(aBasisPln.Axis(), Precision::Confusion())) {
383       if(isFromFaceSet) aBoundingRotAngle = -aBoundingRotAngle;
384     } else {
385       double aSign = (aBoundingPln.Axis().Direction() ^ aBasisPln.Axis().Direction()) *
386                      anAxis.Direction();
387       if((aSign <= 0 && !isFromFaceSet) || (aSign > 0 && isFromFaceSet)) {
388         aBoundingRotAngle = -aBoundingRotAngle;
389       }
390     }
391     gp_Trsf aBoundingTrsf;
392     aBoundingTrsf.SetRotation(anAxis, aBoundingRotAngle / 180.0 * M_PI);
393     BRepBuilderAPI_Transform aBoundingTransform(aBoundingSolid, aBoundingTrsf, true);
394     TopoDS_Shape aRotatedBoundingFace = aBoundingTransform.Modified(aBoundingFace).First();
395     aBoundingSolid = aBoundingTransform.Shape();
396
397     // Making revolution to the 360 angle.
398     if(theBasis->shapeType() == GeomAPI_Shape::FACE) {
399       BRepPrimAPI_MakeRevol* aRevolBuilder = new BRepPrimAPI_MakeRevol(aBasisShape, anAxis, 2 * M_PI, Standard_True);
400       aRevolBuilder->Build();
401       aListOfMakeShape.push_back(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aRevolBuilder)));
402       aResult = aRevolBuilder->Shape();
403     } else {
404       // Making wire for pipe.
405       TopoDS_Edge aPipeEdge = BRepBuilderAPI_MakeEdge(aCircle, 0, 2 * M_PI);
406       TopoDS_Wire aPipeWire = BRepBuilderAPI_MakeWire(aPipeEdge).Wire();
407
408       // Making pipe.
409       BRepOffsetAPI_MakePipe* aPipeBuilder = new BRepOffsetAPI_MakePipe(aPipeWire, aBasisShape);
410       if(!aPipeBuilder || !aPipeBuilder->IsDone()) {
411         return;
412       }
413       std::shared_ptr<GeomAPI_Shape> aWire(new GeomAPI_Shape);
414       std::shared_ptr<GeomAPI_Shape> aBShape(new GeomAPI_Shape);
415       aWire->setImpl(new TopoDS_Shape(aPipeWire));
416       aBShape->setImpl(new TopoDS_Shape(aBasisShape));
417       aListOfMakeShape.push_back(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aPipeBuilder, aWire, aBShape)));
418       aResult = aPipeBuilder->Shape();
419     }
420
421     // Cutting revolution with bounding plane.
422     BRepAlgoAPI_Cut* aBoundingCutBuilder = new BRepAlgoAPI_Cut(aResult, aBoundingSolid);
423     aBoundingCutBuilder->Build();
424     if(!aBoundingCutBuilder->IsDone()) {
425       return;
426     }
427     aListOfMakeShape.push_back(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aBoundingCutBuilder)));
428     aResult = aBoundingCutBuilder->Shape();
429
430     // Setting naming.
431     const TopTools_ListOfShape& aBndShapes = aBoundingCutBuilder->Modified(aBoundingFace);
432     for(TopTools_ListIteratorOfListOfShape anIt(aBndShapes); anIt.More(); anIt.Next()) {
433       std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
434       aShape->setImpl(new TopoDS_Shape(anIt.Value()));
435       isFromFaceSet ? myFromFaces.push_back(aShape) : myToFaces.push_back(aShape);
436     }
437
438     // Try to cut with base face. If it can not be done then keep result of cut with bounding plane.
439     if(isFromFaceSet) {
440       gp_Trsf aMirrorTrsf;
441       aMirrorTrsf.SetMirror(aBasisPlane.Position().Ax2());
442       BRepBuilderAPI_Transform aMirrorTransform(aBasis, aMirrorTrsf, true);
443       aBasis = aMirrorTransform.Shape();
444     }
445
446     // Making solid from basis face.
447     TopoDS_Shape aBasisSolid = makeSolidFromShape(aBasis);
448
449     // Rotating basis face to the specified angle.
450     gp_Trsf aBasisTrsf;
451     double aBasisRotAngle = isFromFaceSet ? theToAngle : -theFromAngle;
452     aBasisTrsf.SetRotation(anAxis, aBasisRotAngle / 180.0 * M_PI);
453     BRepBuilderAPI_Transform aBasisTransform(aBasisSolid, aBasisTrsf, true);
454     TopoDS_Shape aRotatedBasis = aBasisTransform.Modified(aBasis).First();
455     aBasisSolid = aBasisTransform.Shape();
456
457     // Cutting revolution with basis.
458     BRepAlgoAPI_Cut* aBasisCutBuilder = new BRepAlgoAPI_Cut(aResult, aBasisSolid);
459     aBasisCutBuilder->Build();
460     if(aBasisCutBuilder->IsDone()) {
461       TopoDS_Shape aCutResult = aBasisCutBuilder->Shape();
462       TopExp_Explorer anExp(aCutResult, TopAbs_SOLID);
463       if(anExp.More()) {
464         aListOfMakeShape.push_back(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aBasisCutBuilder)));
465         aResult = aCutResult;
466       }
467     }
468
469     const TopTools_ListOfShape& aBsShapes = aBasisCutBuilder->Modified(aBoundingFace);
470     for(TopTools_ListIteratorOfListOfShape anIt(aBsShapes); anIt.More(); anIt.Next()) {
471       std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
472       aShape->setImpl(new TopoDS_Shape(anIt.Value()));
473       isFromFaceSet ? myToFaces.push_back(aShape) : myFromFaces.push_back(aShape);
474     }
475
476     // If after cut we got more than one solids then take closest to the center of mass of the base face.
477     aResult = findClosest(aResult, aBasisCentr);
478
479     // Setting naming.
480     for(TopExp_Explorer anExp(aResult, TopAbs_FACE); anExp.More (); anExp.Next ()) {
481       const TopoDS_Shape& aFaceOnResult = anExp.Current();
482       Handle(Geom_Surface) aFaceSurface = BRep_Tool::Surface(TopoDS::Face(aFaceOnResult));
483       Handle(Geom_Surface) aBoundingSurface = BRep_Tool::Surface(TopoDS::Face(aRotatedBoundingFace));
484       if(aFaceSurface == aBoundingSurface) {
485         std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
486         aShape->setImpl(new TopoDS_Shape(aFaceOnResult));
487         isFromFaceSet ? myFromFaces.push_back(aShape) : myToFaces.push_back(aShape);
488       }
489     }
490   }
491
492   TopExp_Explorer anExp(aResult, TopAbs_SOLID);
493   if(!anExp.More()) {
494     return;
495   }
496   if(aResult.ShapeType() == TopAbs_COMPOUND) {
497     aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
498   }
499   if(aResult.ShapeType() == TopAbs_COMPOUND) {
500     std::shared_ptr<GeomAPI_Shape> aCompound(new GeomAPI_Shape);
501     aCompound->setImpl(new TopoDS_Shape(aResult));
502     ListOfShape aCompSolids, aFreeSolids;
503     GeomAlgoAPI_ShapeTools::combineShapes(aCompound, GeomAPI_Shape::COMPSOLID, aCompSolids, aFreeSolids);
504     if(aCompSolids.size() == 1 && aFreeSolids.size() == 0) {
505       aResult = aCompSolids.front()->impl<TopoDS_Shape>();
506     } else if (aCompSolids.size() > 1 || (aCompSolids.size() >= 1 && aFreeSolids.size() >= 1)) {
507       TopoDS_Compound aResultComp;
508       TopoDS_Builder aBuilder;
509       aBuilder.MakeCompound(aResultComp);
510       for(ListOfShape::const_iterator anIter = aCompSolids.cbegin(); anIter != aCompSolids.cend(); anIter++) {
511         aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
512       }
513       for(ListOfShape::const_iterator anIter = aFreeSolids.cbegin(); anIter != aFreeSolids.cend(); anIter++) {
514         aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
515       }
516       aResult = aResultComp;
517     }
518   }
519
520   // fill data map to keep correct orientation of sub-shapes
521   myMap = std::shared_ptr<GeomAPI_DataMapOfShapeShape>(new GeomAPI_DataMapOfShapeShape());
522   for (TopExp_Explorer Exp(aResult,TopAbs_FACE); Exp.More(); Exp.Next()) {
523     std::shared_ptr<GeomAPI_Shape> aCurrentShape(new GeomAPI_Shape());
524     aCurrentShape->setImpl(new TopoDS_Shape(Exp.Current()));
525     myMap->bind(aCurrentShape, aCurrentShape);
526   }
527   myShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
528   myShape->setImpl(new TopoDS_Shape(aResult));
529   myMkShape = std::shared_ptr<GeomAlgoAPI_MakeShapeList>(new GeomAlgoAPI_MakeShapeList(aListOfMakeShape));
530   myDone = true;
531 }
532
533 //=================================================================================================
534 const bool GeomAlgoAPI_Revolution::isDone() const
535 {
536   return myDone;
537 }
538
539 //=================================================================================================
540 const bool GeomAlgoAPI_Revolution::isValid() const
541 {
542   BRepCheck_Analyzer aChecker(myShape->impl<TopoDS_Shape>());
543   return (aChecker.IsValid() == Standard_True);
544 }
545
546 //=================================================================================================
547 const bool GeomAlgoAPI_Revolution::hasVolume() const
548 {
549   bool hasVolume(false);
550   if(isValid()) {
551     const TopoDS_Shape& aRShape = myShape->impl<TopoDS_Shape>();
552     GProp_GProps aGProp;
553     BRepGProp::VolumeProperties(aRShape, aGProp);
554     if(aGProp.Mass() > Precision::Confusion())
555       hasVolume = true;
556   }
557   return hasVolume;
558 }
559
560 //=================================================================================================
561 const std::shared_ptr<GeomAPI_Shape>& GeomAlgoAPI_Revolution::shape () const
562 {
563   return myShape;
564 }
565
566 //=================================================================================================
567 const ListOfShape& GeomAlgoAPI_Revolution::fromFaces() const
568 {
569   return myFromFaces;
570 }
571
572 //=================================================================================================
573 const ListOfShape& GeomAlgoAPI_Revolution::toFaces() const
574 {
575   return myToFaces;
576 }
577
578 //=================================================================================================
579 std::shared_ptr<GeomAPI_DataMapOfShapeShape> GeomAlgoAPI_Revolution::mapOfShapes() const
580 {
581   return myMap;
582 }
583
584 //=================================================================================================
585 std::shared_ptr<GeomAlgoAPI_MakeShape> GeomAlgoAPI_Revolution::makeShape() const
586 {
587   return myMkShape;
588 }