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 || !aTransformBuilder->IsDone()) {
199 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(
200 new GeomAlgoAPI_MakeShape(aTransformBuilder)));
201 TopoDS_Shape aMovedBase = aTransformBuilder->Shape();
204 BRepPrimAPI_MakePrism* aPrismBuilder =
205 new BRepPrimAPI_MakePrism(aMovedBase, anExtVec * (theFromSize + theToSize));
206 if(!aPrismBuilder || !aPrismBuilder->IsDone()) {
209 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(
210 new GeomAlgoAPI_MakeShape(aPrismBuilder)));
211 aResult = aPrismBuilder->Shape();
214 if(aShapeTypeToExp == TopAbs_COMPOUND) {
215 storeGenerationHistory(this, aMovedBase, TopAbs_EDGE, aPrismBuilder);
216 storeGenerationHistory(this, aMovedBase, TopAbs_FACE, aPrismBuilder);
218 storeGenerationHistory(this, aMovedBase, aShapeTypeToExp, aPrismBuilder);
221 GeomShapePtr aBoundingFromShape = theFromShape ? theFromShape : aBasePlane;
222 GeomShapePtr aBoundingToShape = theToShape ? theToShape : aBasePlane;
224 // Moving prism bounding faces according to "from" and "to" sizes.
225 std::shared_ptr<GeomAPI_Pln> aFromPln = GeomAPI_Face(aBoundingFromShape).getPlane();
226 std::shared_ptr<GeomAPI_Pnt> aFromLoc = aFromPln->location();
227 std::shared_ptr<GeomAPI_Dir> aFromDir = aFromPln->direction();
229 std::shared_ptr<GeomAPI_Pln> aToPln = GeomAPI_Face(aBoundingToShape).getPlane();
230 std::shared_ptr<GeomAPI_Pnt> aToLoc = aToPln->location();
231 std::shared_ptr<GeomAPI_Dir> aToDir = aToPln->direction();
233 bool aSign = aFromLoc->xyz()->dot(anExtDir->xyz()) > aToLoc->xyz()->dot(anExtDir->xyz());
235 std::shared_ptr<GeomAPI_Pnt> aFromPnt(
236 new GeomAPI_Pnt(aFromLoc->xyz()->added(anExtDir->xyz()->multiplied(
237 aSign ? theFromSize : -theFromSize))));
238 aBoundingFromShape = GeomAlgoAPI_FaceBuilder::planarFace(aFromPnt, aFromDir);
240 std::shared_ptr<GeomAPI_Pnt> aToPnt(
241 new GeomAPI_Pnt(aToLoc->xyz()->added(anExtDir->xyz()->multiplied(
242 aSign ? -theToSize : theToSize))));
243 aBoundingToShape = GeomAlgoAPI_FaceBuilder::planarFace(aToPnt, aToDir);
245 // Getting bounding box for base shape.
247 BRepBndLib::Add(aBaseShape, aBndBox);
248 Standard_Real aXArr[2] = {aBndBox.CornerMin().X(), aBndBox.CornerMax().X()};
249 Standard_Real aYArr[2] = {aBndBox.CornerMin().Y(), aBndBox.CornerMax().Y()};
250 Standard_Real aZArr[2] = {aBndBox.CornerMin().Z(), aBndBox.CornerMax().Z()};
253 for(int i = 0; i < 2; i++) {
254 for(int j = 0; j < 2; j++) {
255 for(int k = 0; k < 2; k++) {
256 aPoints[aNum] = gp_Pnt(aXArr[i], aYArr[j], aZArr[k]);
262 // Project points to bounding planes. Search max distance to them.
263 IntAna_Quadric aBndToQuadric(gp_Pln(aToPnt->impl<gp_Pnt>(), aToDir->impl<gp_Dir>()));
264 IntAna_Quadric aBndFromQuadric(gp_Pln(aFromPnt->impl<gp_Pnt>(), aFromDir->impl<gp_Dir>()));
265 Standard_Real aMaxToDist = 0, aMaxFromDist = 0;
266 for(int i = 0; i < 8; i++) {
267 gp_Lin aLine(aPoints[i], anExtVec);
268 IntAna_IntConicQuad aToIntAna(aLine, aBndToQuadric);
269 IntAna_IntConicQuad aFromIntAna(aLine, aBndFromQuadric);
270 if(aToIntAna.NbPoints() == 0 || aFromIntAna.NbPoints() == 0) {
273 const gp_Pnt& aPntOnToFace = aToIntAna.Point(1);
274 const gp_Pnt& aPntOnFromFace = aFromIntAna.Point(1);
275 if(aPoints[i].Distance(aPntOnToFace) > aMaxToDist) {
276 aMaxToDist = aPoints[i].Distance(aPntOnToFace);
278 if(aPoints[i].Distance(aPntOnFromFace) > aMaxFromDist) {
279 aMaxFromDist = aPoints[i].Distance(aPntOnFromFace);
283 // We added 1 just to be sure that prism is long enough for boolean operation.
284 double aPrismLength = aMaxToDist + aMaxFromDist + 1;
286 // Moving base shape.
288 aTrsf.SetTranslation(anExtVec * -aPrismLength);
289 BRepBuilderAPI_Transform* aTransformBuilder = new BRepBuilderAPI_Transform(aBaseShape, aTrsf);
290 if(!aTransformBuilder || !aTransformBuilder->IsDone()) {
293 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(
294 new GeomAlgoAPI_MakeShape(aTransformBuilder)));
295 TopoDS_Shape aMovedBase = aTransformBuilder->Shape();
298 BRepPrimAPI_MakePrism* aPrismBuilder =
299 new BRepPrimAPI_MakePrism(aMovedBase, anExtVec * 2 * aPrismLength);
300 if(!aPrismBuilder || !aPrismBuilder->IsDone()) {
303 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(
304 new GeomAlgoAPI_MakeShape(aPrismBuilder)));
305 aResult = aPrismBuilder->Shape();
307 // Orienting bounding planes.
308 std::shared_ptr<GeomAPI_Pnt> aCentreOfMass = GeomAlgoAPI_ShapeTools::centreOfMass(theBaseShape);
309 const gp_Pnt& aCentrePnt = aCentreOfMass->impl<gp_Pnt>();
310 gp_Lin aLine(aCentrePnt, anExtVec);
311 IntAna_IntConicQuad aToIntAna(aLine, aBndToQuadric);
312 IntAna_IntConicQuad aFromIntAna(aLine, aBndFromQuadric);
313 Standard_Real aToParameter = aToIntAna.ParamOnConic(1);
314 Standard_Real aFromParameter = aFromIntAna.ParamOnConic(1);
315 if(aToParameter > aFromParameter) {
316 gp_Vec aVec = aToDir->impl<gp_Dir>();
317 if((aVec * anExtVec) > 0) {
318 aToDir->setImpl(new gp_Dir(aVec.Reversed()));
319 aBoundingToShape = GeomAlgoAPI_FaceBuilder::planarFace(aToPnt, aToDir);
321 aVec = aFromDir->impl<gp_Dir>();
322 if((aVec * anExtVec) < 0) {
323 aFromDir->setImpl(new gp_Dir(aVec.Reversed()));
324 aBoundingFromShape = GeomAlgoAPI_FaceBuilder::planarFace(aFromPnt, aFromDir);
327 gp_Vec aVec = aToDir->impl<gp_Dir>();
328 if((aVec * anExtVec) < 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 * anExtVec) > 0) {
334 aFromDir->setImpl(new gp_Dir(aVec.Reversed()));
335 aBoundingFromShape = GeomAlgoAPI_FaceBuilder::planarFace(aFromPnt, aFromDir);
339 // Making solids from bounding planes.
340 TopoDS_Shell aToShell, aFromShell;
341 TopoDS_Solid aToSolid, aFromSolid;
342 const TopoDS_Shape& aToShape = aBoundingToShape->impl<TopoDS_Shape>();
343 const TopoDS_Shape& aFromShape = aBoundingFromShape->impl<TopoDS_Shape>();
344 TopoDS_Face aToFace = TopoDS::Face(aToShape);
345 TopoDS_Face aFromFace = TopoDS::Face(aFromShape);
346 BRep_Builder aBoundingBuilder;
347 aBoundingBuilder.MakeShell(aToShell);
348 aBoundingBuilder.Add(aToShell, aToShape);
349 aBoundingBuilder.MakeShell(aFromShell);
350 aBoundingBuilder.Add(aFromShell, aFromShape);
351 aBoundingBuilder.MakeSolid(aToSolid);
352 aBoundingBuilder.Add(aToSolid, aToShell);
353 aBoundingBuilder.MakeSolid(aFromSolid);
354 aBoundingBuilder.Add(aFromSolid, aFromShell);
356 // Cutting with to plane.
357 BRepAlgoAPI_Cut* aToCutBuilder = new BRepAlgoAPI_Cut(aResult, aToSolid);
358 aToCutBuilder->Build();
359 if(!aToCutBuilder->IsDone()) {
362 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(
363 new GeomAlgoAPI_MakeShape(aToCutBuilder)));
364 aResult = aToCutBuilder->Shape();
365 if(aResult.ShapeType() == TopAbs_COMPOUND) {
366 aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
368 if(aShapeTypeToExp == TopAbs_FACE || aShapeTypeToExp == TopAbs_COMPOUND) {
369 const TopTools_ListOfShape& aToShapes = aToCutBuilder->Modified(aToShape);
370 for(TopTools_ListIteratorOfListOfShape anIt(aToShapes); anIt.More(); anIt.Next()) {
371 GeomShapePtr aGeomSh(new GeomAPI_Shape());
372 aGeomSh->setImpl(new TopoDS_Shape(anIt.Value()));
373 fixOrientation(aGeomSh);
374 this->addToShape(aGeomSh);
378 // Cutting with from plane.
379 BRepAlgoAPI_Cut* aFromCutBuilder = new BRepAlgoAPI_Cut(aResult, aFromSolid);
380 aFromCutBuilder->Build();
381 if(!aFromCutBuilder->IsDone()) {
384 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(
385 new GeomAlgoAPI_MakeShape(aFromCutBuilder)));
386 aResult = aFromCutBuilder->Shape();
387 TopoDS_Iterator aCheckIt(aResult);
388 if(!aCheckIt.More()) {
391 if(aResult.ShapeType() == TopAbs_COMPOUND) {
392 aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
394 if(aShapeTypeToExp == TopAbs_FACE || aShapeTypeToExp == TopAbs_COMPOUND) {
395 const TopTools_ListOfShape& aFromShapes = aFromCutBuilder->Modified(aFromShape);
396 for(TopTools_ListIteratorOfListOfShape anIt(aFromShapes); anIt.More(); anIt.Next()) {
397 GeomShapePtr aGeomSh(new GeomAPI_Shape());
398 aGeomSh->setImpl(new TopoDS_Shape(anIt.Value()));
399 fixOrientation(aGeomSh);
400 this->addFromShape(aGeomSh);
404 // Naming for extrusion from vertex, edge.
405 if(aShapeTypeToExp == TopAbs_COMPOUND) {
406 storeGenerationHistory(this, aResult, TopAbs_EDGE, aToFace, aFromFace);
407 storeGenerationHistory(this, aResult, TopAbs_FACE, aToFace, aFromFace);
409 storeGenerationHistory(this, aResult, aShapeTypeToExp, aToFace, aFromFace);
412 if(aResult.ShapeType() == TopAbs_COMPOUND) {
413 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
414 aGeomShape->setImpl(new TopoDS_Shape(aResult));
415 ListOfShape aResults;
416 aGeomShape = GeomAlgoAPI_ShapeTools::combineShapes(aGeomShape,
417 GeomAPI_Shape::COMPSOLID,
419 aResult = aGeomShape->impl<TopoDS_Shape>();
424 if (!aResult.IsNull()) {
425 aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
426 GeomShapePtr aGeomSh(new GeomAPI_Shape());
427 aGeomSh->setImpl(new TopoDS_Shape(aResult));
428 this->setShape(aGeomSh);
433 // Auxilary functions:
434 //==================================================================================================
435 void storeGenerationHistory(GeomAlgoAPI_Prism* thePrismAlgo,
436 const TopoDS_Shape& theBase,
437 const TopAbs_ShapeEnum theType,
438 BRepPrimAPI_MakePrism* thePrismBuilder)
440 for(TopExp_Explorer anExp(theBase, theType); anExp.More(); anExp.Next()) {
441 const TopoDS_Shape& aShape = anExp.Current();
442 GeomShapePtr aFromShape(new GeomAPI_Shape), aToShape(new GeomAPI_Shape);
443 aFromShape->setImpl(new TopoDS_Shape(thePrismBuilder->FirstShape(aShape)));
444 aToShape->setImpl(new TopoDS_Shape(thePrismBuilder->LastShape(aShape)));
445 thePrismAlgo->fixOrientation(aFromShape);
446 thePrismAlgo->fixOrientation(aToShape);
447 thePrismAlgo->addFromShape(aFromShape);
448 thePrismAlgo->addToShape(aToShape);
452 //==================================================================================================
453 void storeGenerationHistory(GeomAlgoAPI_Prism* thePrismAlgo,
454 const TopoDS_Shape& theResult,
455 const TopAbs_ShapeEnum theType,
456 const TopoDS_Face& theToFace,
457 const TopoDS_Face& theFromFace)
459 for(TopExp_Explorer anExp(theResult, theType); anExp.More(); anExp.Next()) {
460 const TopoDS_Shape& aShape = anExp.Current();
461 GeomShapePtr aGeomSh(new GeomAPI_Shape());
462 if(theType == TopAbs_VERTEX) {
463 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aShape));
464 IntTools_Context anIntTools;
465 if(anIntTools.IsValidPointForFace(aPnt,
466 theToFace, Precision::Confusion()) == Standard_True) {
467 aGeomSh->setImpl(new TopoDS_Shape(aShape));
468 thePrismAlgo->fixOrientation(aGeomSh);
469 thePrismAlgo->addToShape(aGeomSh);
471 if(anIntTools.IsValidPointForFace(aPnt,
472 theFromFace, Precision::Confusion()) == Standard_True) {
473 aGeomSh->setImpl(new TopoDS_Shape(aShape));
474 thePrismAlgo->fixOrientation(aGeomSh);
475 thePrismAlgo->addFromShape(aGeomSh);
477 } else if(theType == TopAbs_EDGE) {
478 TopoDS_Edge anEdge = TopoDS::Edge(aShape);
479 BRepLib_CheckCurveOnSurface anEdgeCheck(anEdge, theToFace);
480 anEdgeCheck.Perform();
481 if(anEdgeCheck.MaxDistance() < Precision::Confusion()) {
482 aGeomSh->setImpl(new TopoDS_Shape(aShape));
483 thePrismAlgo->fixOrientation(aGeomSh);
484 thePrismAlgo->addToShape(aGeomSh);
486 anEdgeCheck.Init(anEdge, theFromFace);
487 anEdgeCheck.Perform();
488 if(anEdgeCheck.MaxDistance() < Precision::Confusion()) {
489 aGeomSh->setImpl(new TopoDS_Shape(aShape));
490 thePrismAlgo->fixOrientation(aGeomSh);
491 thePrismAlgo->addFromShape(aGeomSh);