Salome HOME
f165e5aa829efb4b484528408610e9266418b905
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Rotation.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomAlgoAPI_Rotation.cpp
4 // Created:     12 May 2015
5 // Author:      Dmitry Bobylev
6
7 #include <GeomAlgoAPI_Rotation.h>
8
9 #include <GeomAlgoAPI_ShapeProps.h>
10
11 #include <BRepBuilderAPI_Transform.hxx>
12 #include <BRepCheck_Analyzer.hxx>
13 #include <Precision.hxx>
14 #include <TopExp_Explorer.hxx>
15
16 //=================================================================================================
17 GeomAlgoAPI_Rotation::GeomAlgoAPI_Rotation(std::shared_ptr<GeomAPI_Shape> theSourceShape,
18                                            std::shared_ptr<GeomAPI_Ax1>   theAxis,
19                                            double                         theAngle)
20 : myDone(false),
21   myShape(new GeomAPI_Shape())
22 {
23   build(theSourceShape, theAxis, theAngle);
24 }
25
26 //=================================================================================================
27 void GeomAlgoAPI_Rotation::build(std::shared_ptr<GeomAPI_Shape> theSourceShape,
28                                  std::shared_ptr<GeomAPI_Ax1>   theAxis,
29                                  double                         theAngle)
30 {
31   if(!theSourceShape || !theAxis) {
32     return;
33   }
34
35   const TopoDS_Shape& aSourceShape = theSourceShape->impl<TopoDS_Shape>();
36   const gp_Ax1& anAxis = theAxis->impl<gp_Ax1>();
37
38   if(aSourceShape.IsNull()) {
39     return;
40   }
41
42   gp_Trsf aTrsf;
43   aTrsf.SetRotation(anAxis, theAngle / 180.0 * M_PI);
44
45   // Transform the shape with copying it.
46   BRepBuilderAPI_Transform* aBuilder = new BRepBuilderAPI_Transform(aSourceShape, aTrsf, true);
47   if(!aBuilder) {
48     return;
49   }
50
51   setImpl(aBuilder);
52   myDone = aBuilder->IsDone() == Standard_True;
53
54   if(!myDone) {
55     return;
56   }
57
58   TopoDS_Shape aResult = aBuilder->Shape();
59   // Fill data map to keep correct orientation of sub-shapes.
60   for(TopExp_Explorer anExp(aResult, TopAbs_FACE); anExp.More(); anExp.Next()) {
61     std::shared_ptr<GeomAPI_Shape> aCurrentShape(new GeomAPI_Shape());
62     aCurrentShape->setImpl(new TopoDS_Shape(anExp.Current()));
63     myMap.bind(aCurrentShape, aCurrentShape);
64   }
65
66   myShape->setImpl(new TopoDS_Shape(aResult));
67   myMkShape = new GeomAlgoAPI_MakeShape(aBuilder);
68 }
69
70 //=================================================================================================
71 const bool GeomAlgoAPI_Rotation::isValid() const
72 {
73   BRepCheck_Analyzer aChecker(myShape->impl<TopoDS_Shape>());
74   return (aChecker.IsValid() == Standard_True);
75 }
76
77 //=================================================================================================
78 const bool GeomAlgoAPI_Rotation::hasVolume() const
79 {
80   bool hasVolume(false);
81   if(isValid() && (GeomAlgoAPI_ShapeProps::volume(myShape) > Precision::Confusion())) {
82     hasVolume = true;
83   }
84   return hasVolume;
85 }
86
87 //=================================================================================================
88 const std::shared_ptr<GeomAPI_Shape>& GeomAlgoAPI_Rotation::shape() const
89 {
90   return myShape;
91 }
92
93 //=================================================================================================
94 void GeomAlgoAPI_Rotation::mapOfShapes(GeomAPI_DataMapOfShapeShape& theMap) const
95 {
96   theMap = myMap;
97 }
98
99 //=================================================================================================
100 GeomAlgoAPI_MakeShape* GeomAlgoAPI_Rotation::makeShape() const
101 {
102   return myMkShape;
103 }
104
105 //=================================================================================================
106 GeomAlgoAPI_Rotation::~GeomAlgoAPI_Rotation()
107 {
108   if (myImpl) {
109     myMap.clear();
110   }
111 }