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