Salome HOME
FIx for the issue #2399 : optimization of the Intersection feature results (better...
[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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "GeomAlgoAPI_Intersection.h"
22
23 #include <GeomAlgoAPI_DFLoader.h>
24 #include <GeomAlgoAPI_ShapeTools.h>
25
26 #include <BRepAlgoAPI_Section.hxx>
27 #include <TopExp_Explorer.hxx>
28 #include <TopoDS_Builder.hxx>
29
30 //=================================================================================================
31 GeomAlgoAPI_Intersection::GeomAlgoAPI_Intersection(const ListOfShape& theObjects,
32                                                    const ListOfShape& theTools)
33 {
34   build(theObjects, theTools);
35 }
36
37 //=================================================================================================
38 void GeomAlgoAPI_Intersection::build(const ListOfShape& theObjects,
39                                      const ListOfShape& theTools)
40 {
41   if (theObjects.empty() || theTools.empty()) {
42     return;
43   }
44
45   // Creating partition operation.
46   BRepAlgoAPI_Section* anOperation = new BRepAlgoAPI_Section;
47   this->setImpl(anOperation);
48   this->setBuilderType(OCCT_BRepBuilderAPI_MakeShape);
49
50   TopAbs_ShapeEnum aShapeType = TopAbs_COMPOUND;
51
52   // Getting objects.
53   TopTools_ListOfShape anObjects;
54   for (ListOfShape::const_iterator
55     anObjectsIt = theObjects.begin(); anObjectsIt != theObjects.end(); anObjectsIt++) {
56     const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
57     if(!aShape.IsNull()) {
58       anObjects.Append(aShape);
59     }
60   }
61   anOperation->SetArguments(anObjects);
62
63   // Getting tools.
64   TopTools_ListOfShape aTools;
65   for (ListOfShape::const_iterator
66     aToolsIt = theTools.begin(); aToolsIt != theTools.end(); aToolsIt++) {
67     const TopoDS_Shape& aShape = (*aToolsIt)->impl<TopoDS_Shape>();
68     if(!aShape.IsNull()) {
69       aTools.Append(aShape);
70     }
71   }
72   anOperation->SetTools(aTools);
73   // optimization for the issue #2399
74   anOperation->Approximation(Standard_True);
75   anOperation->ComputePCurveOn1(Standard_True);
76   anOperation->ComputePCurveOn2(Standard_True);
77   // Building and getting result.
78   anOperation->Build();
79   if(!anOperation->IsDone()) {
80     return;
81   }
82   TopoDS_Shape aResult = anOperation->Shape();
83
84   if(aResult.ShapeType() == TopAbs_COMPOUND) {
85     aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
86   }
87
88   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
89   aShape->setImpl(new TopoDS_Shape(aResult));
90   this->setShape(aShape);
91   this->setDone(true);
92 }