]> SALOME platform Git repositories - modules/shaper.git/blob - src/GeomAlgoAPI/GeomAlgoAPI_Intersection.cpp
Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Intersection.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_Intersection.h"
21
22 #include <GeomAlgoAPI_DFLoader.h>
23 #include <GeomAlgoAPI_ShapeTools.h>
24
25 #include <BRepAlgoAPI_Section.hxx>
26 #include <TopExp_Explorer.hxx>
27 #include <TopoDS_Builder.hxx>
28
29 //=================================================================================================
30 GeomAlgoAPI_Intersection::GeomAlgoAPI_Intersection(const ListOfShape& theObjects,
31                                                    const ListOfShape& theTools)
32 {
33   build(theObjects, theTools);
34 }
35
36 //=================================================================================================
37 void GeomAlgoAPI_Intersection::build(const ListOfShape& theObjects,
38                                      const ListOfShape& theTools)
39 {
40   if (theObjects.empty() || theTools.empty()) {
41     return;
42   }
43
44   // Creating partition operation.
45   BRepAlgoAPI_Section* anOperation = new BRepAlgoAPI_Section;
46   this->setImpl(anOperation);
47   this->setBuilderType(OCCT_BRepBuilderAPI_MakeShape);
48
49   TopAbs_ShapeEnum aShapeType = TopAbs_COMPOUND;
50
51   // Getting objects.
52   TopTools_ListOfShape anObjects;
53   for (ListOfShape::const_iterator
54     anObjectsIt = theObjects.begin(); anObjectsIt != theObjects.end(); anObjectsIt++) {
55     const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
56     if(!aShape.IsNull()) {
57       anObjects.Append(aShape);
58     }
59   }
60   anOperation->SetArguments(anObjects);
61
62   // Getting tools.
63   TopTools_ListOfShape aTools;
64   for (ListOfShape::const_iterator
65     aToolsIt = theTools.begin(); aToolsIt != theTools.end(); aToolsIt++) {
66     const TopoDS_Shape& aShape = (*aToolsIt)->impl<TopoDS_Shape>();
67     if(!aShape.IsNull()) {
68       aTools.Append(aShape);
69     }
70   }
71   anOperation->SetTools(aTools);
72
73   // Building and getting result.
74   anOperation->Build();
75   if(!anOperation->IsDone()) {
76     return;
77   }
78   TopoDS_Shape aResult = anOperation->Shape();
79
80   if(aResult.ShapeType() == TopAbs_COMPOUND) {
81     aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
82   }
83
84   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
85   aShape->setImpl(new TopoDS_Shape(aResult));
86   this->setShape(aShape);
87   this->setDone(true);
88 }