Salome HOME
Update copyrights
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_MakeVolume.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_MakeVolume.h"
21
22 #include <GeomAlgoAPI_ShapeTools.h>
23
24 #include <BOPAlgo_MakerVolume.hxx>
25
26 //=================================================================================================
27 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_MakeVolume::make(const ListOfShape& theFaces)
28 {
29   GeomAlgoAPI_MakeVolume aMkVolAlgo(theFaces, false);
30   GeomShapePtr aResult;
31   if(aMkVolAlgo.isDone() && !aMkVolAlgo.shape()->isNull() && aMkVolAlgo.isValid())
32     aResult = aMkVolAlgo.shape();
33   return aResult;
34 }
35
36 //=================================================================================================
37 GeomAlgoAPI_MakeVolume::GeomAlgoAPI_MakeVolume(
38   const ListOfShape& theFaces, const bool theAvoidInternal)
39 {
40   myAvoidInternal = theAvoidInternal;
41   build(theFaces);
42 }
43
44 static void convertToTopoDS(const ListOfShape& theShapes, TopTools_ListOfShape& theTopoDSShapes)
45 {
46   for (ListOfShape::const_iterator anIt = theShapes.begin(); anIt != theShapes.end(); ++anIt)
47     theTopoDSShapes.Append((*anIt)->impl<TopoDS_Shape>());
48 }
49
50 //=================================================================================================
51 void GeomAlgoAPI_MakeVolume::build(const ListOfShape& theFaces)
52 {
53   if (theFaces.empty()) {
54     return;
55   }
56
57   // create make volume opration
58   BOPAlgo_MakerVolume* aVolumeMaker = new BOPAlgo_MakerVolume;
59   this->setImpl(aVolumeMaker);
60   this->setBuilderType(OCCT_BOPAlgo_Builder);
61
62   // list of arguments
63   TopTools_ListOfShape anArgs;
64   convertToTopoDS(theFaces, anArgs);
65
66   // parameters of the volume maker
67   aVolumeMaker->SetArguments(anArgs);
68   aVolumeMaker->SetIntersect(true); // split edges and faces
69   aVolumeMaker->SetAvoidInternalShapes(myAvoidInternal);
70   aVolumeMaker->SetGlue(BOPAlgo_GlueOff);
71
72   // building and getting result
73   aVolumeMaker->Perform();
74   if (aVolumeMaker->HasErrors())
75     return;
76   TopoDS_Shape aResult = aVolumeMaker->Shape();
77
78   if(aResult.ShapeType() == TopAbs_COMPOUND) {
79     std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
80     aGeomShape->setImpl(new TopoDS_Shape(aResult));
81     aResult = GeomAlgoAPI_ShapeTools::groupSharedTopology(aGeomShape)->impl<TopoDS_Shape>();
82   }
83
84   // Setting result.
85   if(aResult.IsNull()) {
86     return;
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 }