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(),
61 theToSize, GeomShapePtr(), theFromSize);
64 //==================================================================================================
65 GeomAlgoAPI_Prism::GeomAlgoAPI_Prism(const GeomShapePtr theBaseShape,
66 const std::shared_ptr<GeomAPI_Dir> theDirection,
67 const double theToSize,
68 const double theFromSize)
70 build(theBaseShape, theDirection, GeomShapePtr(), theToSize, GeomShapePtr(), theFromSize);
73 //==================================================================================================
74 GeomAlgoAPI_Prism::GeomAlgoAPI_Prism(const GeomShapePtr theBaseShape,
75 const GeomShapePtr theToShape,
76 const double theToSize,
77 const GeomShapePtr theFromShape,
78 const double theFromSize)
80 build(theBaseShape, std::shared_ptr<GeomAPI_Dir>(), theToShape,
81 theToSize, theFromShape, theFromSize);
84 //==================================================================================================
85 GeomAlgoAPI_Prism::GeomAlgoAPI_Prism(const GeomShapePtr theBaseShape,
86 const std::shared_ptr<GeomAPI_Dir> theDirection,
87 const GeomShapePtr theToShape,
88 const double theToSize,
89 const GeomShapePtr theFromShape,
90 const double theFromSize)
92 build(theBaseShape, theDirection, theToShape, theToSize, theFromShape, theFromSize);
95 //==================================================================================================
96 void GeomAlgoAPI_Prism::build(const GeomShapePtr& theBaseShape,
97 const std::shared_ptr<GeomAPI_Dir> theDirection,
98 const GeomShapePtr& theToShape,
99 const double theToSize,
100 const GeomShapePtr& theFromShape,
101 const double theFromSize)
103 if(!theBaseShape.get() ||
104 (((!theFromShape.get() && !theToShape.get()) ||
105 (theFromShape.get() && theToShape.get() && theFromShape->isEqual(theToShape)))
106 && (theFromSize == -theToSize))) {
110 // Getting base shape.
111 const TopoDS_Shape& aBaseShape = theBaseShape->impl<TopoDS_Shape>();
112 TopAbs_ShapeEnum aShapeTypeToExp;
113 switch(aBaseShape.ShapeType()) {
115 aShapeTypeToExp = TopAbs_VERTEX;
119 aShapeTypeToExp = TopAbs_EDGE;
123 aShapeTypeToExp = TopAbs_FACE;
125 case TopAbs_COMPOUND:
126 aShapeTypeToExp = TopAbs_COMPOUND;
132 // Getting direction.
134 std::shared_ptr<GeomAPI_Pnt> aBaseLoc;
135 std::shared_ptr<GeomAPI_Dir> aBaseDir;
136 GeomShapePtr aBasePlane;
137 const bool isBoundingShapesSet = theFromShape.get() || theToShape.get();
138 BRepBuilderAPI_FindPlane aFindPlane(aBaseShape);
139 if(theDirection.get()) {
140 aBaseDir = theDirection;
141 aDirVec = theDirection->impl<gp_Dir>();
143 if(aBaseShape.ShapeType() == TopAbs_VERTEX
144 || aBaseShape.ShapeType() == TopAbs_EDGE
145 || aFindPlane.Found() == Standard_False) {
149 Handle(Geom_Plane) aPlane;
150 if(aBaseShape.ShapeType() == TopAbs_FACE || aBaseShape.ShapeType() == TopAbs_SHELL) {
151 TopExp_Explorer anExp(aBaseShape, TopAbs_FACE);
152 const TopoDS_Shape& aFace = anExp.Current();
153 Handle(Geom_Surface) aSurface = BRep_Tool::Surface(TopoDS::Face(aFace));
154 if(aSurface->DynamicType() == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
155 Handle(Geom_RectangularTrimmedSurface) aTrimSurface =
156 Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurface);
157 aSurface = aTrimSurface->BasisSurface();
159 if(aSurface->DynamicType() != STANDARD_TYPE(Geom_Plane)) {
162 aPlane = Handle(Geom_Plane)::DownCast(aSurface);
164 aPlane = aFindPlane.Plane();
166 gp_Pnt aLoc = aPlane->Axis().Location();
167 aDirVec = aPlane->Axis().Direction();
168 aBaseLoc.reset(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
169 aBaseDir.reset(new GeomAPI_Dir(aDirVec.X(), aDirVec.Y(), aDirVec.Z()));
171 if(!aBaseLoc.get()) {
173 gp_XYZ aDirXYZ = aDirVec.XYZ();
174 Standard_Real aMinParam = Precision::Infinite();
175 for(TopExp_Explorer anExp(aBaseShape, TopAbs_VERTEX); anExp.More(); anExp.Next()) {
176 const TopoDS_Shape& aVertex = anExp.Current();
177 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aVertex));
178 double aParam = aDirXYZ.Dot(aPnt.XYZ());
179 if(aParam < aMinParam) {
184 aBaseLoc.reset(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
186 aBasePlane = GeomAlgoAPI_FaceBuilder::planarFace(aBaseLoc, aBaseDir);
188 TopoDS_Shape aResult;
189 if(!isBoundingShapesSet) {
190 // Moving base shape.
192 aTrsf.SetTranslation(aDirVec * -theFromSize);
193 BRepBuilderAPI_Transform* aTransformBuilder =
194 new BRepBuilderAPI_Transform(aBaseShape, aTrsf);
195 if(!aTransformBuilder) {
198 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(
199 new GeomAlgoAPI_MakeShape(aTransformBuilder)));
200 if(!aTransformBuilder->IsDone()) {
203 TopoDS_Shape aMovedBase = aTransformBuilder->Shape();
206 BRepPrimAPI_MakePrism* aPrismBuilder =
207 new BRepPrimAPI_MakePrism(aMovedBase, aDirVec * (theFromSize + theToSize));
211 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(
212 new GeomAlgoAPI_MakeShape(aPrismBuilder)));
213 if(!aPrismBuilder->IsDone()) {
216 aResult = aPrismBuilder->Shape();
219 if(aShapeTypeToExp == TopAbs_COMPOUND) {
220 storeGenerationHistory(this, aMovedBase, TopAbs_EDGE, aPrismBuilder);
221 storeGenerationHistory(this, aMovedBase, TopAbs_FACE, aPrismBuilder);
223 storeGenerationHistory(this, aMovedBase, aShapeTypeToExp, aPrismBuilder);
226 GeomShapePtr aBoundingFromShape = theFromShape ? theFromShape : aBasePlane;
227 GeomShapePtr aBoundingToShape = theToShape ? theToShape : aBasePlane;
229 // Moving prism bounding faces according to "from" and "to" sizes.
230 std::shared_ptr<GeomAPI_Pln> aFromPln = GeomAPI_Face(aBoundingFromShape).getPlane();
231 std::shared_ptr<GeomAPI_Pnt> aFromLoc = aFromPln->location();
232 std::shared_ptr<GeomAPI_Dir> aFromDir = aFromPln->direction();
234 std::shared_ptr<GeomAPI_Pln> aToPln = GeomAPI_Face(aBoundingToShape).getPlane();
235 std::shared_ptr<GeomAPI_Pnt> aToLoc = aToPln->location();
236 std::shared_ptr<GeomAPI_Dir> aToDir = aToPln->direction();
238 bool aSign = aFromLoc->xyz()->dot(aBaseDir->xyz()) > aToLoc->xyz()->dot(aBaseDir->xyz());
240 std::shared_ptr<GeomAPI_Pnt> aFromPnt(
241 new GeomAPI_Pnt(aFromLoc->xyz()->added(aBaseDir->xyz()->multiplied(
242 aSign ? theFromSize : -theFromSize))));
243 aBoundingFromShape = GeomAlgoAPI_FaceBuilder::planarFace(aFromPnt, aFromDir);
245 std::shared_ptr<GeomAPI_Pnt> aToPnt(
246 new GeomAPI_Pnt(aToLoc->xyz()->added(aBaseDir->xyz()->multiplied(
247 aSign ? -theToSize : theToSize))));
248 aBoundingToShape = GeomAlgoAPI_FaceBuilder::planarFace(aToPnt, aToDir);
250 // Getting bounding box for base shape.
252 BRepBndLib::Add(aBaseShape, aBndBox);
253 Standard_Real aXArr[2] = {aBndBox.CornerMin().X(), aBndBox.CornerMax().X()};
254 Standard_Real aYArr[2] = {aBndBox.CornerMin().Y(), aBndBox.CornerMax().Y()};
255 Standard_Real aZArr[2] = {aBndBox.CornerMin().Z(), aBndBox.CornerMax().Z()};
258 for(int i = 0; i < 2; i++) {
259 for(int j = 0; j < 2; j++) {
260 for(int k = 0; k < 2; k++) {
261 aPoints[aNum] = gp_Pnt(aXArr[i], aYArr[j], aZArr[k]);
267 // Project points to bounding planes. Search max distance to them.
268 IntAna_Quadric aBndToQuadric(gp_Pln(aToPnt->impl<gp_Pnt>(), aToDir->impl<gp_Dir>()));
269 IntAna_Quadric aBndFromQuadric(gp_Pln(aFromPnt->impl<gp_Pnt>(), aFromDir->impl<gp_Dir>()));
270 Standard_Real aMaxToDist = 0, aMaxFromDist = 0;
271 for(int i = 0; i < 8; i++) {
272 gp_Lin aLine(aPoints[i], aDirVec);
273 IntAna_IntConicQuad aToIntAna(aLine, aBndToQuadric);
274 IntAna_IntConicQuad aFromIntAna(aLine, aBndFromQuadric);
275 if(aToIntAna.NbPoints() == 0 || aFromIntAna.NbPoints() == 0) {
278 const gp_Pnt& aPntOnToFace = aToIntAna.Point(1);
279 const gp_Pnt& aPntOnFromFace = aFromIntAna.Point(1);
280 if(aPoints[i].Distance(aPntOnToFace) > aMaxToDist) {
281 aMaxToDist = aPoints[i].Distance(aPntOnToFace);
283 if(aPoints[i].Distance(aPntOnFromFace) > aMaxFromDist) {
284 aMaxFromDist = aPoints[i].Distance(aPntOnFromFace);
288 // We added 1 just to be sure that prism is long enough for boolean operation.
289 double aPrismLength = aMaxToDist + aMaxFromDist + 1;
291 // Moving base shape.
293 aTrsf.SetTranslation(aDirVec * -aPrismLength);
294 BRepBuilderAPI_Transform* aTransformBuilder = new BRepBuilderAPI_Transform(aBaseShape, aTrsf);
295 if(!aTransformBuilder) {
298 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(
299 new GeomAlgoAPI_MakeShape(aTransformBuilder)));
300 if(!aTransformBuilder->IsDone()) {
303 TopoDS_Shape aMovedBase = aTransformBuilder->Shape();
306 BRepPrimAPI_MakePrism* aPrismBuilder =
307 new BRepPrimAPI_MakePrism(aMovedBase, aDirVec * 2 * aPrismLength);
311 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(
312 new GeomAlgoAPI_MakeShape(aPrismBuilder)));
313 if(!aPrismBuilder->IsDone()) {
316 aResult = aPrismBuilder->Shape();
318 // Orienting bounding planes.
319 std::shared_ptr<GeomAPI_Pnt> aCentreOfMass = GeomAlgoAPI_ShapeTools::centreOfMass(theBaseShape);
320 const gp_Pnt& aCentrePnt = aCentreOfMass->impl<gp_Pnt>();
321 gp_Lin aLine(aCentrePnt, aDirVec);
322 IntAna_IntConicQuad aToIntAna(aLine, aBndToQuadric);
323 IntAna_IntConicQuad aFromIntAna(aLine, aBndFromQuadric);
324 Standard_Real aToParameter = aToIntAna.ParamOnConic(1);
325 Standard_Real aFromParameter = aFromIntAna.ParamOnConic(1);
326 if(aToParameter > aFromParameter) {
327 gp_Vec aVec = aToDir->impl<gp_Dir>();
328 if((aVec * aDirVec) > 0) {
329 aToDir->setImpl(new gp_Dir(aVec.Reversed()));
330 aBoundingToShape = GeomAlgoAPI_FaceBuilder::planarFace(aToPnt, aToDir);
332 aVec = aFromDir->impl<gp_Dir>();
333 if((aVec * aDirVec) < 0) {
334 aFromDir->setImpl(new gp_Dir(aVec.Reversed()));
335 aBoundingFromShape = GeomAlgoAPI_FaceBuilder::planarFace(aFromPnt, aFromDir);
338 gp_Vec aVec = aToDir->impl<gp_Dir>();
339 if((aVec * aDirVec) < 0) {
340 aToDir->setImpl(new gp_Dir(aVec.Reversed()));
341 aBoundingToShape = GeomAlgoAPI_FaceBuilder::planarFace(aToPnt, aToDir);
343 aVec = aFromDir->impl<gp_Dir>();
344 if((aVec * aDirVec) > 0) {
345 aFromDir->setImpl(new gp_Dir(aVec.Reversed()));
346 aBoundingFromShape = GeomAlgoAPI_FaceBuilder::planarFace(aFromPnt, aFromDir);
350 // Making solids from bounding planes.
351 TopoDS_Shell aToShell, aFromShell;
352 TopoDS_Solid aToSolid, aFromSolid;
353 const TopoDS_Shape& aToShape = aBoundingToShape->impl<TopoDS_Shape>();
354 const TopoDS_Shape& aFromShape = aBoundingFromShape->impl<TopoDS_Shape>();
355 TopoDS_Face aToFace = TopoDS::Face(aToShape);
356 TopoDS_Face aFromFace = TopoDS::Face(aFromShape);
357 BRep_Builder aBoundingBuilder;
358 aBoundingBuilder.MakeShell(aToShell);
359 aBoundingBuilder.Add(aToShell, aToShape);
360 aBoundingBuilder.MakeShell(aFromShell);
361 aBoundingBuilder.Add(aFromShell, aFromShape);
362 aBoundingBuilder.MakeSolid(aToSolid);
363 aBoundingBuilder.Add(aToSolid, aToShell);
364 aBoundingBuilder.MakeSolid(aFromSolid);
365 aBoundingBuilder.Add(aFromSolid, aFromShell);
367 // Cutting with to plane.
368 BRepAlgoAPI_Cut* aToCutBuilder = new BRepAlgoAPI_Cut(aResult, aToSolid);
369 aToCutBuilder->Build();
370 if(!aToCutBuilder->IsDone()) {
373 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(
374 new GeomAlgoAPI_MakeShape(aToCutBuilder)));
375 aResult = aToCutBuilder->Shape();
376 if(aResult.ShapeType() == TopAbs_COMPOUND) {
377 aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
379 if(aShapeTypeToExp == TopAbs_FACE || aShapeTypeToExp == TopAbs_COMPOUND) {
380 const TopTools_ListOfShape& aToShapes = aToCutBuilder->Modified(aToShape);
381 for(TopTools_ListIteratorOfListOfShape anIt(aToShapes); anIt.More(); anIt.Next()) {
382 GeomShapePtr aGeomSh(new GeomAPI_Shape());
383 aGeomSh->setImpl(new TopoDS_Shape(anIt.Value()));
384 this->addToShape(aGeomSh);
388 // Cutting with from plane.
389 BRepAlgoAPI_Cut* aFromCutBuilder = new BRepAlgoAPI_Cut(aResult, aFromSolid);
390 aFromCutBuilder->Build();
391 if(!aFromCutBuilder->IsDone()) {
394 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(
395 new GeomAlgoAPI_MakeShape(aFromCutBuilder)));
396 aResult = aFromCutBuilder->Shape();
397 TopoDS_Iterator aCheckIt(aResult);
398 if(!aCheckIt.More()) {
401 if(aResult.ShapeType() == TopAbs_COMPOUND) {
402 aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
404 if(aShapeTypeToExp == TopAbs_FACE || aShapeTypeToExp == TopAbs_COMPOUND) {
405 const TopTools_ListOfShape& aFromShapes = aFromCutBuilder->Modified(aFromShape);
406 for(TopTools_ListIteratorOfListOfShape anIt(aFromShapes); anIt.More(); anIt.Next()) {
407 GeomShapePtr aGeomSh(new GeomAPI_Shape());
408 aGeomSh->setImpl(new TopoDS_Shape(anIt.Value()));
409 this->addFromShape(aGeomSh);
413 // Naming for extrusion from vertex, edge.
414 if(aShapeTypeToExp == TopAbs_COMPOUND) {
415 storeGenerationHistory(this, aResult, TopAbs_EDGE, aToFace, aFromFace);
416 storeGenerationHistory(this, aResult, TopAbs_FACE, aToFace, aFromFace);
418 storeGenerationHistory(this, aResult, aShapeTypeToExp, aToFace, aFromFace);
421 if(aResult.ShapeType() == TopAbs_COMPOUND) {
422 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
423 aGeomShape->setImpl(new TopoDS_Shape(aResult));
424 ListOfShape aCompSolids, aFreeSolids;
425 aGeomShape = GeomAlgoAPI_ShapeTools::combineShapes(aGeomShape,
426 GeomAPI_Shape::COMPSOLID,
429 aResult = aGeomShape->impl<TopoDS_Shape>();
434 if(aResult.IsNull()) {
437 aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
438 GeomShapePtr aGeomSh(new GeomAPI_Shape());
439 aGeomSh->setImpl(new TopoDS_Shape(aResult));
440 this->setShape(aGeomSh);
444 // Auxilary functions:
445 //==================================================================================================
446 void storeGenerationHistory(GeomAlgoAPI_Prism* thePrismAlgo,
447 const TopoDS_Shape& theBase,
448 const TopAbs_ShapeEnum theType,
449 BRepPrimAPI_MakePrism* thePrismBuilder)
451 for(TopExp_Explorer anExp(theBase, theType); anExp.More(); anExp.Next()) {
452 const TopoDS_Shape& aShape = anExp.Current();
453 GeomShapePtr aFromShape(new GeomAPI_Shape), aToShape(new GeomAPI_Shape);
454 aFromShape->setImpl(new TopoDS_Shape(thePrismBuilder->FirstShape(aShape)));
455 aToShape->setImpl(new TopoDS_Shape(thePrismBuilder->LastShape(aShape)));
456 thePrismAlgo->addFromShape(aFromShape);
457 thePrismAlgo->addToShape(aToShape);
461 //==================================================================================================
462 void storeGenerationHistory(GeomAlgoAPI_Prism* thePrismAlgo,
463 const TopoDS_Shape& theResult,
464 const TopAbs_ShapeEnum theType,
465 const TopoDS_Face& theToFace,
466 const TopoDS_Face& theFromFace)
468 for(TopExp_Explorer anExp(theResult, theType); anExp.More(); anExp.Next()) {
469 const TopoDS_Shape& aShape = anExp.Current();
470 GeomShapePtr aGeomSh(new GeomAPI_Shape());
471 if(theType == TopAbs_VERTEX) {
472 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aShape));
473 IntTools_Context anIntTools;
474 if(anIntTools.IsValidPointForFace(aPnt,
475 theToFace, Precision::Confusion()) == Standard_True) {
476 aGeomSh->setImpl(new TopoDS_Shape(aShape));
477 thePrismAlgo->addToShape(aGeomSh);
479 if(anIntTools.IsValidPointForFace(aPnt,
480 theFromFace, Precision::Confusion()) == Standard_True) {
481 aGeomSh->setImpl(new TopoDS_Shape(aShape));
482 thePrismAlgo->addFromShape(aGeomSh);
484 } else if(theType == TopAbs_EDGE) {
485 TopoDS_Edge anEdge = TopoDS::Edge(aShape);
486 BRepLib_CheckCurveOnSurface anEdgeCheck(anEdge, theToFace);
487 anEdgeCheck.Perform();
488 if(anEdgeCheck.MaxDistance() < Precision::Confusion()) {
489 aGeomSh->setImpl(new TopoDS_Shape(aShape));
490 thePrismAlgo->addToShape(aGeomSh);
492 anEdgeCheck.Init(anEdge, theFromFace);
493 anEdgeCheck.Perform();
494 if(anEdgeCheck.MaxDistance() < Precision::Confusion()) {
495 aGeomSh->setImpl(new TopoDS_Shape(aShape));
496 thePrismAlgo->addFromShape(aGeomSh);