1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <GeomAlgoAPI_Box.h>
22 #include <BRepPrimAPI_MakeBox.hxx>
23 #include <TopoDS_Shape.hxx>
25 //=================================================================================================
26 GeomAlgoAPI_Box::GeomAlgoAPI_Box()
30 //=================================================================================================
31 GeomAlgoAPI_Box::GeomAlgoAPI_Box(const double theDx, const double theDy, const double theDz)
36 myMethodType = MethodType::BOX_DIM;
39 //=================================================================================================
40 GeomAlgoAPI_Box::GeomAlgoAPI_Box(std::shared_ptr<GeomAPI_Pnt> theFirstPoint,
41 std::shared_ptr<GeomAPI_Pnt> theSecondPoint)
43 myFirstPoint = theFirstPoint;
44 mySecondPoint = theSecondPoint;
45 myMethodType = MethodType::BOX_POINTS;
48 //=================================================================================================
49 bool GeomAlgoAPI_Box::check()
51 if (myMethodType == MethodType::BOX_DIM) {
52 if (myDx < Precision::Confusion()) {
53 myError = "Box builder with dimensions :: Dx is null or negative.";
55 } else if (myDy < Precision::Confusion()) {
56 myError = "Box builder with dimensions :: Dy is null or negative.";
58 } else if (myDz < Precision::Confusion()) {
59 myError = "Box builder with dimensions :: Dz is null or negative.";
62 } else if (myMethodType == MethodType::BOX_POINTS) {
63 if (!myFirstPoint.get()) {
64 myError = "Box builder with points :: the first point is not valid.";
67 if (!mySecondPoint.get()) {
68 myError = "Box builder with points :: the second point is not valid.";
71 if (myFirstPoint->distance(mySecondPoint) < Precision::Confusion()) {
72 myError = "Box builder with points :: the distance between the two points is null.";
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()) {
82 "Box builder with points :: the points belong both to one of the OXY, OYZ or OZX planes.";
86 myError = "Box builder :: Method not implemented.";
92 //=================================================================================================
93 void GeomAlgoAPI_Box::build()
95 if (myMethodType == MethodType::BOX_DIM) {
96 buildWithDimensions();
97 } else if (myMethodType == MethodType::BOX_POINTS) {
100 myError = "Box builder :: Method not implemented.";
105 //=================================================================================================
106 void GeomAlgoAPI_Box::buildWithDimensions()
108 myCreatedFaces.clear();
111 BRepPrimAPI_MakeBox *aBoxMaker = new BRepPrimAPI_MakeBox(myDx, myDy, myDz);
114 // Test the algorithm
115 if (!aBoxMaker->IsDone()) {
116 myError = "Box builder with dimensions :: algorithm failed.";
120 TopoDS_Shape aResult = aBoxMaker->Shape();
121 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
122 aShape->setImpl(new TopoDS_Shape(aResult));
125 // Test on the shapes
126 if (!aShape.get() || aShape->isNull()) {
127 myError = "Box builder with dimensions :: resulting shape is null.";
136 //=================================================================================================
137 void GeomAlgoAPI_Box::buildWithPoints()
139 myCreatedFaces.clear();
141 const gp_Pnt& aFirstPoint = myFirstPoint->impl<gp_Pnt>();
142 const gp_Pnt& aSecondPoint = mySecondPoint->impl<gp_Pnt>();
145 BRepPrimAPI_MakeBox *aBoxMaker = new BRepPrimAPI_MakeBox(aFirstPoint, aSecondPoint);
148 // Test the algorithm
149 if(!aBoxMaker->IsDone()) {
150 myError = "Box builder with two points :: algorithm failed.";
154 TopoDS_Shape aResult = aBoxMaker->Shape();
156 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
157 aShape->setImpl(new TopoDS_Shape(aResult));
160 // Tests on the shape
161 if (!aShape.get() || aShape->isNull()) {
162 myError = "Box builder with two points :: resulting shape is null.";
171 //=================================================================================================
172 void GeomAlgoAPI_Box::prepareNamingFaces()
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;