Salome HOME
Correction following Mikhail's mail (21/11/2016).
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Translation.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomAlgoAPI_Translation.cpp
4 // Created:     8 June 2015
5 // Author:      Dmitry Bobylev
6 //
7 // Modified by Clarisse Genrault (CEA) : 17 Nov 2016
8
9 #include "GeomAlgoAPI_Translation.h"
10
11 #include <BRepBuilderAPI_Transform.hxx>
12 #include <Precision.hxx>
13 #include <gp_Ax1.hxx>
14
15 //=================================================================================================
16 GeomAlgoAPI_Translation::GeomAlgoAPI_Translation(std::shared_ptr<GeomAPI_Shape> theSourceShape,
17                                                  std::shared_ptr<GeomAPI_Ax1>   theAxis,
18                                                  double                         theDistance)
19 {
20   myMethodType = BY_DISTANCE;
21   mySourceShape = theSourceShape;
22   myAxis = theAxis;
23   myDistance = theDistance;
24 }
25
26 //=================================================================================================
27 GeomAlgoAPI_Translation::GeomAlgoAPI_Translation(std::shared_ptr<GeomAPI_Shape> theSourceShape,
28                                                  double                         theDx,
29                                                  double                         theDy,
30                                                  double                         theDz)
31 {
32   myMethodType = BY_DIM;
33   mySourceShape = theSourceShape;
34   myDx = theDx;
35   myDy = theDy;
36   myDz = theDz;
37 }
38
39 //=================================================================================================
40 bool GeomAlgoAPI_Translation::check()
41 {
42   switch (myMethodType) {
43     case BY_DISTANCE: {
44       if (!myAxis) {
45         myError = "Translation builder :: axis is invalid.";
46         return false;
47       }
48       if (!mySourceShape) {
49         myError = "Translation builder :: source shape is invalid.";
50         return false;
51       }
52       return true;
53     }
54     case BY_DIM: {
55       if (!mySourceShape) {
56         myError = "Translation builder :: source shape is invalid.";
57         return false;
58       }
59       return true;
60     }
61     default: {
62       myError = "Translation builder :: method not implemented.";
63       return false;
64     }
65   }
66 }
67
68 //=================================================================================================
69 void GeomAlgoAPI_Translation::build()
70 {
71   gp_Trsf* aTrsf = new gp_Trsf();
72
73   switch (myMethodType) {
74     case BY_DISTANCE: {
75       const gp_Ax1& anAxis = myAxis->impl<gp_Ax1>();
76       aTrsf->SetTranslation(gp_Vec(anAxis.Direction()) * myDistance);
77       break;
78     }
79     case BY_DIM: {
80       aTrsf->SetTranslation(gp_Vec(myDx, myDy, myDz));
81       break;
82     }
83     default: {
84       myError = "Translation builder :: method not supported";
85       return;
86     }
87   }
88
89   const TopoDS_Shape& aSourceShape = mySourceShape->impl<TopoDS_Shape>();
90
91   if(aSourceShape.IsNull()) {
92     myError = "Translation builder :: source shape does not contain any actual shape.";
93     return;
94   }
95
96   // Transform the shape while copying it.
97   BRepBuilderAPI_Transform* aBuilder = new BRepBuilderAPI_Transform(aSourceShape, *aTrsf, true);
98   if(!aBuilder) {
99     myError = "Translation builder :: source shape does not contain any actual shape.";
100     return;
101   }
102
103   setImpl(aBuilder);
104   setBuilderType(OCCT_BRepBuilderAPI_MakeShape);
105
106   if(!aBuilder->IsDone()) {
107     myError = "Translation builder :: source shape does not contain any actual shape.";
108     return;
109   }
110
111   TopoDS_Shape aResult = aBuilder->Shape();
112
113   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
114   aShape->setImpl(new TopoDS_Shape(aResult));
115   setShape(aShape);
116   setDone(true);
117 }