]> SALOME platform Git repositories - modules/shaper.git/blob - src/GeomAlgoAPI/GeomAlgoAPI_Prism.cpp
Salome HOME
Issue #1343 Fixes
[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_ShapeTools.h>
17
18 #include <Bnd_Box.hxx>
19 #include <BRep_Builder.hxx>
20 #include <BRepAlgoAPI_Cut.hxx>
21 #include <BRepBndLib.hxx>
22 #include <BRepBuilderAPI_FindPlane.hxx>
23 #include <BRepBuilderAPI_Transform.hxx>
24 #include <Geom_Curve.hxx>
25 #include <Geom2d_Curve.hxx>
26 #include <BRepLib_CheckCurveOnSurface.hxx>
27 #include <BRepPrimAPI_MakePrism.hxx>
28 #include <Geom_Plane.hxx>
29 #include <gp_Pln.hxx>
30 #include <IntAna_IntConicQuad.hxx>
31 #include <IntAna_Quadric.hxx>
32 #include <IntTools_Context.hxx>
33 #include <TopExp_Explorer.hxx>
34 #include <TopoDS.hxx>
35 #include <TopoDS_Edge.hxx>
36 #include <TopoDS_Shell.hxx>
37 #include <TopoDS_Solid.hxx>
38 #include <TopTools_ListIteratorOfListOfShape.hxx>
39
40 #include <BRepTools.hxx>
41
42 //=================================================================================================
43 GeomAlgoAPI_Prism::GeomAlgoAPI_Prism(const GeomShapePtr theBaseShape,
44                                      const double       theToSize,
45                                      const double       theFromSize)
46 {
47   build(theBaseShape, std::shared_ptr<GeomAPI_Dir>(), GeomShapePtr(), theToSize, GeomShapePtr(), theFromSize);
48 }
49
50 //=================================================================================================
51 GeomAlgoAPI_Prism::GeomAlgoAPI_Prism(const GeomShapePtr                 theBaseShape,
52                                      const std::shared_ptr<GeomAPI_Dir> theDirection,
53                                      const double                       theToSize,
54                                      const double                       theFromSize)
55 {
56   build(theBaseShape, theDirection, GeomShapePtr(), theToSize, GeomShapePtr(), theFromSize);
57 }
58
59 //=================================================================================================
60 GeomAlgoAPI_Prism::GeomAlgoAPI_Prism(const GeomShapePtr theBaseShape,
61                                      const GeomShapePtr theToShape,
62                                      const double       theToSize,
63                                      const GeomShapePtr theFromShape,
64                                      const double       theFromSize)
65 {
66   build(theBaseShape, std::shared_ptr<GeomAPI_Dir>(), theToShape, theToSize, theFromShape, theFromSize);
67 }
68
69 //=================================================================================================
70 GeomAlgoAPI_Prism::GeomAlgoAPI_Prism(const GeomShapePtr                 theBaseShape,
71                                      const std::shared_ptr<GeomAPI_Dir> theDirection,
72                                      const GeomShapePtr                 theToShape,
73                                      const double                       theToSize,
74                                      const GeomShapePtr                 theFromShape,
75                                      const double                       theFromSize)
76 {
77   build(theBaseShape, theDirection, theToShape, theToSize, theFromShape, theFromSize);
78 }
79
80 //=================================================================================================
81 void GeomAlgoAPI_Prism::build(const GeomShapePtr&                theBaseShape,
82                               const std::shared_ptr<GeomAPI_Dir> theDirection,
83                               const GeomShapePtr&                theToShape,
84                               const double                       theToSize,
85                               const GeomShapePtr&                theFromShape,
86                               const double                       theFromSize)
87 {
88   if(!theBaseShape.get() ||
89     (((!theFromShape.get() && !theToShape.get()) || (theFromShape.get() && theToShape.get() && theFromShape->isEqual(theToShape)))
90     && (theFromSize == -theToSize))) {
91     return;
92   }
93
94   // Getting base shape.
95   const TopoDS_Shape& aBaseShape = theBaseShape->impl<TopoDS_Shape>();
96   TopAbs_ShapeEnum aShapeTypeToExp;
97   switch(aBaseShape.ShapeType()) {
98     case TopAbs_VERTEX:
99       aShapeTypeToExp = TopAbs_VERTEX;
100       break;
101     case TopAbs_EDGE:
102     case TopAbs_WIRE:
103       aShapeTypeToExp = TopAbs_EDGE;
104       break;
105     case TopAbs_FACE:
106     case TopAbs_SHELL:
107       aShapeTypeToExp = TopAbs_FACE;
108       break;
109     default:
110       return;
111   }
112
113   // Getting direction.
114   gp_Pnt aLoc;
115   gp_Vec aDirVec;
116   std::shared_ptr<GeomAPI_Pnt> aBaseLoc;
117   std::shared_ptr<GeomAPI_Dir> aBaseDir;
118   GeomShapePtr aBasePlane;
119   const bool isBoundingShapesSet = theFromShape.get() || theToShape.get();
120   BRepBuilderAPI_FindPlane aFindPlane(aBaseShape);
121   if(aBaseShape.ShapeType() == TopAbs_VERTEX || aBaseShape.ShapeType() == TopAbs_EDGE ||
122      aFindPlane.Found() != Standard_True) {
123     // Direction and both bounding planes should be set or empty.
124     if(!theDirection.get()) {
125       return;
126     }
127
128     aBaseDir = theDirection;
129     aDirVec = theDirection->impl<gp_Dir>();
130     gp_XYZ aDirXYZ = aDirVec.XYZ();
131     Standard_Real aMinParam = std::numeric_limits<double>::max();
132
133     for(TopExp_Explorer anExp(aBaseShape, TopAbs_VERTEX); anExp.More(); anExp.Next()) {
134       const TopoDS_Shape& aVertex = anExp.Current();
135       gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aVertex));
136       double aParam = aDirXYZ.Dot(aPnt.XYZ());
137       if(aParam < aMinParam) {
138         aMinParam = aParam;
139         aLoc = aPnt;
140       }
141     }
142   } else {
143     Handle(Geom_Plane) aPlane = aFindPlane.Plane();
144     aLoc = aPlane->Axis().Location();
145     aDirVec = aPlane->Axis().Direction();
146
147     aBaseDir.reset(new GeomAPI_Dir(aDirVec.X(), aDirVec.Y(), aDirVec.Z()));
148   }
149   aBaseLoc.reset(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
150   aBasePlane = GeomAlgoAPI_FaceBuilder::planarFace(aBaseLoc, aBaseDir);
151
152   TopoDS_Shape aResult;
153   if(!isBoundingShapesSet) {
154     // Moving base shape.
155     gp_Trsf aTrsf;
156     aTrsf.SetTranslation(aDirVec * -theFromSize);
157     BRepBuilderAPI_Transform* aTransformBuilder = new BRepBuilderAPI_Transform(aBaseShape, aTrsf);
158     if(!aTransformBuilder) {
159       return;
160     }
161     this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aTransformBuilder)));
162     if(!aTransformBuilder->IsDone()) {
163       return;
164     }
165     TopoDS_Shape aMovedBase = aTransformBuilder->Shape();
166
167     // Making prism.
168     BRepPrimAPI_MakePrism* aPrismBuilder = new BRepPrimAPI_MakePrism(aMovedBase, aDirVec * (theFromSize + theToSize));
169     if(!aPrismBuilder) {
170       return;
171     }
172     this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aPrismBuilder)));
173     if(!aPrismBuilder->IsDone()) {
174       return;
175     }
176     aResult = aPrismBuilder->Shape();
177
178     // Setting naming.
179     for(TopExp_Explorer anExp(aMovedBase, aShapeTypeToExp); anExp.More(); anExp.Next()) {
180       const TopoDS_Shape& aShape = anExp.Current();
181       GeomShapePtr aFromShape(new GeomAPI_Shape), aToShape(new GeomAPI_Shape);
182       aFromShape->setImpl(new TopoDS_Shape(aPrismBuilder->FirstShape(aShape)));
183       aToShape->setImpl(new TopoDS_Shape(aPrismBuilder->LastShape(aShape)));
184       this->addFromShape(aFromShape);
185       this->addToShape(aToShape);
186     }
187   } else {
188     GeomShapePtr aBoundingFromShape = theFromShape ? theFromShape : aBasePlane;
189     GeomShapePtr aBoundingToShape   = theToShape   ? theToShape   : aBasePlane;
190
191     // Moving prism bounding faces according to "from" and "to" sizes.
192     std::shared_ptr<GeomAPI_Pln> aFromPln = GeomAPI_Face(aBoundingFromShape).getPlane();
193     std::shared_ptr<GeomAPI_Pnt> aFromLoc = aFromPln->location();
194     std::shared_ptr<GeomAPI_Dir> aFromDir = aFromPln->direction();
195
196     std::shared_ptr<GeomAPI_Pln> aToPln = GeomAPI_Face(aBoundingToShape).getPlane();
197     std::shared_ptr<GeomAPI_Pnt> aToLoc = aToPln->location();
198     std::shared_ptr<GeomAPI_Dir> aToDir = aToPln->direction();
199
200     bool aSign = aFromLoc->xyz()->dot(aBaseDir->xyz()) > aToLoc->xyz()->dot(aBaseDir->xyz());
201
202     std::shared_ptr<GeomAPI_Pnt> aFromPnt(new GeomAPI_Pnt(aFromLoc->xyz()->added(aBaseDir->xyz()->multiplied(
203                                                           aSign ? theFromSize : -theFromSize))));
204     aBoundingFromShape = GeomAlgoAPI_FaceBuilder::planarFace(aFromPnt, aFromDir);
205
206     std::shared_ptr<GeomAPI_Pnt> aToPnt(new GeomAPI_Pnt(aToLoc->xyz()->added(aBaseDir->xyz()->multiplied(
207                                                         aSign ? -theToSize : theToSize))));
208     aBoundingToShape = GeomAlgoAPI_FaceBuilder::planarFace(aToPnt, aToDir);
209
210     // Getting bounding box for base shape.
211     Bnd_Box aBndBox;
212     BRepBndLib::Add(aBaseShape, aBndBox);
213     Standard_Real aXArr[2] = {aBndBox.CornerMin().X(), aBndBox.CornerMax().X()};
214     Standard_Real aYArr[2] = {aBndBox.CornerMin().Y(), aBndBox.CornerMax().Y()};
215     Standard_Real aZArr[2] = {aBndBox.CornerMin().Z(), aBndBox.CornerMax().Z()};
216     gp_Pnt aPoints[8];
217     int aNum = 0;
218     for(int i = 0; i < 2; i++) {
219       for(int j = 0; j < 2; j++) {
220         for(int k = 0; k < 2; k++) {
221           aPoints[aNum] = gp_Pnt(aXArr[i], aYArr[j], aZArr[k]);
222           aNum++;
223         }
224       }
225     }
226
227     // Project points to bounding planes. Search max distance to them.
228     IntAna_Quadric aBndToQuadric(gp_Pln(aToPnt->impl<gp_Pnt>(), aToDir->impl<gp_Dir>()));
229     IntAna_Quadric aBndFromQuadric(gp_Pln(aFromPnt->impl<gp_Pnt>(), aFromDir->impl<gp_Dir>()));
230     Standard_Real aMaxToDist = 0, aMaxFromDist = 0;
231     for(int i = 0; i < 8; i++) {
232       gp_Lin aLine(aPoints[i], aDirVec);
233       IntAna_IntConicQuad aToIntAna(aLine, aBndToQuadric);
234       IntAna_IntConicQuad aFromIntAna(aLine, aBndFromQuadric);
235       if(aToIntAna.NbPoints() == 0 || aFromIntAna.NbPoints() == 0) {
236         return;
237       }
238       const gp_Pnt& aPntOnToFace = aToIntAna.Point(1);
239       const gp_Pnt& aPntOnFromFace = aFromIntAna.Point(1);
240       if(aPoints[i].Distance(aPntOnToFace) > aMaxToDist) {
241         aMaxToDist = aPoints[i].Distance(aPntOnToFace);
242       }
243       if(aPoints[i].Distance(aPntOnFromFace) > aMaxFromDist) {
244         aMaxFromDist = aPoints[i].Distance(aPntOnFromFace);
245       }
246     }
247
248     // We added 1 just to be sure that prism is long enough for boolean operation.
249     double aPrismLength = aMaxToDist + aMaxFromDist + 1;
250
251     // Moving base shape.
252     gp_Trsf aTrsf;
253     aTrsf.SetTranslation(aDirVec * -aPrismLength);
254     BRepBuilderAPI_Transform* aTransformBuilder = new BRepBuilderAPI_Transform(aBaseShape, aTrsf);
255     if(!aTransformBuilder) {
256       return;
257     }
258     this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aTransformBuilder)));
259     if(!aTransformBuilder->IsDone()) {
260       return;
261     }
262     TopoDS_Shape aMovedBase = aTransformBuilder->Shape();
263
264     // Making prism.
265     BRepPrimAPI_MakePrism* aPrismBuilder = new BRepPrimAPI_MakePrism(aMovedBase, aDirVec * 2 * aPrismLength);
266     if(!aPrismBuilder) {
267       return;
268     }
269     this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aPrismBuilder)));
270     if(!aPrismBuilder->IsDone()) {
271       return;
272     }
273     aResult = aPrismBuilder->Shape();
274
275     // Orienting bounding planes.
276     std::shared_ptr<GeomAPI_Pnt> aCentreOfMass = GeomAlgoAPI_ShapeTools::centreOfMass(theBaseShape);
277     const gp_Pnt& aCentrePnt = aCentreOfMass->impl<gp_Pnt>();
278     gp_Lin aLine(aCentrePnt, aDirVec);
279     IntAna_IntConicQuad aToIntAna(aLine, aBndToQuadric);
280     IntAna_IntConicQuad aFromIntAna(aLine, aBndFromQuadric);
281     Standard_Real aToParameter = aToIntAna.ParamOnConic(1);
282     Standard_Real aFromParameter = aFromIntAna.ParamOnConic(1);
283     if(aToParameter > aFromParameter) {
284       gp_Vec aVec = aToDir->impl<gp_Dir>();
285       if((aVec * aDirVec) > 0) {
286         aToDir->setImpl(new gp_Dir(aVec.Reversed()));
287         aBoundingToShape = GeomAlgoAPI_FaceBuilder::planarFace(aToPnt, aToDir);
288       }
289       aVec = aFromDir->impl<gp_Dir>();
290       if((aVec * aDirVec) < 0) {
291         aFromDir->setImpl(new gp_Dir(aVec.Reversed()));
292         aBoundingFromShape = GeomAlgoAPI_FaceBuilder::planarFace(aFromPnt, aFromDir);
293       }
294     } else {
295       gp_Vec aVec = aToDir->impl<gp_Dir>();
296       if((aVec * aDirVec) < 0) {
297         aToDir->setImpl(new gp_Dir(aVec.Reversed()));
298         aBoundingToShape = GeomAlgoAPI_FaceBuilder::planarFace(aToPnt, aToDir);
299       }
300       aVec = aFromDir->impl<gp_Dir>();
301       if((aVec * aDirVec) > 0) {
302         aFromDir->setImpl(new gp_Dir(aVec.Reversed()));
303         aBoundingFromShape = GeomAlgoAPI_FaceBuilder::planarFace(aFromPnt, aFromDir);
304       }
305     }
306
307     // Making solids from bounding planes.
308     TopoDS_Shell aToShell, aFromShell;
309     TopoDS_Solid aToSolid, aFromSolid;
310     const TopoDS_Shape& aToShape   = aBoundingToShape->impl<TopoDS_Shape>();
311     const TopoDS_Shape& aFromShape = aBoundingFromShape->impl<TopoDS_Shape>();
312     TopoDS_Face aToFace   = TopoDS::Face(aToShape);
313     TopoDS_Face aFromFace = TopoDS::Face(aFromShape);
314     BRep_Builder aBoundingBuilder;
315     aBoundingBuilder.MakeShell(aToShell);
316     aBoundingBuilder.Add(aToShell, aToShape);
317     aBoundingBuilder.MakeShell(aFromShell);
318     aBoundingBuilder.Add(aFromShell, aFromShape);
319     aBoundingBuilder.MakeSolid(aToSolid);
320     aBoundingBuilder.Add(aToSolid, aToShell);
321     aBoundingBuilder.MakeSolid(aFromSolid);
322     aBoundingBuilder.Add(aFromSolid, aFromShell);
323
324     // Cutting with to plane.
325     BRepAlgoAPI_Cut* aToCutBuilder = new BRepAlgoAPI_Cut(aResult, aToSolid);
326     aToCutBuilder->Build();
327     if(!aToCutBuilder->IsDone()) {
328       return;
329     }
330     this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aToCutBuilder)));
331     aResult = aToCutBuilder->Shape();
332     if(aResult.ShapeType() == TopAbs_COMPOUND) {
333       aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
334     }
335     if(aShapeTypeToExp == TopAbs_FACE) {
336       const TopTools_ListOfShape& aToShapes = aToCutBuilder->Modified(aToShape);
337       for(TopTools_ListIteratorOfListOfShape anIt(aToShapes); anIt.More(); anIt.Next()) {
338         GeomShapePtr aGeomSh(new GeomAPI_Shape());
339         aGeomSh->setImpl(new TopoDS_Shape(anIt.Value()));
340         this->addToShape(aGeomSh);
341       }
342     }
343
344     // Cutting with from plane.
345     BRepAlgoAPI_Cut* aFromCutBuilder = new BRepAlgoAPI_Cut(aResult, aFromSolid);
346     aFromCutBuilder->Build();
347     if(!aFromCutBuilder->IsDone()) {
348       return;
349     }
350     this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aFromCutBuilder)));
351     aResult = aFromCutBuilder->Shape();
352     TopoDS_Iterator aCheckIt(aResult);
353     if(!aCheckIt.More()) {
354       return;
355     }
356     if(aResult.ShapeType() == TopAbs_COMPOUND) {
357       aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
358     }
359     if(aShapeTypeToExp == TopAbs_FACE) {
360       const TopTools_ListOfShape& aFromShapes = aFromCutBuilder->Modified(aFromShape);
361       for(TopTools_ListIteratorOfListOfShape anIt(aFromShapes); anIt.More(); anIt.Next()) {
362         GeomShapePtr aGeomSh(new GeomAPI_Shape());
363         aGeomSh->setImpl(new TopoDS_Shape(anIt.Value()));
364         this->addFromShape(aGeomSh);
365       }
366     }
367
368     // Naming for extrusion from vertex, edge.
369     for(TopExp_Explorer anExp(aResult, aShapeTypeToExp); anExp.More(); anExp.Next()) {
370       const TopoDS_Shape& aShape = anExp.Current();
371       if(aShapeTypeToExp == TopAbs_VERTEX) {
372         gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aShape));
373         IntTools_Context anIntTools;
374         if(anIntTools.IsValidPointForFace(aPnt, aToFace, Precision::Confusion()) == Standard_True) {
375           GeomShapePtr aGeomSh(new GeomAPI_Shape());
376           aGeomSh->setImpl(new TopoDS_Shape(aShape));
377           this->addToShape(aGeomSh);
378         }
379         if(anIntTools.IsValidPointForFace(aPnt, aFromFace, Precision::Confusion()) == Standard_True) {
380           GeomShapePtr aGeomSh(new GeomAPI_Shape());
381           aGeomSh->setImpl(new TopoDS_Shape(aShape));
382           this->addFromShape(aGeomSh);
383         }
384       } else if(aShapeTypeToExp == TopAbs_EDGE) {
385         TopoDS_Edge anEdge = TopoDS::Edge(aShape);
386         BRepLib_CheckCurveOnSurface anEdgeCheck(anEdge, aToFace);
387         anEdgeCheck.Perform();
388         if(anEdgeCheck.MaxDistance() < Precision::Confusion()) {
389           GeomShapePtr aGeomSh(new GeomAPI_Shape());
390           aGeomSh->setImpl(new TopoDS_Shape(aShape));
391           this->addToShape(aGeomSh);
392         }
393         anEdgeCheck.Init(anEdge, aFromFace);
394         anEdgeCheck.Perform();
395         if(anEdgeCheck.MaxDistance() < Precision::Confusion()) {
396           GeomShapePtr aGeomSh(new GeomAPI_Shape());
397           aGeomSh->setImpl(new TopoDS_Shape(aShape));
398           this->addFromShape(aGeomSh);
399         }
400       } else {
401         break;
402       }
403     }
404
405     if(aResult.ShapeType() == TopAbs_COMPOUND) {
406       GeomShapePtr aCompound(new GeomAPI_Shape);
407       aCompound->setImpl(new TopoDS_Shape(aResult));
408       ListOfShape aCompSolids, aFreeSolids;
409       GeomAlgoAPI_ShapeTools::combineShapes(aCompound, GeomAPI_Shape::COMPSOLID, aCompSolids, aFreeSolids);
410       if(aCompSolids.size() == 1 && aFreeSolids.size() == 0) {
411         aResult = aCompSolids.front()->impl<TopoDS_Shape>();
412       } else if (aCompSolids.size() > 1 || (aCompSolids.size() >= 1 && aFreeSolids.size() >= 1)) {
413         TopoDS_Compound aResultComp;
414         TopoDS_Builder aBuilder;
415         aBuilder.MakeCompound(aResultComp);
416         for(ListOfShape::const_iterator anIter = aCompSolids.cbegin(); anIter != aCompSolids.cend(); anIter++) {
417           aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
418         }
419         for(ListOfShape::const_iterator anIter = aFreeSolids.cbegin(); anIter != aFreeSolids.cend(); anIter++) {
420           aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
421         }
422         aResult = aResultComp;
423       }
424     }
425   }
426
427   // Setting result.
428   if(aResult.IsNull()) {
429     return;
430   }
431   GeomShapePtr aGeomSh(new GeomAPI_Shape());
432   aGeomSh->setImpl(new TopoDS_Shape(aResult));
433   this->setShape(aGeomSh);
434   this->setDone(true);
435 }