1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "GeomAlgoAPI_Prism.h"
23 #include <GeomAPI_Face.h>
24 #include <GeomAPI_Pln.h>
25 #include <GeomAPI_Pnt.h>
26 #include <GeomAPI_ShapeExplorer.h>
27 #include <GeomAPI_XYZ.h>
28 #include <GeomAlgoAPI_DFLoader.h>
29 #include <GeomAlgoAPI_FaceBuilder.h>
30 #include <GeomAlgoAPI_ShapeTools.h>
32 #include <Bnd_Box.hxx>
33 #include <BRep_Builder.hxx>
34 #include <BRepAlgoAPI_Cut.hxx>
35 #include <BRepBndLib.hxx>
36 #include <BRepBuilderAPI_FindPlane.hxx>
37 #include <BRepBuilderAPI_Transform.hxx>
38 #include <BRepTools.hxx>
39 #include <Geom_Curve.hxx>
40 #include <Geom2d_Curve.hxx>
41 #include <BRepLib_CheckCurveOnSurface.hxx>
42 #include <BRepPrimAPI_MakePrism.hxx>
43 #include <Geom_Plane.hxx>
44 #include <Geom_RectangularTrimmedSurface.hxx>
46 #include <IntAna_IntConicQuad.hxx>
47 #include <IntAna_Quadric.hxx>
48 #include <IntTools_Context.hxx>
49 #include <TopExp_Explorer.hxx>
51 #include <TopoDS_Edge.hxx>
52 #include <TopoDS_Shell.hxx>
53 #include <TopoDS_Solid.hxx>
54 #include <TopTools_ListIteratorOfListOfShape.hxx>
57 static void storeGenerationHistory(GeomAlgoAPI_Prism* thePrismAlgo,
58 const TopoDS_Shape& theBase,
59 const TopAbs_ShapeEnum theType,
60 BRepPrimAPI_MakePrism* thePrismBuilder);
62 static void storeGenerationHistory(GeomAlgoAPI_Prism* thePrismAlgo,
63 const TopoDS_Shape& theResult,
64 const TopAbs_ShapeEnum theType,
65 const TopoDS_Face& theToFace,
66 const TopoDS_Face& theFromFace);
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)
77 build(theBaseShape, theDirection, theToShape, theToSize, theFromShape, theFromSize);
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)
88 if(!theBaseShape.get() ||
89 (((!theFromShape.get() && !theToShape.get()) ||
90 (theFromShape.get() && theToShape.get() && theFromShape->isEqual(theToShape)))
91 && (theFromSize == -theToSize))) {
95 // Getting base shape.
96 const TopoDS_Shape& aBaseShape = theBaseShape->impl<TopoDS_Shape>();
97 TopAbs_ShapeEnum aShapeTypeToExp;
98 switch(aBaseShape.ShapeType()) {
100 aShapeTypeToExp = TopAbs_VERTEX;
104 aShapeTypeToExp = TopAbs_EDGE;
108 aShapeTypeToExp = TopAbs_FACE;
110 case TopAbs_COMPOUND:
111 aShapeTypeToExp = TopAbs_COMPOUND;
117 // Getting direction.
119 std::shared_ptr<GeomAPI_Pnt> aBaseLoc;
120 std::shared_ptr<GeomAPI_Dir> aBaseDir;
121 BRepBuilderAPI_FindPlane aFindPlane(aBaseShape);
122 if(aFindPlane.Found() == Standard_True)
124 Handle(Geom_Plane) aPlane;
125 if(aBaseShape.ShapeType() == TopAbs_FACE || aBaseShape.ShapeType() == TopAbs_SHELL) {
126 TopExp_Explorer anExp(aBaseShape, TopAbs_FACE);
127 const TopoDS_Shape& aFace = anExp.Current();
128 Handle(Geom_Surface) aSurface = BRep_Tool::Surface(TopoDS::Face(aFace));
129 if(aSurface->DynamicType() == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
130 Handle(Geom_RectangularTrimmedSurface) aTrimSurface =
131 Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurface);
132 aSurface = aTrimSurface->BasisSurface();
134 if(aSurface->DynamicType() != STANDARD_TYPE(Geom_Plane)) {
137 aPlane = Handle(Geom_Plane)::DownCast(aSurface);
139 aPlane = aFindPlane.Plane();
141 gp_Pnt aLoc = aPlane->Axis().Location();
142 aBaseVec = aPlane->Axis().Direction();
143 aBaseLoc.reset(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
144 aBaseDir.reset(new GeomAPI_Dir(aBaseVec.X(), aBaseVec.Y(), aBaseVec.Z()));
146 else if (theDirection.get())
148 aBaseDir = theDirection;
149 aBaseVec = theDirection->impl<gp_Dir>();
156 if(!aBaseLoc.get()) {
158 gp_XYZ aDirXYZ = aBaseVec.XYZ();
159 Standard_Real aMinParam = Precision::Infinite();
160 for(TopExp_Explorer anExp(aBaseShape, TopAbs_VERTEX); anExp.More(); anExp.Next()) {
161 const TopoDS_Shape& aVertex = anExp.Current();
162 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aVertex));
163 double aParam = aDirXYZ.Dot(aPnt.XYZ());
164 if(aParam < aMinParam) {
169 aBaseLoc.reset(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
172 GeomShapePtr aBasePlane = GeomAlgoAPI_FaceBuilder::planarFace(aBaseLoc, aBaseDir);
175 std::shared_ptr<GeomAPI_Dir> anExtDir;
176 if (theDirection.get())
178 anExtDir = theDirection;
179 anExtVec = theDirection->impl<gp_Dir>();
184 anExtVec = aBaseDir->impl<gp_Dir>();
188 TopoDS_Shape aResult;
189 const bool isBoundingShapesSet = theFromShape.get() || theToShape.get();
190 if(!isBoundingShapesSet) {
191 // Moving base shape.
193 aTrsf.SetTranslation(anExtVec * -theFromSize);
194 BRepBuilderAPI_Transform* aTransformBuilder =
195 new BRepBuilderAPI_Transform(aBaseShape, aTrsf);
196 if(!aTransformBuilder) {
199 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(
200 new GeomAlgoAPI_MakeShape(aTransformBuilder)));
201 if(!aTransformBuilder->IsDone()) {
204 TopoDS_Shape aMovedBase = aTransformBuilder->Shape();
207 BRepPrimAPI_MakePrism* aPrismBuilder =
208 new BRepPrimAPI_MakePrism(aMovedBase, anExtVec * (theFromSize + theToSize));
212 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(
213 new GeomAlgoAPI_MakeShape(aPrismBuilder)));
214 if(!aPrismBuilder->IsDone()) {
217 aResult = aPrismBuilder->Shape();
220 if(aShapeTypeToExp == TopAbs_COMPOUND) {
221 storeGenerationHistory(this, aMovedBase, TopAbs_EDGE, aPrismBuilder);
222 storeGenerationHistory(this, aMovedBase, TopAbs_FACE, aPrismBuilder);
224 storeGenerationHistory(this, aMovedBase, aShapeTypeToExp, aPrismBuilder);
227 GeomShapePtr aBoundingFromShape = theFromShape ? theFromShape : aBasePlane;
228 GeomShapePtr aBoundingToShape = theToShape ? theToShape : aBasePlane;
230 // Moving prism bounding faces according to "from" and "to" sizes.
231 std::shared_ptr<GeomAPI_Pln> aFromPln = GeomAPI_Face(aBoundingFromShape).getPlane();
232 std::shared_ptr<GeomAPI_Pnt> aFromLoc = aFromPln->location();
233 std::shared_ptr<GeomAPI_Dir> aFromDir = aFromPln->direction();
235 std::shared_ptr<GeomAPI_Pln> aToPln = GeomAPI_Face(aBoundingToShape).getPlane();
236 std::shared_ptr<GeomAPI_Pnt> aToLoc = aToPln->location();
237 std::shared_ptr<GeomAPI_Dir> aToDir = aToPln->direction();
239 bool aSign = aFromLoc->xyz()->dot(anExtDir->xyz()) > aToLoc->xyz()->dot(anExtDir->xyz());
241 std::shared_ptr<GeomAPI_Pnt> aFromPnt(
242 new GeomAPI_Pnt(aFromLoc->xyz()->added(anExtDir->xyz()->multiplied(
243 aSign ? theFromSize : -theFromSize))));
244 aBoundingFromShape = GeomAlgoAPI_FaceBuilder::planarFace(aFromPnt, aFromDir);
246 std::shared_ptr<GeomAPI_Pnt> aToPnt(
247 new GeomAPI_Pnt(aToLoc->xyz()->added(anExtDir->xyz()->multiplied(
248 aSign ? -theToSize : theToSize))));
249 aBoundingToShape = GeomAlgoAPI_FaceBuilder::planarFace(aToPnt, aToDir);
251 // Getting bounding box for base shape.
253 BRepBndLib::Add(aBaseShape, aBndBox);
254 Standard_Real aXArr[2] = {aBndBox.CornerMin().X(), aBndBox.CornerMax().X()};
255 Standard_Real aYArr[2] = {aBndBox.CornerMin().Y(), aBndBox.CornerMax().Y()};
256 Standard_Real aZArr[2] = {aBndBox.CornerMin().Z(), aBndBox.CornerMax().Z()};
259 for(int i = 0; i < 2; i++) {
260 for(int j = 0; j < 2; j++) {
261 for(int k = 0; k < 2; k++) {
262 aPoints[aNum] = gp_Pnt(aXArr[i], aYArr[j], aZArr[k]);
268 // Project points to bounding planes. Search max distance to them.
269 IntAna_Quadric aBndToQuadric(gp_Pln(aToPnt->impl<gp_Pnt>(), aToDir->impl<gp_Dir>()));
270 IntAna_Quadric aBndFromQuadric(gp_Pln(aFromPnt->impl<gp_Pnt>(), aFromDir->impl<gp_Dir>()));
271 Standard_Real aMaxToDist = 0, aMaxFromDist = 0;
272 for(int i = 0; i < 8; i++) {
273 gp_Lin aLine(aPoints[i], anExtVec);
274 IntAna_IntConicQuad aToIntAna(aLine, aBndToQuadric);
275 IntAna_IntConicQuad aFromIntAna(aLine, aBndFromQuadric);
276 if(aToIntAna.NbPoints() == 0 || aFromIntAna.NbPoints() == 0) {
279 const gp_Pnt& aPntOnToFace = aToIntAna.Point(1);
280 const gp_Pnt& aPntOnFromFace = aFromIntAna.Point(1);
281 if(aPoints[i].Distance(aPntOnToFace) > aMaxToDist) {
282 aMaxToDist = aPoints[i].Distance(aPntOnToFace);
284 if(aPoints[i].Distance(aPntOnFromFace) > aMaxFromDist) {
285 aMaxFromDist = aPoints[i].Distance(aPntOnFromFace);
289 // We added 1 just to be sure that prism is long enough for boolean operation.
290 double aPrismLength = aMaxToDist + aMaxFromDist + 1;
292 // Moving base shape.
294 aTrsf.SetTranslation(anExtVec * -aPrismLength);
295 BRepBuilderAPI_Transform* aTransformBuilder = new BRepBuilderAPI_Transform(aBaseShape, aTrsf);
296 if(!aTransformBuilder) {
299 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(
300 new GeomAlgoAPI_MakeShape(aTransformBuilder)));
301 if(!aTransformBuilder->IsDone()) {
304 TopoDS_Shape aMovedBase = aTransformBuilder->Shape();
307 BRepPrimAPI_MakePrism* aPrismBuilder =
308 new BRepPrimAPI_MakePrism(aMovedBase, anExtVec * 2 * aPrismLength);
312 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(
313 new GeomAlgoAPI_MakeShape(aPrismBuilder)));
314 if(!aPrismBuilder->IsDone()) {
317 aResult = aPrismBuilder->Shape();
319 // Orienting bounding planes.
320 std::shared_ptr<GeomAPI_Pnt> aCentreOfMass = GeomAlgoAPI_ShapeTools::centreOfMass(theBaseShape);
321 const gp_Pnt& aCentrePnt = aCentreOfMass->impl<gp_Pnt>();
322 gp_Lin aLine(aCentrePnt, anExtVec);
323 IntAna_IntConicQuad aToIntAna(aLine, aBndToQuadric);
324 IntAna_IntConicQuad aFromIntAna(aLine, aBndFromQuadric);
325 Standard_Real aToParameter = aToIntAna.ParamOnConic(1);
326 Standard_Real aFromParameter = aFromIntAna.ParamOnConic(1);
327 if(aToParameter > aFromParameter) {
328 gp_Vec aVec = aToDir->impl<gp_Dir>();
329 if((aVec * anExtVec) > 0) {
330 aToDir->setImpl(new gp_Dir(aVec.Reversed()));
331 aBoundingToShape = GeomAlgoAPI_FaceBuilder::planarFace(aToPnt, aToDir);
333 aVec = aFromDir->impl<gp_Dir>();
334 if((aVec * anExtVec) < 0) {
335 aFromDir->setImpl(new gp_Dir(aVec.Reversed()));
336 aBoundingFromShape = GeomAlgoAPI_FaceBuilder::planarFace(aFromPnt, aFromDir);
339 gp_Vec aVec = aToDir->impl<gp_Dir>();
340 if((aVec * anExtVec) < 0) {
341 aToDir->setImpl(new gp_Dir(aVec.Reversed()));
342 aBoundingToShape = GeomAlgoAPI_FaceBuilder::planarFace(aToPnt, aToDir);
344 aVec = aFromDir->impl<gp_Dir>();
345 if((aVec * anExtVec) > 0) {
346 aFromDir->setImpl(new gp_Dir(aVec.Reversed()));
347 aBoundingFromShape = GeomAlgoAPI_FaceBuilder::planarFace(aFromPnt, aFromDir);
351 // Making solids from bounding planes.
352 TopoDS_Shell aToShell, aFromShell;
353 TopoDS_Solid aToSolid, aFromSolid;
354 const TopoDS_Shape& aToShape = aBoundingToShape->impl<TopoDS_Shape>();
355 const TopoDS_Shape& aFromShape = aBoundingFromShape->impl<TopoDS_Shape>();
356 TopoDS_Face aToFace = TopoDS::Face(aToShape);
357 TopoDS_Face aFromFace = TopoDS::Face(aFromShape);
358 BRep_Builder aBoundingBuilder;
359 aBoundingBuilder.MakeShell(aToShell);
360 aBoundingBuilder.Add(aToShell, aToShape);
361 aBoundingBuilder.MakeShell(aFromShell);
362 aBoundingBuilder.Add(aFromShell, aFromShape);
363 aBoundingBuilder.MakeSolid(aToSolid);
364 aBoundingBuilder.Add(aToSolid, aToShell);
365 aBoundingBuilder.MakeSolid(aFromSolid);
366 aBoundingBuilder.Add(aFromSolid, aFromShell);
368 // Cutting with to plane.
369 BRepAlgoAPI_Cut* aToCutBuilder = new BRepAlgoAPI_Cut(aResult, aToSolid);
370 aToCutBuilder->Build();
371 if(!aToCutBuilder->IsDone()) {
374 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(
375 new GeomAlgoAPI_MakeShape(aToCutBuilder)));
376 aResult = aToCutBuilder->Shape();
377 if(aResult.ShapeType() == TopAbs_COMPOUND) {
378 aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
380 if(aShapeTypeToExp == TopAbs_FACE || aShapeTypeToExp == TopAbs_COMPOUND) {
381 const TopTools_ListOfShape& aToShapes = aToCutBuilder->Modified(aToShape);
382 for(TopTools_ListIteratorOfListOfShape anIt(aToShapes); anIt.More(); anIt.Next()) {
383 GeomShapePtr aGeomSh(new GeomAPI_Shape());
384 aGeomSh->setImpl(new TopoDS_Shape(anIt.Value()));
385 fixOrientation(aGeomSh);
386 this->addToShape(aGeomSh);
390 // Cutting with from plane.
391 BRepAlgoAPI_Cut* aFromCutBuilder = new BRepAlgoAPI_Cut(aResult, aFromSolid);
392 aFromCutBuilder->Build();
393 if(!aFromCutBuilder->IsDone()) {
396 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(
397 new GeomAlgoAPI_MakeShape(aFromCutBuilder)));
398 aResult = aFromCutBuilder->Shape();
399 TopoDS_Iterator aCheckIt(aResult);
400 if(!aCheckIt.More()) {
403 if(aResult.ShapeType() == TopAbs_COMPOUND) {
404 aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
406 if(aShapeTypeToExp == TopAbs_FACE || aShapeTypeToExp == TopAbs_COMPOUND) {
407 const TopTools_ListOfShape& aFromShapes = aFromCutBuilder->Modified(aFromShape);
408 for(TopTools_ListIteratorOfListOfShape anIt(aFromShapes); anIt.More(); anIt.Next()) {
409 GeomShapePtr aGeomSh(new GeomAPI_Shape());
410 aGeomSh->setImpl(new TopoDS_Shape(anIt.Value()));
411 fixOrientation(aGeomSh);
412 this->addFromShape(aGeomSh);
416 // Naming for extrusion from vertex, edge.
417 if(aShapeTypeToExp == TopAbs_COMPOUND) {
418 storeGenerationHistory(this, aResult, TopAbs_EDGE, aToFace, aFromFace);
419 storeGenerationHistory(this, aResult, TopAbs_FACE, aToFace, aFromFace);
421 storeGenerationHistory(this, aResult, aShapeTypeToExp, aToFace, aFromFace);
424 if(aResult.ShapeType() == TopAbs_COMPOUND) {
425 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
426 aGeomShape->setImpl(new TopoDS_Shape(aResult));
427 ListOfShape aCompSolids, aFreeSolids;
428 aGeomShape = GeomAlgoAPI_ShapeTools::combineShapes(aGeomShape,
429 GeomAPI_Shape::COMPSOLID,
432 aResult = aGeomShape->impl<TopoDS_Shape>();
437 if(aResult.IsNull()) {
440 aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
441 GeomShapePtr aGeomSh(new GeomAPI_Shape());
442 aGeomSh->setImpl(new TopoDS_Shape(aResult));
443 this->setShape(aGeomSh);
447 // Auxilary functions:
448 //==================================================================================================
449 void storeGenerationHistory(GeomAlgoAPI_Prism* thePrismAlgo,
450 const TopoDS_Shape& theBase,
451 const TopAbs_ShapeEnum theType,
452 BRepPrimAPI_MakePrism* thePrismBuilder)
454 for(TopExp_Explorer anExp(theBase, theType); anExp.More(); anExp.Next()) {
455 const TopoDS_Shape& aShape = anExp.Current();
456 GeomShapePtr aFromShape(new GeomAPI_Shape), aToShape(new GeomAPI_Shape);
457 aFromShape->setImpl(new TopoDS_Shape(thePrismBuilder->FirstShape(aShape)));
458 aToShape->setImpl(new TopoDS_Shape(thePrismBuilder->LastShape(aShape)));
459 thePrismAlgo->fixOrientation(aFromShape);
460 thePrismAlgo->fixOrientation(aToShape);
461 thePrismAlgo->addFromShape(aFromShape);
462 thePrismAlgo->addToShape(aToShape);
466 //==================================================================================================
467 void storeGenerationHistory(GeomAlgoAPI_Prism* thePrismAlgo,
468 const TopoDS_Shape& theResult,
469 const TopAbs_ShapeEnum theType,
470 const TopoDS_Face& theToFace,
471 const TopoDS_Face& theFromFace)
473 for(TopExp_Explorer anExp(theResult, theType); anExp.More(); anExp.Next()) {
474 const TopoDS_Shape& aShape = anExp.Current();
475 GeomShapePtr aGeomSh(new GeomAPI_Shape());
476 if(theType == TopAbs_VERTEX) {
477 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aShape));
478 IntTools_Context anIntTools;
479 if(anIntTools.IsValidPointForFace(aPnt,
480 theToFace, Precision::Confusion()) == Standard_True) {
481 aGeomSh->setImpl(new TopoDS_Shape(aShape));
482 thePrismAlgo->fixOrientation(aGeomSh);
483 thePrismAlgo->addToShape(aGeomSh);
485 if(anIntTools.IsValidPointForFace(aPnt,
486 theFromFace, Precision::Confusion()) == Standard_True) {
487 aGeomSh->setImpl(new TopoDS_Shape(aShape));
488 thePrismAlgo->fixOrientation(aGeomSh);
489 thePrismAlgo->addFromShape(aGeomSh);
491 } else if(theType == TopAbs_EDGE) {
492 TopoDS_Edge anEdge = TopoDS::Edge(aShape);
493 BRepLib_CheckCurveOnSurface anEdgeCheck(anEdge, theToFace);
494 anEdgeCheck.Perform();
495 if(anEdgeCheck.MaxDistance() < Precision::Confusion()) {
496 aGeomSh->setImpl(new TopoDS_Shape(aShape));
497 thePrismAlgo->fixOrientation(aGeomSh);
498 thePrismAlgo->addToShape(aGeomSh);
500 anEdgeCheck.Init(anEdge, theFromFace);
501 anEdgeCheck.Perform();
502 if(anEdgeCheck.MaxDistance() < Precision::Confusion()) {
503 aGeomSh->setImpl(new TopoDS_Shape(aShape));
504 thePrismAlgo->fixOrientation(aGeomSh);
505 thePrismAlgo->addFromShape(aGeomSh);