Salome HOME
Update copyrights
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Translation.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "GeomAlgoAPI_Translation.h"
21
22 #include <BRepBuilderAPI_Transform.hxx>
23 #include <Precision.hxx>
24 #include <gp_Ax1.hxx>
25
26 //=================================================================================================
27 GeomAlgoAPI_Translation::GeomAlgoAPI_Translation(std::shared_ptr<GeomAPI_Shape> theSourceShape,
28                                                  std::shared_ptr<GeomAPI_Ax1>   theAxis,
29                                                  double                         theDistance)
30 {
31   myMethodType = BY_DISTANCE;
32   mySourceShape = theSourceShape;
33   myAxis = theAxis;
34   myDistance = theDistance;
35 }
36
37 //=================================================================================================
38 GeomAlgoAPI_Translation::GeomAlgoAPI_Translation(std::shared_ptr<GeomAPI_Shape> theSourceShape,
39                                                  double                         theDx,
40                                                  double                         theDy,
41                                                  double                         theDz)
42 {
43   myMethodType = BY_DIM;
44   mySourceShape = theSourceShape;
45   myDx = theDx;
46   myDy = theDy;
47   myDz = theDz;
48 }
49
50 //=================================================================================================
51 GeomAlgoAPI_Translation::GeomAlgoAPI_Translation(std::shared_ptr<GeomAPI_Shape> theSourceShape,
52                                                  std::shared_ptr<GeomAPI_Pnt>   theStartPoint,
53                                                  std::shared_ptr<GeomAPI_Pnt>   theEndPoint)
54 {
55   myMethodType = BY_POINTS;
56   mySourceShape = theSourceShape;
57   myStartPoint = theStartPoint;
58   myEndPoint = theEndPoint;
59 }
60
61 //=================================================================================================
62 bool GeomAlgoAPI_Translation::check()
63 {
64   switch (myMethodType) {
65     case BY_DISTANCE: {
66       if (!myAxis) {
67         myError = "Translation builder :: axis is not valid.";
68         return false;
69       }
70       if (!mySourceShape) {
71         myError = "Translation builder :: source shape is not valid.";
72         return false;
73       }
74       return true;
75     }
76     case BY_DIM: {
77       if (!mySourceShape) {
78         myError = "Translation builder :: source shape is not valid.";
79         return false;
80       }
81       return true;
82     }
83     case BY_POINTS: {
84       if (!myStartPoint) {
85         myError = "Translation builder :: start point is not valid.";
86         return false;
87       }
88       if (!myEndPoint) {
89         myError = "Translation builder :: end point is not valid.";
90         return false;
91       }
92       if (!mySourceShape) {
93         myError = "Translation builder :: source shape is invalid.";
94         return false;
95       }
96       if(myStartPoint->distance(myEndPoint) < Precision::Confusion()) {
97         myError = "Translation builder :: start point and end point coincide.";
98         return false;
99       }
100       return true;
101     }
102     default: {
103       myError = "Translation builder :: method not implemented.";
104       return false;
105     }
106   }
107 }
108
109 //=================================================================================================
110 void GeomAlgoAPI_Translation::build()
111 {
112   gp_Trsf* aTrsf = new gp_Trsf();
113
114   switch (myMethodType) {
115     case BY_DISTANCE: {
116       const gp_Ax1& anAxis = myAxis->impl<gp_Ax1>();
117       aTrsf->SetTranslation(gp_Vec(anAxis.Direction()) * myDistance);
118       break;
119     }
120     case BY_DIM: {
121       aTrsf->SetTranslation(gp_Vec(myDx, myDy, myDz));
122       break;
123     }
124     case BY_POINTS: {
125       const gp_Pnt& aStartPoint = myStartPoint->impl<gp_Pnt>();
126       const gp_Pnt& aEndPoint = myEndPoint->impl<gp_Pnt>();
127       aTrsf->SetTranslation(aStartPoint, aEndPoint);
128       break;
129     }
130     default: {
131       myError = "Translation builder :: method not supported";
132       return;
133     }
134   }
135
136   const TopoDS_Shape& aSourceShape = mySourceShape->impl<TopoDS_Shape>();
137
138   if(aSourceShape.IsNull()) {
139     myError = "Translation builder :: source shape does not contain any actual shape.";
140     return;
141   }
142
143   // Transform the shape while copying it.
144   BRepBuilderAPI_Transform* aBuilder = new BRepBuilderAPI_Transform(aSourceShape, *aTrsf, true);
145   if(!aBuilder) {
146     myError = "Translation builder :: transform initialization failed.";
147     return;
148   }
149
150   setImpl(aBuilder);
151   setBuilderType(OCCT_BRepBuilderAPI_MakeShape);
152
153   if(!aBuilder->IsDone()) {
154     myError = "Translation builder :: algorithm failed.";
155     return;
156   }
157
158   TopoDS_Shape aResult = aBuilder->Shape();
159
160   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
161   aShape->setImpl(new TopoDS_Shape(aResult));
162   setShape(aShape);
163   setDone(true);
164 }