]> SALOME platform Git repositories - modules/shaper.git/blob - src/GeomAlgoAPI/GeomAlgoAPI_MakeShape.cpp
Salome HOME
Merge branch 'master' into cgt/devCEA
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_MakeShape.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_MakeShape.h"
22
23 #include <BOPAlgo_Builder.hxx>
24 #include <BRepBuilderAPI_MakeShape.hxx>
25 #include <BRepCheck_Analyzer.hxx>
26 #include <BRepGProp.hxx>
27 #include <GProp_GProps.hxx>
28 #include <Precision.hxx>
29 #include <TopExp_Explorer.hxx>
30 #include <TopTools_ListOfShape.hxx>
31 #include <TopTools_ListIteratorOfListOfShape.hxx>
32 #include <GeomAPI_ShapeExplorer.h>
33
34 //=================================================================================================
35 GeomAlgoAPI_MakeShape::GeomAlgoAPI_MakeShape()
36 : myBuilderType(Unknown),
37   myDone(false)
38 {
39 }
40
41 //=================================================================================================
42 bool GeomAlgoAPI_MakeShape::isDone() const
43 {
44   return myDone;
45 }
46
47 //=================================================================================================
48 const std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_MakeShape::shape() const
49 {
50   return myShape;
51 }
52
53 //=================================================================================================
54 bool GeomAlgoAPI_MakeShape::isValid() const
55 {
56   BRepCheck_Analyzer aChecker(myShape->impl<TopoDS_Shape>());
57   return (aChecker.IsValid() == Standard_True);
58 }
59
60 //=================================================================================================
61 bool GeomAlgoAPI_MakeShape::hasVolume() const
62 {
63   bool hasVolume = false;
64   if(isValid()) {
65     const TopoDS_Shape& aRShape = myShape->impl<TopoDS_Shape>();
66     GProp_GProps aGProp;
67     BRepGProp::VolumeProperties(aRShape, aGProp);
68     if(aGProp.Mass() > Precision::Confusion())
69       hasVolume = true;
70   }
71   return hasVolume;
72 }
73
74 //=================================================================================================
75 std::shared_ptr<GeomAPI_DataMapOfShapeShape> GeomAlgoAPI_MakeShape::mapOfSubShapes() const
76 {
77   return myMap;
78 }
79
80 //=================================================================================================
81 void GeomAlgoAPI_MakeShape::generated(const std::shared_ptr<GeomAPI_Shape> theShape,
82                                       ListOfShape& theHistory)
83 {
84   TopTools_ListOfShape aList;
85   if(myBuilderType == OCCT_BRepBuilderAPI_MakeShape) {
86     BRepBuilderAPI_MakeShape* aMakeShape = implPtr<BRepBuilderAPI_MakeShape>();
87     aList = aMakeShape->Generated(theShape->impl<TopoDS_Shape>());
88   } else if(myBuilderType == OCCT_BOPAlgo_Builder) {
89     BOPAlgo_Builder* aBOPBuilder = implPtr<BOPAlgo_Builder>();
90     aList = aBOPBuilder->Generated(theShape->impl<TopoDS_Shape>());
91   }
92   for(TopTools_ListIteratorOfListOfShape anIt(aList); anIt.More(); anIt.Next()) {
93     if(anIt.Value().IsNull()) {
94       continue;
95     }
96     std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
97     aShape->setImpl(new TopoDS_Shape(anIt.Value()));
98     theHistory.push_back(aShape);
99   }
100 }
101
102 //=================================================================================================
103 void GeomAlgoAPI_MakeShape::modified(const std::shared_ptr<GeomAPI_Shape> theShape,
104                                      ListOfShape& theHistory)
105 {
106   TopTools_ListOfShape aList;
107   if(myBuilderType == OCCT_BRepBuilderAPI_MakeShape) {
108     BRepBuilderAPI_MakeShape* aMakeShape = implPtr<BRepBuilderAPI_MakeShape>();
109     try {
110       aList = aMakeShape->Modified(theShape->impl<TopoDS_Shape>());
111     } catch(Standard_NoSuchObject) {
112     }
113   } else if(myBuilderType == OCCT_BOPAlgo_Builder) {
114     BOPAlgo_Builder* aBOPBuilder = implPtr<BOPAlgo_Builder>();
115     aList = aBOPBuilder->Modified(theShape->impl<TopoDS_Shape>());
116   }
117   for(TopTools_ListIteratorOfListOfShape anIt(aList); anIt.More(); anIt.Next()) {
118     if(anIt.Value().IsNull()) {
119       continue;
120     }
121     std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
122     aShape->setImpl(new TopoDS_Shape(anIt.Value()));
123     theHistory.push_back(aShape);
124   }
125 }
126
127 //=================================================================================================
128 bool GeomAlgoAPI_MakeShape::isDeleted(const std::shared_ptr<GeomAPI_Shape> theShape)
129 {
130   bool isDeleted = false;
131   if(myBuilderType == OCCT_BRepBuilderAPI_MakeShape) {
132     BRepBuilderAPI_MakeShape* aMakeShape = implPtr<BRepBuilderAPI_MakeShape>();
133     isDeleted = aMakeShape->IsDeleted(theShape->impl<TopoDS_Shape>()) == Standard_True;
134   } else if(myBuilderType == OCCT_BOPAlgo_Builder) {
135     BOPAlgo_Builder* aBOPBuilder = implPtr<BOPAlgo_Builder>();
136     isDeleted = aBOPBuilder->IsDeleted(theShape->impl<TopoDS_Shape>()) == Standard_True;
137   }
138
139   return isDeleted;
140 }
141
142 //=================================================================================================
143 void GeomAlgoAPI_MakeShape::setBuilderType(const BuilderType theBuilderType)
144 {
145   myBuilderType = theBuilderType;
146 }
147
148 //=================================================================================================
149 void GeomAlgoAPI_MakeShape::setDone(const bool theFlag)
150 {
151   myDone = theFlag;
152 }
153
154 //=================================================================================================
155 void GeomAlgoAPI_MakeShape::setShape(const std::shared_ptr<GeomAPI_Shape> theShape)
156 {
157   if(myShape.get() && myShape->isEqual(theShape)) {
158     return;
159   }
160
161   myShape = theShape;
162
163   // Filling data map to keep correct orientation of sub-shapes.
164   if(myShape.get()) {
165     if(myMap.get()) {
166       myMap->clear();
167     } else {
168       myMap.reset(new GeomAPI_DataMapOfShapeShape);
169     }
170
171     const TopoDS_Shape& aTopoDSShape = myShape->impl<TopoDS_Shape>();
172     for(TopExp_Explorer anExp(aTopoDSShape,TopAbs_VERTEX); anExp.More(); anExp.Next()) {
173       std::shared_ptr<GeomAPI_Shape> aCurrentShape(new GeomAPI_Shape());
174       aCurrentShape->setImpl(new TopoDS_Shape(anExp.Current()));
175       myMap->bind(aCurrentShape, aCurrentShape);
176     }
177     for(TopExp_Explorer anExp(aTopoDSShape,TopAbs_EDGE); anExp.More(); anExp.Next()) {
178       std::shared_ptr<GeomAPI_Shape> aCurrentShape(new GeomAPI_Shape());
179       aCurrentShape->setImpl(new TopoDS_Shape(anExp.Current()));
180       myMap->bind(aCurrentShape, aCurrentShape);
181     }
182     for(TopExp_Explorer anExp(aTopoDSShape,TopAbs_FACE); anExp.More(); anExp.Next()) {
183       std::shared_ptr<GeomAPI_Shape> aCurrentShape(new GeomAPI_Shape());
184       aCurrentShape->setImpl(new TopoDS_Shape(anExp.Current()));
185       myMap->bind(aCurrentShape, aCurrentShape);
186     }
187   } else {
188     if(myMap.get()) {
189       myMap->clear();
190     }
191   }
192 }
193
194 //=================================================================================================
195 void GeomAlgoAPI_MakeShape::initialize() {
196   switch (myBuilderType) {
197     case OCCT_BRepBuilderAPI_MakeShape: {
198       myDone = implPtr<BRepBuilderAPI_MakeShape>()->IsDone() == Standard_True;
199       myShape.reset(new GeomAPI_Shape());
200       myShape->setImpl(new TopoDS_Shape(implPtr<BRepBuilderAPI_MakeShape>()->Shape()));
201       break;
202     }
203     case OCCT_BOPAlgo_Builder: {
204       myDone = true;
205       myShape.reset(new GeomAPI_Shape());
206       myShape->setImpl(new TopoDS_Shape(implPtr<BOPAlgo_Builder>()->Shape()));
207       break;
208     }
209   }
210
211   if(myMap.get()) {
212     myMap->clear();
213   } else {
214     myMap.reset(new GeomAPI_DataMapOfShapeShape);
215   }
216
217   const TopoDS_Shape& aTopoDSShape = myShape->impl<TopoDS_Shape>();
218   for(TopExp_Explorer anExp(aTopoDSShape,TopAbs_FACE); anExp.More(); anExp.Next()) {
219     std::shared_ptr<GeomAPI_Shape> aCurrentShape(new GeomAPI_Shape());
220     aCurrentShape->setImpl(new TopoDS_Shape(anExp.Current()));
221     myMap->bind(aCurrentShape, aCurrentShape);
222   }
223 }
224
225
226 //=================================================================================================
227 void GeomAlgoAPI_MakeShape::prepareNamingFaces()
228 {
229   long long index = 1;
230   GeomAPI_ShapeExplorer anExp(shape(), GeomAPI_Shape::FACE);
231   for(GeomAPI_ShapeExplorer anExp(shape(), GeomAPI_Shape::FACE); anExp.more(); anExp.next()) {
232     std::shared_ptr<GeomAPI_Shape> aFace = anExp.current();
233     myCreatedFaces["Face_" + std::to_string(index++)] = aFace;
234   }
235 }
236
237
238 //=================================================================================================
239 bool GeomAlgoAPI_MakeShape::checkValid(std::string theMessage){
240
241   // isValid() is called from this method
242   if (!isValid()) {
243     myError = theMessage + " :: resulting shape is not valid.";
244     return false;
245   }
246
247   // Check the number of volumes in myShape, make sure there's one and only one.
248   TopoDS_Shape aTopoDSShape = myShape->impl<TopoDS_Shape>();
249   int aNbVolumes = 0;
250   for(TopExp_Explorer anExp(aTopoDSShape,TopAbs_SOLID); anExp.More(); anExp.Next()) {
251     aNbVolumes ++;
252   }
253
254   if (aNbVolumes != 1) {
255     myError = theMessage +
256       " :: connexity error, the resulting shape is made of several separate solids.";
257     return false;
258   }
259
260   return true ;
261 }
262