1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: GeomAlgoAPI_Prism.cpp
5 // Author: Dmitry Bobylev
7 #include "GeomAlgoAPI_Prism.h"
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>
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 <Geom_RectangularTrimmedSurface.hxx>
32 #include <IntAna_IntConicQuad.hxx>
33 #include <IntAna_Quadric.hxx>
34 #include <IntTools_Context.hxx>
35 #include <TopExp_Explorer.hxx>
37 #include <TopoDS_Edge.hxx>
38 #include <TopoDS_Shell.hxx>
39 #include <TopoDS_Solid.hxx>
40 #include <TopTools_ListIteratorOfListOfShape.hxx>
43 static void storeGenerationHistory(GeomAlgoAPI_Prism* thePrismAlgo,
44 const TopoDS_Shape& theBase,
45 const TopAbs_ShapeEnum theType,
46 BRepPrimAPI_MakePrism* thePrismBuilder);
48 static void storeGenerationHistory(GeomAlgoAPI_Prism* thePrismAlgo,
49 const TopoDS_Shape& theResult,
50 const TopAbs_ShapeEnum theType,
51 const TopoDS_Face& theToFace,
52 const TopoDS_Face& theFromFace);
55 //==================================================================================================
56 GeomAlgoAPI_Prism::GeomAlgoAPI_Prism(const GeomShapePtr theBaseShape,
57 const double theToSize,
58 const double theFromSize)
60 build(theBaseShape, std::shared_ptr<GeomAPI_Dir>(), GeomShapePtr(), theToSize, GeomShapePtr(), theFromSize);
63 //==================================================================================================
64 GeomAlgoAPI_Prism::GeomAlgoAPI_Prism(const GeomShapePtr theBaseShape,
65 const std::shared_ptr<GeomAPI_Dir> theDirection,
66 const double theToSize,
67 const double theFromSize)
69 build(theBaseShape, theDirection, GeomShapePtr(), theToSize, GeomShapePtr(), theFromSize);
72 //==================================================================================================
73 GeomAlgoAPI_Prism::GeomAlgoAPI_Prism(const GeomShapePtr theBaseShape,
74 const GeomShapePtr theToShape,
75 const double theToSize,
76 const GeomShapePtr theFromShape,
77 const double theFromSize)
79 build(theBaseShape, std::shared_ptr<GeomAPI_Dir>(), theToShape, theToSize, theFromShape, theFromSize);
82 //==================================================================================================
83 GeomAlgoAPI_Prism::GeomAlgoAPI_Prism(const GeomShapePtr theBaseShape,
84 const std::shared_ptr<GeomAPI_Dir> theDirection,
85 const GeomShapePtr theToShape,
86 const double theToSize,
87 const GeomShapePtr theFromShape,
88 const double theFromSize)
90 build(theBaseShape, theDirection, theToShape, theToSize, theFromShape, theFromSize);
93 //==================================================================================================
94 void GeomAlgoAPI_Prism::build(const GeomShapePtr& theBaseShape,
95 const std::shared_ptr<GeomAPI_Dir> theDirection,
96 const GeomShapePtr& theToShape,
97 const double theToSize,
98 const GeomShapePtr& theFromShape,
99 const double theFromSize)
101 if(!theBaseShape.get() ||
102 (((!theFromShape.get() && !theToShape.get()) || (theFromShape.get() && theToShape.get() && theFromShape->isEqual(theToShape)))
103 && (theFromSize == -theToSize))) {
107 // Getting base shape.
108 const TopoDS_Shape& aBaseShape = theBaseShape->impl<TopoDS_Shape>();
109 TopAbs_ShapeEnum aShapeTypeToExp;
110 switch(aBaseShape.ShapeType()) {
112 aShapeTypeToExp = TopAbs_VERTEX;
116 aShapeTypeToExp = TopAbs_EDGE;
120 aShapeTypeToExp = TopAbs_FACE;
122 case TopAbs_COMPOUND:
123 aShapeTypeToExp = TopAbs_COMPOUND;
129 // Getting direction.
131 std::shared_ptr<GeomAPI_Pnt> aBaseLoc;
132 std::shared_ptr<GeomAPI_Dir> aBaseDir;
133 GeomShapePtr aBasePlane;
134 const bool isBoundingShapesSet = theFromShape.get() || theToShape.get();
135 BRepBuilderAPI_FindPlane aFindPlane(aBaseShape);
136 if(theDirection.get()) {
137 aBaseDir = theDirection;
138 aDirVec = theDirection->impl<gp_Dir>();
140 if(aBaseShape.ShapeType() == TopAbs_VERTEX
141 || aBaseShape.ShapeType() == TopAbs_EDGE
142 || aFindPlane.Found() == Standard_False) {
146 Handle(Geom_Plane) aPlane;
147 if(aBaseShape.ShapeType() == TopAbs_FACE || aBaseShape.ShapeType() == TopAbs_SHELL) {
148 TopExp_Explorer anExp(aBaseShape, TopAbs_FACE);
149 const TopoDS_Shape& aFace = anExp.Current();
150 Handle(Geom_Surface) aSurface = BRep_Tool::Surface(TopoDS::Face(aFace));
151 if(aSurface->DynamicType() == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
152 Handle(Geom_RectangularTrimmedSurface) aTrimSurface =
153 Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurface);
154 aSurface = aTrimSurface->BasisSurface();
156 if(aSurface->DynamicType() != STANDARD_TYPE(Geom_Plane)) {
159 aPlane = Handle(Geom_Plane)::DownCast(aSurface);
161 aPlane = aFindPlane.Plane();
163 gp_Pnt aLoc = aPlane->Axis().Location();
164 aDirVec = aPlane->Axis().Direction();
165 aBaseLoc.reset(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
166 aBaseDir.reset(new GeomAPI_Dir(aDirVec.X(), aDirVec.Y(), aDirVec.Z()));
168 if(!aBaseLoc.get()) {
170 gp_XYZ aDirXYZ = aDirVec.XYZ();
171 Standard_Real aMinParam = Precision::Infinite();
172 for(TopExp_Explorer anExp(aBaseShape, TopAbs_VERTEX); anExp.More(); anExp.Next()) {
173 const TopoDS_Shape& aVertex = anExp.Current();
174 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aVertex));
175 double aParam = aDirXYZ.Dot(aPnt.XYZ());
176 if(aParam < aMinParam) {
181 aBaseLoc.reset(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
183 aBasePlane = GeomAlgoAPI_FaceBuilder::planarFace(aBaseLoc, aBaseDir);
185 TopoDS_Shape aResult;
186 if(!isBoundingShapesSet) {
187 // Moving base shape.
189 aTrsf.SetTranslation(aDirVec * -theFromSize);
190 BRepBuilderAPI_Transform* aTransformBuilder = new BRepBuilderAPI_Transform(aBaseShape, aTrsf);
191 if(!aTransformBuilder) {
194 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aTransformBuilder)));
195 if(!aTransformBuilder->IsDone()) {
198 TopoDS_Shape aMovedBase = aTransformBuilder->Shape();
201 BRepPrimAPI_MakePrism* aPrismBuilder = new BRepPrimAPI_MakePrism(aMovedBase, aDirVec * (theFromSize + theToSize));
205 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aPrismBuilder)));
206 if(!aPrismBuilder->IsDone()) {
209 aResult = aPrismBuilder->Shape();
212 if(aShapeTypeToExp == TopAbs_COMPOUND) {
213 storeGenerationHistory(this, aMovedBase, TopAbs_EDGE, aPrismBuilder);
214 storeGenerationHistory(this, aMovedBase, TopAbs_FACE, aPrismBuilder);
216 storeGenerationHistory(this, aMovedBase, aShapeTypeToExp, aPrismBuilder);
219 GeomShapePtr aBoundingFromShape = theFromShape ? theFromShape : aBasePlane;
220 GeomShapePtr aBoundingToShape = theToShape ? theToShape : aBasePlane;
222 // Moving prism bounding faces according to "from" and "to" sizes.
223 std::shared_ptr<GeomAPI_Pln> aFromPln = GeomAPI_Face(aBoundingFromShape).getPlane();
224 std::shared_ptr<GeomAPI_Pnt> aFromLoc = aFromPln->location();
225 std::shared_ptr<GeomAPI_Dir> aFromDir = aFromPln->direction();
227 std::shared_ptr<GeomAPI_Pln> aToPln = GeomAPI_Face(aBoundingToShape).getPlane();
228 std::shared_ptr<GeomAPI_Pnt> aToLoc = aToPln->location();
229 std::shared_ptr<GeomAPI_Dir> aToDir = aToPln->direction();
231 bool aSign = aFromLoc->xyz()->dot(aBaseDir->xyz()) > aToLoc->xyz()->dot(aBaseDir->xyz());
233 std::shared_ptr<GeomAPI_Pnt> aFromPnt(new GeomAPI_Pnt(aFromLoc->xyz()->added(aBaseDir->xyz()->multiplied(
234 aSign ? theFromSize : -theFromSize))));
235 aBoundingFromShape = GeomAlgoAPI_FaceBuilder::planarFace(aFromPnt, aFromDir);
237 std::shared_ptr<GeomAPI_Pnt> aToPnt(new GeomAPI_Pnt(aToLoc->xyz()->added(aBaseDir->xyz()->multiplied(
238 aSign ? -theToSize : theToSize))));
239 aBoundingToShape = GeomAlgoAPI_FaceBuilder::planarFace(aToPnt, aToDir);
241 // Getting bounding box for base shape.
243 BRepBndLib::Add(aBaseShape, aBndBox);
244 Standard_Real aXArr[2] = {aBndBox.CornerMin().X(), aBndBox.CornerMax().X()};
245 Standard_Real aYArr[2] = {aBndBox.CornerMin().Y(), aBndBox.CornerMax().Y()};
246 Standard_Real aZArr[2] = {aBndBox.CornerMin().Z(), aBndBox.CornerMax().Z()};
249 for(int i = 0; i < 2; i++) {
250 for(int j = 0; j < 2; j++) {
251 for(int k = 0; k < 2; k++) {
252 aPoints[aNum] = gp_Pnt(aXArr[i], aYArr[j], aZArr[k]);
258 // Project points to bounding planes. Search max distance to them.
259 IntAna_Quadric aBndToQuadric(gp_Pln(aToPnt->impl<gp_Pnt>(), aToDir->impl<gp_Dir>()));
260 IntAna_Quadric aBndFromQuadric(gp_Pln(aFromPnt->impl<gp_Pnt>(), aFromDir->impl<gp_Dir>()));
261 Standard_Real aMaxToDist = 0, aMaxFromDist = 0;
262 for(int i = 0; i < 8; i++) {
263 gp_Lin aLine(aPoints[i], aDirVec);
264 IntAna_IntConicQuad aToIntAna(aLine, aBndToQuadric);
265 IntAna_IntConicQuad aFromIntAna(aLine, aBndFromQuadric);
266 if(aToIntAna.NbPoints() == 0 || aFromIntAna.NbPoints() == 0) {
269 const gp_Pnt& aPntOnToFace = aToIntAna.Point(1);
270 const gp_Pnt& aPntOnFromFace = aFromIntAna.Point(1);
271 if(aPoints[i].Distance(aPntOnToFace) > aMaxToDist) {
272 aMaxToDist = aPoints[i].Distance(aPntOnToFace);
274 if(aPoints[i].Distance(aPntOnFromFace) > aMaxFromDist) {
275 aMaxFromDist = aPoints[i].Distance(aPntOnFromFace);
279 // We added 1 just to be sure that prism is long enough for boolean operation.
280 double aPrismLength = aMaxToDist + aMaxFromDist + 1;
282 // Moving base shape.
284 aTrsf.SetTranslation(aDirVec * -aPrismLength);
285 BRepBuilderAPI_Transform* aTransformBuilder = new BRepBuilderAPI_Transform(aBaseShape, aTrsf);
286 if(!aTransformBuilder) {
289 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aTransformBuilder)));
290 if(!aTransformBuilder->IsDone()) {
293 TopoDS_Shape aMovedBase = aTransformBuilder->Shape();
296 BRepPrimAPI_MakePrism* aPrismBuilder = new BRepPrimAPI_MakePrism(aMovedBase, aDirVec * 2 * aPrismLength);
300 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aPrismBuilder)));
301 if(!aPrismBuilder->IsDone()) {
304 aResult = aPrismBuilder->Shape();
306 // Orienting bounding planes.
307 std::shared_ptr<GeomAPI_Pnt> aCentreOfMass = GeomAlgoAPI_ShapeTools::centreOfMass(theBaseShape);
308 const gp_Pnt& aCentrePnt = aCentreOfMass->impl<gp_Pnt>();
309 gp_Lin aLine(aCentrePnt, aDirVec);
310 IntAna_IntConicQuad aToIntAna(aLine, aBndToQuadric);
311 IntAna_IntConicQuad aFromIntAna(aLine, aBndFromQuadric);
312 Standard_Real aToParameter = aToIntAna.ParamOnConic(1);
313 Standard_Real aFromParameter = aFromIntAna.ParamOnConic(1);
314 if(aToParameter > aFromParameter) {
315 gp_Vec aVec = aToDir->impl<gp_Dir>();
316 if((aVec * aDirVec) > 0) {
317 aToDir->setImpl(new gp_Dir(aVec.Reversed()));
318 aBoundingToShape = GeomAlgoAPI_FaceBuilder::planarFace(aToPnt, aToDir);
320 aVec = aFromDir->impl<gp_Dir>();
321 if((aVec * aDirVec) < 0) {
322 aFromDir->setImpl(new gp_Dir(aVec.Reversed()));
323 aBoundingFromShape = GeomAlgoAPI_FaceBuilder::planarFace(aFromPnt, aFromDir);
326 gp_Vec aVec = aToDir->impl<gp_Dir>();
327 if((aVec * aDirVec) < 0) {
328 aToDir->setImpl(new gp_Dir(aVec.Reversed()));
329 aBoundingToShape = GeomAlgoAPI_FaceBuilder::planarFace(aToPnt, aToDir);
331 aVec = aFromDir->impl<gp_Dir>();
332 if((aVec * aDirVec) > 0) {
333 aFromDir->setImpl(new gp_Dir(aVec.Reversed()));
334 aBoundingFromShape = GeomAlgoAPI_FaceBuilder::planarFace(aFromPnt, aFromDir);
338 // Making solids from bounding planes.
339 TopoDS_Shell aToShell, aFromShell;
340 TopoDS_Solid aToSolid, aFromSolid;
341 const TopoDS_Shape& aToShape = aBoundingToShape->impl<TopoDS_Shape>();
342 const TopoDS_Shape& aFromShape = aBoundingFromShape->impl<TopoDS_Shape>();
343 TopoDS_Face aToFace = TopoDS::Face(aToShape);
344 TopoDS_Face aFromFace = TopoDS::Face(aFromShape);
345 BRep_Builder aBoundingBuilder;
346 aBoundingBuilder.MakeShell(aToShell);
347 aBoundingBuilder.Add(aToShell, aToShape);
348 aBoundingBuilder.MakeShell(aFromShell);
349 aBoundingBuilder.Add(aFromShell, aFromShape);
350 aBoundingBuilder.MakeSolid(aToSolid);
351 aBoundingBuilder.Add(aToSolid, aToShell);
352 aBoundingBuilder.MakeSolid(aFromSolid);
353 aBoundingBuilder.Add(aFromSolid, aFromShell);
355 // Cutting with to plane.
356 BRepAlgoAPI_Cut* aToCutBuilder = new BRepAlgoAPI_Cut(aResult, aToSolid);
357 aToCutBuilder->Build();
358 if(!aToCutBuilder->IsDone()) {
361 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aToCutBuilder)));
362 aResult = aToCutBuilder->Shape();
363 if(aResult.ShapeType() == TopAbs_COMPOUND) {
364 aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
366 if(aShapeTypeToExp == TopAbs_FACE || aShapeTypeToExp == TopAbs_COMPOUND) {
367 const TopTools_ListOfShape& aToShapes = aToCutBuilder->Modified(aToShape);
368 for(TopTools_ListIteratorOfListOfShape anIt(aToShapes); anIt.More(); anIt.Next()) {
369 GeomShapePtr aGeomSh(new GeomAPI_Shape());
370 aGeomSh->setImpl(new TopoDS_Shape(anIt.Value()));
371 this->addToShape(aGeomSh);
375 // Cutting with from plane.
376 BRepAlgoAPI_Cut* aFromCutBuilder = new BRepAlgoAPI_Cut(aResult, aFromSolid);
377 aFromCutBuilder->Build();
378 if(!aFromCutBuilder->IsDone()) {
381 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aFromCutBuilder)));
382 aResult = aFromCutBuilder->Shape();
383 TopoDS_Iterator aCheckIt(aResult);
384 if(!aCheckIt.More()) {
387 if(aResult.ShapeType() == TopAbs_COMPOUND) {
388 aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
390 if(aShapeTypeToExp == TopAbs_FACE || aShapeTypeToExp == TopAbs_COMPOUND) {
391 const TopTools_ListOfShape& aFromShapes = aFromCutBuilder->Modified(aFromShape);
392 for(TopTools_ListIteratorOfListOfShape anIt(aFromShapes); anIt.More(); anIt.Next()) {
393 GeomShapePtr aGeomSh(new GeomAPI_Shape());
394 aGeomSh->setImpl(new TopoDS_Shape(anIt.Value()));
395 this->addFromShape(aGeomSh);
399 // Naming for extrusion from vertex, edge.
400 if(aShapeTypeToExp == TopAbs_COMPOUND) {
401 storeGenerationHistory(this, aResult, TopAbs_EDGE, aToFace, aFromFace);
402 storeGenerationHistory(this, aResult, TopAbs_FACE, aToFace, aFromFace);
404 storeGenerationHistory(this, aResult, aShapeTypeToExp, aToFace, aFromFace);
407 if(aResult.ShapeType() == TopAbs_COMPOUND) {
408 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
409 aGeomShape->setImpl(new TopoDS_Shape(aResult));
410 ListOfShape aCompSolids, aFreeSolids;
411 aGeomShape = GeomAlgoAPI_ShapeTools::combineShapes(aGeomShape,
412 GeomAPI_Shape::COMPSOLID,
415 aResult = aGeomShape->impl<TopoDS_Shape>();
420 if(aResult.IsNull()) {
423 aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
424 GeomShapePtr aGeomSh(new GeomAPI_Shape());
425 aGeomSh->setImpl(new TopoDS_Shape(aResult));
426 this->setShape(aGeomSh);
430 // Auxilary functions:
431 //==================================================================================================
432 void storeGenerationHistory(GeomAlgoAPI_Prism* thePrismAlgo,
433 const TopoDS_Shape& theBase,
434 const TopAbs_ShapeEnum theType,
435 BRepPrimAPI_MakePrism* thePrismBuilder)
437 for(TopExp_Explorer anExp(theBase, theType); anExp.More(); anExp.Next()) {
438 const TopoDS_Shape& aShape = anExp.Current();
439 GeomShapePtr aFromShape(new GeomAPI_Shape), aToShape(new GeomAPI_Shape);
440 aFromShape->setImpl(new TopoDS_Shape(thePrismBuilder->FirstShape(aShape)));
441 aToShape->setImpl(new TopoDS_Shape(thePrismBuilder->LastShape(aShape)));
442 thePrismAlgo->addFromShape(aFromShape);
443 thePrismAlgo->addToShape(aToShape);
447 //==================================================================================================
448 void storeGenerationHistory(GeomAlgoAPI_Prism* thePrismAlgo,
449 const TopoDS_Shape& theResult,
450 const TopAbs_ShapeEnum theType,
451 const TopoDS_Face& theToFace,
452 const TopoDS_Face& theFromFace)
454 for(TopExp_Explorer anExp(theResult, theType); anExp.More(); anExp.Next()) {
455 const TopoDS_Shape& aShape = anExp.Current();
456 GeomShapePtr aGeomSh(new GeomAPI_Shape());
457 if(theType == TopAbs_VERTEX) {
458 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aShape));
459 IntTools_Context anIntTools;
460 if(anIntTools.IsValidPointForFace(aPnt, theToFace, Precision::Confusion()) == Standard_True) {
461 aGeomSh->setImpl(new TopoDS_Shape(aShape));
462 thePrismAlgo->addToShape(aGeomSh);
464 if(anIntTools.IsValidPointForFace(aPnt, theFromFace, Precision::Confusion()) == Standard_True) {
465 aGeomSh->setImpl(new TopoDS_Shape(aShape));
466 thePrismAlgo->addFromShape(aGeomSh);
468 } else if(theType == TopAbs_EDGE) {
469 TopoDS_Edge anEdge = TopoDS::Edge(aShape);
470 BRepLib_CheckCurveOnSurface anEdgeCheck(anEdge, theToFace);
471 anEdgeCheck.Perform();
472 if(anEdgeCheck.MaxDistance() < Precision::Confusion()) {
473 aGeomSh->setImpl(new TopoDS_Shape(aShape));
474 thePrismAlgo->addToShape(aGeomSh);
476 anEdgeCheck.Init(anEdge, theFromFace);
477 anEdgeCheck.Perform();
478 if(anEdgeCheck.MaxDistance() < Precision::Confusion()) {
479 aGeomSh->setImpl(new TopoDS_Shape(aShape));
480 thePrismAlgo->addFromShape(aGeomSh);