1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: GeomAlgoAPI_Boolean.cpp
4 // Created: 02 Sept 2014
5 // Author: Vitaly Smetannikov
7 #include "GeomAlgoAPI_Boolean.h"
9 #include <GeomAlgoAPI_DFLoader.h>
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>
19 //=================================================================================================
20 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_Boolean::makeCut(const ListOfShape& theObjects,
21 const ListOfShape& theTools)
23 GeomAlgoAPI_Boolean aBoolAlgo(theObjects, theTools, BOOL_CUT);
24 if(aBoolAlgo.isDone() && !aBoolAlgo.shape()->isNull() && aBoolAlgo.isValid()) {
25 return aBoolAlgo.shape();
27 return std::shared_ptr<GeomAPI_Shape>();
30 //=================================================================================================
31 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_Boolean::makeFuse(const ListOfShape& theObjects,
32 const ListOfShape& theTools)
34 GeomAlgoAPI_Boolean aBoolAlgo(theObjects, theTools, BOOL_FUSE);
35 if(aBoolAlgo.isDone() && !aBoolAlgo.shape()->isNull() && aBoolAlgo.isValid()) {
36 return aBoolAlgo.shape();
38 return std::shared_ptr<GeomAPI_Shape>();
41 //=================================================================================================
42 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_Boolean::makeCommon(const ListOfShape& theObjects,
43 const ListOfShape& theTools)
45 GeomAlgoAPI_Boolean aBoolAlgo(theObjects, theTools, BOOL_COMMON);
46 if(aBoolAlgo.isDone() && !aBoolAlgo.shape()->isNull() && aBoolAlgo.isValid()) {
47 return aBoolAlgo.shape();
49 return std::shared_ptr<GeomAPI_Shape>();
52 //=================================================================================================
53 GeomAlgoAPI_Boolean::GeomAlgoAPI_Boolean(const ListOfShape& theObjects,
54 const ListOfShape& theTools,
55 const OperationType theOperationType)
58 build(theObjects, theTools, theOperationType);
62 //=================================================================================================
63 void GeomAlgoAPI_Boolean::build(const ListOfShape& theObjects,
64 const ListOfShape& theTools,
65 const OperationType theOperationType)
67 if(theObjects.empty() || theTools.empty()) {
72 TopTools_ListOfShape anObjects;
73 for(ListOfShape::const_iterator anObjectsIt = theObjects.begin(); anObjectsIt != theObjects.end(); anObjectsIt++)
75 anObjects.Append((*anObjectsIt)->impl<TopoDS_Shape>());
79 TopTools_ListOfShape aTools;
80 for(ListOfShape::const_iterator aToolsIt = theTools.begin(); aToolsIt != theTools.end(); aToolsIt++)
82 aTools.Append((*aToolsIt)->impl<TopoDS_Shape>());
85 // Creating boolean operation.
86 BRepAlgoAPI_BooleanOperation* anOperation;
87 switch (theOperationType) {
89 anOperation = new BRepAlgoAPI_Cut();
93 anOperation = new BRepAlgoAPI_Fuse();
97 anOperation = new BRepAlgoAPI_Common();
104 myMkShape.reset(new GeomAlgoAPI_MakeShape(anOperation));
105 anOperation->SetArguments(anObjects);
106 anOperation->SetTools(aTools);
108 // Building and getting result.
109 anOperation->Build();
110 myDone = anOperation->IsDone() == Standard_True;
114 TopoDS_Shape aResult = anOperation->Shape();
116 if(aResult.ShapeType() == TopAbs_COMPOUND) {
117 aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
120 // fill data map to keep correct orientation of sub-shapes
121 myMap.reset(new GeomAPI_DataMapOfShapeShape());
122 for (TopExp_Explorer Exp(aResult,TopAbs_FACE); Exp.More(); Exp.Next()) {
123 std::shared_ptr<GeomAPI_Shape> aCurrentShape(new GeomAPI_Shape());
124 aCurrentShape->setImpl(new TopoDS_Shape(Exp.Current()));
125 myMap->bind(aCurrentShape, aCurrentShape);
127 myShape.reset(new GeomAPI_Shape());
128 myShape->setImpl(new TopoDS_Shape(aResult));
132 //=================================================================================================
133 const bool GeomAlgoAPI_Boolean::isDone() const
138 //=================================================================================================
139 const bool GeomAlgoAPI_Boolean::isValid() const
141 BRepCheck_Analyzer aChecker(myShape->impl<TopoDS_Shape>());
142 return (aChecker.IsValid() == Standard_True);
145 //=================================================================================================
146 const std::shared_ptr<GeomAPI_Shape>& GeomAlgoAPI_Boolean::shape() const
151 //=================================================================================================
152 std::shared_ptr<GeomAPI_DataMapOfShapeShape> GeomAlgoAPI_Boolean::mapOfShapes() const
157 //=================================================================================================
158 std::shared_ptr<GeomAlgoAPI_MakeShape> GeomAlgoAPI_Boolean::makeShape() const