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)
57 myShape(new GeomAPI_Shape()),
58 myMap(new GeomAPI_DataMapOfShapeShape()),
59 myMkShape(new GeomAlgoAPI_MakeShape())
61 build(theObjects, theTools, theOperationType);
65 //=================================================================================================
66 void GeomAlgoAPI_Boolean::build(const ListOfShape& theObjects,
67 const ListOfShape& theTools,
68 const OperationType theOperationType)
70 if(theObjects.empty() || theTools.empty()) {
75 TopTools_ListOfShape anObjects;
76 for(ListOfShape::const_iterator anObjectsIt = theObjects.begin(); anObjectsIt != theObjects.end(); anObjectsIt++)
78 anObjects.Append((*anObjectsIt)->impl<TopoDS_Shape>());
82 TopTools_ListOfShape aTools;
83 for(ListOfShape::const_iterator aToolsIt = theTools.begin(); aToolsIt != theTools.end(); aToolsIt++)
85 aTools.Append((*aToolsIt)->impl<TopoDS_Shape>());
88 // Creating boolean operation.
89 BRepAlgoAPI_BooleanOperation* anOperation;
90 switch (theOperationType) {
92 anOperation = new BRepAlgoAPI_Cut();
96 anOperation = new BRepAlgoAPI_Fuse();
100 anOperation = new BRepAlgoAPI_Common();
107 myMkShape->setImpl(anOperation);
108 anOperation->SetArguments(anObjects);
109 anOperation->SetTools(aTools);
111 // Building and getting result.
112 anOperation->Build();
113 myDone = anOperation->IsDone() == Standard_True;
117 TopoDS_Shape aResult = anOperation->Shape();
119 if(aResult.ShapeType() == TopAbs_COMPOUND) {
120 aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
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);
129 myShape->setImpl(new TopoDS_Shape(aResult));
133 //=================================================================================================
134 const bool GeomAlgoAPI_Boolean::isDone() const
139 //=================================================================================================
140 const bool GeomAlgoAPI_Boolean::isValid() const
142 BRepCheck_Analyzer aChecker(myShape->impl<TopoDS_Shape>());
143 return (aChecker.IsValid() == Standard_True);
146 //=================================================================================================
147 const std::shared_ptr<GeomAPI_Shape>& GeomAlgoAPI_Boolean::shape() const
152 //=================================================================================================
153 std::shared_ptr<GeomAPI_DataMapOfShapeShape> GeomAlgoAPI_Boolean::mapOfShapes() const
158 //=================================================================================================
159 std::shared_ptr<GeomAlgoAPI_MakeShape> GeomAlgoAPI_Boolean::makeShape() const