Salome HOME
a8d66af6ebd5c65cb2064b732aead186b48ce169
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Symmetry.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 email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
18 //
19
20 #include "GeomAlgoAPI_Symmetry.h"
21
22 #include <BRepBuilderAPI_Transform.hxx>
23
24 //=================================================================================================
25 GeomAlgoAPI_Symmetry::GeomAlgoAPI_Symmetry(std::shared_ptr<GeomAPI_Shape> theSourceShape,
26                                            std::shared_ptr<GeomAPI_Pnt>   thePoint)
27 {
28   myMethodType = BY_POINT;
29   mySourceShape = theSourceShape;
30   myPoint = thePoint;
31 }
32
33 //=================================================================================================
34 GeomAlgoAPI_Symmetry::GeomAlgoAPI_Symmetry(std::shared_ptr<GeomAPI_Shape> theSourceShape,
35                                            std::shared_ptr<GeomAPI_Ax1>   theAxis)
36 {
37   myMethodType = BY_AXIS;
38   mySourceShape = theSourceShape;
39   myAxis = theAxis;
40 }
41
42 //=================================================================================================
43 GeomAlgoAPI_Symmetry::GeomAlgoAPI_Symmetry(std::shared_ptr<GeomAPI_Shape> theSourceShape,
44                                            std::shared_ptr<GeomAPI_Ax2>   thePlane)
45 {
46   myMethodType = BY_PLANE;
47   mySourceShape = theSourceShape;
48   myPlane = thePlane;
49 }
50
51 //=================================================================================================
52 bool GeomAlgoAPI_Symmetry::check()
53 {
54   switch (myMethodType) {
55     case BY_POINT: {
56       if (!myPoint) {
57         myError = "Symmetry builder :: point is not valid.";
58         return false;
59       }
60       if (!mySourceShape) {
61         myError = "Symmetry builder :: source shape is not valid.";
62         return false;
63       }
64       return true;
65     }
66     case BY_AXIS: {
67       if (!myAxis) {
68         myError = "Symmetry builder :: axis is not valid.";
69         return false;
70       }
71       if (!mySourceShape) {
72         myError = "Symmetry builder :: source shape is not valid.";
73         return false;
74       }
75       return true;
76     }
77     case BY_PLANE: {
78       if (!myPlane) {
79         myError = "Symmetry builder :: plane is not valid.";
80         return false;
81       }
82       if (!mySourceShape) {
83         myError = "Symmetry builder :: source shape is not valid.";
84         return false;
85       }
86       return true;
87     }
88     default: {
89       myError = "Symmetry builder :: method not implemented.";
90       return false;
91     }
92   }
93 }
94
95 //=================================================================================================
96 void GeomAlgoAPI_Symmetry::build()
97 {
98   gp_Trsf* aTrsf = new gp_Trsf();
99
100   switch (myMethodType) {
101     case BY_POINT: {
102       const gp_Pnt& aPoint = myPoint->impl<gp_Pnt>();
103       aTrsf->SetMirror(aPoint);
104       break;
105     }
106     case BY_AXIS: {
107       const gp_Ax1& anAxis = myAxis->impl<gp_Ax1>();
108       aTrsf->SetMirror(anAxis);
109       break;
110     }
111     case BY_PLANE: {
112       const gp_Ax2& aPlane = myPlane->impl<gp_Ax2>();
113       aTrsf->SetMirror(aPlane);
114       break;
115     }
116     default: {
117       myError = "Symmetry builder :: method not supported";
118       return;
119     }
120   }
121
122   const TopoDS_Shape& aSourceShape = mySourceShape->impl<TopoDS_Shape>();
123
124   if(aSourceShape.IsNull()) {
125     myError = "Symmetry builder :: source shape does not contain any actual shape.";
126     return;
127   }
128
129   // Transform the shape while copying it.
130   BRepBuilderAPI_Transform* aBuilder = new BRepBuilderAPI_Transform(aSourceShape, *aTrsf, true);
131   if(!aBuilder) {
132     myError = "Symmetry builder :: transform initialization failed.";
133     return;
134   }
135
136   setImpl(aBuilder);
137   setBuilderType(OCCT_BRepBuilderAPI_MakeShape);
138
139   if(!aBuilder->IsDone()) {
140     myError = "Symmetry builder :: algorithm failed.";
141     return;
142   }
143
144   TopoDS_Shape aResult = aBuilder->Shape();
145
146   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
147   aShape->setImpl(new TopoDS_Shape(aResult));
148   setShape(aShape);
149   setDone(true);
150 }