1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "GeomAlgoAPI_MakeShape.h"
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>
34 //=================================================================================================
35 GeomAlgoAPI_MakeShape::GeomAlgoAPI_MakeShape()
36 : myBuilderType(Unknown),
41 //=================================================================================================
42 bool GeomAlgoAPI_MakeShape::isDone() const
47 //=================================================================================================
48 const std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_MakeShape::shape() const
53 //=================================================================================================
54 bool GeomAlgoAPI_MakeShape::isValid() const
56 BRepCheck_Analyzer aChecker(myShape->impl<TopoDS_Shape>());
57 return (aChecker.IsValid() == Standard_True);
60 //=================================================================================================
61 bool GeomAlgoAPI_MakeShape::hasVolume() const
63 bool hasVolume = false;
65 const TopoDS_Shape& aRShape = myShape->impl<TopoDS_Shape>();
67 BRepGProp::VolumeProperties(aRShape, aGProp);
68 if(aGProp.Mass() > Precision::Confusion())
74 //=================================================================================================
75 std::shared_ptr<GeomAPI_DataMapOfShapeShape> GeomAlgoAPI_MakeShape::mapOfSubShapes() const
80 //=================================================================================================
81 void GeomAlgoAPI_MakeShape::generated(const std::shared_ptr<GeomAPI_Shape> theShape,
82 ListOfShape& theHistory)
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>());
92 for(TopTools_ListIteratorOfListOfShape anIt(aList); anIt.More(); anIt.Next()) {
93 if(anIt.Value().IsNull()) {
96 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
97 aShape->setImpl(new TopoDS_Shape(anIt.Value()));
98 theHistory.push_back(aShape);
102 //=================================================================================================
103 void GeomAlgoAPI_MakeShape::modified(const std::shared_ptr<GeomAPI_Shape> theShape,
104 ListOfShape& theHistory)
106 TopTools_ListOfShape aList;
107 if(myBuilderType == OCCT_BRepBuilderAPI_MakeShape) {
108 BRepBuilderAPI_MakeShape* aMakeShape = implPtr<BRepBuilderAPI_MakeShape>();
110 aList = aMakeShape->Modified(theShape->impl<TopoDS_Shape>());
111 } catch(Standard_NoSuchObject) {
113 } else if(myBuilderType == OCCT_BOPAlgo_Builder) {
114 BOPAlgo_Builder* aBOPBuilder = implPtr<BOPAlgo_Builder>();
115 aList = aBOPBuilder->Modified(theShape->impl<TopoDS_Shape>());
117 for(TopTools_ListIteratorOfListOfShape anIt(aList); anIt.More(); anIt.Next()) {
118 if(anIt.Value().IsNull()) {
121 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
122 aShape->setImpl(new TopoDS_Shape(anIt.Value()));
123 theHistory.push_back(aShape);
127 //=================================================================================================
128 bool GeomAlgoAPI_MakeShape::isDeleted(const std::shared_ptr<GeomAPI_Shape> theShape)
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;
142 //=================================================================================================
143 void GeomAlgoAPI_MakeShape::setBuilderType(const BuilderType theBuilderType)
145 myBuilderType = theBuilderType;
148 //=================================================================================================
149 void GeomAlgoAPI_MakeShape::setDone(const bool theFlag)
154 //=================================================================================================
155 void GeomAlgoAPI_MakeShape::setShape(const std::shared_ptr<GeomAPI_Shape> theShape)
157 if(myShape.get() && myShape->isEqual(theShape)) {
163 // Filling data map to keep correct orientation of sub-shapes.
168 myMap.reset(new GeomAPI_DataMapOfShapeShape);
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);
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);
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);
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()));
203 case OCCT_BOPAlgo_Builder: {
205 myShape.reset(new GeomAPI_Shape());
206 myShape->setImpl(new TopoDS_Shape(implPtr<BOPAlgo_Builder>()->Shape()));
214 myMap.reset(new GeomAPI_DataMapOfShapeShape);
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);
226 //=================================================================================================
227 void GeomAlgoAPI_MakeShape::prepareNamingFaces()
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;
238 //=================================================================================================
239 bool GeomAlgoAPI_MakeShape::checkValid(std::string theMessage){
241 // isValid() is called from this method
243 myError = theMessage + " :: resulting shape is not valid.";
247 // Check the number of volumes in myShape, make sure there's one and only one.
248 TopoDS_Shape aTopoDSShape = myShape->impl<TopoDS_Shape>();
250 for(TopExp_Explorer anExp(aTopoDSShape,TopAbs_SOLID); anExp.More(); anExp.Next()) {
254 if (aNbVolumes != 1) {
255 myError = theMessage +
256 " :: connexity error, the resulting shape is made of several separate solids.";