Salome HOME
Update copyrights
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Intersection.cpp
1 // Copyright (C) 2014-2019  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
18 //
19
20 #include "GeomAlgoAPI_Intersection.h"
21
22 #include <GeomAlgoAPI_DFLoader.h>
23
24 #include <BOPAlgo_PaveFiller.hxx>
25 #include <BOPAlgo_Section.hxx>
26
27 //==================================================================================================
28 GeomAlgoAPI_Intersection::GeomAlgoAPI_Intersection(const ListOfShape& theObjects)
29   : myFiller(0)
30 {
31   build(theObjects);
32 }
33
34 GeomAlgoAPI_Intersection::~GeomAlgoAPI_Intersection() {
35   if (myFiller)
36     delete (BOPAlgo_PaveFiller*)myFiller;
37 }
38 //==================================================================================================
39 void GeomAlgoAPI_Intersection::build(const ListOfShape& theObjects)
40 {
41   if (theObjects.empty()) {
42     return;
43   }
44
45   // Creating partition operation.
46   BOPAlgo_Section* anOperation = new BOPAlgo_Section;
47   this->setImpl(anOperation);
48   this->setBuilderType(OCCT_BOPAlgo_Builder);
49
50   // Getting objects.
51   TopTools_ListOfShape anObjects;
52   for (ListOfShape::const_iterator
53     anObjectsIt = theObjects.begin(); anObjectsIt != theObjects.end(); anObjectsIt++) {
54     const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
55     if(!aShape.IsNull()) {
56       anObjects.Append(aShape);
57     }
58   }
59
60   BOPAlgo_PaveFiller* aDSFiller = new BOPAlgo_PaveFiller;
61   myFiller = aDSFiller;
62   aDSFiller->SetArguments(anObjects);
63
64   aDSFiller->SetRunParallel(false);
65   aDSFiller->SetNonDestructive(false);
66   aDSFiller->SetGlue(BOPAlgo_GlueOff);
67
68   // optimization for the issue #2399
69   BOPAlgo_SectionAttribute theSecAttr(Standard_True,
70                                       Standard_True,
71                                       Standard_True);
72   aDSFiller->SetSectionAttribute(theSecAttr);
73
74   aDSFiller->Perform();
75   if (aDSFiller->HasErrors()) {
76     return;
77   }
78
79   anOperation->SetArguments(anObjects);
80   anOperation->SetRunParallel(false);
81   anOperation->SetCheckInverted(true);
82
83   anOperation->PerformWithFiller(*aDSFiller); // it references a filler fields, so keep the filler
84   if(anOperation->HasErrors()) {
85     return;
86   }
87   TopoDS_Shape aResult = anOperation->Shape();
88
89   if(aResult.ShapeType() == TopAbs_COMPOUND) {
90     aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
91   }
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 }