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