Salome HOME
Issue #1383 Preview button: correction for the case: switch off auto_preview in extru...
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_MakeShape.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomAlgoAPI_MakeShape.cpp
4 // Created:     20 Oct 2014
5 // Author:      Sergey ZARITCHNY
6
7 #include "GeomAlgoAPI_MakeShape.h"
8
9 #include <BOPAlgo_Builder.hxx>
10 #include <BRepBuilderAPI_MakeShape.hxx>
11 #include <BRepCheck_Analyzer.hxx>
12 #include <BRepGProp.hxx>
13 #include <GProp_GProps.hxx>
14 #include <Precision.hxx>
15 #include <TopExp_Explorer.hxx>
16 #include <TopTools_ListOfShape.hxx>
17 #include <TopTools_ListIteratorOfListOfShape.hxx>
18
19 //=================================================================================================
20 GeomAlgoAPI_MakeShape::GeomAlgoAPI_MakeShape()
21 : myBuilderType(Unknown),
22   myDone(false)
23 {
24 }
25
26 //=================================================================================================
27 bool GeomAlgoAPI_MakeShape::isDone() const
28 {
29   return myDone;
30 }
31
32 //=================================================================================================
33 const std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_MakeShape::shape() const
34 {
35   return myShape;
36 }
37
38 //=================================================================================================
39 bool GeomAlgoAPI_MakeShape::isValid() const
40 {
41   BRepCheck_Analyzer aChecker(myShape->impl<TopoDS_Shape>());
42   return (aChecker.IsValid() == Standard_True);
43 }
44
45 //=================================================================================================
46 bool GeomAlgoAPI_MakeShape::hasVolume() const
47 {
48   bool hasVolume = false;
49   if(isValid()) {
50     const TopoDS_Shape& aRShape = myShape->impl<TopoDS_Shape>();
51     GProp_GProps aGProp;
52     BRepGProp::VolumeProperties(aRShape, aGProp);
53     if(aGProp.Mass() > Precision::Confusion())
54       hasVolume = true;
55   }
56   return hasVolume;
57 }
58
59 //=================================================================================================
60 std::shared_ptr<GeomAPI_DataMapOfShapeShape> GeomAlgoAPI_MakeShape::mapOfSubShapes() const
61 {
62   return myMap;
63 }
64
65 //=================================================================================================
66 void GeomAlgoAPI_MakeShape::generated(const std::shared_ptr<GeomAPI_Shape> theShape,
67                                       ListOfShape& theHistory)
68 {
69   TopTools_ListOfShape aList;
70   if(myBuilderType == OCCT_BRepBuilderAPI_MakeShape) {
71     BRepBuilderAPI_MakeShape* aMakeShape = implPtr<BRepBuilderAPI_MakeShape>();
72     aList = aMakeShape->Generated(theShape->impl<TopoDS_Shape>());
73   } else if(myBuilderType == OCCT_BOPAlgo_Builder) {
74     BOPAlgo_Builder* aBOPBuilder = implPtr<BOPAlgo_Builder>();
75     aList = aBOPBuilder->Generated(theShape->impl<TopoDS_Shape>());
76   }
77   for(TopTools_ListIteratorOfListOfShape anIt(aList); anIt.More(); anIt.Next()) {
78     if(anIt.Value().IsNull()) {
79       continue;
80     }
81     std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
82     aShape->setImpl(new TopoDS_Shape(anIt.Value()));
83     theHistory.push_back(aShape);
84   }
85 }
86
87 //=================================================================================================
88 void GeomAlgoAPI_MakeShape::modified(const std::shared_ptr<GeomAPI_Shape> theShape,
89                                      ListOfShape& theHistory)
90 {
91   TopTools_ListOfShape aList;
92   if(myBuilderType == OCCT_BRepBuilderAPI_MakeShape) {
93     BRepBuilderAPI_MakeShape* aMakeShape = implPtr<BRepBuilderAPI_MakeShape>();
94     aList = aMakeShape->Modified(theShape->impl<TopoDS_Shape>());
95   } else if(myBuilderType == OCCT_BOPAlgo_Builder) {
96     BOPAlgo_Builder* aBOPBuilder = implPtr<BOPAlgo_Builder>();
97     aList = aBOPBuilder->Modified(theShape->impl<TopoDS_Shape>());
98   }
99   for(TopTools_ListIteratorOfListOfShape anIt(aList); anIt.More(); anIt.Next()) {
100     if(anIt.Value().IsNull()) {
101       continue;
102     }
103     std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
104     aShape->setImpl(new TopoDS_Shape(anIt.Value()));
105     theHistory.push_back(aShape);
106   }
107 }
108
109 //=================================================================================================
110 bool GeomAlgoAPI_MakeShape::isDeleted(const std::shared_ptr<GeomAPI_Shape> theShape)
111 {
112   bool isDeleted = false;
113   if(myBuilderType == OCCT_BRepBuilderAPI_MakeShape) {
114     BRepBuilderAPI_MakeShape* aMakeShape = implPtr<BRepBuilderAPI_MakeShape>();
115     isDeleted = aMakeShape->IsDeleted(theShape->impl<TopoDS_Shape>()) == Standard_True;
116   } else if(myBuilderType == OCCT_BOPAlgo_Builder) {
117     BOPAlgo_Builder* aBOPBuilder = implPtr<BOPAlgo_Builder>();
118     isDeleted = aBOPBuilder->IsDeleted(theShape->impl<TopoDS_Shape>()) == Standard_True;
119   }
120
121   return isDeleted;
122 }
123
124 //=================================================================================================
125 void GeomAlgoAPI_MakeShape::setBuilderType(const BuilderType theBuilderType)
126 {
127   myBuilderType = theBuilderType;
128 }
129
130 //=================================================================================================
131 void GeomAlgoAPI_MakeShape::setDone(const bool theFlag)
132 {
133   myDone = theFlag;
134 }
135
136 //=================================================================================================
137 void GeomAlgoAPI_MakeShape::setShape(const std::shared_ptr<GeomAPI_Shape> theShape)
138 {
139   if(myShape.get() && myShape->isEqual(theShape)) {
140     return;
141   }
142
143   myShape = theShape;
144
145   // Filling data map to keep correct orientation of sub-shapes.
146   if(myShape.get()) {
147     if(myMap.get()) {
148       myMap->clear();
149     } else {
150       myMap.reset(new GeomAPI_DataMapOfShapeShape);
151     }
152
153     const TopoDS_Shape& aTopoDSShape = myShape->impl<TopoDS_Shape>();
154     for(TopExp_Explorer anExp(aTopoDSShape,TopAbs_FACE); anExp.More(); anExp.Next()) {
155       std::shared_ptr<GeomAPI_Shape> aCurrentShape(new GeomAPI_Shape());
156       aCurrentShape->setImpl(new TopoDS_Shape(anExp.Current()));
157       myMap->bind(aCurrentShape, aCurrentShape);
158     }
159   } else {
160     if(myMap.get()) {
161       myMap->clear();
162     }
163   }
164 }
165
166 //=================================================================================================
167 void GeomAlgoAPI_MakeShape::initialize() {
168   switch (myBuilderType) {
169     case OCCT_BRepBuilderAPI_MakeShape: {
170       myDone = implPtr<BRepBuilderAPI_MakeShape>()->IsDone() == Standard_True;
171       myShape.reset(new GeomAPI_Shape());
172       myShape->setImpl(new TopoDS_Shape(implPtr<BRepBuilderAPI_MakeShape>()->Shape()));
173       break;
174     }
175     case OCCT_BOPAlgo_Builder: {
176       myDone = true;
177       myShape.reset(new GeomAPI_Shape());
178       myShape->setImpl(new TopoDS_Shape(implPtr<BOPAlgo_Builder>()->Shape()));
179       break;
180     }
181   }
182
183   if(myMap.get()) {
184     myMap->clear();
185   } else {
186     myMap.reset(new GeomAPI_DataMapOfShapeShape);
187   }
188
189   const TopoDS_Shape& aTopoDSShape = myShape->impl<TopoDS_Shape>();
190   for(TopExp_Explorer anExp(aTopoDSShape,TopAbs_FACE); anExp.More(); anExp.Next()) {
191     std::shared_ptr<GeomAPI_Shape> aCurrentShape(new GeomAPI_Shape());
192     aCurrentShape->setImpl(new TopoDS_Shape(anExp.Current()));
193     myMap->bind(aCurrentShape, aCurrentShape);
194   }
195 }