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