Salome HOME
Support of connected topology icon for issue #1332
[modules/shaper.git] / src / GeomAPI / GeomAPI_Shape.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomAPI_Shape.cpp
4 // Created:     23 Apr 2014
5 // Author:      Mikhail PONIKAROV
6
7 #include "GeomAPI_Shape.h"
8
9 #include <BRep_Tool.hxx>
10 #include <BRepBndLib.hxx>
11 #include <BRepBuilderAPI_FindPlane.hxx>
12 #include <BRepTools.hxx>
13 #include <Bnd_Box.hxx>
14 #include <Geom_Circle.hxx>
15 #include <Geom_Conic.hxx>
16 #include <Geom_Curve.hxx>
17 #include <Geom_Ellipse.hxx>
18 #include <Geom_Hyperbola.hxx>
19 #include <Geom_Line.hxx>
20 #include <Geom_Parabola.hxx>
21 #include <Geom_Plane.hxx>
22 #include <Geom_RectangularTrimmedSurface.hxx>
23 #include <Geom_TrimmedCurve.hxx>
24 #include <TopExp_Explorer.hxx>
25 #include <TopoDS.hxx>
26 #include <TopoDS_Iterator.hxx>
27 #include <TopoDS_Shape.hxx>
28 #include <NCollection_List.hxx>
29
30 #include <sstream>
31
32 #define MY_SHAPE implPtr<TopoDS_Shape>()
33
34 GeomAPI_Shape::GeomAPI_Shape()
35     : GeomAPI_Interface(new TopoDS_Shape())
36 {
37 }
38
39 bool GeomAPI_Shape::isNull() const
40 {
41   return MY_SHAPE->IsNull() == Standard_True;
42 }
43
44 bool GeomAPI_Shape::isEqual(const std::shared_ptr<GeomAPI_Shape> theShape) const
45 {
46   if (!theShape.get())
47     return false;
48   if (isNull())
49     return theShape->isNull();
50   if (theShape->isNull())
51     return false;
52
53   return MY_SHAPE->IsEqual(theShape->impl<TopoDS_Shape>()) == Standard_True;
54 }
55
56 bool GeomAPI_Shape::isVertex() const
57 {
58   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
59   return !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX;
60 }
61
62 bool GeomAPI_Shape::isEdge() const
63 {
64   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
65   return !aShape.IsNull() && aShape.ShapeType() == TopAbs_EDGE;
66 }
67
68 bool GeomAPI_Shape::isFace() const
69 {
70   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
71   return !aShape.IsNull() && aShape.ShapeType() == TopAbs_FACE;
72 }
73
74 bool GeomAPI_Shape::isCompound() const
75 {
76   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
77   return !aShape.IsNull() && aShape.ShapeType() == TopAbs_COMPOUND;
78 }
79
80 bool GeomAPI_Shape::isCompoundOfSolids() const
81 {
82   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
83   if (aShape.IsNull() || aShape.ShapeType() != TopAbs_COMPOUND)
84     return false;
85   bool isAtLeastOne = false;
86   for(TopoDS_Iterator aSubs(aShape); aSubs.More(); aSubs.Next()) {
87     if (aSubs.Value().IsNull() || aSubs.Value().ShapeType() != TopAbs_SOLID)
88       return false;
89     isAtLeastOne = true;
90   }
91   return isAtLeastOne;
92 }
93
94 // adds the nopt-compound elements recursively to the list
95 static void addSimpleToList(const TopoDS_Shape& theShape, NCollection_List<TopoDS_Shape>& theList)
96 {
97   if (!theShape.IsNull()) {
98     if (theShape.ShapeType() == TopAbs_COMPOUND) {
99       for(TopoDS_Iterator aSubs(theShape); aSubs.More(); aSubs.Next()) {
100         addSimpleToList(aSubs.Value(), theList);
101       }
102     } else {
103       theList.Append(theShape);
104     }
105   }
106 }
107
108 bool GeomAPI_Shape::isConnectedTopology() const
109 {
110   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
111   if (aShape.IsNull() || aShape.ShapeType() != TopAbs_COMPOUND)
112     return false;
113   NCollection_List<TopoDS_Shape> aNotConnected; // list of simple elements that are not detected in connection to others
114   addSimpleToList(aShape, aNotConnected);
115   if (aNotConnected.IsEmpty()) // an empty compound
116     return false;
117
118   // collect here the group of connected subs, starting with one first element
119   NCollection_List<TopoDS_Shape> aNewConnected;
120   aNewConnected.Append(aNotConnected.First());
121   aNotConnected.RemoveFirst();
122   // iterate until some new element become connected
123   while(!aNewConnected.IsEmpty() && !aNotConnected.IsEmpty()) {
124     NCollection_List<TopoDS_Shape> aNew; // very new connected to new connected
125     NCollection_List<TopoDS_Shape>::Iterator aNotIter(aNotConnected);
126     while(aNotIter.More()) {
127       bool aConnected =  false;
128       NCollection_List<TopoDS_Shape>::Iterator aNewIter(aNewConnected);
129       for(; !aConnected && aNewIter.More(); aNewIter.Next()) {
130         // checking topological connecion of aNotIter and aNewIter (if shapes are connected, vertices are connected for sure)
131         TopExp_Explorer anExp1(aNotIter.Value(), TopAbs_VERTEX);
132         for(; !aConnected && anExp1.More(); anExp1.Next()) {
133           TopExp_Explorer anExp2(aNewIter.Value(), TopAbs_VERTEX);
134           for(; anExp2.More(); anExp2.Next()) {
135             if (anExp1.Current().IsSame(anExp2.Current())) {
136               aConnected = true;
137               break;
138             }
139           }
140         }
141       }
142       if (aConnected) {
143         aNew.Append(aNotIter.Value());
144         aNotConnected.Remove(aNotIter);
145       } else {
146         aNotIter.Next();
147       }
148     }
149     // remove all new connected and put to this list very new connected
150     aNewConnected.Clear();
151     aNewConnected.Append(aNew);
152   }
153   return aNotConnected.IsEmpty() == Standard_True;
154 }
155
156 bool GeomAPI_Shape::isSolid() const
157 {
158   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
159   return !aShape.IsNull() && aShape.ShapeType() == TopAbs_SOLID;
160 }
161
162 bool GeomAPI_Shape::isCompSolid() const
163 {
164   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
165   return !aShape.IsNull() && aShape.ShapeType() == TopAbs_COMPSOLID;
166 }
167
168 bool GeomAPI_Shape::isPlanar() const
169 {
170   TopoDS_Shape aShape = impl<TopoDS_Shape>();
171
172   if(aShape.IsNull()) {
173     return false;
174   }
175
176   TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
177   if(aShapeType == TopAbs_COMPOUND) {
178     TopoDS_Iterator anIt(aShape);
179     int aShNum = 0;
180     for(; anIt.More(); anIt.Next()) {
181       ++aShNum;
182     }
183     if(aShNum == 1) {
184       anIt.Initialize(aShape);
185       aShape = anIt.Value();
186     }
187   }
188
189   aShapeType = aShape.ShapeType();
190   if(aShapeType == TopAbs_VERTEX) {
191     return true;
192   } else if(aShapeType == TopAbs_FACE) {
193     const Handle(Geom_Surface)& aSurface = BRep_Tool::Surface(TopoDS::Face(aShape));
194     Handle(Standard_Type) aType = aSurface->DynamicType();
195
196     if(aType == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
197       Handle(Geom_RectangularTrimmedSurface) aTrimSurface = Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurface);
198       aType = aTrimSurface->BasisSurface()->DynamicType();
199     }
200     return (aType == STANDARD_TYPE(Geom_Plane)) == Standard_True;
201   } else {
202     BRepBuilderAPI_FindPlane aFindPlane(aShape);
203     bool isFound = aFindPlane.Found() == Standard_True;
204
205     if(!isFound && aShapeType == TopAbs_EDGE) {
206       Standard_Real aFirst, aLast;
207       Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(aShape), aFirst, aLast);
208       Handle(Standard_Type) aType = aCurve->DynamicType();
209
210       if(aType == STANDARD_TYPE(Geom_TrimmedCurve)) {
211         Handle(Geom_TrimmedCurve) aTrimCurve = Handle(Geom_TrimmedCurve)::DownCast(aCurve);
212         aType = aTrimCurve->BasisCurve()->DynamicType();
213       }
214
215       if(aType == STANDARD_TYPE(Geom_Line)
216           || aType == STANDARD_TYPE(Geom_Conic)
217           || aType == STANDARD_TYPE(Geom_Circle)
218           || aType == STANDARD_TYPE(Geom_Ellipse)
219           || aType == STANDARD_TYPE(Geom_Hyperbola)
220           || aType == STANDARD_TYPE(Geom_Parabola)) {
221         isFound = true;
222       }
223     }
224
225     return isFound;
226   }
227
228   return false;
229 }
230
231 GeomAPI_Shape::ShapeType GeomAPI_Shape::shapeType() const
232 {
233   const TopoDS_Shape& aShape = impl<TopoDS_Shape>();
234
235   ShapeType aST = GeomAPI_Shape::SHAPE;
236
237   switch(aShape.ShapeType()) {
238   case TopAbs_COMPOUND:
239     aST = GeomAPI_Shape::COMPOUND;
240     break;
241   case TopAbs_COMPSOLID:
242     aST = GeomAPI_Shape::COMPSOLID;
243     break;
244   case TopAbs_SOLID:
245     aST = GeomAPI_Shape::SOLID;
246     break;
247   case TopAbs_SHELL:
248     aST = GeomAPI_Shape::SHELL;
249     break;
250   case TopAbs_FACE:
251     aST = GeomAPI_Shape::FACE;
252     break;
253   case TopAbs_WIRE:
254     aST = GeomAPI_Shape::WIRE;
255     break;
256   case TopAbs_EDGE:
257     aST = GeomAPI_Shape::EDGE;
258     break;
259   case TopAbs_VERTEX:
260     aST = GeomAPI_Shape::VERTEX;
261     break;
262   case TopAbs_SHAPE:
263     aST = GeomAPI_Shape::SHAPE;
264     break;
265   }
266
267   return aST;
268 }
269
270 std::string GeomAPI_Shape::shapeTypeStr() const
271 {
272   ShapeType aShapeType = shapeType();
273   std::string aShapeTypeStr;
274
275   switch(aShapeType) {
276     case COMPOUND: {
277       aShapeTypeStr = "Compound";
278       break;
279     }
280     case COMPSOLID: {
281       aShapeTypeStr = "CompSolid";
282       break;
283     }
284     case SOLID: {
285       aShapeTypeStr = "Solid";
286       break;
287     }
288     case SHELL: {
289       aShapeTypeStr = "Shell";
290       break;
291     }
292     case FACE: {
293       aShapeTypeStr = "Face";
294       break;
295     }
296     case WIRE: {
297       aShapeTypeStr = "Wire";
298       break;
299     }
300     case EDGE: {
301       aShapeTypeStr = "Edge";
302       break;
303     }
304     case VERTEX: {
305       aShapeTypeStr = "Vertex";
306       break;
307     }
308     case SHAPE: {
309       aShapeTypeStr = "Shape";
310       break;
311     }
312   }
313
314   return aShapeTypeStr;
315 }
316
317 bool GeomAPI_Shape::isSubShape(const std::shared_ptr<GeomAPI_Shape> theShape) const
318 {
319   if(!theShape.get()) {
320     return false;
321   }
322
323   const TopoDS_Shape& aShapeToSearch = theShape->impl<TopoDS_Shape>();
324   if(aShapeToSearch.IsNull()) {
325     return false;
326   }
327
328   for(TopExp_Explorer anExp(*MY_SHAPE, aShapeToSearch.ShapeType()); anExp.More(); anExp.Next()) {
329     if(aShapeToSearch.IsEqual(anExp.Current())) {
330       return true;
331     }
332   }
333
334   return false;
335 }
336
337 bool GeomAPI_Shape::computeSize(double& theXmin, double& theYmin, double& theZmin,
338                                 double& theXmax, double& theYmax, double& theZmax) const
339 {
340   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
341   if (aShape.IsNull())
342     return false;
343   Bnd_Box aBndBox;
344   BRepBndLib::Add(aShape, aBndBox);
345   aBndBox.Get(theXmin, theYmin, theZmin, theXmax, theYmax, theZmax);
346   return true;
347 }
348
349 std::string GeomAPI_Shape::getShapeStream() const
350 {
351   std::ostringstream aStream;
352   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
353   BRepTools::Write(aShape, aStream);
354   return aStream.str();
355 }