Salome HOME
Issue #1860: fix end lines with spaces
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Partition.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomAlgoAPI_Partition.cpp
4 // Created:     21 Aug 2015
5 // Author:      Sergey POKHODENKO
6
7 #include "GeomAlgoAPI_Partition.h"
8
9 #include <GeomAlgoAPI_DFLoader.h>
10 #include <GeomAlgoAPI_ShapeTools.h>
11
12 #include <GEOMAlgo_Splitter.hxx>
13
14 #include <TopExp_Explorer.hxx>
15 #include <TopoDS_Builder.hxx>
16
17 //=================================================================================================
18 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_Partition::make(const ListOfShape& theObjects,
19                                                            const ListOfShape& theTools)
20 {
21   GeomAlgoAPI_Partition aPartitionAlgo(theObjects, theTools);
22   if(aPartitionAlgo.isDone() && !aPartitionAlgo.shape()->isNull() && aPartitionAlgo.isValid()) {
23     return aPartitionAlgo.shape();
24   }
25   return std::shared_ptr<GeomAPI_Shape>();
26 }
27
28 //=================================================================================================
29 GeomAlgoAPI_Partition::GeomAlgoAPI_Partition(const ListOfShape& theObjects,
30                                              const ListOfShape& theTools)
31 {
32   build(theObjects, theTools);
33 }
34
35
36 //=================================================================================================
37 void GeomAlgoAPI_Partition::build(const ListOfShape& theObjects,
38                                   const ListOfShape& theTools)
39 {
40   if (theObjects.empty()) {
41     return;
42   }
43
44   // Creating partition operation.
45   GEOMAlgo_Splitter* anOperation = new GEOMAlgo_Splitter;
46   this->setImpl(anOperation);
47   this->setBuilderType(OCCT_BOPAlgo_Builder);
48
49   // Getting objects.
50   for (ListOfShape::const_iterator
51        anObjectsIt = theObjects.begin(); anObjectsIt != theObjects.end(); anObjectsIt++) {
52     const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
53     anOperation->AddArgument(aShape);
54   }
55
56   // Getting tools.
57   for (ListOfShape::const_iterator
58        aToolsIt = theTools.begin(); aToolsIt != theTools.end(); aToolsIt++) {
59     const TopoDS_Shape& aShape = (*aToolsIt)->impl<TopoDS_Shape>();
60     anOperation->AddTool(aShape);
61   }
62
63   // Building and getting result.
64   anOperation->Perform();
65   if(anOperation->ErrorStatus() != 0) {
66     return;
67   }
68   TopoDS_Shape aResult = anOperation->Shape();
69
70   if(aResult.ShapeType() == TopAbs_COMPOUND) {
71     std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
72     aGeomShape->setImpl(new TopoDS_Shape(aResult));
73     aResult = GeomAlgoAPI_ShapeTools::groupSharedTopology(aGeomShape)->impl<TopoDS_Shape>();
74   }
75
76   // Setting result.
77   if(aResult.IsNull()) {
78     return;
79   }
80   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
81   aShape->setImpl(new TopoDS_Shape(aResult));
82   this->setShape(aShape);
83   this->setDone(true);
84 }