Salome HOME
Add a new method for translation feature : translation by DX, DY, DZ vector.
[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       // TODO : verification de la distance
49       if (!mySourceShape) {
50         myError = "Translation builder :: source shape is invalid.";
51         return false;
52       }
53       return true;
54     }
55     case BY_DIM: {
56       if ((fabs(myDx) < Precision::Confusion()) && 
57           (fabs(myDy) < Precision::Confusion()) && 
58           (fabs(myDz) < Precision::Confusion())) {
59         myError = "Translation builder :: Dx, Dy and Dz are null.";
60         return false;
61       }
62       if (!mySourceShape) {
63         myError = "Translation builder :: source shape is invalid.";
64         return false;
65       }
66       return true;
67     }
68     default: {
69       myError = "Translation builder :: method not implemented.";
70       return false;
71     }
72   }
73 }
74
75 //=================================================================================================
76 void GeomAlgoAPI_Translation::build()
77 {
78   gp_Trsf* aTrsf = new gp_Trsf();
79   
80   switch (myMethodType) {
81     case BY_DISTANCE: {
82       const gp_Ax1& anAxis = myAxis->impl<gp_Ax1>();
83       aTrsf->SetTranslation(gp_Vec(anAxis.Direction()) * myDistance);
84       break;
85     }
86     case BY_DIM: {
87       aTrsf->SetTranslation(gp_Vec(myDx, myDy, myDz));
88       break;
89     }
90     default: {
91       myError = "Translation builder :: method not supported";
92       return;
93     }
94   }
95
96   const TopoDS_Shape& aSourceShape = mySourceShape->impl<TopoDS_Shape>();
97
98   if(aSourceShape.IsNull()) {
99     myError = "Translation builder :: source shape does not contain any actual shape.";
100     return;
101   }
102   
103   // Transform the shape while copying it.
104   BRepBuilderAPI_Transform* aBuilder = new BRepBuilderAPI_Transform(aSourceShape, *aTrsf, true);
105   if(!aBuilder) {
106     myError = "Translation builder :: source shape does not contain any actual shape.";
107     return;
108   }
109
110   setImpl(aBuilder);
111   setBuilderType(OCCT_BRepBuilderAPI_MakeShape);
112
113   if(!aBuilder->IsDone()) {
114     myError = "Translation builder :: source shape does not contain any actual shape.";
115     return;
116   }
117
118   TopoDS_Shape aResult = aBuilder->Shape();
119
120   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
121   aShape->setImpl(new TopoDS_Shape(aResult));
122   setShape(aShape);
123   setDone(true);
124 }