Salome HOME
Debug (Google style).
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Symmetry.cpp
1 // Copyright (C) 2014-2016 CEA/DEN, EDF R&D
2
3 // File:        GeomAlgoAPI_Symmetry.cpp
4 // Created:     30 Nov 2016
5 // Author:      Clarisse Genrault (CEA)
6
7 #include "GeomAlgoAPI_Symmetry.h"
8
9 #include <BRepBuilderAPI_Transform.hxx>
10
11 //=================================================================================================
12 GeomAlgoAPI_Symmetry::GeomAlgoAPI_Symmetry(std::shared_ptr<GeomAPI_Shape> theSourceShape,
13                                            std::shared_ptr<GeomAPI_Pnt>   thePoint)
14 {
15   myMethodType = BY_POINT;
16   mySourceShape = theSourceShape;
17   myPoint = thePoint;
18 }
19
20 //=================================================================================================
21 GeomAlgoAPI_Symmetry::GeomAlgoAPI_Symmetry(std::shared_ptr<GeomAPI_Shape> theSourceShape,
22                                            std::shared_ptr<GeomAPI_Ax1>   theAxis)
23 {
24   myMethodType = BY_AXIS;
25   mySourceShape = theSourceShape;
26   myAxis = theAxis;
27 }
28
29 //=================================================================================================
30 GeomAlgoAPI_Symmetry::GeomAlgoAPI_Symmetry(std::shared_ptr<GeomAPI_Shape> theSourceShape,
31                                            std::shared_ptr<GeomAPI_Ax2>   thePlane)
32 {
33   myMethodType = BY_PLANE;
34   mySourceShape = theSourceShape;
35   myPlane = thePlane;
36 }
37
38 //=================================================================================================
39 bool GeomAlgoAPI_Symmetry::check()
40 {
41   switch (myMethodType) {
42     case BY_POINT: {
43       if (!myPoint) {
44         myError = "Mirror builder :: point is invalid.";
45         return false;
46       }
47       if (!mySourceShape) {
48         myError = "Mirror builder :: source shape is invalid.";
49         return false;
50       }
51       return true;
52     }
53     case BY_AXIS: {
54       if (!myAxis) {
55         myError = "Mirror builder :: axis is invalid.";
56         return false;
57       }
58       if (!mySourceShape) {
59         myError = "Mirror builder :: source shape is invalid.";
60         return false;
61       }
62       return true;
63     }
64     case BY_PLANE: {
65       if (!myPlane) {
66         myError = "Mirror builder :: plane is invalid.";
67         return false;
68       }
69       if (!mySourceShape) {
70         myError = "Translation builder :: source shape is invalid.";
71         return false;
72       }
73       return true;
74     }
75     default: {
76       myError = "Translation builder :: method not implemented.";
77       return false;
78     }
79   }
80 }
81
82 //=================================================================================================
83 void GeomAlgoAPI_Symmetry::build()
84 {
85   gp_Trsf* aTrsf = new gp_Trsf();
86
87   switch (myMethodType) {
88     case BY_POINT: {
89       const gp_Pnt& aPoint = myPoint->impl<gp_Pnt>();
90       aTrsf->SetMirror(aPoint);
91       break;
92     }
93     case BY_AXIS: {
94       const gp_Ax1& anAxis = myAxis->impl<gp_Ax1>();
95       aTrsf->SetMirror(anAxis);
96       break;
97     }
98     case BY_PLANE: {
99       const gp_Ax2& aPlane = myPlane->impl<gp_Ax2>();
100       aTrsf->SetMirror(aPlane);
101       break;
102     }
103     default: {
104       myError = "Mirror builder :: method not supported";
105       return;
106     }
107   }
108
109   const TopoDS_Shape& aSourceShape = mySourceShape->impl<TopoDS_Shape>();
110
111   if(aSourceShape.IsNull()) {
112     myError = "Mirror builder :: source shape does not contain any actual shape.";
113     return;
114   }
115
116   // Transform the shape while copying it.
117   BRepBuilderAPI_Transform* aBuilder = new BRepBuilderAPI_Transform(aSourceShape, *aTrsf, true);
118   if(!aBuilder) {
119     myError = "Mirror builder :: source shape does not contain any actual shape.";
120     return;
121   }
122
123   setImpl(aBuilder);
124   setBuilderType(OCCT_BRepBuilderAPI_MakeShape);
125
126   if(!aBuilder->IsDone()) {
127     myError = "Mirror builder :: source shape does not contain any actual shape.";
128     return;
129   }
130
131   TopoDS_Shape aResult = aBuilder->Shape();
132
133   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
134   aShape->setImpl(new TopoDS_Shape(aResult));
135   setShape(aShape);
136   setDone(true);
137 }