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