Salome HOME
36c644f39ddd2c935bd088f2165c5b38dfcd7a27
[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 <BRepFeat_MakePrism.hxx>
30 #include <BRepGProp.hxx>
31 #include <BRepOffsetAPI_MakePipe.hxx>
32 #include <BRepTools.hxx>
33 #include <Geom_Plane.hxx>
34 #include <gp_Pln.hxx>
35 #include <GProp_GProps.hxx>
36 #include <TCollection_AsciiString.hxx>
37 #include <TopExp_Explorer.hxx>
38 #include <TopoDS.hxx>
39 #include <TopoDS_Edge.hxx>
40 #include <TopoDS_Shell.hxx>
41 #include <TopoDS_Solid.hxx>
42 #include <TopoDS_Wire.hxx>
43 #include <TopTools_ListIteratorOfListOfShape.hxx>
44
45 //=================================================================================================
46 GeomAlgoAPI_Prism::GeomAlgoAPI_Prism(std::shared_ptr<GeomAPI_Shape> theBasis,
47                                      double                         theToSize,
48                                      double                         theFromSize)
49 : myDone(false)
50 {
51   build(theBasis, std::shared_ptr<GeomAPI_Shape>(), theToSize, std::shared_ptr<GeomAPI_Shape>(), theFromSize);
52 }
53
54 //=================================================================================================
55 GeomAlgoAPI_Prism::GeomAlgoAPI_Prism(std::shared_ptr<GeomAPI_Shape> theBasis,
56                                      std::shared_ptr<GeomAPI_Shape> theToShape,
57                                      double                         theToSize,
58                                      std::shared_ptr<GeomAPI_Shape> theFromShape,
59                                      double                         theFromSize)
60 : myDone(false)
61 {
62   build(theBasis, theToShape, theToSize, theFromShape, theFromSize);
63 }
64
65 //=================================================================================================
66 void GeomAlgoAPI_Prism::build(const std::shared_ptr<GeomAPI_Shape>& theBasis,
67                               const std::shared_ptr<GeomAPI_Shape>& theToShape,
68                               double                                theToSize,
69                               const std::shared_ptr<GeomAPI_Shape>& theFromShape,
70                               double                                theFromSize)
71 {
72   if(!theBasis ||
73     (((!theFromShape && !theToShape) || (theFromShape && theToShape && theFromShape->isEqual(theToShape)))
74     && (theFromSize == -theToSize))) {
75     return;
76   }
77
78   // If bounding faces was not set creating them.
79   std::shared_ptr<GeomAPI_Face> aBaseFace;
80   if(theBasis->shapeType() == GeomAPI_Shape::FACE) {
81     aBaseFace = std::shared_ptr<GeomAPI_Face>(new GeomAPI_Face(theBasis));
82   } else if(theBasis->shapeType() == GeomAPI_Shape::SHELL){
83     GeomAPI_ShapeExplorer anExp(theBasis, GeomAPI_Shape::FACE);
84     if(anExp.more()) {
85       std::shared_ptr<GeomAPI_Shape> aFaceOnShell = anExp.current();
86       aBaseFace = std::shared_ptr<GeomAPI_Face>(new GeomAPI_Face(aFaceOnShell));
87     }
88   }
89   if(!aBaseFace.get()) {
90     return;
91   }
92   std::shared_ptr<GeomAPI_Pln>   aBasePln = aBaseFace->getPlane();
93   std::shared_ptr<GeomAPI_Dir>   aBaseDir = aBasePln->direction();
94   std::shared_ptr<GeomAPI_Pnt>   aBaseLoc = aBasePln->location();
95   std::shared_ptr<GeomAPI_Shape> aBasePlane = GeomAlgoAPI_FaceBuilder::planarFace(aBaseLoc, aBaseDir);
96
97   std::shared_ptr<GeomAPI_Shape> aBoundingFromShape = theFromShape ? theFromShape : aBasePlane;
98   std::shared_ptr<GeomAPI_Shape> aBoundingToShape   = theToShape   ? theToShape   : aBasePlane;
99
100   // Moving bounding faces according to "from" and "to" sizes.
101   std::shared_ptr<GeomAPI_Face> aFromFace(new GeomAPI_Face(aBoundingFromShape));
102   std::shared_ptr<GeomAPI_Pln>  aFromPln = aFromFace->getPlane();
103   std::shared_ptr<GeomAPI_Pnt>  aFromLoc = aFromPln->location();
104   std::shared_ptr<GeomAPI_Dir>  aFromDir = aFromPln->direction();
105
106   std::shared_ptr<GeomAPI_Face> aToFace(new GeomAPI_Face(aBoundingToShape));
107   std::shared_ptr<GeomAPI_Pln>  aToPln = aToFace->getPlane();
108   std::shared_ptr<GeomAPI_Pnt>  aToLoc = aToPln->location();
109   std::shared_ptr<GeomAPI_Dir>  aToDir = aToPln->direction();
110
111   bool aSign = aFromLoc->xyz()->dot(aBaseDir->xyz()) > aToLoc->xyz()->dot(aBaseDir->xyz());
112
113   std::shared_ptr<GeomAPI_Pnt> aFromPnt(new GeomAPI_Pnt(aFromLoc->xyz()->added(aBaseDir->xyz()->multiplied(
114                                                         aSign ? theFromSize : -theFromSize))));
115   aBoundingFromShape = GeomAlgoAPI_FaceBuilder::planarFace(aFromPnt, aFromDir);
116
117   std::shared_ptr<GeomAPI_Pnt> aToPnt(new GeomAPI_Pnt(aToLoc->xyz()->added(aBaseDir->xyz()->multiplied(
118                                                       aSign ? -theToSize : theToSize))));
119   aBoundingToShape = GeomAlgoAPI_FaceBuilder::planarFace(aToPnt, aToDir);
120
121   if(theBasis->shapeType() == GeomAPI_Shape::FACE) {
122     TopoDS_Face aBasis = TopoDS::Face(aBaseFace->impl<TopoDS_Shape>());
123     const gp_Dir& aNormal = aBaseDir->impl<gp_Dir>();
124     BRepFeat_MakePrism* aBuilder = new BRepFeat_MakePrism(aBasis, aBasis, aBasis, aNormal, 2, Standard_True);
125
126     if(aBuilder) {
127       const TopoDS_Shape& aFromShape = aBoundingFromShape->impl<TopoDS_Shape>();
128       const TopoDS_Shape& aToShape   = aBoundingToShape->impl<TopoDS_Shape>();
129       aBuilder->Perform(aFromShape, aToShape);
130       myDone = aBuilder->IsDone() == Standard_True;
131       if(myDone){
132         TopoDS_Shape aResult = aBuilder->Shape();
133         TopExp_Explorer anExp(aResult, TopAbs_SOLID);
134         if(!anExp.More()) {
135           return;
136         }
137         if(aResult.ShapeType() == TopAbs_COMPOUND) {
138           aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
139         }
140         // fill data map to keep correct orientation of sub-shapes
141         myMap.reset(new GeomAPI_DataMapOfShapeShape);
142         for (TopExp_Explorer Exp(aResult,TopAbs_FACE); Exp.More(); Exp.Next()) {
143           std::shared_ptr<GeomAPI_Shape> aCurrentShape(new GeomAPI_Shape());
144           aCurrentShape->setImpl(new TopoDS_Shape(Exp.Current()));
145           myMap->bind(aCurrentShape, aCurrentShape);
146         }
147         myShape.reset(new GeomAPI_Shape);
148         myShape->setImpl(new TopoDS_Shape(aResult));
149         std::shared_ptr<GeomAPI_Shape> aFrom(new GeomAPI_Shape());
150         aFrom->setImpl(new TopoDS_Shape(aBuilder->Modified(aFromShape).First()));
151         myFromFaces.push_back(aFrom);
152         std::shared_ptr<GeomAPI_Shape> aTo(new GeomAPI_Shape());
153         aTo->setImpl(new TopoDS_Shape(aBuilder->Modified(aToShape).First()));
154         myToFaces.push_back(aTo);
155         myMkShape.reset(new GeomAlgoAPI_MakeShape(aBuilder));
156       }
157     }
158   } else {
159     // Getting bounding box for base shape.
160     const TopoDS_Shape& aBasisShape = theBasis->impl<TopoDS_Shape>();
161     Bnd_Box aBndBox;
162     BRepBndLib::Add(aBasisShape, aBndBox);
163     Standard_Real aXArr[2] = {aBndBox.CornerMin().X(), aBndBox.CornerMax().X()};
164     Standard_Real aYArr[2] = {aBndBox.CornerMin().Y(), aBndBox.CornerMax().Y()};
165     Standard_Real aZArr[2] = {aBndBox.CornerMin().Z(), aBndBox.CornerMax().Z()};
166     gp_Pnt aPoints[8];
167     int aNum = 0;
168     for(int i = 0; i < 2; i++) {
169       for(int j = 0; j < 2; j++) {
170         for(int k = 0; k < 2; k++) {
171           aPoints[aNum] = gp_Pnt(aXArr[i], aYArr[j], aZArr[k]);
172           aNum++;
173         }
174       }
175     }
176
177     // Project points to bounding planes. Search max distance to them.
178     const TopoDS_Shape& aBndToShape = aBoundingToShape->impl<TopoDS_Shape>();
179     const TopoDS_Shape& aBndFromShape = aBoundingFromShape->impl<TopoDS_Shape>();
180     Standard_Real aMaxToDist = 0, aMaxFromDist = 0;
181     gp_Vec aNormal(aBaseDir->impl<gp_Dir>());
182     for(int i = 0; i < 8; i++) {
183       gp_Lin aLine(aPoints[i], 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       if(aToExt.NbExt() == 0 || aFromExt.NbExt() == 0) {
188         return;
189       }
190       const gp_Pnt& aPntOnToFace = aToExt.PointOnFace(1);
191       const gp_Pnt& aPntOnFromFace = aFromExt.PointOnFace(1);
192       if(aPoints[i].Distance(aPntOnToFace) > aMaxToDist) {
193         aMaxToDist = aPoints[i].Distance(aPntOnToFace);
194       }
195       if(aPoints[i].Distance(aPntOnFromFace) > aMaxFromDist) {
196         aMaxFromDist = aPoints[i].Distance(aPntOnFromFace);
197       }
198     }
199     // We added 1 just to be sure that pipe is long enough for boolean operation.
200     Standard_Real aPipeLength = aMaxToDist + aMaxFromDist + 1;
201
202     // Making wire for pipe.
203     std::shared_ptr<GeomAPI_Pnt> aCentreOfMass = GeomAlgoAPI_ShapeTools::centreOfMass(theBasis);
204     const gp_Pnt aCentrePnt = aCentreOfMass->impl<gp_Pnt>();
205     TopoDS_Face aFace = TopoDS::Face(aBaseFace->impl<TopoDS_Shape>());
206     gp_Pnt aPipeStartPnt = aCentrePnt.Translated(aNormal.Scaled(aPipeLength));
207     gp_Pnt aPipeEndPnt = aCentrePnt.Translated(aNormal.Scaled(-aPipeLength));
208     TopoDS_Edge aPipeEdge = BRepBuilderAPI_MakeEdge(aPipeStartPnt, aPipeEndPnt);
209     TopoDS_Wire aPipeWire = BRepBuilderAPI_MakeWire(aPipeEdge).Wire();
210
211     // Making pipe.
212     ListOfMakeShape aListOfMakeShape;
213     BRepOffsetAPI_MakePipe* aPipeBuilder = new BRepOffsetAPI_MakePipe(aPipeWire, aBasisShape);
214     if(!aPipeBuilder) {
215       return;
216     }
217     std::shared_ptr<GeomAPI_Shape> aWire(new GeomAPI_Shape);
218     std::shared_ptr<GeomAPI_Shape> aBShape(new GeomAPI_Shape);
219     aWire->setImpl(new TopoDS_Shape(aPipeWire));
220     aBShape->setImpl(new TopoDS_Shape(aBasisShape));
221     aListOfMakeShape.push_back(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aPipeBuilder, aWire, aBShape)));
222     TopoDS_Shape aResult = aPipeBuilder->Shape();
223
224     // Orienting bounding planes.
225     gp_Lin aLine(aCentrePnt, aNormal);
226     TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge(aLine).Edge();
227     BRepExtrema_ExtCF aToExt(anEdge, TopoDS::Face(aBndToShape));
228     BRepExtrema_ExtCF aFromExt(anEdge, TopoDS::Face(aBndFromShape));
229     Standard_Real aToParameter = aToExt.ParameterOnEdge(1);
230     Standard_Real aFromParameter = aFromExt.ParameterOnEdge(1);
231     if(aToParameter > aFromParameter) {
232       gp_Vec aVec = aToDir->impl<gp_Dir>();
233       if((aVec * aNormal) > 0) {
234         aToDir->setImpl(new gp_Dir(aVec.Reversed()));
235         aBoundingToShape = GeomAlgoAPI_FaceBuilder::planarFace(aToPnt, aToDir);
236       }
237       aVec = aFromDir->impl<gp_Dir>();
238       if((aVec * aNormal) < 0) {
239         aFromDir->setImpl(new gp_Dir(aVec.Reversed()));
240         aBoundingFromShape = GeomAlgoAPI_FaceBuilder::planarFace(aFromPnt, aFromDir);
241       }
242     } else {
243       gp_Vec aVec = aToDir->impl<gp_Dir>();
244       if((aVec * aNormal) < 0) {
245         aToDir->setImpl(new gp_Dir(aVec.Reversed()));
246         aBoundingToShape = GeomAlgoAPI_FaceBuilder::planarFace(aToPnt, aToDir);
247       }
248       aVec = aFromDir->impl<gp_Dir>();
249       if((aVec * aNormal) > 0) {
250         aFromDir->setImpl(new gp_Dir(aVec.Reversed()));
251         aBoundingFromShape = GeomAlgoAPI_FaceBuilder::planarFace(aFromPnt, aFromDir);
252       }
253     }
254
255     // Making solids from bounding planes.
256     TopoDS_Shell aToShell, aFromShell;
257     TopoDS_Solid aToSolid, aFromSolid;
258     const TopoDS_Shape& aToShape   = aBoundingToShape->impl<TopoDS_Shape>();
259     const TopoDS_Shape& aFromShape = aBoundingFromShape->impl<TopoDS_Shape>();
260     BRep_Builder aBoundingBuilder;
261     aBoundingBuilder.MakeShell(aToShell);
262     aBoundingBuilder.MakeShell(aFromShell);
263     aBoundingBuilder.Add(aToShell, aToShape);
264     aBoundingBuilder.Add(aFromShell, aFromShape);
265     aBoundingBuilder.MakeSolid(aToSolid);
266     aBoundingBuilder.MakeSolid(aFromSolid);
267     aBoundingBuilder.Add(aToSolid, aToShell);
268     aBoundingBuilder.Add(aFromSolid, aFromShell);
269
270     // Cutting with to plane.
271     BRepAlgoAPI_Cut* aToCutBuilder = new BRepAlgoAPI_Cut(aResult, aToSolid);
272     aToCutBuilder->Build();
273     if(!aToCutBuilder->IsDone()) {
274       return;
275     }
276     aListOfMakeShape.push_back(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aToCutBuilder)));
277     const TopTools_ListOfShape& aToShapes = aToCutBuilder->Modified(aToShape);
278     for(TopTools_ListIteratorOfListOfShape anIt(aToShapes); anIt.More(); anIt.Next()) {
279       std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
280       aShape->setImpl(new TopoDS_Shape(anIt.Value()));
281       myToFaces.push_back(aShape);
282     }
283     aResult = aToCutBuilder->Shape();
284
285     // Cutting with from plane.
286     BRepAlgoAPI_Cut* aFromCutBuilder = new BRepAlgoAPI_Cut(aResult, aFromSolid);
287     aFromCutBuilder->Build();
288     if(!aFromCutBuilder->IsDone()) {
289       return;
290     }
291     aListOfMakeShape.push_back(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aFromCutBuilder)));
292     const TopTools_ListOfShape& aFromShapes = aFromCutBuilder->Modified(aFromShape);
293     for(TopTools_ListIteratorOfListOfShape anIt(aFromShapes); anIt.More(); anIt.Next()) {
294       std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
295       aShape->setImpl(new TopoDS_Shape(anIt.Value()));
296       myFromFaces.push_back(aShape);
297     }
298     aResult = aFromCutBuilder->Shape();
299
300     TopExp_Explorer anExp(aResult, TopAbs_SOLID);
301     if(!anExp.More()) {
302       return;
303     }
304     if(aResult.ShapeType() == TopAbs_COMPOUND) {
305       aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
306     }
307     if(aResult.ShapeType() == TopAbs_COMPOUND) {
308       std::shared_ptr<GeomAPI_Shape> aCompound(new GeomAPI_Shape);
309       aCompound->setImpl(new TopoDS_Shape(aResult));
310       ListOfShape aCompSolids, aFreeSolids;
311       GeomAlgoAPI_ShapeTools::combineShapes(aCompound, GeomAPI_Shape::COMPSOLID, aCompSolids, aFreeSolids);
312       if(aCompSolids.size() == 1 && aFreeSolids.size() == 0) {
313         aResult = aCompSolids.front()->impl<TopoDS_Shape>();
314       } else if (aCompSolids.size() > 1 || (aCompSolids.size() >= 1 && aFreeSolids.size() >= 1)) {
315         TopoDS_Compound aResultComp;
316         TopoDS_Builder aBuilder;
317         aBuilder.MakeCompound(aResultComp);
318         for(ListOfShape::const_iterator anIter = aCompSolids.cbegin(); anIter != aCompSolids.cend(); anIter++) {
319           aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
320         }
321         for(ListOfShape::const_iterator anIter = aFreeSolids.cbegin(); anIter != aFreeSolids.cend(); anIter++) {
322           aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
323         }
324         aResult = aResultComp;
325       }
326     }
327
328     // Fill data map to keep correct orientation of sub-shapes.
329     myMap = std::shared_ptr<GeomAPI_DataMapOfShapeShape>(new GeomAPI_DataMapOfShapeShape);
330     for (TopExp_Explorer Exp(aResult,TopAbs_FACE); Exp.More(); Exp.Next()) {
331       std::shared_ptr<GeomAPI_Shape> aCurrentShape(new GeomAPI_Shape());
332       aCurrentShape->setImpl(new TopoDS_Shape(Exp.Current()));
333       myMap->bind(aCurrentShape, aCurrentShape);
334     }
335     myShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape);
336     myShape->setImpl(new TopoDS_Shape(aResult));
337     myMkShape = std::shared_ptr<GeomAlgoAPI_MakeShapeList>(new GeomAlgoAPI_MakeShapeList(aListOfMakeShape));
338     myDone = true;
339   }
340 }
341
342 //=================================================================================================
343 bool GeomAlgoAPI_Prism::isDone() const
344 {
345   return myDone;
346 }
347
348 //=================================================================================================
349 bool GeomAlgoAPI_Prism::isValid() const
350 {
351   BRepCheck_Analyzer aChecker(myShape->impl<TopoDS_Shape>());
352   return (aChecker.IsValid() == Standard_True);
353 }
354
355 //=================================================================================================
356 bool GeomAlgoAPI_Prism::hasVolume() const
357 {
358   bool hasVolume(false);
359   if(isValid()) {
360     const TopoDS_Shape& aRShape = myShape->impl<TopoDS_Shape>();
361     GProp_GProps aGProp;
362     BRepGProp::VolumeProperties(aRShape, aGProp);
363     if(aGProp.Mass() > Precision::Confusion())
364       hasVolume = true;
365   }
366   return hasVolume;
367 }
368
369 //=================================================================================================
370 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_Prism::shape() const
371 {
372   return myShape;
373 }
374
375 //=================================================================================================
376 const ListOfShape& GeomAlgoAPI_Prism::fromFaces() const
377 {
378   return myFromFaces;
379 }
380
381 //=================================================================================================
382 const ListOfShape& GeomAlgoAPI_Prism::toFaces() const
383 {
384   return myToFaces;
385 }
386
387 //=================================================================================================
388 std::shared_ptr<GeomAPI_DataMapOfShapeShape> GeomAlgoAPI_Prism::mapOfShapes() const
389 {
390   return myMap;
391 }
392
393 //=================================================================================================
394 std::shared_ptr<GeomAlgoAPI_MakeShape> GeomAlgoAPI_Prism::makeShape() const
395 {
396   return myMkShape;
397 }