Salome HOME
Fix for Eclipse launch
[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 <BRepCheck_Analyzer.hxx>
15 #include <TopExp_Explorer.hxx>
16 #include <TopoDS_Builder.hxx>
17
18 //=================================================================================================
19 GeomAlgoAPI_PaveFiller::GeomAlgoAPI_PaveFiller(const ListOfShape& theListOfShape, const bool theIsMakeCompSolids)
20 : myDone(false)
21 {
22   build(theListOfShape, theIsMakeCompSolids);
23 }
24
25
26 //=================================================================================================
27 void GeomAlgoAPI_PaveFiller::build(const ListOfShape& theListOfShape, const bool theIsMakeCompSolids)
28 {
29   BOPAlgo_PaveFiller aPaveFiller;
30   BOPCol_ListOfShape aListOfShape;
31   for(ListOfShape::const_iterator anIt = theListOfShape.cbegin(); anIt != theListOfShape.cend(); anIt++) {
32     aListOfShape.Append((*anIt)->impl<TopoDS_Shape>());
33   }
34   aPaveFiller.SetArguments(aListOfShape);
35   aPaveFiller.Perform();
36   Standard_Integer iErr = aPaveFiller.ErrorStatus();
37   if(iErr) {
38     return;
39   }
40
41   BOPAlgo_Builder* aBuilder = new BOPAlgo_Builder();
42   myMkShape.reset(new GeomAlgoAPI_MakeShape(aBuilder, GeomAlgoAPI_MakeShape::BOPAlgoBuilder));
43   aBuilder->SetArguments(aListOfShape);
44   aBuilder->PerformWithFiller(aPaveFiller);
45   iErr = aBuilder->ErrorStatus();
46   if(iErr) {
47     return;
48   }
49
50   TopoDS_Shape aResult = aBuilder->Shape();
51   if(aResult.ShapeType() == TopAbs_COMPOUND) {
52     aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
53   }
54   if(theIsMakeCompSolids && aResult.ShapeType() == TopAbs_COMPOUND) {
55     std::shared_ptr<GeomAPI_Shape> aCompound(new GeomAPI_Shape);
56     aCompound->setImpl(new TopoDS_Shape(aResult));
57     ListOfShape aCompSolids, aFreeSolids;
58     GeomAlgoAPI_ShapeTools::combineShapes(aCompound, GeomAPI_Shape::COMPSOLID, aCompSolids, aFreeSolids);
59     if(aCompSolids.size() == 1 && aFreeSolids.size() == 0) {
60       aResult = aCompSolids.front()->impl<TopoDS_Shape>();
61     } else if (aCompSolids.size() > 1 || (aCompSolids.size() >= 1 && aFreeSolids.size() >= 1)) {
62       TopoDS_Compound aResultComp;
63       TopoDS_Builder aBuilder;
64       aBuilder.MakeCompound(aResultComp);
65       for(ListOfShape::const_iterator anIter = aCompSolids.cbegin(); anIter != aCompSolids.cend(); anIter++) {
66         aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
67       }
68       for(ListOfShape::const_iterator anIter = aFreeSolids.cbegin(); anIter != aFreeSolids.cend(); anIter++) {
69         aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
70       }
71       aResult = aResultComp;
72     }
73   }
74
75   // fill data map to keep correct orientation of sub-shapes
76   myMap.reset(new GeomAPI_DataMapOfShapeShape());
77   for (TopExp_Explorer Exp(aResult, TopAbs_FACE); Exp.More(); Exp.Next()) {
78     std::shared_ptr<GeomAPI_Shape> aCurrentShape(new GeomAPI_Shape());
79     aCurrentShape->setImpl(new TopoDS_Shape(Exp.Current()));
80     myMap->bind(aCurrentShape, aCurrentShape);
81   }
82
83   myShape.reset(new GeomAPI_Shape());
84   myShape->setImpl(new TopoDS_Shape(aResult));
85
86   myDone = true;
87 }
88
89 //=================================================================================================
90 const bool GeomAlgoAPI_PaveFiller::isDone() const
91 {
92   return myDone;
93 }
94
95 //=================================================================================================
96 const bool GeomAlgoAPI_PaveFiller::isValid() const
97 {
98   BRepCheck_Analyzer aChecker(myShape->impl<TopoDS_Shape>());
99   return (aChecker.IsValid() == Standard_True);
100 }
101
102 //=================================================================================================
103 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_PaveFiller::shape() const
104 {
105   return myShape;
106 }
107
108 //=================================================================================================
109 std::shared_ptr<GeomAPI_DataMapOfShapeShape> GeomAlgoAPI_PaveFiller::mapOfShapes() const
110 {
111   return myMap;
112 }
113
114 //=================================================================================================
115 std::shared_ptr<GeomAlgoAPI_MakeShape> GeomAlgoAPI_PaveFiller::makeShape() const
116 {
117   return myMkShape;
118 }