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