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