Salome HOME
Issue #1383 Preview button: correction for the case: switch off auto_preview in extru...
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_PaveFiller.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomAlgoAPI_PaveFiller.cpp
4 // Created:     27 August 2015
5 // Author:      Dmitry Bobylev
6
7 #include "GeomAlgoAPI_PaveFiller.h"
8
9 #include <GeomAlgoAPI_DFLoader.h>
10 #include <GeomAlgoAPI_ShapeTools.h>
11
12 #include <BOPAlgo_Builder.hxx>
13 #include <BOPAlgo_PaveFiller.hxx>
14 #include <TopoDS_Builder.hxx>
15 #include <TopoDS_Iterator.hxx>
16
17 //=================================================================================================
18 GeomAlgoAPI_PaveFiller::GeomAlgoAPI_PaveFiller(const ListOfShape& theListOfShape, const bool theIsMakeCompSolids)
19 {
20   build(theListOfShape, theIsMakeCompSolids);
21 }
22
23
24 //=================================================================================================
25 void GeomAlgoAPI_PaveFiller::build(const ListOfShape& theListOfShape, const bool theIsMakeCompSolids)
26 {
27   BOPAlgo_PaveFiller aPaveFiller;
28   BOPCol_ListOfShape aListOfShape;
29   for(ListOfShape::const_iterator anIt = theListOfShape.cbegin(); anIt != theListOfShape.cend(); anIt++) {
30     const TopoDS_Shape& aShape = (*anIt)->impl<TopoDS_Shape>();
31     if(aShape.ShapeType() == TopAbs_COMPOUND) {
32       for(TopoDS_Iterator anIter(aShape); anIter.More(); anIter.Next()) {
33         aListOfShape.Append(anIter.Value());
34       }
35     } else {
36       aListOfShape.Append(aShape);
37     }
38   }
39   aPaveFiller.SetArguments(aListOfShape);
40   aPaveFiller.Perform();
41   Standard_Integer iErr = aPaveFiller.ErrorStatus();
42   if(iErr) {
43     return;
44   }
45
46   BOPAlgo_Builder* aBuilder = new BOPAlgo_Builder();
47   this->setImpl(aBuilder);
48   this->setBuilderType(OCCT_BOPAlgo_Builder);
49   aBuilder->SetArguments(aListOfShape);
50   aBuilder->PerformWithFiller(aPaveFiller);
51   iErr = aBuilder->ErrorStatus();
52   if(iErr) {
53     return;
54   }
55
56   TopoDS_Shape aResult = aBuilder->Shape();
57   if(aResult.ShapeType() == TopAbs_COMPOUND) {
58     aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
59   }
60   if(theIsMakeCompSolids && aResult.ShapeType() == TopAbs_COMPOUND) {
61     std::shared_ptr<GeomAPI_Shape> aCompound(new GeomAPI_Shape);
62     aCompound->setImpl(new TopoDS_Shape(aResult));
63     ListOfShape aCompSolids, aFreeSolids;
64     GeomAlgoAPI_ShapeTools::combineShapes(aCompound, GeomAPI_Shape::COMPSOLID, aCompSolids, aFreeSolids);
65     if(aCompSolids.size() == 1 && aFreeSolids.size() == 0) {
66       aResult = aCompSolids.front()->impl<TopoDS_Shape>();
67     } else if (aCompSolids.size() > 1 || (aCompSolids.size() >= 1 && aFreeSolids.size() >= 1)) {
68       TopoDS_Compound aResultComp;
69       TopoDS_Builder aBuilder;
70       aBuilder.MakeCompound(aResultComp);
71       for(ListOfShape::const_iterator anIter = aCompSolids.cbegin(); anIter != aCompSolids.cend(); anIter++) {
72         aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
73       }
74       for(ListOfShape::const_iterator anIter = aFreeSolids.cbegin(); anIter != aFreeSolids.cend(); anIter++) {
75         aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
76       }
77       aResult = aResultComp;
78     }
79   }
80
81   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
82   aShape->setImpl(new TopoDS_Shape(aResult));
83   this->setShape(aShape);
84   this->setDone(true);
85 }