Salome HOME
c084b60a649c37836a07554ca75c5d31e5145949
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Prism.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomAlgoAPI_Prism.cpp
4 // Created:     5 May 2015
5 // Author:      Dmitry Bobylev
6
7 #include <GeomAlgoAPI_Prism.h>
8
9 #include <GeomAPI_Face.h>
10 #include <GeomAPI_Pln.h>
11 #include <GeomAPI_Pnt.h>
12 #include <GeomAPI_ShapeExplorer.h>
13 #include <GeomAPI_XYZ.h>
14 #include <GeomAlgoAPI_DFLoader.h>
15 #include <GeomAlgoAPI_FaceBuilder.h>
16 #include <GeomAlgoAPI_MakeShapeList.h>
17 #include <GeomAlgoAPI_ShapeTools.h>
18
19 #include <Bnd_Box.hxx>
20 #include <BRep_Builder.hxx>
21 #include <BRep_Tool.hxx>
22 #include <BRepAlgoAPI_Cut.hxx>
23 #include <BRepBndLib.hxx>
24 #include <BRepBuilderAPI_MakeEdge.hxx>
25 #include <BRepBuilderAPI_MakeShape.hxx>
26 #include <BRepBuilderAPI_MakeWire.hxx>
27 #include <BRepCheck_Analyzer.hxx>
28 #include <BRepExtrema_ExtCF.hxx>
29 #include <BRepGProp.hxx>
30 #include <BRepOffsetAPI_MakePipe.hxx>
31 #include <BRepTools.hxx>
32 #include <Geom_Plane.hxx>
33 #include <gp_Pln.hxx>
34 #include <GProp_GProps.hxx>
35 #include <TCollection_AsciiString.hxx>
36 #include <TopExp_Explorer.hxx>
37 #include <TopoDS.hxx>
38 #include <TopoDS_Edge.hxx>
39 #include <TopoDS_Shell.hxx>
40 #include <TopoDS_Solid.hxx>
41 #include <TopoDS_Wire.hxx>
42 #include <TopTools_ListIteratorOfListOfShape.hxx>
43
44 //=================================================================================================
45 GeomAlgoAPI_Prism::GeomAlgoAPI_Prism(std::shared_ptr<GeomAPI_Shape> theBasis,
46                                      double                         theToSize,
47                                      double                         theFromSize)
48 : myDone(false)
49 {
50   build(theBasis, std::shared_ptr<GeomAPI_Shape>(), theToSize, std::shared_ptr<GeomAPI_Shape>(), theFromSize);
51 }
52
53 //=================================================================================================
54 GeomAlgoAPI_Prism::GeomAlgoAPI_Prism(std::shared_ptr<GeomAPI_Shape> theBasis,
55                                      std::shared_ptr<GeomAPI_Shape> theToShape,
56                                      double                         theToSize,
57                                      std::shared_ptr<GeomAPI_Shape> theFromShape,
58                                      double                         theFromSize)
59 : myDone(false)
60 {
61   build(theBasis, theToShape, theToSize, theFromShape, theFromSize);
62 }
63
64 //=================================================================================================
65 void GeomAlgoAPI_Prism::build(const std::shared_ptr<GeomAPI_Shape>& theBasis,
66                               const std::shared_ptr<GeomAPI_Shape>& theToShape,
67                               double                                theToSize,
68                               const std::shared_ptr<GeomAPI_Shape>& theFromShape,
69                               double                                theFromSize)
70 {
71   if(!theBasis ||
72     (((!theFromShape && !theToShape) || (theFromShape && theToShape && theFromShape->isEqual(theToShape)))
73     && (theFromSize == -theToSize))) {
74     return;
75   }
76
77   // If bounding faces was not set creating them.
78   std::shared_ptr<GeomAPI_Face> aBaseFace;
79   if(theBasis->shapeType() == GeomAPI_Shape::SHELL) {
80     GeomAPI_ShapeExplorer anExp(theBasis, GeomAPI_Shape::FACE);
81     if(anExp.more()) {
82       std::shared_ptr<GeomAPI_Shape> aFaceOnShell = anExp.current();
83       aBaseFace = std::shared_ptr<GeomAPI_Face>(new GeomAPI_Face(aFaceOnShell));
84     }
85   } else {
86     aBaseFace = std::shared_ptr<GeomAPI_Face>(new GeomAPI_Face(theBasis));
87   }
88   if(!aBaseFace.get()) {
89     return;
90   }
91   std::shared_ptr<GeomAPI_Pln>   aBasePln = aBaseFace->getPlane();
92   std::shared_ptr<GeomAPI_Dir>   aBaseDir = aBasePln->direction();
93   std::shared_ptr<GeomAPI_Pnt>   aBaseLoc = aBasePln->location();
94   std::shared_ptr<GeomAPI_Shape> aBasePlane = GeomAlgoAPI_FaceBuilder::planarFace(aBaseLoc, aBaseDir);
95
96   std::shared_ptr<GeomAPI_Shape> aBoundingFromShape = theFromShape ? theFromShape : aBasePlane;
97   std::shared_ptr<GeomAPI_Shape> aBoundingToShape   = theToShape   ? theToShape   : aBasePlane;
98
99   // Moving bounding faces according to "from" and "to" sizes.
100   std::shared_ptr<GeomAPI_Face> aFromFace(new GeomAPI_Face(aBoundingFromShape));
101   std::shared_ptr<GeomAPI_Pln>  aFromPln = aFromFace->getPlane();
102   std::shared_ptr<GeomAPI_Pnt>  aFromLoc = aFromPln->location();
103   std::shared_ptr<GeomAPI_Dir>  aFromDir = aFromPln->direction();
104
105   std::shared_ptr<GeomAPI_Face> aToFace(new GeomAPI_Face(aBoundingToShape));
106   std::shared_ptr<GeomAPI_Pln>  aToPln = aToFace->getPlane();
107   std::shared_ptr<GeomAPI_Pnt>  aToLoc = aToPln->location();
108   std::shared_ptr<GeomAPI_Dir>  aToDir = aToPln->direction();
109
110   bool aSign = aFromLoc->xyz()->dot(aBaseDir->xyz()) > aToLoc->xyz()->dot(aBaseDir->xyz());
111
112   std::shared_ptr<GeomAPI_Pnt> aFromPnt(new GeomAPI_Pnt(aFromLoc->xyz()->added(aBaseDir->xyz()->multiplied(
113                                                         aSign ? theFromSize : -theFromSize))));
114   aBoundingFromShape = GeomAlgoAPI_FaceBuilder::planarFace(aFromPnt, aFromDir);
115
116   std::shared_ptr<GeomAPI_Pnt> aToPnt(new GeomAPI_Pnt(aToLoc->xyz()->added(aBaseDir->xyz()->multiplied(
117                                                       aSign ? -theToSize : theToSize))));
118   aBoundingToShape = GeomAlgoAPI_FaceBuilder::planarFace(aToPnt, aToDir);
119
120   // Getting bounding box for base shape.
121   const TopoDS_Shape& aBasisShape = theBasis->impl<TopoDS_Shape>();
122   Bnd_Box aBndBox;
123   BRepBndLib::Add(aBasisShape, aBndBox);
124   Standard_Real aXArr[2] = {aBndBox.CornerMin().X(), aBndBox.CornerMax().X()};
125   Standard_Real aYArr[2] = {aBndBox.CornerMin().Y(), aBndBox.CornerMax().Y()};
126   Standard_Real aZArr[2] = {aBndBox.CornerMin().Z(), aBndBox.CornerMax().Z()};
127   gp_Pnt aPoints[8];
128   int aNum = 0;
129   for(int i = 0; i < 2; i++) {
130     for(int j = 0; j < 2; j++) {
131       for(int k = 0; k < 2; k++) {
132         aPoints[aNum] = gp_Pnt(aXArr[i], aYArr[j], aZArr[k]);
133         aNum++;
134       }
135     }
136   }
137
138   // Project points to bounding planes. Search max distance to them.
139   const TopoDS_Shape& aBndToShape = aBoundingToShape->impl<TopoDS_Shape>();
140   const TopoDS_Shape& aBndFromShape = aBoundingFromShape->impl<TopoDS_Shape>();
141   Standard_Real aMaxToDist = 0, aMaxFromDist = 0;
142   gp_Vec aNormal(aBaseDir->impl<gp_Dir>());
143   for(int i = 0; i < 8; i++) {
144     gp_Lin aLine(aPoints[i], aNormal);
145     TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge(aLine).Edge();
146     BRepExtrema_ExtCF aToExt(anEdge, TopoDS::Face(aBndToShape));
147     BRepExtrema_ExtCF aFromExt(anEdge, TopoDS::Face(aBndFromShape));
148     if(aToExt.NbExt() == 0 || aFromExt.NbExt() == 0) {
149       return;
150     }
151     const gp_Pnt& aPntOnToFace = aToExt.PointOnFace(1);
152     const gp_Pnt& aPntOnFromFace = aFromExt.PointOnFace(1);
153     if(aPoints[i].Distance(aPntOnToFace) > aMaxToDist) {
154       aMaxToDist = aPoints[i].Distance(aPntOnToFace);
155     }
156     if(aPoints[i].Distance(aPntOnFromFace) > aMaxFromDist) {
157       aMaxFromDist = aPoints[i].Distance(aPntOnFromFace);
158     }
159   }
160   Standard_Real aPipeLength = aMaxToDist + aMaxFromDist;
161
162   // Making wire for pipe.
163   std::shared_ptr<GeomAPI_Pnt> aCentreOfMass = GeomAlgoAPI_ShapeTools::centreOfMass(theBasis);
164   const gp_Pnt aCentrePnt = aCentreOfMass->impl<gp_Pnt>();
165   TopoDS_Face aFace = TopoDS::Face(aBaseFace->impl<TopoDS_Shape>());
166   gp_Pnt aPipeStartPnt = aCentrePnt.Translated(aNormal.Scaled(aPipeLength));
167   gp_Pnt aPipeEndPnt = aCentrePnt.Translated(aNormal.Scaled(-aPipeLength));
168   TopoDS_Edge aPipeEdge = BRepBuilderAPI_MakeEdge(aPipeStartPnt, aPipeEndPnt);
169   TopoDS_Wire aPipeWire = BRepBuilderAPI_MakeWire(aPipeEdge).Wire();
170
171   // Making pipe.
172   ListOfMakeShape aListOfMakeShape;
173   BRepOffsetAPI_MakePipe* aPipeBuilder = new BRepOffsetAPI_MakePipe(aPipeWire, aBasisShape);
174   if(!aPipeBuilder) {
175     return;
176   }
177   std::shared_ptr<GeomAPI_Shape> aWire(new GeomAPI_Shape);
178   aWire->setImpl(new TopoDS_Shape(aPipeWire));
179   aListOfMakeShape.push_back(std::make_shared<GeomAlgoAPI_MakeShape>(aPipeBuilder, aWire));
180   TopoDS_Shape aResult = aPipeBuilder->Shape();
181
182   // Orienting bounding planes.
183   gp_Lin aLine(aCentrePnt, aNormal);
184   TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge(aLine).Edge();
185   BRepExtrema_ExtCF aToExt(anEdge, TopoDS::Face(aBndToShape));
186   BRepExtrema_ExtCF aFromExt(anEdge, TopoDS::Face(aBndFromShape));
187   Standard_Real aToParameter = aToExt.ParameterOnEdge(1);
188   Standard_Real aFromParameter = aFromExt.ParameterOnEdge(1);
189   if(aToParameter > aFromParameter) {
190     gp_Vec aVec = aToDir->impl<gp_Dir>();
191     if((aVec * aNormal) > 0) {
192       aToDir->setImpl(new gp_Dir(aVec.Reversed()));
193       aBoundingToShape = GeomAlgoAPI_FaceBuilder::planarFace(aToPnt, aToDir);
194     }
195     aVec = aFromDir->impl<gp_Dir>();
196     if((aVec * aNormal) < 0) {
197       aFromDir->setImpl(new gp_Dir(aVec.Reversed()));
198       aBoundingFromShape = GeomAlgoAPI_FaceBuilder::planarFace(aFromPnt, aFromDir);
199     }
200   } else {
201     gp_Vec aVec = aToDir->impl<gp_Dir>();
202     if((aVec * aNormal) < 0) {
203       aToDir->setImpl(new gp_Dir(aVec.Reversed()));
204       aBoundingToShape = GeomAlgoAPI_FaceBuilder::planarFace(aToPnt, aToDir);
205     }
206     aVec = aFromDir->impl<gp_Dir>();
207     if((aVec * aNormal) > 0) {
208       aFromDir->setImpl(new gp_Dir(aVec.Reversed()));
209       aBoundingFromShape = GeomAlgoAPI_FaceBuilder::planarFace(aFromPnt, aFromDir);
210     }
211   }
212
213   // Making solids from bounding planes.
214   TopoDS_Shell aToShell, aFromShell;
215   TopoDS_Solid aToSolid, aFromSolid;
216   const TopoDS_Shape& aToShape   = aBoundingToShape->impl<TopoDS_Shape>();
217   const TopoDS_Shape& aFromShape = aBoundingFromShape->impl<TopoDS_Shape>();
218   BRep_Builder aBoundingBuilder;
219   aBoundingBuilder.MakeShell(aToShell);
220   aBoundingBuilder.MakeShell(aFromShell);
221   aBoundingBuilder.Add(aToShell, aToShape);
222   aBoundingBuilder.Add(aFromShell, aFromShape);
223   aBoundingBuilder.MakeSolid(aToSolid);
224   aBoundingBuilder.MakeSolid(aFromSolid);
225   aBoundingBuilder.Add(aToSolid, aToShell);
226   aBoundingBuilder.Add(aFromSolid, aFromShell);
227
228   // Cutting with to plane.
229   BRepAlgoAPI_Cut* aToCutBuilder = new BRepAlgoAPI_Cut(aResult, aToSolid);
230   aToCutBuilder->Build();
231   if(!aToCutBuilder->IsDone()) {
232     return;
233   }
234   aListOfMakeShape.push_back(std::make_shared<GeomAlgoAPI_MakeShape>(aToCutBuilder));
235   const TopTools_ListOfShape& aToShapes = aToCutBuilder->Modified(aToShape);
236   for(TopTools_ListIteratorOfListOfShape anIt(aToShapes); anIt.More(); anIt.Next()) {
237     std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
238     aShape->setImpl(new TopoDS_Shape(anIt.Value()));
239     myToFaces.push_back(aShape);
240   }
241   aResult = aToCutBuilder->Shape();
242
243   // Cutting with from plane.
244   BRepAlgoAPI_Cut* aFromCutBuilder = new BRepAlgoAPI_Cut(aResult, aFromSolid);
245   aFromCutBuilder->Build();
246   if(!aFromCutBuilder->IsDone()) {
247     return;
248   }
249   aListOfMakeShape.push_back(std::make_shared<GeomAlgoAPI_MakeShape>(aFromCutBuilder));
250   const TopTools_ListOfShape& aFromShapes = aFromCutBuilder->Modified(aFromShape);
251   for(TopTools_ListIteratorOfListOfShape anIt(aFromShapes); anIt.More(); anIt.Next()) {
252     std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
253     aShape->setImpl(new TopoDS_Shape(anIt.Value()));
254     myFromFaces.push_back(aShape);
255   }
256   aResult = aFromCutBuilder->Shape();
257
258   TopExp_Explorer anExp(aResult, TopAbs_SOLID);
259   if(!anExp.More()) {
260     return;
261   }
262   if(aResult.ShapeType() == TopAbs_COMPOUND) {
263     aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
264   }
265
266   myShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape);
267   myShape->setImpl(new TopoDS_Shape(aResult));
268
269   // Fill data map to keep correct orientation of sub-shapes.
270   myMap = std::shared_ptr<GeomAPI_DataMapOfShapeShape>(new GeomAPI_DataMapOfShapeShape);
271   for (TopExp_Explorer Exp(aResult,TopAbs_FACE); Exp.More(); Exp.Next()) {
272     std::shared_ptr<GeomAPI_Shape> aCurrentShape(new GeomAPI_Shape());
273     aCurrentShape->setImpl(new TopoDS_Shape(Exp.Current()));
274     myMap->bind(aCurrentShape, aCurrentShape);
275   }
276
277   myMkShape = std::shared_ptr<GeomAlgoAPI_MakeShapeList>(new GeomAlgoAPI_MakeShapeList(aListOfMakeShape));
278   myDone = true;
279 }
280
281 //=================================================================================================
282 bool GeomAlgoAPI_Prism::isDone() const
283 {
284   return myDone;
285 }
286
287 //=================================================================================================
288 bool GeomAlgoAPI_Prism::isValid() const
289 {
290   BRepCheck_Analyzer aChecker(myShape->impl<TopoDS_Shape>());
291   return (aChecker.IsValid() == Standard_True);
292 }
293
294 //=================================================================================================
295 bool GeomAlgoAPI_Prism::hasVolume() const
296 {
297   bool hasVolume(false);
298   if(isValid()) {
299     const TopoDS_Shape& aRShape = myShape->impl<TopoDS_Shape>();
300     GProp_GProps aGProp;
301     BRepGProp::VolumeProperties(aRShape, aGProp);
302     if(aGProp.Mass() > Precision::Confusion())
303       hasVolume = true;
304   }
305   return hasVolume;
306 }
307
308 //=================================================================================================
309 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_Prism::shape() const
310 {
311   return myShape;
312 }
313
314 //=================================================================================================
315 const ListOfShape& GeomAlgoAPI_Prism::fromFaces() const
316 {
317   return myFromFaces;
318 }
319
320 //=================================================================================================
321 const ListOfShape& GeomAlgoAPI_Prism::toFaces() const
322 {
323   return myToFaces;
324 }
325
326 //=================================================================================================
327 std::shared_ptr<GeomAPI_DataMapOfShapeShape> GeomAlgoAPI_Prism::mapOfShapes() const
328 {
329   return myMap;
330 }
331
332 //=================================================================================================
333 std::shared_ptr<GeomAlgoAPI_MakeShape> GeomAlgoAPI_Prism::makeShape() const
334 {
335   return myMkShape;
336 }