Salome HOME
Merge branch 'master' into cgt/devCEA
[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 GeomAlgoAPI_Translation::GeomAlgoAPI_Translation(std::shared_ptr<GeomAPI_Shape> theSourceShape,
41                                                  std::shared_ptr<GeomAPI_Pnt>   theStartPoint,
42                                                  std::shared_ptr<GeomAPI_Pnt>   theEndPoint)
43 {
44   myMethodType = BY_POINTS;
45   mySourceShape = theSourceShape;
46   myStartPoint = theStartPoint;
47   myEndPoint = theEndPoint;
48 }
49
50 //=================================================================================================
51 bool GeomAlgoAPI_Translation::check()
52 {
53   switch (myMethodType) {
54     case BY_DISTANCE: {
55       if (!myAxis) {
56         myError = "Translation builder :: axis is invalid.";
57         return false;
58       }
59       if (!mySourceShape) {
60         myError = "Translation builder :: source shape is invalid.";
61         return false;
62       }
63       return true;
64     }
65     case BY_DIM: {
66       if (!mySourceShape) {
67         myError = "Translation builder :: source shape is invalid.";
68         return false;
69       }
70       return true;
71     }
72     case BY_POINTS: {
73       if (!myStartPoint) {
74         myError = "Translation builder :: start point is invalid.";
75         return false;
76       }
77       if (!myEndPoint) {
78         myError = "Translation builder :: start point is invalid.";
79         return false;
80       }
81       if (!mySourceShape) {
82         myError = "Translation builder :: source shape is invalid.";
83         return false;
84       }
85       return true;
86     }
87     default: {
88       myError = "Translation builder :: method not implemented.";
89       return false;
90     }
91   }
92 }
93
94 //=================================================================================================
95 void GeomAlgoAPI_Translation::build()
96 {
97   gp_Trsf* aTrsf = new gp_Trsf();
98
99   switch (myMethodType) {
100     case BY_DISTANCE: {
101       const gp_Ax1& anAxis = myAxis->impl<gp_Ax1>();
102       aTrsf->SetTranslation(gp_Vec(anAxis.Direction()) * myDistance);
103       break;
104     }
105     case BY_DIM: {
106       aTrsf->SetTranslation(gp_Vec(myDx, myDy, myDz));
107       break;
108     }
109     case BY_POINTS: {
110       const gp_Pnt& aStartPoint = myStartPoint->impl<gp_Pnt>();
111       const gp_Pnt& aEndPoint = myEndPoint->impl<gp_Pnt>();
112       aTrsf->SetTranslation(aStartPoint, aEndPoint);
113       break;
114     }
115     default: {
116       myError = "Translation builder :: method not supported";
117       return;
118     }
119   }
120
121   const TopoDS_Shape& aSourceShape = mySourceShape->impl<TopoDS_Shape>();
122
123   if(aSourceShape.IsNull()) {
124     myError = "Translation builder :: source shape does not contain any actual shape.";
125     return;
126   }
127
128   // Transform the shape while copying it.
129   BRepBuilderAPI_Transform* aBuilder = new BRepBuilderAPI_Transform(aSourceShape, *aTrsf, true);
130   if(!aBuilder) {
131     myError = "Translation builder :: source shape does not contain any actual shape.";
132     return;
133   }
134
135   setImpl(aBuilder);
136   setBuilderType(OCCT_BRepBuilderAPI_MakeShape);
137
138   if(!aBuilder->IsDone()) {
139     myError = "Translation builder :: source shape does not contain any actual shape.";
140     return;
141   }
142
143   TopoDS_Shape aResult = aBuilder->Shape();
144
145   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
146   aShape->setImpl(new TopoDS_Shape(aResult));
147   setShape(aShape);
148   setDone(true);
149 }