]> SALOME platform Git repositories - modules/shaper.git/blob - src/GeomAlgoAPI/GeomAlgoAPI_Box.cpp
Salome HOME
Updating Primitive Box.
[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 :GeomAlgoAPI_Box()
30 {
31   myFirstPoint = theFirstPoint;
32   mySecondPoint = theSecondPoint;
33   myMethodType = MethodType::BOX_POINTS;
34 }
35
36 //=================================================================================================
37 bool GeomAlgoAPI_Box::check()
38 {
39   if (myMethodType == MethodType::BOX_DIM) {
40     if (myDx < Precision::Confusion()) {
41       myError = "Box builder with dimensions :: Dx is null.";
42       return false;
43     } else if (myDy < Precision::Confusion()) {
44       myError = "Box builder with dimensions :: Dy is null.";
45       return false;
46     } else if (myDz < Precision::Confusion()) {
47       myError = "Box builder with dimensions :: Dz is null.";
48       return false;
49     }
50   } else if (myMethodType == MethodType::BOX_POINTS) {
51     if (!myFirstPoint.get()) {
52       myError = "Box builder with points :: the first point is not a correct";
53       return false;
54     }
55     if (!mySecondPoint.get()) {
56       myError = "Box builder with points :: the second point is not a correct";
57       return false;
58     }
59     if (myFirstPoint->distance(mySecondPoint) < Precision::Confusion()) {
60       myError = "Box builder with points :: the distance between the two points is null.";
61       return false;
62     }
63     double aDiffX = myFirstPoint->x() - mySecondPoint->x();
64     double aDiffY = myFirstPoint->y() - mySecondPoint->y();
65     double aDiffZ = myFirstPoint->z() - mySecondPoint->z();
66     if (fabs(aDiffX)  < Precision::Confusion() ||
67         fabs(aDiffY)  < Precision::Confusion() ||
68         fabs(aDiffZ)  < Precision::Confusion()) {
69       myError = "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 //=================================================================================================
125 void GeomAlgoAPI_Box::buildWithPoints()
126 {
127   myCreatedFaces.clear();
128   
129   const gp_Pnt& aFirstPoint = myFirstPoint->impl<gp_Pnt>();
130   const gp_Pnt& aSecondPoint = mySecondPoint->impl<gp_Pnt>();
131
132   // Construct the box
133   BRepPrimAPI_MakeBox *aBoxMaker = new  BRepPrimAPI_MakeBox(aFirstPoint, aSecondPoint);
134   aBoxMaker->Build();
135   
136   // Test the algorithm
137   if(!aBoxMaker->IsDone()) {
138     myError = "Box builder with two points  :: algorithm failed.";
139     return;
140   }
141     
142   TopoDS_Shape aResult = aBoxMaker->Shape();
143   
144   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
145   aShape->setImpl(new TopoDS_Shape(aResult));
146   setShape(aShape);
147   
148   // Tests on the shape
149   if (!aShape.get() || aShape->isNull()) {
150     myError = "Box builder with two points  :: resulting shape is null.";
151     return;
152   }
153   
154   setImpl(aBoxMaker);
155   
156   setDone(true);
157 }
158
159 //=================================================================================================
160 void GeomAlgoAPI_Box::prepareNamingFaces()
161 {
162   BRepPrimAPI_MakeBox aBoxMaker = impl<BRepPrimAPI_MakeBox>();
163   std::shared_ptr<GeomAPI_Shape> aShapeFront(new GeomAPI_Shape);
164   aShapeFront->setImpl(new TopoDS_Shape(aBoxMaker.FrontFace()));
165   myCreatedFaces["Front"] = aShapeFront;
166   std::shared_ptr<GeomAPI_Shape> aShapeBack(new GeomAPI_Shape);
167   aShapeBack->setImpl(new TopoDS_Shape(aBoxMaker.BackFace()));
168   myCreatedFaces["Back"] = aShapeBack;
169   std::shared_ptr<GeomAPI_Shape> aShapeTop(new GeomAPI_Shape); 
170   aShapeTop->setImpl(new TopoDS_Shape(aBoxMaker.TopFace()));
171   myCreatedFaces["Top"] = aShapeTop;
172   std::shared_ptr<GeomAPI_Shape> aShapeBottom(new GeomAPI_Shape); 
173   aShapeBottom->setImpl(new TopoDS_Shape(aBoxMaker.BottomFace()));
174   myCreatedFaces["Bottom"] = aShapeBottom;
175   std::shared_ptr<GeomAPI_Shape> aShapeLeft(new GeomAPI_Shape); 
176   aShapeLeft->setImpl(new TopoDS_Shape(aBoxMaker.LeftFace()));
177   myCreatedFaces["Left"] = aShapeLeft;
178   std::shared_ptr<GeomAPI_Shape> aShapeRight(new GeomAPI_Shape); 
179   aShapeRight->setImpl(new TopoDS_Shape(aBoxMaker.RightFace()));
180   myCreatedFaces["Right"] = aShapeRight;
181 }