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