]> SALOME platform Git repositories - modules/shaper.git/blob - src/GeomAlgoAPI/GeomAlgoAPI_Partition.cpp
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 email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
18 //
19
20 #include "GeomAlgoAPI_Partition.h"
21
22 #include <GeomAlgoAPI_DFLoader.h>
23 #include <GeomAlgoAPI_ShapeTools.h>
24
25 #include <GEOMAlgo_Splitter.hxx>
26
27 #include <TopExp_Explorer.hxx>
28 #include <TopoDS_Builder.hxx>
29
30 //=================================================================================================
31 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_Partition::make(const ListOfShape& theObjects,
32                                                            const ListOfShape& theTools)
33 {
34   GeomAlgoAPI_Partition aPartitionAlgo(theObjects, theTools);
35   if(aPartitionAlgo.isDone() && !aPartitionAlgo.shape()->isNull() && aPartitionAlgo.isValid()) {
36     return aPartitionAlgo.shape();
37   }
38   return std::shared_ptr<GeomAPI_Shape>();
39 }
40
41 //=================================================================================================
42 GeomAlgoAPI_Partition::GeomAlgoAPI_Partition(const ListOfShape& theObjects,
43                                              const ListOfShape& theTools)
44 {
45   build(theObjects, theTools);
46 }
47
48
49 //=================================================================================================
50 void GeomAlgoAPI_Partition::build(const ListOfShape& theObjects,
51                                   const ListOfShape& theTools)
52 {
53   if (theObjects.empty()) {
54     return;
55   }
56
57   // Creating partition operation.
58   GEOMAlgo_Splitter* anOperation = new GEOMAlgo_Splitter;
59   this->setImpl(anOperation);
60   this->setBuilderType(OCCT_BOPAlgo_Builder);
61
62   // Getting objects.
63   for (ListOfShape::const_iterator
64        anObjectsIt = theObjects.begin(); anObjectsIt != theObjects.end(); anObjectsIt++) {
65     const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
66     anOperation->AddArgument(aShape);
67   }
68
69   // Getting tools.
70   for (ListOfShape::const_iterator
71        aToolsIt = theTools.begin(); aToolsIt != theTools.end(); aToolsIt++) {
72     const TopoDS_Shape& aShape = (*aToolsIt)->impl<TopoDS_Shape>();
73     anOperation->AddTool(aShape);
74   }
75
76   // Building and getting result.
77   anOperation->Perform();
78   if(anOperation->ErrorStatus() != 0) {
79     return;
80   }
81   TopoDS_Shape aResult = anOperation->Shape();
82
83   if(aResult.ShapeType() == TopAbs_COMPOUND) {
84     std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
85     aGeomShape->setImpl(new TopoDS_Shape(aResult));
86     aResult = GeomAlgoAPI_ShapeTools::groupSharedTopology(aGeomShape)->impl<TopoDS_Shape>();
87   }
88
89   // Setting result.
90   if(aResult.IsNull()) {
91     return;
92   }
93   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
94   aShape->setImpl(new TopoDS_Shape(aResult));
95   this->setShape(aShape);
96   this->setDone(true);
97 }