]> SALOME platform Git repositories - modules/shaper.git/blob - src/GeomAlgoAPI/GeomAlgoAPI_Prism.cpp
Salome HOME
Compsolids creation in revolution
[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   std::shared_ptr<GeomAPI_Shape> aBShape(new GeomAPI_Shape);
179   aWire->setImpl(new TopoDS_Shape(aPipeWire));
180   aBShape->setImpl(new TopoDS_Shape(aBasisShape));
181   aListOfMakeShape.push_back(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aPipeBuilder, aWire, aBShape)));
182   TopoDS_Shape aResult = aPipeBuilder->Shape();
183
184   // Orienting bounding planes.
185   gp_Lin aLine(aCentrePnt, aNormal);
186   TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge(aLine).Edge();
187   BRepExtrema_ExtCF aToExt(anEdge, TopoDS::Face(aBndToShape));
188   BRepExtrema_ExtCF aFromExt(anEdge, TopoDS::Face(aBndFromShape));
189   Standard_Real aToParameter = aToExt.ParameterOnEdge(1);
190   Standard_Real aFromParameter = aFromExt.ParameterOnEdge(1);
191   if(aToParameter > aFromParameter) {
192     gp_Vec aVec = aToDir->impl<gp_Dir>();
193     if((aVec * aNormal) > 0) {
194       aToDir->setImpl(new gp_Dir(aVec.Reversed()));
195       aBoundingToShape = GeomAlgoAPI_FaceBuilder::planarFace(aToPnt, aToDir);
196     }
197     aVec = aFromDir->impl<gp_Dir>();
198     if((aVec * aNormal) < 0) {
199       aFromDir->setImpl(new gp_Dir(aVec.Reversed()));
200       aBoundingFromShape = GeomAlgoAPI_FaceBuilder::planarFace(aFromPnt, aFromDir);
201     }
202   } else {
203     gp_Vec aVec = aToDir->impl<gp_Dir>();
204     if((aVec * aNormal) < 0) {
205       aToDir->setImpl(new gp_Dir(aVec.Reversed()));
206       aBoundingToShape = GeomAlgoAPI_FaceBuilder::planarFace(aToPnt, aToDir);
207     }
208     aVec = aFromDir->impl<gp_Dir>();
209     if((aVec * aNormal) > 0) {
210       aFromDir->setImpl(new gp_Dir(aVec.Reversed()));
211       aBoundingFromShape = GeomAlgoAPI_FaceBuilder::planarFace(aFromPnt, aFromDir);
212     }
213   }
214
215   // Making solids from bounding planes.
216   TopoDS_Shell aToShell, aFromShell;
217   TopoDS_Solid aToSolid, aFromSolid;
218   const TopoDS_Shape& aToShape   = aBoundingToShape->impl<TopoDS_Shape>();
219   const TopoDS_Shape& aFromShape = aBoundingFromShape->impl<TopoDS_Shape>();
220   BRep_Builder aBoundingBuilder;
221   aBoundingBuilder.MakeShell(aToShell);
222   aBoundingBuilder.MakeShell(aFromShell);
223   aBoundingBuilder.Add(aToShell, aToShape);
224   aBoundingBuilder.Add(aFromShell, aFromShape);
225   aBoundingBuilder.MakeSolid(aToSolid);
226   aBoundingBuilder.MakeSolid(aFromSolid);
227   aBoundingBuilder.Add(aToSolid, aToShell);
228   aBoundingBuilder.Add(aFromSolid, aFromShell);
229
230   // Cutting with to plane.
231   BRepAlgoAPI_Cut* aToCutBuilder = new BRepAlgoAPI_Cut(aResult, aToSolid);
232   aToCutBuilder->Build();
233   if(!aToCutBuilder->IsDone()) {
234     return;
235   }
236   aListOfMakeShape.push_back(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aToCutBuilder)));
237   const TopTools_ListOfShape& aToShapes = aToCutBuilder->Modified(aToShape);
238   for(TopTools_ListIteratorOfListOfShape anIt(aToShapes); anIt.More(); anIt.Next()) {
239     std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
240     aShape->setImpl(new TopoDS_Shape(anIt.Value()));
241     myToFaces.push_back(aShape);
242   }
243   aResult = aToCutBuilder->Shape();
244
245   // Cutting with from plane.
246   BRepAlgoAPI_Cut* aFromCutBuilder = new BRepAlgoAPI_Cut(aResult, aFromSolid);
247   aFromCutBuilder->Build();
248   if(!aFromCutBuilder->IsDone()) {
249     return;
250   }
251   aListOfMakeShape.push_back(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aFromCutBuilder)));
252   const TopTools_ListOfShape& aFromShapes = aFromCutBuilder->Modified(aFromShape);
253   for(TopTools_ListIteratorOfListOfShape anIt(aFromShapes); anIt.More(); anIt.Next()) {
254     std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
255     aShape->setImpl(new TopoDS_Shape(anIt.Value()));
256     myFromFaces.push_back(aShape);
257   }
258   aResult = aFromCutBuilder->Shape();
259
260   TopExp_Explorer anExp(aResult, TopAbs_SOLID);
261   if(!anExp.More()) {
262     return;
263   }
264   if(aResult.ShapeType() == TopAbs_COMPOUND) {
265     aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
266   }
267
268   myShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape);
269   myShape->setImpl(new TopoDS_Shape(aResult));
270
271   // Fill data map to keep correct orientation of sub-shapes.
272   myMap = std::shared_ptr<GeomAPI_DataMapOfShapeShape>(new GeomAPI_DataMapOfShapeShape);
273   for (TopExp_Explorer Exp(aResult,TopAbs_FACE); Exp.More(); Exp.Next()) {
274     std::shared_ptr<GeomAPI_Shape> aCurrentShape(new GeomAPI_Shape());
275     aCurrentShape->setImpl(new TopoDS_Shape(Exp.Current()));
276     myMap->bind(aCurrentShape, aCurrentShape);
277   }
278
279   myMkShape = std::shared_ptr<GeomAlgoAPI_MakeShapeList>(new GeomAlgoAPI_MakeShapeList(aListOfMakeShape));
280   myDone = true;
281 }
282
283 //=================================================================================================
284 bool GeomAlgoAPI_Prism::isDone() const
285 {
286   return myDone;
287 }
288
289 //=================================================================================================
290 bool GeomAlgoAPI_Prism::isValid() const
291 {
292   BRepCheck_Analyzer aChecker(myShape->impl<TopoDS_Shape>());
293   return (aChecker.IsValid() == Standard_True);
294 }
295
296 //=================================================================================================
297 bool GeomAlgoAPI_Prism::hasVolume() const
298 {
299   bool hasVolume(false);
300   if(isValid()) {
301     const TopoDS_Shape& aRShape = myShape->impl<TopoDS_Shape>();
302     GProp_GProps aGProp;
303     BRepGProp::VolumeProperties(aRShape, aGProp);
304     if(aGProp.Mass() > Precision::Confusion())
305       hasVolume = true;
306   }
307   return hasVolume;
308 }
309
310 //=================================================================================================
311 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_Prism::shape() const
312 {
313   return myShape;
314 }
315
316 //=================================================================================================
317 const ListOfShape& GeomAlgoAPI_Prism::fromFaces() const
318 {
319   return myFromFaces;
320 }
321
322 //=================================================================================================
323 const ListOfShape& GeomAlgoAPI_Prism::toFaces() const
324 {
325   return myToFaces;
326 }
327
328 //=================================================================================================
329 std::shared_ptr<GeomAPI_DataMapOfShapeShape> GeomAlgoAPI_Prism::mapOfShapes() const
330 {
331   return myMap;
332 }
333
334 //=================================================================================================
335 std::shared_ptr<GeomAlgoAPI_MakeShape> GeomAlgoAPI_Prism::makeShape() const
336 {
337   return myMkShape;
338 }