+// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
+
// File: GeomAlgoAPI_Boolean.cpp
// Created: 02 Sept 2014
// Author: Vitaly Smetannikov
#include "GeomAlgoAPI_Boolean.h"
-#include <BRepAlgoAPI_Cut.hxx>
-#include <BRepAlgoAPI_Common.hxx>
-#include <BRepAlgoAPI_Fuse.hxx>
+#include <GeomAlgoAPI_DFLoader.h>
+#include <BOPAlgo_BOP.hxx>
+#include <TopTools_ListOfShape.hxx>
-boost::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_Boolean::makeCut(
- boost::shared_ptr<GeomAPI_Shape> theShape,
- boost::shared_ptr<GeomAPI_Shape> theTool)
+//=================================================================================================
+GeomAlgoAPI_Boolean::GeomAlgoAPI_Boolean(const ListOfShape& theObjects,
+ const ListOfShape& theTools,
+ const OperationType theOperationType)
{
- const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
- const TopoDS_Shape& aTool = theTool->impl<TopoDS_Shape>();
-
- BRepAlgoAPI_Cut aCut(aShape, aTool);
- if (aCut.IsDone()) {
- boost::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape());
- aResult->setImpl(new TopoDS_Shape(aCut.Shape()));
- return aResult;
- }
- return boost::shared_ptr<GeomAPI_Shape>();
+ build(theObjects, theTools, theOperationType);
}
-boost::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_Boolean::makeFuse(
- boost::shared_ptr<GeomAPI_Shape> theShape,
- boost::shared_ptr<GeomAPI_Shape> theTool)
+//=================================================================================================
+void GeomAlgoAPI_Boolean::build(const ListOfShape& theObjects,
+ const ListOfShape& theTools,
+ const OperationType theOperationType)
{
- const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
- const TopoDS_Shape& aTool = theTool->impl<TopoDS_Shape>();
-
- BRepAlgoAPI_Fuse aFuse(aShape, aTool);
- if (aFuse.IsDone()) {
- boost::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape());
- aResult->setImpl(new TopoDS_Shape(aFuse.Shape()));
- return aResult;
+ if(theObjects.empty() || theTools.empty()) {
+ return;
}
- return boost::shared_ptr<GeomAPI_Shape>();
-}
+ // Getting objects.
+ TopTools_ListOfShape anObjects;
+ for(ListOfShape::const_iterator anObjectsIt = theObjects.begin(); anObjectsIt != theObjects.end(); anObjectsIt++)
+ {
+ anObjects.Append((*anObjectsIt)->impl<TopoDS_Shape>());
+ }
-boost::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_Boolean::makeCommon(
- boost::shared_ptr<GeomAPI_Shape> theShape,
- boost::shared_ptr<GeomAPI_Shape> theTool)
-{
- const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
- const TopoDS_Shape& aTool = theTool->impl<TopoDS_Shape>();
-
- BRepAlgoAPI_Common aCommon(aShape, aTool);
- if (aCommon.IsDone()) {
- boost::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape());
- aResult->setImpl(new TopoDS_Shape(aCommon.Shape()));
- return aResult;
+ // Getting tools.
+ TopTools_ListOfShape aTools;
+ for(ListOfShape::const_iterator aToolsIt = theTools.begin(); aToolsIt != theTools.end(); aToolsIt++)
+ {
+ aTools.Append((*aToolsIt)->impl<TopoDS_Shape>());
+ }
+
+ // Creating boolean operation.
+ BOPAlgo_BOP* aBuilder = new BOPAlgo_BOP();
+ switch (theOperationType) {
+ case BOOL_CUT: {
+ aBuilder->SetOperation(BOPAlgo_CUT);
+ break;
+ }
+ case BOOL_FUSE: {
+ aBuilder->SetOperation(BOPAlgo_FUSE);
+ break;
+ }
+ case BOOL_COMMON: {
+ aBuilder->SetOperation(BOPAlgo_COMMON);
+ break;
+ }
+ default: {
+ return;
+ }
}
- return boost::shared_ptr<GeomAPI_Shape>();
-}
\ No newline at end of file
+ this->setImpl(aBuilder);
+ this->setBuilderType(OCCT_BOPAlgo_Builder);
+ aBuilder->SetArguments(anObjects);
+ aBuilder->SetTools(aTools);
+
+ // Building and getting result.
+ aBuilder->Perform();
+ if(aBuilder->ErrorStatus() != 0) {
+ return;
+ }
+ TopoDS_Shape aResult = aBuilder->Shape();
+
+ if(aResult.ShapeType() == TopAbs_COMPOUND) {
+ aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
+ }
+
+ std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
+ aShape->setImpl(new TopoDS_Shape(aResult));
+ this->setShape(aShape);
+ this->setDone(true);
+}