Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Partition.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "GeomAlgoAPI_Partition.h"
22
23 #include <GeomAlgoAPI_DFLoader.h>
24 #include <GeomAlgoAPI_ShapeTools.h>
25
26 #include <GEOMAlgo_Splitter.hxx>
27
28 #include <TopExp_Explorer.hxx>
29 #include <TopoDS_Builder.hxx>
30
31 //=================================================================================================
32 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_Partition::make(const ListOfShape& theObjects,
33                                                            const ListOfShape& theTools)
34 {
35   GeomAlgoAPI_Partition aPartitionAlgo(theObjects, theTools);
36   if(aPartitionAlgo.isDone() && !aPartitionAlgo.shape()->isNull() && aPartitionAlgo.isValid()) {
37     return aPartitionAlgo.shape();
38   }
39   return std::shared_ptr<GeomAPI_Shape>();
40 }
41
42 //=================================================================================================
43 GeomAlgoAPI_Partition::GeomAlgoAPI_Partition(const ListOfShape& theObjects,
44                                              const ListOfShape& theTools)
45 {
46   build(theObjects, theTools);
47 }
48
49
50 //=================================================================================================
51 void GeomAlgoAPI_Partition::build(const ListOfShape& theObjects,
52                                   const ListOfShape& theTools)
53 {
54   if (theObjects.empty()) {
55     return;
56   }
57
58   // Creating partition operation.
59   GEOMAlgo_Splitter* anOperation = new GEOMAlgo_Splitter;
60   this->setImpl(anOperation);
61   this->setBuilderType(OCCT_BOPAlgo_Builder);
62
63   // Getting objects.
64   for (ListOfShape::const_iterator
65        anObjectsIt = theObjects.begin(); anObjectsIt != theObjects.end(); anObjectsIt++) {
66     const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
67     anOperation->AddArgument(aShape);
68   }
69
70   // Getting tools.
71   for (ListOfShape::const_iterator
72        aToolsIt = theTools.begin(); aToolsIt != theTools.end(); aToolsIt++) {
73     const TopoDS_Shape& aShape = (*aToolsIt)->impl<TopoDS_Shape>();
74     anOperation->AddTool(aShape);
75   }
76
77   // Building and getting result.
78   anOperation->Perform();
79   if(anOperation->ErrorStatus() != 0) {
80     return;
81   }
82   TopoDS_Shape aResult = anOperation->Shape();
83
84   if(aResult.ShapeType() == TopAbs_COMPOUND) {
85     std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
86     aGeomShape->setImpl(new TopoDS_Shape(aResult));
87     aResult = GeomAlgoAPI_ShapeTools::groupSharedTopology(aGeomShape)->impl<TopoDS_Shape>();
88   }
89
90   // Setting result.
91   if(aResult.IsNull()) {
92     return;
93   }
94   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
95   aShape->setImpl(new TopoDS_Shape(aResult));
96   this->setShape(aShape);
97   this->setDone(true);
98 }