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