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 email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
20 #include "GeomAlgoAPI_MakeShape.h"
22 #include <BOPAlgo_Builder.hxx>
23 #include <BRepBuilderAPI_MakeShape.hxx>
24 #include <BRepCheck_Analyzer.hxx>
25 #include <BRepGProp.hxx>
26 #include <GProp_GProps.hxx>
27 #include <Precision.hxx>
28 #include <TopExp_Explorer.hxx>
29 #include <TopTools_ListOfShape.hxx>
30 #include <TopTools_ListIteratorOfListOfShape.hxx>
31 #include <GeomAPI_ShapeExplorer.h>
33 //=================================================================================================
34 GeomAlgoAPI_MakeShape::GeomAlgoAPI_MakeShape()
35 : myBuilderType(Unknown),
40 //=================================================================================================
41 bool GeomAlgoAPI_MakeShape::isDone() const
46 //=================================================================================================
47 const std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_MakeShape::shape() const
52 //=================================================================================================
53 bool GeomAlgoAPI_MakeShape::isValid() const
55 BRepCheck_Analyzer aChecker(myShape->impl<TopoDS_Shape>());
56 return (aChecker.IsValid() == Standard_True);
59 //=================================================================================================
60 bool GeomAlgoAPI_MakeShape::hasVolume() const
62 bool hasVolume = false;
64 const TopoDS_Shape& aRShape = myShape->impl<TopoDS_Shape>();
66 BRepGProp::VolumeProperties(aRShape, aGProp);
67 if(aGProp.Mass() > Precision::Confusion())
73 //=================================================================================================
74 std::shared_ptr<GeomAPI_DataMapOfShapeShape> GeomAlgoAPI_MakeShape::mapOfSubShapes() const
79 //=================================================================================================
80 void GeomAlgoAPI_MakeShape::generated(const std::shared_ptr<GeomAPI_Shape> theShape,
81 ListOfShape& theHistory)
83 TopTools_ListOfShape aList;
84 if(myBuilderType == OCCT_BRepBuilderAPI_MakeShape) {
85 BRepBuilderAPI_MakeShape* aMakeShape = implPtr<BRepBuilderAPI_MakeShape>();
86 aList = aMakeShape->Generated(theShape->impl<TopoDS_Shape>());
87 } else if(myBuilderType == OCCT_BOPAlgo_Builder) {
88 BOPAlgo_Builder* aBOPBuilder = implPtr<BOPAlgo_Builder>();
89 aList = aBOPBuilder->Generated(theShape->impl<TopoDS_Shape>());
91 for(TopTools_ListIteratorOfListOfShape anIt(aList); anIt.More(); anIt.Next()) {
92 if(anIt.Value().IsNull()) {
95 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
96 aShape->setImpl(new TopoDS_Shape(anIt.Value()));
97 theHistory.push_back(aShape);
101 //=================================================================================================
102 void GeomAlgoAPI_MakeShape::modified(const std::shared_ptr<GeomAPI_Shape> theShape,
103 ListOfShape& theHistory)
105 TopTools_ListOfShape aList;
106 if(myBuilderType == OCCT_BRepBuilderAPI_MakeShape) {
107 BRepBuilderAPI_MakeShape* aMakeShape = implPtr<BRepBuilderAPI_MakeShape>();
109 aList = aMakeShape->Modified(theShape->impl<TopoDS_Shape>());
110 } catch(Standard_NoSuchObject) {
112 } else if(myBuilderType == OCCT_BOPAlgo_Builder) {
113 BOPAlgo_Builder* aBOPBuilder = implPtr<BOPAlgo_Builder>();
114 aList = aBOPBuilder->Modified(theShape->impl<TopoDS_Shape>());
116 for(TopTools_ListIteratorOfListOfShape anIt(aList); anIt.More(); anIt.Next()) {
117 if(anIt.Value().IsNull()) {
120 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
121 aShape->setImpl(new TopoDS_Shape(anIt.Value()));
122 theHistory.push_back(aShape);
126 //=================================================================================================
127 bool GeomAlgoAPI_MakeShape::isDeleted(const std::shared_ptr<GeomAPI_Shape> theShape)
129 bool isDeleted = false;
130 if(myBuilderType == OCCT_BRepBuilderAPI_MakeShape) {
131 BRepBuilderAPI_MakeShape* aMakeShape = implPtr<BRepBuilderAPI_MakeShape>();
132 isDeleted = aMakeShape->IsDeleted(theShape->impl<TopoDS_Shape>()) == Standard_True;
133 } else if(myBuilderType == OCCT_BOPAlgo_Builder) {
134 BOPAlgo_Builder* aBOPBuilder = implPtr<BOPAlgo_Builder>();
135 isDeleted = aBOPBuilder->IsDeleted(theShape->impl<TopoDS_Shape>()) == Standard_True;
141 //=================================================================================================
142 void GeomAlgoAPI_MakeShape::setBuilderType(const BuilderType theBuilderType)
144 myBuilderType = theBuilderType;
147 //=================================================================================================
148 void GeomAlgoAPI_MakeShape::setDone(const bool theFlag)
153 //=================================================================================================
154 void GeomAlgoAPI_MakeShape::setShape(const std::shared_ptr<GeomAPI_Shape> theShape)
156 if(myShape.get() && myShape->isEqual(theShape)) {
162 // Filling data map to keep correct orientation of sub-shapes.
167 myMap.reset(new GeomAPI_DataMapOfShapeShape);
170 const TopoDS_Shape& aTopoDSShape = myShape->impl<TopoDS_Shape>();
171 for(TopExp_Explorer anExp(aTopoDSShape,TopAbs_VERTEX); anExp.More(); anExp.Next()) {
172 std::shared_ptr<GeomAPI_Shape> aCurrentShape(new GeomAPI_Shape());
173 aCurrentShape->setImpl(new TopoDS_Shape(anExp.Current()));
174 myMap->bind(aCurrentShape, aCurrentShape);
176 for(TopExp_Explorer anExp(aTopoDSShape,TopAbs_EDGE); anExp.More(); anExp.Next()) {
177 std::shared_ptr<GeomAPI_Shape> aCurrentShape(new GeomAPI_Shape());
178 aCurrentShape->setImpl(new TopoDS_Shape(anExp.Current()));
179 myMap->bind(aCurrentShape, aCurrentShape);
181 for(TopExp_Explorer anExp(aTopoDSShape,TopAbs_FACE); anExp.More(); anExp.Next()) {
182 std::shared_ptr<GeomAPI_Shape> aCurrentShape(new GeomAPI_Shape());
183 aCurrentShape->setImpl(new TopoDS_Shape(anExp.Current()));
184 myMap->bind(aCurrentShape, aCurrentShape);
193 //=================================================================================================
194 void GeomAlgoAPI_MakeShape::initialize() {
195 switch (myBuilderType) {
196 case OCCT_BRepBuilderAPI_MakeShape: {
197 myDone = implPtr<BRepBuilderAPI_MakeShape>()->IsDone() == Standard_True;
198 myShape.reset(new GeomAPI_Shape());
199 myShape->setImpl(new TopoDS_Shape(implPtr<BRepBuilderAPI_MakeShape>()->Shape()));
202 case OCCT_BOPAlgo_Builder: {
204 myShape.reset(new GeomAPI_Shape());
205 myShape->setImpl(new TopoDS_Shape(implPtr<BOPAlgo_Builder>()->Shape()));
213 myMap.reset(new GeomAPI_DataMapOfShapeShape);
216 const TopoDS_Shape& aTopoDSShape = myShape->impl<TopoDS_Shape>();
217 for(TopExp_Explorer anExp(aTopoDSShape,TopAbs_FACE); anExp.More(); anExp.Next()) {
218 std::shared_ptr<GeomAPI_Shape> aCurrentShape(new GeomAPI_Shape());
219 aCurrentShape->setImpl(new TopoDS_Shape(anExp.Current()));
220 myMap->bind(aCurrentShape, aCurrentShape);
225 //=================================================================================================
226 void GeomAlgoAPI_MakeShape::prepareNamingFaces()
229 GeomAPI_ShapeExplorer anExp(shape(), GeomAPI_Shape::FACE);
230 for(GeomAPI_ShapeExplorer anExp(shape(), GeomAPI_Shape::FACE); anExp.more(); anExp.next()) {
231 std::shared_ptr<GeomAPI_Shape> aFace = anExp.current();
232 myCreatedFaces["Face_" + std::to_string(index++)] = aFace;
237 //=================================================================================================
238 bool GeomAlgoAPI_MakeShape::checkValid(std::string theMessage){
240 // isValid() is called from this method
242 myError = theMessage + " :: resulting shape is not valid.";
246 // Check the number of volumes in myShape, make sure there's one and only one.
247 TopoDS_Shape aTopoDSShape = myShape->impl<TopoDS_Shape>();
249 for(TopExp_Explorer anExp(aTopoDSShape,TopAbs_SOLID); anExp.More(); anExp.Next()) {
253 if (aNbVolumes != 1) {
254 myError = theMessage +
255 " :: connexity error, the resulting shape is made of several separate solids.";