Salome HOME
Translation and Rotation code clean up
[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_ShapeTools.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 {
22   build(theSourceShape, theAxis, theAngle);
23 }
24
25 //=================================================================================================
26 void GeomAlgoAPI_Rotation::build(std::shared_ptr<GeomAPI_Shape> theSourceShape,
27                                  std::shared_ptr<GeomAPI_Ax1>   theAxis,
28                                  double                         theAngle)
29 {
30   if(!theSourceShape || !theAxis) {
31     return;
32   }
33
34   const TopoDS_Shape& aSourceShape = theSourceShape->impl<TopoDS_Shape>();
35   const gp_Ax1& anAxis = theAxis->impl<gp_Ax1>();
36
37   if(aSourceShape.IsNull()) {
38     return;
39   }
40
41   gp_Trsf* aTrsf = new gp_Trsf();
42   aTrsf->SetRotation(anAxis, theAngle / 180.0 * M_PI);
43   myTrsf.reset(new GeomAPI_Trsf(aTrsf));
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   myMkShape.reset(new GeomAlgoAPI_MakeShape(aBuilder));
51
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   myMap.reset(new GeomAPI_DataMapOfShapeShape());
61   for(TopExp_Explorer anExp(aResult, TopAbs_FACE); anExp.More(); anExp.Next()) {
62     std::shared_ptr<GeomAPI_Shape> aCurrentShape(new GeomAPI_Shape());
63     aCurrentShape->setImpl(new TopoDS_Shape(anExp.Current()));
64     myMap->bind(aCurrentShape, aCurrentShape);
65   }
66
67   myShape.reset(new GeomAPI_Shape());
68   myShape->setImpl(new TopoDS_Shape(aResult));
69 }
70
71 //=================================================================================================
72 const bool GeomAlgoAPI_Rotation::isValid() const
73 {
74   BRepCheck_Analyzer aChecker(myShape->impl<TopoDS_Shape>());
75   return (aChecker.IsValid() == Standard_True);
76 }
77
78 //=================================================================================================
79 const bool GeomAlgoAPI_Rotation::hasVolume() const
80 {
81   bool hasVolume(false);
82   if(isValid() && (GeomAlgoAPI_ShapeTools::volume(myShape) > Precision::Confusion())) {
83     hasVolume = true;
84   }
85   return hasVolume;
86 }
87
88 //=================================================================================================
89 const std::shared_ptr<GeomAPI_Shape>& GeomAlgoAPI_Rotation::shape() const
90 {
91   return myShape;
92 }
93
94 //=================================================================================================
95 std::shared_ptr<GeomAPI_DataMapOfShapeShape> GeomAlgoAPI_Rotation::mapOfShapes() const
96 {
97   return myMap;
98 }
99
100 //=================================================================================================
101 std::shared_ptr<GeomAlgoAPI_MakeShape> GeomAlgoAPI_Rotation::makeShape() const
102 {
103   return myMkShape;
104 }
105
106 //=================================================================================================
107 std::shared_ptr<GeomAPI_Trsf> GeomAlgoAPI_Rotation::transformation() const
108 {
109   return myTrsf;
110 }