Salome HOME
Issue #532 - 4.13. Partition with splitting-arguments making solids with shared faces
[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   myShape(new GeomAPI_Shape()),
33   myMap(new GeomAPI_DataMapOfShapeShape()),
34   myMkShape(new GeomAlgoAPI_MakeShape())
35 {
36   build(theObjects, theTools);
37 }
38
39
40 //=================================================================================================
41 void GeomAlgoAPI_Partition::build(const ListOfShape& theObjects,
42                                   const ListOfShape& theTools)
43 {
44   if (theObjects.empty() || theTools.empty()) {
45     return;
46   }
47
48   // Creating partition operation.
49   GEOMAlgo_Splitter * anOperation = new GEOMAlgo_Splitter;
50   myMkShape->setImpl(anOperation);
51
52   // Getting objects.
53   TopTools_ListOfShape anObjects;
54   for (ListOfShape::const_iterator anObjectsIt = theObjects.begin(); anObjectsIt != theObjects.end(); anObjectsIt++) {
55     const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
56     anOperation->AddArgument(aShape);
57   }
58
59   // Getting tools.
60   TopTools_ListOfShape aTools;
61   for (ListOfShape::const_iterator aToolsIt = theTools.begin(); aToolsIt != theTools.end(); aToolsIt++) {
62     const TopoDS_Shape& aShape = (*aToolsIt)->impl<TopoDS_Shape>();
63     anOperation->AddTool(aShape);
64   }
65
66   // Building and getting result.
67   anOperation->Perform();
68   TopoDS_Shape aResult = anOperation->Shape();
69   myDone = !aResult.IsNull();
70   if (!myDone) {
71     return;
72   }
73
74   if(aResult.ShapeType() == TopAbs_COMPOUND) {
75     aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
76   }
77
78   // fill data map to keep correct orientation of sub-shapes
79   for (TopExp_Explorer Exp(aResult,TopAbs_FACE); Exp.More(); Exp.Next()) {
80     std::shared_ptr<GeomAPI_Shape> aCurrentShape(new GeomAPI_Shape());
81     aCurrentShape->setImpl(new TopoDS_Shape(Exp.Current()));
82     myMap->bind(aCurrentShape, aCurrentShape);
83   }
84   myShape->setImpl(new TopoDS_Shape(aResult));
85
86 }
87
88 //=================================================================================================
89 const bool GeomAlgoAPI_Partition::isDone() const
90 {
91   return myDone;
92 }
93
94 //=================================================================================================
95 const bool GeomAlgoAPI_Partition::isValid() const
96 {
97   BRepCheck_Analyzer aChecker(myShape->impl<TopoDS_Shape>());
98   return (aChecker.IsValid() == Standard_True);
99 }
100
101 //=================================================================================================
102 const std::shared_ptr<GeomAPI_Shape>& GeomAlgoAPI_Partition::shape() const
103 {
104   return myShape;
105 }
106
107 //=================================================================================================
108 std::shared_ptr<GeomAPI_DataMapOfShapeShape> GeomAlgoAPI_Partition::mapOfShapes() const
109 {
110   return myMap;
111 }
112
113 //=================================================================================================
114 std::shared_ptr<GeomAlgoAPI_MakeShape> GeomAlgoAPI_Partition::makeShape() const
115 {
116   return myMkShape;
117 }