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_Revolution.h"
23 #include <GeomAPI_Face.h>
24 #include <GeomAPI_Pln.h>
25 #include <GeomAPI_ShapeExplorer.h>
26 #include <GeomAlgoAPI_DFLoader.h>
27 #include <GeomAlgoAPI_FaceBuilder.h>
28 #include <GeomAlgoAPI_MakeShapeList.h>
29 #include <GeomAlgoAPI_ShapeTools.h>
31 #include <BRep_Builder.hxx>
32 #include <BRep_Tool.hxx>
33 #include <BRepAlgoAPI_Cut.hxx>
34 #include <BRepBuilderAPI_FindPlane.hxx>
35 #include <BRepBuilderAPI_MakeFace.hxx>
36 #include <BRepBuilderAPI_Transform.hxx>
37 #include <BRepCheck_Analyzer.hxx>
38 #include <Geom_Curve.hxx>
39 #include <Geom2d_Curve.hxx>
40 #include <BRepLib_CheckCurveOnSurface.hxx>
41 #include <BRepPrimAPI_MakeRevol.hxx>
42 #include <BRepGProp.hxx>
43 #include <GC_MakePlane.hxx>
44 #include <Geom_Plane.hxx>
45 #include <GeomLib_IsPlanarSurface.hxx>
47 #include <GProp_GProps.hxx>
48 #include <IntTools_Context.hxx>
49 #include <TopExp_Explorer.hxx>
51 #include <TopoDS_Edge.hxx>
52 #include <TopTools_ListIteratorOfListOfShape.hxx>
54 /// \brief Constructs infinite face from thePlane, and with axis located on the same side
55 /// of the plane as thePoint. Modifies thePlane axis direction.
56 /// \param[in,out] thePlane plane to construct face.
57 /// \param[in] thePoint point to locate plane axis.
58 /// \return constructed face.
59 static TopoDS_Face makeFaceFromPlane(gp_Pln& thePlane, const gp_Pnt& thePoint);
61 /// \return solid created from face or shell.
62 static TopoDS_Solid makeSolidFromShape(const TopoDS_Shape& theShape);
64 /// \brief return centre of mass for theShape.
65 /// \param[in] theShape shape.
66 static gp_Pnt centreOfMass(const TopoDS_Shape& theShape);
68 /// \brief Selects solid from theShape with closest center of mass to thePoint
69 /// \param[in] theShape compound with solids.
70 /// \param[in] thePoint point.
72 static TopoDS_Shape findClosest(const TopoDS_Shape& theShape, const gp_Pnt& thePoint);
74 static void storeGenerationHistory(GeomAlgoAPI_Revolution* theRevolutionAlgo,
75 const TopoDS_Shape& theBase,
76 const TopAbs_ShapeEnum theType,
77 BRepPrimAPI_MakeRevol* theRevolBuilder);
79 static void storeGenerationHistory(GeomAlgoAPI_Revolution* theRevolutionAlgo,
80 const TopoDS_Shape& theResult,
81 const TopAbs_ShapeEnum theType,
82 const TopoDS_Shape& theToFace,
83 const TopoDS_Shape& theFromFace);
85 static void storeGenerationHistory(GeomAlgoAPI_Revolution* theRevolutionAlgo,
86 const TopoDS_Shape& theResult,
87 const TopAbs_ShapeEnum theType,
88 const TopoDS_Shape& theRotatedBoundingFace,
89 const TopoDS_Shape& theModifiedBaseShape,
90 const bool theIsFromFaceSet);
92 //==================================================================================================
93 GeomAlgoAPI_Revolution::GeomAlgoAPI_Revolution(const GeomShapePtr theBaseShape,
94 const std::shared_ptr<GeomAPI_Ax1> theAxis,
95 const double theToAngle,
96 const double theFromAngle)
98 build(theBaseShape, theAxis, GeomShapePtr(), theToAngle, GeomShapePtr(), theFromAngle);
101 //==================================================================================================
102 GeomAlgoAPI_Revolution::GeomAlgoAPI_Revolution(const GeomShapePtr theBaseShape,
103 const std::shared_ptr<GeomAPI_Ax1> theAxis,
104 const GeomShapePtr theToShape,
105 const double theToAngle,
106 const GeomShapePtr theFromShape,
107 const double theFromAngle)
109 build(theBaseShape, theAxis, theToShape, theToAngle, theFromShape, theFromAngle);
112 //==================================================================================================
113 void GeomAlgoAPI_Revolution::build(const GeomShapePtr& theBaseShape,
114 const std::shared_ptr<GeomAPI_Ax1>& theAxis,
115 const GeomShapePtr& theToShape,
116 const double theToAngle,
117 const GeomShapePtr& theFromShape,
118 const double theFromAngle)
120 if(!theBaseShape || !theAxis ||
121 (((!theFromShape && !theToShape) ||
122 (theFromShape && theToShape && theFromShape->isEqual(theToShape)))
123 && (theFromAngle == -theToAngle))) {
127 // Getting base shape.
128 const TopoDS_Shape& aBaseShape = theBaseShape->impl<TopoDS_Shape>();
129 TopAbs_ShapeEnum aShapeTypeToExp;
130 switch(aBaseShape.ShapeType()) {
132 aShapeTypeToExp = TopAbs_VERTEX;
136 aShapeTypeToExp = TopAbs_EDGE;
140 aShapeTypeToExp = TopAbs_FACE;
142 case TopAbs_COMPOUND:
143 aShapeTypeToExp = TopAbs_COMPOUND;
150 gp_Ax1 anAxis = theAxis->impl<gp_Ax1>();
152 // Getting base plane.
153 Handle(Geom_Plane) aBasePlane;
154 BRepBuilderAPI_FindPlane aFindPlane(aBaseShape);
155 if(aShapeTypeToExp == TopAbs_FACE && aFindPlane.Found() == Standard_True) {
156 aBasePlane = aFindPlane.Plane();
158 gp_Pnt aPnt1 = anAxis.Location();
159 gp_Pnt aPnt2 = aPnt1;
160 aPnt2.Translate(anAxis.Direction());
163 for(TopExp_Explorer anExp(aBaseShape, TopAbs_VERTEX); anExp.More(); anExp.Next()) {
164 aPnt3 = BRep_Tool::Pnt(TopoDS::Vertex(anExp.Current()));
166 GC_MakePlane aMkPlane(aPnt1, aPnt2, aPnt3);
167 if(aMkPlane.IsDone() != Standard_True) {
171 aBasePlane = aMkPlane.Value();
175 if(aBasePlane.IsNull()) {
176 aPnt3 = centreOfMass(aBaseShape);
178 GC_MakePlane aMkPlane(aPnt1, aPnt2, aPnt3);
179 if(aMkPlane.IsDone() != Standard_True) {
183 aBasePlane = aMkPlane.Value();
187 if(aShapeTypeToExp == TopAbs_FACE) {
188 if(aBasePlane->Axis().Angle(anAxis) < Precision::Confusion()) {
193 gp_Pnt aBaseCentre = GeomAlgoAPI_ShapeTools::centreOfMass(theBaseShape)->impl<gp_Pnt>();
195 TopoDS_Shape aResult;
196 if(!theFromShape && !theToShape) { // Case 1: When only angles was set.
197 // Rotating base face with the negative value of "from angle".
199 aBaseTrsf.SetRotation(anAxis, -theFromAngle / 180.0 * M_PI);
200 BRepBuilderAPI_Transform* aBaseTransform = new BRepBuilderAPI_Transform(aBaseShape,
203 if(!aBaseTransform) {
206 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(
207 new GeomAlgoAPI_MakeShape(aBaseTransform)));
208 if(!aBaseTransform->IsDone()) {
211 TopoDS_Shape aRotatedBase = aBaseTransform->Shape();
213 // Making revolution to the angle equal to the sum of "from angle" and "to angle".
214 BRepPrimAPI_MakeRevol* aRevolBuilder = new BRepPrimAPI_MakeRevol(aRotatedBase,
216 (theFromAngle + theToAngle) / 180 * M_PI,
221 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(
222 new GeomAlgoAPI_MakeShape(aRevolBuilder)));
223 if(!aRevolBuilder->IsDone()) {
226 aResult = aRevolBuilder->Shape();
229 if(aShapeTypeToExp == TopAbs_COMPOUND) {
230 storeGenerationHistory(this, aRotatedBase, TopAbs_EDGE, aRevolBuilder);
231 storeGenerationHistory(this, aRotatedBase, TopAbs_FACE, aRevolBuilder);
233 storeGenerationHistory(this, aRotatedBase, aShapeTypeToExp, aRevolBuilder);
235 } else if(theFromShape && theToShape) { // Case 2: When both bounding planes were set.
236 // Making revolution to the 360 angle.
237 BRepPrimAPI_MakeRevol* aRevolBuilder =
238 new BRepPrimAPI_MakeRevol(aBaseShape, anAxis, 2 * M_PI, Standard_True);
242 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(
243 new GeomAlgoAPI_MakeShape(aRevolBuilder)));
244 if(!aRevolBuilder->IsDone()) {
247 aResult = aRevolBuilder->Shape();
249 // Getting bounding faces.
250 TopoDS_Face aFromFace = TopoDS::Face(theFromShape->impl<TopoDS_Shape>());
251 TopoDS_Face aToFace = TopoDS::Face(theToShape->impl<TopoDS_Shape>());
253 // Getting planes from bounding face.
254 GeomLib_IsPlanarSurface isFromPlanar(BRep_Tool::Surface(aFromFace));
255 GeomLib_IsPlanarSurface isToPlanar(BRep_Tool::Surface(aToFace));
256 if(!isFromPlanar.IsPlanar() || !isToPlanar.IsPlanar()) {
257 // non-planar shapes is not supported for revolution bounding
261 std::shared_ptr<GeomAPI_Face> aGeomFromFace(new GeomAPI_Face(theFromShape));
262 std::shared_ptr<GeomAPI_Face> aGeomToFace(new GeomAPI_Face(theToShape));
264 gp_Pln aFromPln = aGeomFromFace->getPlane()->impl<gp_Pln>();
265 gp_Pln aToPln = aGeomToFace->getPlane()->impl<gp_Pln>();
267 // Orienting bounding planes properly so that the center of mass of the base face stays
268 // on the result shape after cut.
269 aFromFace = makeFaceFromPlane(aFromPln, aBaseCentre);
270 aToFace = makeFaceFromPlane(aToPln, aBaseCentre);
272 // Making solids from bounding planes and putting them in compound.
273 TopoDS_Shape aFromSolid = makeSolidFromShape(aFromFace);
274 TopoDS_Shape aToSolid = makeSolidFromShape(aToFace);
276 // Rotating bounding planes to the specified angle.
279 double aFromRotAngle =
280 ((aFromPln.Axis().Direction() * aBasePlane->Axis().Direction()) > 0) ? -theFromAngle :
283 ((aToPln.Axis().Direction() * aBasePlane->Axis().Direction()) > 0) ? -theToAngle :
285 aFromTrsf.SetRotation(anAxis,aFromRotAngle / 180.0 * M_PI);
286 aToTrsf.SetRotation(anAxis, aToRotAngle / 180.0 * M_PI);
287 BRepBuilderAPI_Transform aFromTransform(aFromSolid, aFromTrsf, true);
288 BRepBuilderAPI_Transform aToTransform(aToSolid, aToTrsf, true);
289 TopoDS_Shape aRotatedFromFace = aFromTransform.Modified(aFromFace).First();
290 TopoDS_Shape aRotatedToFace = aToTransform.Modified(aToFace).First();
291 aFromSolid = aFromTransform.Shape();
292 aToSolid = aToTransform.Shape();
294 // Cutting revolution with from plane.
295 BRepAlgoAPI_Cut* aFromCutBuilder = new BRepAlgoAPI_Cut(aResult, aFromSolid);
296 aFromCutBuilder->Build();
297 if(!aFromCutBuilder->IsDone()) {
300 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(
301 new GeomAlgoAPI_MakeShape(aFromCutBuilder)));
302 aResult = aFromCutBuilder->Shape();
303 if(aResult.ShapeType() == TopAbs_COMPOUND) {
304 aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
307 // Cutting revolution with to plane.
308 BRepAlgoAPI_Cut* aToCutBuilder = new BRepAlgoAPI_Cut(aResult, aToSolid);
309 aToCutBuilder->Build();
310 if(!aToCutBuilder->IsDone()) {
313 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(
314 new GeomAlgoAPI_MakeShape(aToCutBuilder)));
315 aResult = aToCutBuilder->Shape();
316 TopoDS_Iterator aCheckIt(aResult);
317 if(!aCheckIt.More()) {
320 if(aResult.ShapeType() == TopAbs_COMPOUND) {
321 aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
323 if(aResult.ShapeType() == TopAbs_COMPOUND) {
324 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
325 aGeomShape->setImpl(new TopoDS_Shape(aResult));
326 ListOfShape aCompSolids, aFreeSolids;
327 aGeomShape = GeomAlgoAPI_ShapeTools::combineShapes(aGeomShape,
328 GeomAPI_Shape::COMPSOLID,
331 aResult = aGeomShape->impl<TopoDS_Shape>();
334 // If after cut we got more than one solids then take closest
335 // to the center of mass of the base face.
336 aResult = findClosest(aResult, aBaseCentre);
339 if(aShapeTypeToExp == TopAbs_COMPOUND) {
340 storeGenerationHistory(this, aResult, TopAbs_EDGE, aRotatedToFace, aRotatedFromFace);
341 storeGenerationHistory(this, aResult, TopAbs_FACE, aRotatedToFace, aRotatedFromFace);
343 storeGenerationHistory(this, aResult, aShapeTypeToExp, aRotatedToFace, aRotatedFromFace);
345 } else { //Case 3: When only one bounding plane was set.
346 // Making revolution to the 360 angle.
347 BRepPrimAPI_MakeRevol* aRevolBuilder =
348 new BRepPrimAPI_MakeRevol(aBaseShape, anAxis, 2 * M_PI, Standard_True);
352 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(
353 new GeomAlgoAPI_MakeShape(aRevolBuilder)));
354 if(!aRevolBuilder->IsDone()) {
357 aResult = aRevolBuilder->Shape();
359 // Getting bounding face.
360 bool isFromFaceSet = false;
361 std::shared_ptr<GeomAPI_Face> aGeomBoundingFace;
363 aGeomBoundingFace.reset(new GeomAPI_Face(theFromShape));
364 isFromFaceSet = true;
365 } else if(theToShape) {
366 aGeomBoundingFace.reset(new GeomAPI_Face(theToShape));
368 TopoDS_Face aBoundingFace = TopoDS::Face(aGeomBoundingFace->impl<TopoDS_Shape>());
370 // Getting plane from bounding face.
371 GeomLib_IsPlanarSurface isBoundingPlanar(BRep_Tool::Surface(aBoundingFace));
372 if(!isBoundingPlanar.IsPlanar()) { // non-planar shapes is not supported for revolution bounding
376 gp_Pln aBoundingPln = aGeomBoundingFace->getPlane()->impl<gp_Pln>();
378 // Orienting bounding plane properly so that the center of mass of the base face stays
379 // on the result shape after cut.
380 aBoundingFace = makeFaceFromPlane(aBoundingPln, aBaseCentre);
382 // Making solid from bounding plane.
383 TopoDS_Shape aBoundingSolid = makeSolidFromShape(aBoundingFace);
385 // Rotating bounding plane to the specified angle.
386 double aBoundingRotAngle = isFromFaceSet ? theFromAngle : theToAngle;
387 if(aBoundingPln.Axis().IsParallel(aBasePlane->Axis(), Precision::Confusion())) {
388 if(isFromFaceSet) aBoundingRotAngle = -aBoundingRotAngle;
390 double aSign = (aBoundingPln.Axis().Direction() ^ aBasePlane->Axis().Direction()) *
392 if((aSign <= 0 && !isFromFaceSet) || (aSign > 0 && isFromFaceSet)) {
393 aBoundingRotAngle = -aBoundingRotAngle;
396 gp_Trsf aBoundingTrsf;
397 aBoundingTrsf.SetRotation(anAxis, aBoundingRotAngle / 180.0 * M_PI);
398 BRepBuilderAPI_Transform aBoundingTransform(aBoundingSolid, aBoundingTrsf, true);
399 TopoDS_Shape aRotatedBoundingFace = aBoundingTransform.Modified(aBoundingFace).First();
400 aBoundingSolid = aBoundingTransform.Shape();
402 // Cutting revolution with bounding plane.
403 BRepAlgoAPI_Cut* aBoundingCutBuilder = new BRepAlgoAPI_Cut(aResult, aBoundingSolid);
404 aBoundingCutBuilder->Build();
405 if(!aBoundingCutBuilder->IsDone()) {
408 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(
409 new GeomAlgoAPI_MakeShape(aBoundingCutBuilder)));
410 aResult = aBoundingCutBuilder->Shape();
411 if(aResult.ShapeType() == TopAbs_COMPOUND) {
412 aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
416 if(aShapeTypeToExp == TopAbs_FACE || aShapeTypeToExp == TopAbs_COMPOUND) {
417 const TopTools_ListOfShape& aBndShapes = aBoundingCutBuilder->Modified(aBoundingFace);
418 for(TopTools_ListIteratorOfListOfShape anIt(aBndShapes); anIt.More(); anIt.Next()) {
419 GeomShapePtr aShape(new GeomAPI_Shape());
420 aShape->setImpl(new TopoDS_Shape(anIt.Value()));
421 isFromFaceSet ? this->addFromShape(aShape) : this->addToShape(aShape);
425 // Try to cut with base face. If it can not be done then keep result of cut with bounding plane.
426 TopoDS_Shape aModifiedBaseShape;
427 if(aShapeTypeToExp != TopAbs_FACE) {
429 GeomShapePtr aSh(new GeomAPI_Shape());
430 aSh->setImpl(new TopoDS_Shape(aBaseShape));
431 std::shared_ptr<GeomAPI_Pnt> theCenter(new GeomAPI_Pnt(aBasePlane->Location().X(),
432 aBasePlane->Location().Y(),
433 aBasePlane->Location().Z()));
434 std::shared_ptr<GeomAPI_Dir> theNormal(new GeomAPI_Dir(aBasePlane->Axis().Direction().X(),
435 aBasePlane->Axis().Direction().Y(),
436 aBasePlane->Axis().Direction().Z()));
437 GeomShapePtr aPln = GeomAlgoAPI_FaceBuilder::planarFace(theCenter, theNormal);
438 aList.push_back(aSh);
439 std::list<std::shared_ptr<GeomAPI_Pnt> >
440 aBoundingPoints = GeomAlgoAPI_ShapeTools::getBoundingBox(aList);
441 aSh = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aPln, aBoundingPoints);
442 aModifiedBaseShape = aSh->impl<TopoDS_Shape>();
444 aModifiedBaseShape = aBaseShape;
447 if(aModifiedBaseShape.ShapeType() == TopAbs_FACE) {
448 aModifiedBaseShape.Orientation(TopAbs_REVERSED);
451 aMirrorTrsf.SetMirror(aBasePlane->Position().Ax2());
452 BRepBuilderAPI_Transform aMirrorTransform(aModifiedBaseShape, aMirrorTrsf, true);
453 aModifiedBaseShape = aMirrorTransform.Shape();
457 // Making solid from base face.
458 TopoDS_Shape aBaseSolid = makeSolidFromShape(aModifiedBaseShape);
460 // Rotating base face to the specified angle.
462 double aBaseRotAngle = isFromFaceSet ? theToAngle : -theFromAngle;
463 aBaseTrsf.SetRotation(anAxis, aBaseRotAngle / 180.0 * M_PI);
464 BRepBuilderAPI_Transform aBaseTransform(aBaseSolid, aBaseTrsf, true);
465 aBaseSolid = aBaseTransform.Shape();
467 // Cutting revolution with base.
468 BRepAlgoAPI_Cut* aBaseCutBuilder = new BRepAlgoAPI_Cut(aResult, aBaseSolid);
469 aBaseCutBuilder->Build();
470 if(aBaseCutBuilder->IsDone()) {
471 TopoDS_Shape aCutResult = aBaseCutBuilder->Shape();
472 TopoDS_Iterator aCheckIt(aCutResult);
473 if(aCheckIt.More()) {
474 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(
475 new GeomAlgoAPI_MakeShape(aBaseCutBuilder)));
476 aResult = aCutResult;
477 if(aResult.ShapeType() == TopAbs_COMPOUND) {
478 aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
480 if(aShapeTypeToExp == TopAbs_FACE || aShapeTypeToExp == TopAbs_COMPOUND) {
481 const TopTools_ListOfShape& aBsShapes = aBaseCutBuilder->Modified(aBoundingFace);
482 for(TopTools_ListIteratorOfListOfShape anIt(aBsShapes); anIt.More(); anIt.Next()) {
483 GeomShapePtr aShape(new GeomAPI_Shape());
484 aShape->setImpl(new TopoDS_Shape(anIt.Value()));
485 isFromFaceSet ? this->addToShape(aShape) : this->addFromShape(aShape);
491 if(aResult.ShapeType() == TopAbs_COMPOUND) {
492 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
493 aGeomShape->setImpl(new TopoDS_Shape(aResult));
494 ListOfShape aCompSolids, aFreeSolids;
495 aGeomShape = GeomAlgoAPI_ShapeTools::combineShapes(aGeomShape,
496 GeomAPI_Shape::COMPSOLID,
499 aResult = aGeomShape->impl<TopoDS_Shape>();
502 // If after cut we got more than one solids then take
503 // closest to the center of mass of the base face.
504 aResult = findClosest(aResult, aBaseCentre);
507 if(aShapeTypeToExp == TopAbs_COMPOUND) {
508 storeGenerationHistory(this, aResult, TopAbs_EDGE,
509 aRotatedBoundingFace, aModifiedBaseShape, isFromFaceSet);
510 storeGenerationHistory(this, aResult, TopAbs_FACE,
511 aRotatedBoundingFace, aModifiedBaseShape, isFromFaceSet);
513 storeGenerationHistory(this, aResult, aShapeTypeToExp,
514 aRotatedBoundingFace, aModifiedBaseShape, isFromFaceSet);
519 if(aResult.IsNull()) {
522 aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
523 GeomShapePtr aShape(new GeomAPI_Shape());
524 aShape->setImpl(new TopoDS_Shape(aResult));
525 this->setShape(aShape);
529 //==================================================================================================
530 TopoDS_Face makeFaceFromPlane(gp_Pln& thePlane, const gp_Pnt& thePoint)
532 if(!thePlane.Contains(thePoint, Precision::Confusion())) {
533 gp_XYZ aVec = thePoint.XYZ() - thePlane.Location().XYZ();
534 double aSign = aVec * thePlane.Axis().Direction().XYZ();
535 if(aSign < 0) thePlane.SetAxis(thePlane.Axis().Reversed());
538 BRepBuilderAPI_MakeFace aMakeFace(thePlane);
539 TopoDS_Face aResultFace = TopoDS::Face(aMakeFace.Shape());
544 //==================================================================================================
545 TopoDS_Solid makeSolidFromShape(const TopoDS_Shape& theShape)
550 BRep_Builder aBoundingBuilder;
551 if(theShape.ShapeType() == TopAbs_SHELL) {
552 aShell = TopoDS::Shell(theShape);
554 aBoundingBuilder.MakeShell(aShell);
555 aBoundingBuilder.Add(aShell, theShape);
557 aBoundingBuilder.MakeSolid(aSolid);
558 aBoundingBuilder.Add(aSolid, aShell);
563 //================================================================================================
564 gp_Pnt centreOfMass(const TopoDS_Shape& theShape)
566 TopAbs_ShapeEnum aShType = theShape.ShapeType();
567 GProp_GProps aGProps;
569 if(aShType == TopAbs_EDGE || aShType == TopAbs_WIRE) {
570 BRepGProp::LinearProperties(theShape, aGProps);
571 } else if(aShType == TopAbs_FACE || aShType == TopAbs_SHELL) {
572 BRepGProp::SurfaceProperties(theShape, aGProps);
573 } else if(aShType == TopAbs_SOLID || aShType == TopAbs_COMPSOLID) {
574 BRepGProp::VolumeProperties(theShape, aGProps);
577 return aGProps.CentreOfMass();
580 //================================================================================================
581 TopoDS_Shape findClosest(const TopoDS_Shape& theShape, const gp_Pnt& thePoint)
583 TopoDS_Shape aResult = theShape;
585 if(theShape.ShapeType() == TopAbs_COMPOUND) {
586 double aMinDistance = Precision::Infinite();
589 for (TopoDS_Iterator anItr(theShape); anItr.More(); anItr.Next()) {
590 TopoDS_Shape aValue = anItr.Value();
591 aCentr = centreOfMass(aValue);
592 aCurDistance = aCentr.Distance(thePoint);
594 if(aCurDistance < aMinDistance) {
595 aMinDistance = aCurDistance;
604 //================================================================================================
605 void storeGenerationHistory(GeomAlgoAPI_Revolution* theRevolutionAlgo,
606 const TopoDS_Shape& theBase,
607 const TopAbs_ShapeEnum theType,
608 BRepPrimAPI_MakeRevol* theRevolBuilder)
610 for(TopExp_Explorer anExp(theBase, theType); anExp.More(); anExp.Next()) {
611 const TopoDS_Shape& aShape = anExp.Current();
612 GeomShapePtr aFromShape(new GeomAPI_Shape), aToShape(new GeomAPI_Shape);
613 aFromShape->setImpl(new TopoDS_Shape(theRevolBuilder->FirstShape(aShape)));
614 aToShape->setImpl(new TopoDS_Shape(theRevolBuilder->LastShape(aShape)));
615 theRevolutionAlgo->addFromShape(aFromShape);
616 theRevolutionAlgo->addToShape(aToShape);
620 //================================================================================================
621 void storeGenerationHistory(GeomAlgoAPI_Revolution* theRevolutionAlgo,
622 const TopoDS_Shape& theResult,
623 const TopAbs_ShapeEnum theType,
624 const TopoDS_Shape& theToFace,
625 const TopoDS_Shape& theFromFace)
627 for(TopExp_Explorer anExp(theResult, theType); anExp.More (); anExp.Next ()) {
628 const TopoDS_Shape& aShape = anExp.Current();
629 GeomShapePtr aGeomSh(new GeomAPI_Shape());
630 if(theType == TopAbs_VERTEX) {
631 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aShape));
632 IntTools_Context anIntTools;
633 if(anIntTools.IsValidPointForFace(aPnt, TopoDS::Face(theToFace),
634 Precision::Confusion()) == Standard_True) {
635 aGeomSh->setImpl(new TopoDS_Shape(aShape));
636 theRevolutionAlgo->addToShape(aGeomSh);
638 if(anIntTools.IsValidPointForFace(aPnt, TopoDS::Face(theFromFace),
639 Precision::Confusion()) == Standard_True) {
640 aGeomSh->setImpl(new TopoDS_Shape(aShape));
641 theRevolutionAlgo->addFromShape(aGeomSh);
643 } else if(theType == TopAbs_EDGE) {
644 TopoDS_Edge anEdge = TopoDS::Edge(aShape);
645 BRepLib_CheckCurveOnSurface anEdgeCheck(anEdge, TopoDS::Face(theToFace));
646 anEdgeCheck.Perform();
647 if(anEdgeCheck.MaxDistance() < Precision::Confusion()) {
648 aGeomSh->setImpl(new TopoDS_Shape(aShape));
649 theRevolutionAlgo->addToShape(aGeomSh);
651 anEdgeCheck.Init(anEdge, TopoDS::Face(theFromFace));
652 anEdgeCheck.Perform();
653 if(anEdgeCheck.MaxDistance() < Precision::Confusion()) {
654 aGeomSh->setImpl(new TopoDS_Shape(aShape));
655 theRevolutionAlgo->addFromShape(aGeomSh);
658 Handle(Geom_Surface) aFaceSurface = BRep_Tool::Surface(TopoDS::Face(aShape));
659 Handle(Geom_Surface) aFromSurface = BRep_Tool::Surface(TopoDS::Face(theFromFace));
660 Handle(Geom_Surface) aToSurface = BRep_Tool::Surface(TopoDS::Face(theToFace));
661 if(aFaceSurface == aFromSurface) {
662 aGeomSh->setImpl(new TopoDS_Shape(aShape));
663 theRevolutionAlgo->addFromShape(aGeomSh);
665 if(aFaceSurface == aToSurface) {
666 aGeomSh->setImpl(new TopoDS_Shape(aShape));
667 theRevolutionAlgo->addToShape(aGeomSh);
673 void storeGenerationHistory(GeomAlgoAPI_Revolution* theRevolutionAlgo,
674 const TopoDS_Shape& theResult,
675 const TopAbs_ShapeEnum theType,
676 const TopoDS_Shape& theRotatedBoundingFace,
677 const TopoDS_Shape& theModifiedBaseShape,
678 const bool theIsFromFaceSet)
680 for(TopExp_Explorer anExp(theResult, theType); anExp.More (); anExp.Next ()) {
681 const TopoDS_Shape& aShape = anExp.Current();
682 GeomShapePtr aGeomSh(new GeomAPI_Shape());
683 if(theType == TopAbs_VERTEX) {
684 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aShape));
685 IntTools_Context anIntTools;
686 if(anIntTools.IsValidPointForFace(aPnt, TopoDS::Face(theRotatedBoundingFace),
687 Precision::Confusion()) == Standard_True) {
688 aGeomSh->setImpl(new TopoDS_Shape(aShape));
689 theIsFromFaceSet ? theRevolutionAlgo->addFromShape(aGeomSh) :
690 theRevolutionAlgo->addToShape(aGeomSh);
692 if(anIntTools.IsValidPointForFace(aPnt, TopoDS::Face(theModifiedBaseShape),
693 Precision::Confusion()) == Standard_True) {
694 aGeomSh->setImpl(new TopoDS_Shape(aShape));
695 theIsFromFaceSet ? theRevolutionAlgo->addToShape(aGeomSh) :
696 theRevolutionAlgo->addFromShape(aGeomSh);
698 } else if(theType == TopAbs_EDGE) {
699 TopoDS_Edge anEdge = TopoDS::Edge(aShape);
700 BRepLib_CheckCurveOnSurface anEdgeCheck(anEdge, TopoDS::Face(theRotatedBoundingFace));
701 anEdgeCheck.Perform();
702 if(anEdgeCheck.MaxDistance() < Precision::Confusion()) {
703 aGeomSh->setImpl(new TopoDS_Shape(aShape));
704 theIsFromFaceSet ? theRevolutionAlgo->addFromShape(aGeomSh) :
705 theRevolutionAlgo->addToShape(aGeomSh);
707 anEdgeCheck.Init(anEdge, TopoDS::Face(theModifiedBaseShape));
708 anEdgeCheck.Perform();
709 if(anEdgeCheck.MaxDistance() < Precision::Confusion()) {
710 aGeomSh->setImpl(new TopoDS_Shape(aShape));
711 theIsFromFaceSet ? theRevolutionAlgo->addToShape(aGeomSh) :
712 theRevolutionAlgo->addFromShape(aGeomSh);
715 Handle(Geom_Surface) aFaceSurface = BRep_Tool::Surface(TopoDS::Face(aShape));
716 Handle(Geom_Surface) aBoundingSurface =
717 BRep_Tool::Surface(TopoDS::Face(theRotatedBoundingFace));
718 Handle(Geom_Surface) aBaseSurface = BRep_Tool::Surface(TopoDS::Face(theModifiedBaseShape));
719 if(aFaceSurface == aBoundingSurface) {
720 aGeomSh->setImpl(new TopoDS_Shape(aShape));
721 theIsFromFaceSet ? theRevolutionAlgo->addFromShape(aGeomSh) :
722 theRevolutionAlgo->addToShape(aGeomSh);
724 if(aFaceSurface == aBaseSurface) {
725 aGeomSh->setImpl(new TopoDS_Shape(aShape));
726 theIsFromFaceSet ? theRevolutionAlgo->addToShape(aGeomSh) :
727 theRevolutionAlgo->addFromShape(aGeomSh);