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 <BRepAlgoAPI_BooleanOperation.hxx>
10 #include <BRepAlgoAPI_Common.hxx>
11 #include <BRepAlgoAPI_Cut.hxx>
12 #include <BRepAlgoAPI_Fuse.hxx>
13 #include <BRepCheck_Analyzer.hxx>
14 #include <TopExp_Explorer.hxx>
15 #include <TopTools_ListOfShape.hxx>
17 //=================================================================================================
18 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_Boolean::makeCut(const ListOfShape& theObjects,
19 const ListOfShape& theTools)
21 GeomAlgoAPI_Boolean aBoolAlgo(theObjects, theTools, BOOL_CUT);
22 if(aBoolAlgo.isDone() && !aBoolAlgo.shape()->isNull() && aBoolAlgo.isValid()) {
23 return aBoolAlgo.shape();
25 return std::shared_ptr<GeomAPI_Shape>();
28 //=================================================================================================
29 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_Boolean::makeFuse(const ListOfShape& theObjects,
30 const ListOfShape& theTools)
32 GeomAlgoAPI_Boolean aBoolAlgo(theObjects, theTools, BOOL_FUSE);
33 if(aBoolAlgo.isDone() && !aBoolAlgo.shape()->isNull() && aBoolAlgo.isValid()) {
34 return aBoolAlgo.shape();
36 return std::shared_ptr<GeomAPI_Shape>();
39 //=================================================================================================
40 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_Boolean::makeCommon(const ListOfShape& theObjects,
41 const ListOfShape& theTools)
43 GeomAlgoAPI_Boolean aBoolAlgo(theObjects, theTools, BOOL_COMMON);
44 if(aBoolAlgo.isDone() && !aBoolAlgo.shape()->isNull() && aBoolAlgo.isValid()) {
45 return aBoolAlgo.shape();
47 return std::shared_ptr<GeomAPI_Shape>();
50 //=================================================================================================
51 GeomAlgoAPI_Boolean::GeomAlgoAPI_Boolean(const ListOfShape& theObjects,
52 const ListOfShape& theTools,
53 const OperationType theOperationType)
55 myShape(new GeomAPI_Shape()),
56 myMap(new GeomAPI_DataMapOfShapeShape()),
57 myMkShape(new GeomAlgoAPI_MakeShape())
59 build(theObjects, theTools, theOperationType);
63 //=================================================================================================
64 void GeomAlgoAPI_Boolean::build(const ListOfShape& theObjects,
65 const ListOfShape& theTools,
66 const OperationType theOperationType)
68 if(theObjects.empty() || theTools.empty()) {
73 TopTools_ListOfShape anObjects;
74 for(ListOfShape::const_iterator anObjectsIt = theObjects.begin(); anObjectsIt != theObjects.end(); anObjectsIt++)
76 anObjects.Append((*anObjectsIt)->impl<TopoDS_Shape>());
80 TopTools_ListOfShape aTools;
81 for(ListOfShape::const_iterator aToolsIt = theTools.begin(); aToolsIt != theTools.end(); aToolsIt++)
83 aTools.Append((*aToolsIt)->impl<TopoDS_Shape>());
86 // Creating boolean operation.
87 BRepAlgoAPI_BooleanOperation* anOperation;
88 switch (theOperationType) {
90 anOperation = new BRepAlgoAPI_Cut();
94 anOperation = new BRepAlgoAPI_Fuse();
98 anOperation = new BRepAlgoAPI_Common();
105 myMkShape->setImpl(anOperation);
106 anOperation->SetArguments(anObjects);
107 anOperation->SetTools(aTools);
109 // Building and getting result.
110 anOperation->Build();
111 myDone = anOperation->IsDone() == Standard_True;
115 TopoDS_Shape aResult = anOperation->Shape();
117 // fill data map to keep correct orientation of sub-shapes
118 for (TopExp_Explorer Exp(aResult,TopAbs_FACE); Exp.More(); Exp.Next()) {
119 std::shared_ptr<GeomAPI_Shape> aCurrentShape(new GeomAPI_Shape());
120 aCurrentShape->setImpl(new TopoDS_Shape(Exp.Current()));
121 myMap->bind(aCurrentShape, aCurrentShape);
123 myShape->setImpl(new TopoDS_Shape(aResult));
127 //=================================================================================================
128 const bool GeomAlgoAPI_Boolean::isDone() const
133 //=================================================================================================
134 const bool GeomAlgoAPI_Boolean::isValid() const
136 BRepCheck_Analyzer aChecker(myShape->impl<TopoDS_Shape>());
137 return (aChecker.IsValid() == Standard_True);
140 //=================================================================================================
141 const std::shared_ptr<GeomAPI_Shape>& GeomAlgoAPI_Boolean::shape() const
146 //=================================================================================================
147 std::shared_ptr<GeomAPI_DataMapOfShapeShape> GeomAlgoAPI_Boolean::mapOfShapes() const
152 //=================================================================================================
153 std::shared_ptr<GeomAlgoAPI_MakeShape> GeomAlgoAPI_Boolean::makeShape() const