1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: GeomAlgoAPI_Partition.cpp
4 // Created: 21 Aug 2015
5 // Author: Sergey POKHODENKO
7 #include "GeomAlgoAPI_Partition.h"
9 #include <GeomAlgoAPI_DFLoader.h>
10 #include <GeomAlgoAPI_ShapeTools.h>
12 #include <GEOMAlgo_Splitter.hxx>
14 #include <TopExp_Explorer.hxx>
15 #include <TopoDS_Builder.hxx>
17 //=================================================================================================
18 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_Partition::make(const ListOfShape& theObjects,
19 const ListOfShape& theTools)
21 GeomAlgoAPI_Partition aPartitionAlgo(theObjects, theTools);
22 if(aPartitionAlgo.isDone() && !aPartitionAlgo.shape()->isNull() && aPartitionAlgo.isValid()) {
23 return aPartitionAlgo.shape();
25 return std::shared_ptr<GeomAPI_Shape>();
28 //=================================================================================================
29 GeomAlgoAPI_Partition::GeomAlgoAPI_Partition(const ListOfShape& theObjects,
30 const ListOfShape& theTools)
32 build(theObjects, theTools);
36 //=================================================================================================
37 void GeomAlgoAPI_Partition::build(const ListOfShape& theObjects,
38 const ListOfShape& theTools)
40 if (theObjects.empty()) {
44 // Creating partition operation.
45 GEOMAlgo_Splitter* anOperation = new GEOMAlgo_Splitter;
46 this->setImpl(anOperation);
47 this->setBuilderType(OCCT_BOPAlgo_Builder);
50 for (ListOfShape::const_iterator anObjectsIt = theObjects.begin(); anObjectsIt != theObjects.end(); anObjectsIt++) {
51 const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
52 anOperation->AddArgument(aShape);
56 for (ListOfShape::const_iterator aToolsIt = theTools.begin(); aToolsIt != theTools.end(); aToolsIt++) {
57 const TopoDS_Shape& aShape = (*aToolsIt)->impl<TopoDS_Shape>();
58 anOperation->AddTool(aShape);
61 // Building and getting result.
62 anOperation->Perform();
63 if(anOperation->ErrorStatus() != 0) {
66 TopoDS_Shape aResult = anOperation->Shape();
68 if(aResult.ShapeType() == TopAbs_COMPOUND) {
69 aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
71 if(aResult.ShapeType() == TopAbs_COMPOUND) {
72 std::shared_ptr<GeomAPI_Shape> aCompound(new GeomAPI_Shape);
73 aCompound->setImpl(new TopoDS_Shape(aResult));
74 ListOfShape aCompSolids, aFreeSolids;
75 GeomAlgoAPI_ShapeTools::combineShapes(aCompound, GeomAPI_Shape::COMPSOLID, aCompSolids, aFreeSolids);
76 if(aCompSolids.size() == 1 && aFreeSolids.size() == 0) {
77 aResult = aCompSolids.front()->impl<TopoDS_Shape>();
78 } else if (aCompSolids.size() > 1 || (aCompSolids.size() >= 1 && aFreeSolids.size() >= 1)) {
79 TopoDS_Compound aResultComp;
80 TopoDS_Builder aBuilder;
81 aBuilder.MakeCompound(aResultComp);
82 for(ListOfShape::const_iterator anIter = aCompSolids.cbegin(); anIter != aCompSolids.cend(); anIter++) {
83 aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
85 for(ListOfShape::const_iterator anIter = aFreeSolids.cbegin(); anIter != aFreeSolids.cend(); anIter++) {
86 aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
88 aResult = aResultComp;
92 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
93 aShape->setImpl(new TopoDS_Shape(aResult));
94 this->setShape(aShape);