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 or negative.";
41       return false;
42     } else if (myDy < Precision::Confusion()) {
43       myError = "Box builder with dimensions :: Dy is null or negative.";
44       return false;
45     } else if (myDz < Precision::Confusion()) {
46       myError = "Box builder with dimensions :: Dz is null or negative.";
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 correct.";
52       return false;
53     }
54     if (!mySecondPoint.get()) {
55       myError = "Box builder with points :: the second point is not 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 =
69         "Box builder with points :: the points belong both to one of the OXY, OYZ or OZX planes.";
70       return false;
71     }
72   } else {
73     myError = "Box builder :: Method not implemented.";
74     return false;
75   }
76   return true;
77 }
78
79 //=================================================================================================
80 void GeomAlgoAPI_Box::build()
81 {
82   if (myMethodType == MethodType::BOX_DIM) {
83     buildWithDimensions();
84   } else if (myMethodType == MethodType::BOX_POINTS) {
85     buildWithPoints();
86   } else {
87     myError = "Box builder :: Method not implemented.";
88     return;
89   }
90 }
91
92 //=================================================================================================
93 void GeomAlgoAPI_Box::buildWithDimensions()
94 {
95   myCreatedFaces.clear();
96
97   // Construct the box
98   BRepPrimAPI_MakeBox *aBoxMaker = new BRepPrimAPI_MakeBox(myDx, myDy, myDz);
99   aBoxMaker->Build();
100
101   // Test the algorithm
102   if (!aBoxMaker->IsDone()) {
103     myError = "Box builder with dimensions  :: algorithm failed.";
104     return;
105   }
106
107   TopoDS_Shape aResult = aBoxMaker->Shape();
108   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
109   aShape->setImpl(new TopoDS_Shape(aResult));
110   setShape(aShape);
111
112   // Test on the shapes
113   if (!aShape.get() || aShape->isNull()) {
114     myError = "Box builder with dimensions  :: resulting shape is null.";
115     return;
116   }
117
118   setImpl(aBoxMaker);
119
120   setDone(true);
121 }
122
123 //=================================================================================================
124 void GeomAlgoAPI_Box::buildWithPoints()
125 {
126   myCreatedFaces.clear();
127
128   const gp_Pnt& aFirstPoint = myFirstPoint->impl<gp_Pnt>();
129   const gp_Pnt& aSecondPoint = mySecondPoint->impl<gp_Pnt>();
130
131   // Construct the box
132   BRepPrimAPI_MakeBox *aBoxMaker = new  BRepPrimAPI_MakeBox(aFirstPoint, aSecondPoint);
133   aBoxMaker->Build();
134
135   // Test the algorithm
136   if(!aBoxMaker->IsDone()) {
137     myError = "Box builder with two points  :: algorithm failed.";
138     return;
139   }
140
141   TopoDS_Shape aResult = aBoxMaker->Shape();
142
143   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
144   aShape->setImpl(new TopoDS_Shape(aResult));
145   setShape(aShape);
146
147   // Tests on the shape
148   if (!aShape.get() || aShape->isNull()) {
149     myError = "Box builder with two points  :: resulting shape is null.";
150     return;
151   }
152
153   setImpl(aBoxMaker);
154
155   setDone(true);
156 }
157
158 //=================================================================================================
159 void GeomAlgoAPI_Box::prepareNamingFaces()
160 {
161   BRepPrimAPI_MakeBox aBoxMaker = impl<BRepPrimAPI_MakeBox>();
162   std::shared_ptr<GeomAPI_Shape> aShapeFront(new GeomAPI_Shape);
163   aShapeFront->setImpl(new TopoDS_Shape(aBoxMaker.FrontFace()));
164   myCreatedFaces["Front"] = aShapeFront;
165   std::shared_ptr<GeomAPI_Shape> aShapeBack(new GeomAPI_Shape);
166   aShapeBack->setImpl(new TopoDS_Shape(aBoxMaker.BackFace()));
167   myCreatedFaces["Back"] = aShapeBack;
168   std::shared_ptr<GeomAPI_Shape> aShapeTop(new GeomAPI_Shape);
169   aShapeTop->setImpl(new TopoDS_Shape(aBoxMaker.TopFace()));
170   myCreatedFaces["Top"] = aShapeTop;
171   std::shared_ptr<GeomAPI_Shape> aShapeBottom(new GeomAPI_Shape);
172   aShapeBottom->setImpl(new TopoDS_Shape(aBoxMaker.BottomFace()));
173   myCreatedFaces["Bottom"] = aShapeBottom;
174   std::shared_ptr<GeomAPI_Shape> aShapeLeft(new GeomAPI_Shape);
175   aShapeLeft->setImpl(new TopoDS_Shape(aBoxMaker.LeftFace()));
176   myCreatedFaces["Left"] = aShapeLeft;
177   std::shared_ptr<GeomAPI_Shape> aShapeRight(new GeomAPI_Shape);
178   aShapeRight->setImpl(new TopoDS_Shape(aBoxMaker.RightFace()));
179   myCreatedFaces["Right"] = aShapeRight;
180 }