Salome HOME
cb043a4ef0d401fb8857e181326936764b962e2d
[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_ShapeProps.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 : myDone(false),
22   myShape(new GeomAPI_Shape()),
23   myMap(new GeomAPI_DataMapOfShapeShape()),
24   myMkShape(new GeomAlgoAPI_MakeShape())
25 {
26   build(theSourceShape, theAxis, theDistance);
27 }
28
29 //=================================================================================================
30 void GeomAlgoAPI_Movement::build(std::shared_ptr<GeomAPI_Shape> theSourceShape,
31                                  std::shared_ptr<GeomAPI_Ax1>   theAxis,
32                                  double                         theDistance)
33 {
34   if(!theSourceShape || !theAxis) {
35     return;
36   }
37
38   const TopoDS_Shape& aSourceShape = theSourceShape->impl<TopoDS_Shape>();
39   const gp_Ax1& anAxis = theAxis->impl<gp_Ax1>();
40
41   if(aSourceShape.IsNull()) {
42     return;
43   }
44
45   gp_Trsf aTrsf;
46   aTrsf.SetTranslation(gp_Vec(anAxis.Direction()) * theDistance);
47
48   // Transform the shape with copying it.
49   BRepBuilderAPI_Transform* aBuilder = new BRepBuilderAPI_Transform(aSourceShape, aTrsf, true);
50   if(!aBuilder) {
51     return;
52   }
53
54   myDone = aBuilder->IsDone() == Standard_True;
55
56   if(!myDone) {
57     return;
58   }
59
60   TopoDS_Shape aResult = aBuilder->Shape();
61   // Fill data map to keep correct orientation of sub-shapes.
62   for(TopExp_Explorer anExp(aResult, TopAbs_FACE); anExp.More(); anExp.Next()) {
63     std::shared_ptr<GeomAPI_Shape> aCurrentShape(new GeomAPI_Shape());
64     aCurrentShape->setImpl(new TopoDS_Shape(anExp.Current()));
65     myMap->bind(aCurrentShape, aCurrentShape);
66   }
67
68   myShape->setImpl(new TopoDS_Shape(aResult));
69   myMkShape->setImpl(aBuilder);
70 }
71
72 //=================================================================================================
73 const bool GeomAlgoAPI_Movement::isValid() const
74 {
75   BRepCheck_Analyzer aChecker(myShape->impl<TopoDS_Shape>());
76   return (aChecker.IsValid() == Standard_True);
77 }
78
79 //=================================================================================================
80 const bool GeomAlgoAPI_Movement::hasVolume() const
81 {
82   bool hasVolume(false);
83   if(isValid() && (GeomAlgoAPI_ShapeProps::volume(myShape) > Precision::Confusion())) {
84     hasVolume = true;
85   }
86   return hasVolume;
87 }
88
89 //=================================================================================================
90 const std::shared_ptr<GeomAPI_Shape>& GeomAlgoAPI_Movement::shape() const
91 {
92   return myShape;
93 }
94
95 //=================================================================================================
96 std::shared_ptr<GeomAPI_DataMapOfShapeShape> GeomAlgoAPI_Movement::mapOfShapes() const
97 {
98   return myMap;
99 }
100
101 //=================================================================================================
102 std::shared_ptr<GeomAlgoAPI_MakeShape> GeomAlgoAPI_Movement::makeShape() const
103 {
104   return myMkShape;
105 }