]> SALOME platform Git repositories - modules/shaper.git/blob - src/GeomAlgoAPI/GeomAlgoAPI_MakeShape.cpp
Salome HOME
Issue #1369: Added "SubShapes" feature.
[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     try {
95       aList = aMakeShape->Modified(theShape->impl<TopoDS_Shape>());
96     } catch(Standard_NoSuchObject) {
97     }
98   } else if(myBuilderType == OCCT_BOPAlgo_Builder) {
99     BOPAlgo_Builder* aBOPBuilder = implPtr<BOPAlgo_Builder>();
100     aList = aBOPBuilder->Modified(theShape->impl<TopoDS_Shape>());
101   }
102   for(TopTools_ListIteratorOfListOfShape anIt(aList); anIt.More(); anIt.Next()) {
103     if(anIt.Value().IsNull()) {
104       continue;
105     }
106     std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
107     aShape->setImpl(new TopoDS_Shape(anIt.Value()));
108     theHistory.push_back(aShape);
109   }
110 }
111
112 //=================================================================================================
113 bool GeomAlgoAPI_MakeShape::isDeleted(const std::shared_ptr<GeomAPI_Shape> theShape)
114 {
115   bool isDeleted = false;
116   if(myBuilderType == OCCT_BRepBuilderAPI_MakeShape) {
117     BRepBuilderAPI_MakeShape* aMakeShape = implPtr<BRepBuilderAPI_MakeShape>();
118     isDeleted = aMakeShape->IsDeleted(theShape->impl<TopoDS_Shape>()) == Standard_True;
119   } else if(myBuilderType == OCCT_BOPAlgo_Builder) {
120     BOPAlgo_Builder* aBOPBuilder = implPtr<BOPAlgo_Builder>();
121     isDeleted = aBOPBuilder->IsDeleted(theShape->impl<TopoDS_Shape>()) == Standard_True;
122   }
123
124   return isDeleted;
125 }
126
127 //=================================================================================================
128 void GeomAlgoAPI_MakeShape::setBuilderType(const BuilderType theBuilderType)
129 {
130   myBuilderType = theBuilderType;
131 }
132
133 //=================================================================================================
134 void GeomAlgoAPI_MakeShape::setDone(const bool theFlag)
135 {
136   myDone = theFlag;
137 }
138
139 //=================================================================================================
140 void GeomAlgoAPI_MakeShape::setShape(const std::shared_ptr<GeomAPI_Shape> theShape)
141 {
142   if(myShape.get() && myShape->isEqual(theShape)) {
143     return;
144   }
145
146   myShape = theShape;
147
148   // Filling data map to keep correct orientation of sub-shapes.
149   if(myShape.get()) {
150     if(myMap.get()) {
151       myMap->clear();
152     } else {
153       myMap.reset(new GeomAPI_DataMapOfShapeShape);
154     }
155
156     const TopoDS_Shape& aTopoDSShape = myShape->impl<TopoDS_Shape>();
157     for(TopExp_Explorer anExp(aTopoDSShape,TopAbs_FACE); anExp.More(); anExp.Next()) {
158       std::shared_ptr<GeomAPI_Shape> aCurrentShape(new GeomAPI_Shape());
159       aCurrentShape->setImpl(new TopoDS_Shape(anExp.Current()));
160       myMap->bind(aCurrentShape, aCurrentShape);
161     }
162   } else {
163     if(myMap.get()) {
164       myMap->clear();
165     }
166   }
167 }
168
169 //=================================================================================================
170 void GeomAlgoAPI_MakeShape::initialize() {
171   switch (myBuilderType) {
172     case OCCT_BRepBuilderAPI_MakeShape: {
173       myDone = implPtr<BRepBuilderAPI_MakeShape>()->IsDone() == Standard_True;
174       myShape.reset(new GeomAPI_Shape());
175       myShape->setImpl(new TopoDS_Shape(implPtr<BRepBuilderAPI_MakeShape>()->Shape()));
176       break;
177     }
178     case OCCT_BOPAlgo_Builder: {
179       myDone = true;
180       myShape.reset(new GeomAPI_Shape());
181       myShape->setImpl(new TopoDS_Shape(implPtr<BOPAlgo_Builder>()->Shape()));
182       break;
183     }
184   }
185
186   if(myMap.get()) {
187     myMap->clear();
188   } else {
189     myMap.reset(new GeomAPI_DataMapOfShapeShape);
190   }
191
192   const TopoDS_Shape& aTopoDSShape = myShape->impl<TopoDS_Shape>();
193   for(TopExp_Explorer anExp(aTopoDSShape,TopAbs_FACE); anExp.More(); anExp.Next()) {
194     std::shared_ptr<GeomAPI_Shape> aCurrentShape(new GeomAPI_Shape());
195     aCurrentShape->setImpl(new TopoDS_Shape(anExp.Current()));
196     myMap->bind(aCurrentShape, aCurrentShape);
197   }
198 }