1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <GeomAlgoAPI_Box.h>
23 #include <BRepPrimAPI_MakeBox.hxx>
24 #include <TopoDS_Shape.hxx>
26 //=================================================================================================
27 GeomAlgoAPI_Box::GeomAlgoAPI_Box()
31 //=================================================================================================
32 GeomAlgoAPI_Box::GeomAlgoAPI_Box(const double theDx, const double theDy, const double theDz)
37 myMethodType = MethodType::BOX_DIM;
40 //=================================================================================================
41 GeomAlgoAPI_Box::GeomAlgoAPI_Box(std::shared_ptr<GeomAPI_Pnt> theFirstPoint,
42 std::shared_ptr<GeomAPI_Pnt> theSecondPoint)
44 myFirstPoint = theFirstPoint;
45 mySecondPoint = theSecondPoint;
46 myMethodType = MethodType::BOX_POINTS;
49 //=================================================================================================
50 bool GeomAlgoAPI_Box::check()
52 if (myMethodType == MethodType::BOX_DIM) {
53 if (myDx < Precision::Confusion()) {
54 myError = "Box builder with dimensions :: Dx is null or negative.";
56 } else if (myDy < Precision::Confusion()) {
57 myError = "Box builder with dimensions :: Dy is null or negative.";
59 } else if (myDz < Precision::Confusion()) {
60 myError = "Box builder with dimensions :: Dz is null or negative.";
63 } else if (myMethodType == MethodType::BOX_POINTS) {
64 if (!myFirstPoint.get()) {
65 myError = "Box builder with points :: the first point is not valid.";
68 if (!mySecondPoint.get()) {
69 myError = "Box builder with points :: the second point is not valid.";
72 if (myFirstPoint->distance(mySecondPoint) < Precision::Confusion()) {
73 myError = "Box builder with points :: the distance between the two points is null.";
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()) {
83 "Box builder with points :: the points belong both to one of the OXY, OYZ or OZX planes.";
87 myError = "Box builder :: Method not implemented.";
93 //=================================================================================================
94 void GeomAlgoAPI_Box::build()
96 if (myMethodType == MethodType::BOX_DIM) {
97 buildWithDimensions();
98 } else if (myMethodType == MethodType::BOX_POINTS) {
101 myError = "Box builder :: Method not implemented.";
106 //=================================================================================================
107 void GeomAlgoAPI_Box::buildWithDimensions()
109 myCreatedFaces.clear();
112 BRepPrimAPI_MakeBox *aBoxMaker = new BRepPrimAPI_MakeBox(myDx, myDy, myDz);
115 // Test the algorithm
116 if (!aBoxMaker->IsDone()) {
117 myError = "Box builder with dimensions :: algorithm failed.";
121 TopoDS_Shape aResult = aBoxMaker->Shape();
122 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
123 aShape->setImpl(new TopoDS_Shape(aResult));
126 // Test on the shapes
127 if (!aShape.get() || aShape->isNull()) {
128 myError = "Box builder with dimensions :: resulting shape is null.";
137 //=================================================================================================
138 void GeomAlgoAPI_Box::buildWithPoints()
140 myCreatedFaces.clear();
142 const gp_Pnt& aFirstPoint = myFirstPoint->impl<gp_Pnt>();
143 const gp_Pnt& aSecondPoint = mySecondPoint->impl<gp_Pnt>();
146 BRepPrimAPI_MakeBox *aBoxMaker = new BRepPrimAPI_MakeBox(aFirstPoint, aSecondPoint);
149 // Test the algorithm
150 if(!aBoxMaker->IsDone()) {
151 myError = "Box builder with two points :: algorithm failed.";
155 TopoDS_Shape aResult = aBoxMaker->Shape();
157 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
158 aShape->setImpl(new TopoDS_Shape(aResult));
161 // Tests on the shape
162 if (!aShape.get() || aShape->isNull()) {
163 myError = "Box builder with two points :: resulting shape is null.";
172 //=================================================================================================
173 void GeomAlgoAPI_Box::prepareNamingFaces()
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;