]> SALOME platform Git repositories - modules/shaper.git/blob - src/GeomAlgoAPI/GeomAlgoAPI_Partition.cpp
Salome HOME
Compsolids in boolean operations
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Partition.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomAlgoAPI_Partition.cpp
4 // Created:     21 Aug 2015
5 // Author:      Sergey POKHODENKO
6
7 #include "GeomAlgoAPI_Partition.h"
8
9 #include <GeomAlgoAPI_DFLoader.h>
10
11 #include <GEOMAlgo_Splitter.hxx>
12
13 #include <BRepCheck_Analyzer.hxx>
14 #include <TopExp_Explorer.hxx>
15 #include <TopTools_ListOfShape.hxx>
16
17 //=================================================================================================
18 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_Partition::make(const ListOfShape& theObjects,
19                                                            const ListOfShape& theTools)
20 {
21   GeomAlgoAPI_Partition aBoolAlgo(theObjects, theTools);
22   if(aBoolAlgo.isDone() && !aBoolAlgo.shape()->isNull() && aBoolAlgo.isValid()) {
23     return aBoolAlgo.shape();
24   }
25   return std::shared_ptr<GeomAPI_Shape>();
26 }
27
28 //=================================================================================================
29 GeomAlgoAPI_Partition::GeomAlgoAPI_Partition(const ListOfShape& theObjects,
30                                              const ListOfShape& theTools)
31 : myDone(false)
32 {
33   build(theObjects, theTools);
34 }
35
36
37 //=================================================================================================
38 void GeomAlgoAPI_Partition::build(const ListOfShape& theObjects,
39                                   const ListOfShape& theTools)
40 {
41   if (theObjects.empty() || theTools.empty()) {
42     return;
43   }
44
45   // Creating partition operation.
46   GEOMAlgo_Splitter* anOperation = new GEOMAlgo_Splitter;
47   myMkShape.reset(new GeomAlgoAPI_MakeShape(anOperation, GeomAlgoAPI_MakeShape::BOPAlgoBuilder));
48
49   // Getting objects.
50   TopTools_ListOfShape anObjects;
51   for (ListOfShape::const_iterator anObjectsIt = theObjects.begin(); anObjectsIt != theObjects.end(); anObjectsIt++) {
52     const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
53     anOperation->AddArgument(aShape);
54   }
55
56   // Getting tools.
57   TopTools_ListOfShape aTools;
58   for (ListOfShape::const_iterator aToolsIt = theTools.begin(); aToolsIt != theTools.end(); aToolsIt++) {
59     const TopoDS_Shape& aShape = (*aToolsIt)->impl<TopoDS_Shape>();
60     anOperation->AddTool(aShape);
61   }
62
63   // Building and getting result.
64   anOperation->Perform();
65   TopoDS_Shape aResult = anOperation->Shape();
66   myDone = !aResult.IsNull();
67   if (!myDone) {
68     return;
69   }
70
71   if(aResult.ShapeType() == TopAbs_COMPOUND) {
72     aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
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   myShape.reset(new GeomAPI_Shape());
83   myShape->setImpl(new TopoDS_Shape(aResult));
84
85 }
86
87 //=================================================================================================
88 const bool GeomAlgoAPI_Partition::isDone() const
89 {
90   return myDone;
91 }
92
93 //=================================================================================================
94 const bool GeomAlgoAPI_Partition::isValid() const
95 {
96   BRepCheck_Analyzer aChecker(myShape->impl<TopoDS_Shape>());
97   return (aChecker.IsValid() == Standard_True);
98 }
99
100 //=================================================================================================
101 const std::shared_ptr<GeomAPI_Shape>& GeomAlgoAPI_Partition::shape() const
102 {
103   return myShape;
104 }
105
106 //=================================================================================================
107 std::shared_ptr<GeomAPI_DataMapOfShapeShape> GeomAlgoAPI_Partition::mapOfShapes() const
108 {
109   return myMap;
110 }
111
112 //=================================================================================================
113 std::shared_ptr<GeomAlgoAPI_MakeShape> GeomAlgoAPI_Partition::makeShape() const
114 {
115   return myMkShape;
116 }