Salome HOME
High level objects history implementation for BuildPlugin features.
[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 <GeomAPI_ShapeExplorer.h>
24
25 #include <GeomAlgoAPI_ShapeTools.h>
26
27 #include <BOPAlgo_MakerVolume.hxx>
28
29 //=================================================================================================
30 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_MakeVolume::make(const ListOfShape& theFaces)
31 {
32   GeomAlgoAPI_MakeVolume aMkVolAlgo(theFaces, false);
33   GeomShapePtr aResult;
34   if(aMkVolAlgo.isDone() && !aMkVolAlgo.shape()->isNull() && aMkVolAlgo.isValid())
35     aResult = aMkVolAlgo.shape();
36   return aResult;
37 }
38
39 //=================================================================================================
40 GeomAlgoAPI_MakeVolume::GeomAlgoAPI_MakeVolume(
41   const ListOfShape& theFaces, const bool theAvoidInternal)
42 {
43   myAvoidInternal = theAvoidInternal;
44   build(theFaces);
45 }
46
47 static void convertToTopoDS(const ListOfShape& theShapes, TopTools_ListOfShape& theTopoDSShapes)
48 {
49   for (ListOfShape::const_iterator anIt = theShapes.begin(); anIt != theShapes.end(); ++anIt)
50     theTopoDSShapes.Append((*anIt)->impl<TopoDS_Shape>());
51 }
52
53 //=================================================================================================
54 void GeomAlgoAPI_MakeVolume::build(const ListOfShape& theFaces)
55 {
56   if (theFaces.empty()) {
57     return;
58   }
59
60   // create make volume opration
61   BOPAlgo_MakerVolume* aVolumeMaker = new BOPAlgo_MakerVolume;
62   this->setImpl(aVolumeMaker);
63   this->setBuilderType(OCCT_BOPAlgo_Builder);
64
65   // list of arguments
66   TopTools_ListOfShape anArgs;
67   convertToTopoDS(theFaces, anArgs);
68
69   // parameters of the volume maker
70   aVolumeMaker->SetArguments(anArgs);
71   aVolumeMaker->SetIntersect(true); // split edges and faces
72   aVolumeMaker->SetAvoidInternalShapes(myAvoidInternal);
73   aVolumeMaker->SetGlue(BOPAlgo_GlueOff);
74
75   // building and getting result
76   aVolumeMaker->Perform();
77   if (aVolumeMaker->HasErrors())
78     return;
79   TopoDS_Shape aResult = aVolumeMaker->Shape();
80
81   if(aResult.ShapeType() == TopAbs_COMPOUND) {
82     std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
83     aGeomShape->setImpl(new TopoDS_Shape(aResult));
84     aResult = GeomAlgoAPI_ShapeTools::groupSharedTopology(aGeomShape)->impl<TopoDS_Shape>();
85   }
86
87   // Setting result.
88   if(aResult.IsNull()) {
89     return;
90   }
91   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
92   aShape->setImpl(new TopoDS_Shape(aResult));
93   this->setShape(aShape);
94   this->setDone(true);
95 }
96
97 //=================================================================================================
98 void GeomAlgoAPI_MakeVolume::modified(const GeomShapePtr theOldShape,
99                                       ListOfShape& theNewShapes)
100 {
101   if (theOldShape->shapeType() == GeomAPI_Shape::SOLID) {
102     ListOfShape aNewShapes;
103     // collect faces and parent shapes, if it is not done yet
104     if (!isNewShapesCollected(theOldShape, GeomAPI_Shape::FACE))
105       collectNewShapes(theOldShape, GeomAPI_Shape::FACE);
106
107     for (GeomAPI_ShapeExplorer anIt(shape(), GeomAPI_Shape::SOLID); anIt.more(); anIt.next()) {
108       for (GeomAPI_ShapeExplorer anExp(anIt.current(), GeomAPI_Shape::FACE);
109            anExp.more(); anExp.next()) {
110         GeomShapePtr anOldShapesCompound =
111             oldShapesForNew(theOldShape, anExp.current(), GeomAPI_Shape::FACE);
112         if (!anOldShapesCompound->isNull()) {
113           aNewShapes.push_back(anIt.current());
114           break;
115         }
116       }
117     }
118
119     if (!aNewShapes.empty())
120       theNewShapes = aNewShapes;
121   }
122   else
123     GeomAlgoAPI_MakeShape::modified(theOldShape, theNewShapes);
124 }