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