Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Box.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_Box.h>
22
23 #include <BRepPrimAPI_MakeBox.hxx>
24 #include <TopoDS_Shape.hxx>
25
26 //=================================================================================================
27 GeomAlgoAPI_Box::GeomAlgoAPI_Box()
28 {
29 }
30
31 //=================================================================================================
32 GeomAlgoAPI_Box::GeomAlgoAPI_Box(const double theDx, const double theDy, const double theDz)
33 {
34   myDx = theDx;
35   myDy = theDy;
36   myDz = theDz;
37   myMethodType = MethodType::BOX_DIM;
38 }
39
40 //=================================================================================================
41 GeomAlgoAPI_Box::GeomAlgoAPI_Box(std::shared_ptr<GeomAPI_Pnt> theFirstPoint,
42                                  std::shared_ptr<GeomAPI_Pnt> theSecondPoint)
43 {
44   myFirstPoint = theFirstPoint;
45   mySecondPoint = theSecondPoint;
46   myMethodType = MethodType::BOX_POINTS;
47 }
48
49 //=================================================================================================
50 bool GeomAlgoAPI_Box::check()
51 {
52   if (myMethodType == MethodType::BOX_DIM) {
53     if (myDx < Precision::Confusion()) {
54       myError = "Box builder with dimensions :: Dx is null or negative.";
55       return false;
56     } else if (myDy < Precision::Confusion()) {
57       myError = "Box builder with dimensions :: Dy is null or negative.";
58       return false;
59     } else if (myDz < Precision::Confusion()) {
60       myError = "Box builder with dimensions :: Dz is null or negative.";
61       return false;
62     }
63   } else if (myMethodType == MethodType::BOX_POINTS) {
64     if (!myFirstPoint.get()) {
65       myError = "Box builder with points :: the first point is not valid.";
66       return false;
67     }
68     if (!mySecondPoint.get()) {
69       myError = "Box builder with points :: the second point is not valid.";
70       return false;
71     }
72     if (myFirstPoint->distance(mySecondPoint) < Precision::Confusion()) {
73       myError = "Box builder with points :: the distance between the two points is null.";
74       return false;
75     }
76     double aDiffX = myFirstPoint->x() - mySecondPoint->x();
77     double aDiffY = myFirstPoint->y() - mySecondPoint->y();
78     double aDiffZ = myFirstPoint->z() - mySecondPoint->z();
79     if (fabs(aDiffX)  < Precision::Confusion() ||
80         fabs(aDiffY)  < Precision::Confusion() ||
81         fabs(aDiffZ)  < Precision::Confusion()) {
82       myError =
83         "Box builder with points :: the points belong both to one of the OXY, OYZ or OZX planes.";
84       return false;
85     }
86   } else {
87     myError = "Box builder :: Method not implemented.";
88     return false;
89   }
90   return true;
91 }
92
93 //=================================================================================================
94 void GeomAlgoAPI_Box::build()
95 {
96   if (myMethodType == MethodType::BOX_DIM) {
97     buildWithDimensions();
98   } else if (myMethodType == MethodType::BOX_POINTS) {
99     buildWithPoints();
100   } else {
101     myError = "Box builder :: Method not implemented.";
102     return;
103   }
104 }
105
106 //=================================================================================================
107 void GeomAlgoAPI_Box::buildWithDimensions()
108 {
109   myCreatedFaces.clear();
110
111   // Construct the box
112   BRepPrimAPI_MakeBox *aBoxMaker = new BRepPrimAPI_MakeBox(myDx, myDy, myDz);
113   aBoxMaker->Build();
114
115   // Test the algorithm
116   if (!aBoxMaker->IsDone()) {
117     myError = "Box builder with dimensions  :: algorithm failed.";
118     return;
119   }
120
121   TopoDS_Shape aResult = aBoxMaker->Shape();
122   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
123   aShape->setImpl(new TopoDS_Shape(aResult));
124   setShape(aShape);
125
126   // Test on the shapes
127   if (!aShape.get() || aShape->isNull()) {
128     myError = "Box builder with dimensions  :: resulting shape is null.";
129     return;
130   }
131
132   setImpl(aBoxMaker);
133
134   setDone(true);
135 }
136
137 //=================================================================================================
138 void GeomAlgoAPI_Box::buildWithPoints()
139 {
140   myCreatedFaces.clear();
141
142   const gp_Pnt& aFirstPoint = myFirstPoint->impl<gp_Pnt>();
143   const gp_Pnt& aSecondPoint = mySecondPoint->impl<gp_Pnt>();
144
145   // Construct the box
146   BRepPrimAPI_MakeBox *aBoxMaker = new  BRepPrimAPI_MakeBox(aFirstPoint, aSecondPoint);
147   aBoxMaker->Build();
148
149   // Test the algorithm
150   if(!aBoxMaker->IsDone()) {
151     myError = "Box builder with two points  :: algorithm failed.";
152     return;
153   }
154
155   TopoDS_Shape aResult = aBoxMaker->Shape();
156
157   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
158   aShape->setImpl(new TopoDS_Shape(aResult));
159   setShape(aShape);
160
161   // Tests on the shape
162   if (!aShape.get() || aShape->isNull()) {
163     myError = "Box builder with two points  :: resulting shape is null.";
164     return;
165   }
166
167   setImpl(aBoxMaker);
168
169   setDone(true);
170 }
171
172 //=================================================================================================
173 void GeomAlgoAPI_Box::prepareNamingFaces()
174 {
175   BRepPrimAPI_MakeBox aBoxMaker = impl<BRepPrimAPI_MakeBox>();
176   std::shared_ptr<GeomAPI_Shape> aShapeFront(new GeomAPI_Shape);
177   aShapeFront->setImpl(new TopoDS_Shape(aBoxMaker.FrontFace()));
178   myCreatedFaces["Front"] = aShapeFront;
179   std::shared_ptr<GeomAPI_Shape> aShapeBack(new GeomAPI_Shape);
180   aShapeBack->setImpl(new TopoDS_Shape(aBoxMaker.BackFace()));
181   myCreatedFaces["Back"] = aShapeBack;
182   std::shared_ptr<GeomAPI_Shape> aShapeTop(new GeomAPI_Shape);
183   aShapeTop->setImpl(new TopoDS_Shape(aBoxMaker.TopFace()));
184   myCreatedFaces["Top"] = aShapeTop;
185   std::shared_ptr<GeomAPI_Shape> aShapeBottom(new GeomAPI_Shape);
186   aShapeBottom->setImpl(new TopoDS_Shape(aBoxMaker.BottomFace()));
187   myCreatedFaces["Bottom"] = aShapeBottom;
188   std::shared_ptr<GeomAPI_Shape> aShapeLeft(new GeomAPI_Shape);
189   aShapeLeft->setImpl(new TopoDS_Shape(aBoxMaker.LeftFace()));
190   myCreatedFaces["Left"] = aShapeLeft;
191   std::shared_ptr<GeomAPI_Shape> aShapeRight(new GeomAPI_Shape);
192   aShapeRight->setImpl(new TopoDS_Shape(aBoxMaker.RightFace()));
193   myCreatedFaces["Right"] = aShapeRight;
194 }