Salome HOME
Compsolids in boolean operations
[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                                            bool theSimpleTransform)
21 : myDone(false)
22 {
23   build(theSourceShape, theAxis, theAngle, theSimpleTransform);
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                                  bool theSimpleTransform)
31 {
32   if(!theSourceShape || !theAxis) {
33     return;
34   }
35
36   const TopoDS_Shape& aSourceShape = theSourceShape->impl<TopoDS_Shape>();
37   const gp_Ax1& anAxis = theAxis->impl<gp_Ax1>();
38
39   if(aSourceShape.IsNull()) {
40     return;
41   }
42
43   gp_Trsf aTrsf;
44   aTrsf.SetRotation(anAxis, theAngle / 180.0 * M_PI);
45
46   TopoDS_Shape aResult;
47   // Transform the shape with copying it.
48   if (theSimpleTransform) {
49     TopLoc_Location aDelta(aTrsf);
50     aResult = aSourceShape.Moved(aDelta);
51     //myTrsf = std::shared_ptr<GeomAPI_Trsf>(new GeomAPI_Trsf(new gp_Trsf(aTrsf * aSourceShape.Location().Transformation())));
52     myTrsf = std::shared_ptr<GeomAPI_Trsf>(new GeomAPI_Trsf(new gp_Trsf(aTrsf)));
53     myDone = true; // is OK for sure
54   } else {
55     BRepBuilderAPI_Transform* aBuilder = new BRepBuilderAPI_Transform(aSourceShape, aTrsf, true);
56     if(!aBuilder) {
57       return;
58     }
59     myMkShape.reset(new GeomAlgoAPI_MakeShape(aBuilder));
60
61     myDone = aBuilder->IsDone() == Standard_True;
62
63     if(!myDone) {
64       return;
65     }
66
67     aResult = aBuilder->Shape();
68     // Fill data map to keep correct orientation of sub-shapes.
69     myMap.reset(new GeomAPI_DataMapOfShapeShape());
70     for(TopExp_Explorer anExp(aResult, TopAbs_FACE); anExp.More(); anExp.Next()) {
71       std::shared_ptr<GeomAPI_Shape> aCurrentShape(new GeomAPI_Shape());
72       aCurrentShape->setImpl(new TopoDS_Shape(anExp.Current()));
73       myMap->bind(aCurrentShape, aCurrentShape);
74     }
75   }
76
77   myShape.reset(new GeomAPI_Shape());
78   myShape->setImpl(new TopoDS_Shape(aResult));
79 }
80
81 //=================================================================================================
82 const bool GeomAlgoAPI_Rotation::isValid() const
83 {
84   BRepCheck_Analyzer aChecker(myShape->impl<TopoDS_Shape>());
85   return (aChecker.IsValid() == Standard_True);
86 }
87
88 //=================================================================================================
89 const bool GeomAlgoAPI_Rotation::hasVolume() const
90 {
91   bool hasVolume(false);
92   if(isValid() && (GeomAlgoAPI_ShapeTools::volume(myShape) > Precision::Confusion())) {
93     hasVolume = true;
94   }
95   return hasVolume;
96 }
97
98 //=================================================================================================
99 const std::shared_ptr<GeomAPI_Shape>& GeomAlgoAPI_Rotation::shape() const
100 {
101   return myShape;
102 }
103
104 //=================================================================================================
105 std::shared_ptr<GeomAPI_DataMapOfShapeShape> GeomAlgoAPI_Rotation::mapOfShapes() const
106 {
107   return myMap;
108 }
109
110 //=================================================================================================
111 std::shared_ptr<GeomAlgoAPI_MakeShape> GeomAlgoAPI_Rotation::makeShape() const
112 {
113   return myMkShape;
114 }
115
116 //=================================================================================================
117 std::shared_ptr<GeomAPI_Trsf> GeomAlgoAPI_Rotation::transformation() const
118 {
119   return myTrsf;
120 }