]> SALOME platform Git repositories - modules/shaper.git/blob - src/GeomAlgoAPI/GeomAlgoAPI_Boolean.cpp
Salome HOME
32b7660e211f8d2658217eaa24dee421d2f665f0
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Boolean.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomAlgoAPI_Boolean.cpp
4 // Created:     02 Sept 2014
5 // Author:      Vitaly Smetannikov
6
7 #include "GeomAlgoAPI_Boolean.h"
8
9 #include <GeomAlgoAPI_DFLoader.h>
10
11 #include <BRepAlgoAPI_BooleanOperation.hxx>
12 #include <BRepAlgoAPI_Common.hxx>
13 #include <BRepAlgoAPI_Cut.hxx>
14 #include <BRepAlgoAPI_Fuse.hxx>
15 #include <BRepCheck_Analyzer.hxx>
16 #include <TopExp_Explorer.hxx>
17 #include <TopTools_ListOfShape.hxx>
18
19 //=================================================================================================
20 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_Boolean::makeCut(const ListOfShape& theObjects,
21                                                             const ListOfShape& theTools)
22 {
23   GeomAlgoAPI_Boolean aBoolAlgo(theObjects, theTools, BOOL_CUT);
24   if(aBoolAlgo.isDone() && !aBoolAlgo.shape()->isNull() && aBoolAlgo.isValid()) {
25     return aBoolAlgo.shape();
26   }
27   return std::shared_ptr<GeomAPI_Shape>();
28 }
29
30 //=================================================================================================
31 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_Boolean::makeFuse(const ListOfShape& theObjects,
32                                                              const ListOfShape& theTools)
33 {
34   GeomAlgoAPI_Boolean aBoolAlgo(theObjects, theTools, BOOL_FUSE);
35   if(aBoolAlgo.isDone() && !aBoolAlgo.shape()->isNull() && aBoolAlgo.isValid()) {
36     return aBoolAlgo.shape();
37   }
38   return std::shared_ptr<GeomAPI_Shape>();
39 }
40
41 //=================================================================================================
42 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_Boolean::makeCommon(const ListOfShape& theObjects,
43                                                                const ListOfShape& theTools)
44 {
45   GeomAlgoAPI_Boolean aBoolAlgo(theObjects, theTools, BOOL_COMMON);
46   if(aBoolAlgo.isDone() && !aBoolAlgo.shape()->isNull() && aBoolAlgo.isValid()) {
47     return aBoolAlgo.shape();
48   }
49   return std::shared_ptr<GeomAPI_Shape>();
50 }
51
52 //=================================================================================================
53 GeomAlgoAPI_Boolean::GeomAlgoAPI_Boolean(const ListOfShape& theObjects,
54                                          const ListOfShape& theTools,
55                                          const OperationType theOperationType)
56 : myDone(false),
57   myShape(new GeomAPI_Shape()),
58   myMap(new GeomAPI_DataMapOfShapeShape()),
59   myMkShape(new GeomAlgoAPI_MakeShape())
60 {
61   build(theObjects, theTools, theOperationType);
62 }
63
64
65 //=================================================================================================
66 void GeomAlgoAPI_Boolean::build(const ListOfShape& theObjects,
67                                 const ListOfShape& theTools,
68                                 const OperationType theOperationType)
69 {
70   if(theObjects.empty() || theTools.empty()) {
71     return;
72   }
73
74   // Getting objects.
75   TopTools_ListOfShape anObjects;
76   for(ListOfShape::const_iterator anObjectsIt = theObjects.begin(); anObjectsIt != theObjects.end(); anObjectsIt++)
77   {
78     anObjects.Append((*anObjectsIt)->impl<TopoDS_Shape>());
79   }
80
81   // Getting tools.
82   TopTools_ListOfShape aTools;
83   for(ListOfShape::const_iterator aToolsIt = theTools.begin(); aToolsIt != theTools.end(); aToolsIt++)
84   {
85     aTools.Append((*aToolsIt)->impl<TopoDS_Shape>());
86   }
87
88   // Creating boolean operation.
89   BRepAlgoAPI_BooleanOperation* anOperation;
90   switch (theOperationType) {
91     case BOOL_CUT: {
92       anOperation = new BRepAlgoAPI_Cut();
93       break;
94     }
95     case BOOL_FUSE: {
96       anOperation = new BRepAlgoAPI_Fuse();
97       break;
98     }
99     case BOOL_COMMON: {
100       anOperation = new BRepAlgoAPI_Common();
101       break;
102     }
103     default: {
104       return;
105     }
106   }
107   myMkShape->setImpl(anOperation);
108   anOperation->SetArguments(anObjects);
109   anOperation->SetTools(aTools);
110
111   // Building and getting result.
112   anOperation->Build();
113   myDone = anOperation->IsDone() == Standard_True;
114   if(!myDone) {
115     return;
116   }
117   TopoDS_Shape aResult = anOperation->Shape();
118
119   if(aResult.ShapeType() == TopAbs_COMPOUND) {
120     aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
121   }
122
123   // fill data map to keep correct orientation of sub-shapes
124   for (TopExp_Explorer Exp(aResult,TopAbs_FACE); Exp.More(); Exp.Next()) {
125     std::shared_ptr<GeomAPI_Shape> aCurrentShape(new GeomAPI_Shape());
126     aCurrentShape->setImpl(new TopoDS_Shape(Exp.Current()));
127     myMap->bind(aCurrentShape, aCurrentShape);
128   }
129   myShape->setImpl(new TopoDS_Shape(aResult));
130
131 }
132
133 //=================================================================================================
134 const bool GeomAlgoAPI_Boolean::isDone() const
135 {
136   return myDone;
137 }
138
139 //=================================================================================================
140 const bool GeomAlgoAPI_Boolean::isValid() const
141 {
142   BRepCheck_Analyzer aChecker(myShape->impl<TopoDS_Shape>());
143   return (aChecker.IsValid() == Standard_True);
144 }
145
146 //=================================================================================================
147 const std::shared_ptr<GeomAPI_Shape>& GeomAlgoAPI_Boolean::shape() const
148 {
149   return myShape;
150 }
151
152 //=================================================================================================
153 std::shared_ptr<GeomAPI_DataMapOfShapeShape> GeomAlgoAPI_Boolean::mapOfShapes() const
154 {
155   return myMap;
156 }
157
158 //=================================================================================================
159 std::shared_ptr<GeomAlgoAPI_MakeShape> GeomAlgoAPI_Boolean::makeShape() const
160 {
161   return myMkShape;
162 }