Salome HOME
Fix bug on the error message for the translation.
[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 not valid.";
57         return false;
58       }
59       if (!mySourceShape) {
60         myError = "Translation builder :: source shape is not valid.";
61         return false;
62       }
63       return true;
64     }
65     case BY_DIM: {
66       if (!mySourceShape) {
67         myError = "Translation builder :: source shape is not valid.";
68         return false;
69       }
70       return true;
71     }
72     case BY_POINTS: {
73       if (!myStartPoint) {
74         myError = "Translation builder :: start point is not valid.";
75         return false;
76       }
77       if (!myEndPoint) {
78         myError = "Translation builder :: end point is not valid.";
79         return false;
80       }
81       if (!mySourceShape) {
82         myError = "Translation builder :: source shape is invalid.";
83         return false;
84       }
85       if(myStartPoint->distance(myEndPoint) < Precision::Confusion()) {
86         myError = "Translation builder :: start point and end point coincide.";
87         return false;
88       }
89       return true;
90     }
91     default: {
92       myError = "Translation builder :: method not implemented.";
93       return false;
94     }
95   }
96 }
97
98 //=================================================================================================
99 void GeomAlgoAPI_Translation::build()
100 {
101   gp_Trsf* aTrsf = new gp_Trsf();
102
103   switch (myMethodType) {
104     case BY_DISTANCE: {
105       const gp_Ax1& anAxis = myAxis->impl<gp_Ax1>();
106       aTrsf->SetTranslation(gp_Vec(anAxis.Direction()) * myDistance);
107       break;
108     }
109     case BY_DIM: {
110       aTrsf->SetTranslation(gp_Vec(myDx, myDy, myDz));
111       break;
112     }
113     case BY_POINTS: {
114       const gp_Pnt& aStartPoint = myStartPoint->impl<gp_Pnt>();
115       const gp_Pnt& aEndPoint = myEndPoint->impl<gp_Pnt>();
116       aTrsf->SetTranslation(aStartPoint, aEndPoint);
117       break;
118     }
119     default: {
120       myError = "Translation builder :: method not supported";
121       return;
122     }
123   }
124
125   const TopoDS_Shape& aSourceShape = mySourceShape->impl<TopoDS_Shape>();
126
127   if(aSourceShape.IsNull()) {
128     myError = "Translation builder :: source shape does not contain any actual shape.";
129     return;
130   }
131
132   // Transform the shape while copying it.
133   BRepBuilderAPI_Transform* aBuilder = new BRepBuilderAPI_Transform(aSourceShape, *aTrsf, true);
134   if(!aBuilder) {
135     myError = "Translation builder :: transform initialization failed.";
136     return;
137   }
138
139   setImpl(aBuilder);
140   setBuilderType(OCCT_BRepBuilderAPI_MakeShape);
141
142   if(!aBuilder->IsDone()) {
143     myError = "Translation builder :: algorithm failed.";
144     return;
145   }
146
147   TopoDS_Shape aResult = aBuilder->Shape();
148
149   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
150   aShape->setImpl(new TopoDS_Shape(aResult));
151   setShape(aShape);
152   setDone(true);
153 }