Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Translation.cpp
1 // Copyright (C) 2014-2017  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "GeomAlgoAPI_Translation.h"
22
23 #include <BRepBuilderAPI_Transform.hxx>
24 #include <Precision.hxx>
25 #include <gp_Ax1.hxx>
26
27 //=================================================================================================
28 GeomAlgoAPI_Translation::GeomAlgoAPI_Translation(std::shared_ptr<GeomAPI_Shape> theSourceShape,
29                                                  std::shared_ptr<GeomAPI_Ax1>   theAxis,
30                                                  double                         theDistance)
31 {
32   myMethodType = BY_DISTANCE;
33   mySourceShape = theSourceShape;
34   myAxis = theAxis;
35   myDistance = theDistance;
36 }
37
38 //=================================================================================================
39 GeomAlgoAPI_Translation::GeomAlgoAPI_Translation(std::shared_ptr<GeomAPI_Shape> theSourceShape,
40                                                  double                         theDx,
41                                                  double                         theDy,
42                                                  double                         theDz)
43 {
44   myMethodType = BY_DIM;
45   mySourceShape = theSourceShape;
46   myDx = theDx;
47   myDy = theDy;
48   myDz = theDz;
49 }
50
51 //=================================================================================================
52 GeomAlgoAPI_Translation::GeomAlgoAPI_Translation(std::shared_ptr<GeomAPI_Shape> theSourceShape,
53                                                  std::shared_ptr<GeomAPI_Pnt>   theStartPoint,
54                                                  std::shared_ptr<GeomAPI_Pnt>   theEndPoint)
55 {
56   myMethodType = BY_POINTS;
57   mySourceShape = theSourceShape;
58   myStartPoint = theStartPoint;
59   myEndPoint = theEndPoint;
60 }
61
62 //=================================================================================================
63 bool GeomAlgoAPI_Translation::check()
64 {
65   switch (myMethodType) {
66     case BY_DISTANCE: {
67       if (!myAxis) {
68         myError = "Translation builder :: axis is not valid.";
69         return false;
70       }
71       if (!mySourceShape) {
72         myError = "Translation builder :: source shape is not valid.";
73         return false;
74       }
75       return true;
76     }
77     case BY_DIM: {
78       if (!mySourceShape) {
79         myError = "Translation builder :: source shape is not valid.";
80         return false;
81       }
82       return true;
83     }
84     case BY_POINTS: {
85       if (!myStartPoint) {
86         myError = "Translation builder :: start point is not valid.";
87         return false;
88       }
89       if (!myEndPoint) {
90         myError = "Translation builder :: end point is not valid.";
91         return false;
92       }
93       if (!mySourceShape) {
94         myError = "Translation builder :: source shape is invalid.";
95         return false;
96       }
97       if(myStartPoint->distance(myEndPoint) < Precision::Confusion()) {
98         myError = "Translation builder :: start point and end point coincide.";
99         return false;
100       }
101       return true;
102     }
103     default: {
104       myError = "Translation builder :: method not implemented.";
105       return false;
106     }
107   }
108 }
109
110 //=================================================================================================
111 void GeomAlgoAPI_Translation::build()
112 {
113   gp_Trsf* aTrsf = new gp_Trsf();
114
115   switch (myMethodType) {
116     case BY_DISTANCE: {
117       const gp_Ax1& anAxis = myAxis->impl<gp_Ax1>();
118       aTrsf->SetTranslation(gp_Vec(anAxis.Direction()) * myDistance);
119       break;
120     }
121     case BY_DIM: {
122       aTrsf->SetTranslation(gp_Vec(myDx, myDy, myDz));
123       break;
124     }
125     case BY_POINTS: {
126       const gp_Pnt& aStartPoint = myStartPoint->impl<gp_Pnt>();
127       const gp_Pnt& aEndPoint = myEndPoint->impl<gp_Pnt>();
128       aTrsf->SetTranslation(aStartPoint, aEndPoint);
129       break;
130     }
131     default: {
132       myError = "Translation builder :: method not supported";
133       return;
134     }
135   }
136
137   const TopoDS_Shape& aSourceShape = mySourceShape->impl<TopoDS_Shape>();
138
139   if(aSourceShape.IsNull()) {
140     myError = "Translation builder :: source shape does not contain any actual shape.";
141     return;
142   }
143
144   // Transform the shape while copying it.
145   BRepBuilderAPI_Transform* aBuilder = new BRepBuilderAPI_Transform(aSourceShape, *aTrsf, true);
146   if(!aBuilder) {
147     myError = "Translation builder :: transform initialization failed.";
148     return;
149   }
150
151   setImpl(aBuilder);
152   setBuilderType(OCCT_BRepBuilderAPI_MakeShape);
153
154   if(!aBuilder->IsDone()) {
155     myError = "Translation builder :: algorithm failed.";
156     return;
157   }
158
159   TopoDS_Shape aResult = aBuilder->Shape();
160
161   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
162   aShape->setImpl(new TopoDS_Shape(aResult));
163   setShape(aShape);
164   setDone(true);
165 }