Salome HOME
Issue #1860: fix end lines with spaces
[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   // list of simple elements that are not detected in connection to others
121   NCollection_List<TopoDS_Shape> aNotConnected;
122   addSimpleToList(aShape, aNotConnected);
123   if (aNotConnected.IsEmpty()) // an empty compound
124     return false;
125
126   // collect here the group of connected subs, starting with one first element
127   NCollection_List<TopoDS_Shape> aNewConnected;
128   aNewConnected.Append(aNotConnected.First());
129   aNotConnected.RemoveFirst();
130   // iterate until some new element become connected
131   while(!aNewConnected.IsEmpty() && !aNotConnected.IsEmpty()) {
132     NCollection_List<TopoDS_Shape> aNew; // very new connected to new connected
133     NCollection_List<TopoDS_Shape>::Iterator aNotIter(aNotConnected);
134     while(aNotIter.More()) {
135       // optimization to avoid TopExp_Explorer double-cycle, collect all vertices in the list first
136       NCollection_List<TopoDS_Shape> aNotVertices;
137       for(TopExp_Explorer anExp1(aNotIter.Value(), TopAbs_VERTEX); anExp1.More(); anExp1.Next()) {
138         aNotVertices.Append(anExp1.Current());
139       }
140
141       bool aConnected =  false;
142       NCollection_List<TopoDS_Shape>::Iterator aNewIter(aNewConnected);
143       for(; !aConnected && aNewIter.More(); aNewIter.Next()) {
144         // checking topological connecion of aNotIter and aNewIter
145         // (if shapes are connected, vertices are connected for sure)
146         TopExp_Explorer anExp2(aNewIter.Value(), TopAbs_VERTEX);
147         for(; !aConnected && anExp2.More(); anExp2.Next()) {
148           NCollection_List<TopoDS_Shape>::Iterator aNotIter(aNotVertices);
149           for(; aNotIter.More(); aNotIter.Next()) {
150             if (aNotIter.Value().IsSame(anExp2.Current())) {
151               aConnected = true;
152               break;
153             }
154           }
155         }
156       }
157       if (aConnected) {
158         aNew.Append(aNotIter.Value());
159         aNotConnected.Remove(aNotIter);
160       } else {
161         aNotIter.Next();
162       }
163     }
164     // remove all new connected and put to this list very new connected
165     aNewConnected.Clear();
166     aNewConnected.Append(aNew);
167   }
168   return aNotConnected.IsEmpty() == Standard_True;
169 }
170
171 bool GeomAPI_Shape::isSolid() const
172 {
173   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
174   return !aShape.IsNull() && aShape.ShapeType() == TopAbs_SOLID;
175 }
176
177 bool GeomAPI_Shape::isCompSolid() const
178 {
179   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
180   return !aShape.IsNull() && aShape.ShapeType() == TopAbs_COMPSOLID;
181 }
182
183 bool GeomAPI_Shape::isPlanar() const
184 {
185   TopoDS_Shape aShape = impl<TopoDS_Shape>();
186
187   if(aShape.IsNull()) {
188     return false;
189   }
190
191   TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
192   if(aShapeType == TopAbs_COMPOUND) {
193     TopoDS_Iterator anIt(aShape);
194     int aShNum = 0;
195     for(; anIt.More(); anIt.Next()) {
196       ++aShNum;
197     }
198     if(aShNum == 1) {
199       anIt.Initialize(aShape);
200       aShape = anIt.Value();
201     }
202   }
203
204   aShapeType = aShape.ShapeType();
205   if(aShapeType == TopAbs_VERTEX) {
206     return true;
207   } else if(aShapeType == TopAbs_FACE) {
208     const Handle(Geom_Surface)& aSurface = BRep_Tool::Surface(TopoDS::Face(aShape));
209     Handle(Standard_Type) aType = aSurface->DynamicType();
210
211     if(aType == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
212       Handle(Geom_RectangularTrimmedSurface) aTrimSurface =
213         Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurface);
214       aType = aTrimSurface->BasisSurface()->DynamicType();
215     }
216     return (aType == STANDARD_TYPE(Geom_Plane)) == Standard_True;
217   } else {
218     BRepBuilderAPI_FindPlane aFindPlane(aShape);
219     bool isFound = aFindPlane.Found() == Standard_True;
220
221     if(!isFound && aShapeType == TopAbs_EDGE) {
222       Standard_Real aFirst, aLast;
223       Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(aShape), aFirst, aLast);
224       Handle(Standard_Type) aType = aCurve->DynamicType();
225
226       if(aType == STANDARD_TYPE(Geom_TrimmedCurve)) {
227         Handle(Geom_TrimmedCurve) aTrimCurve = Handle(Geom_TrimmedCurve)::DownCast(aCurve);
228         aType = aTrimCurve->BasisCurve()->DynamicType();
229       }
230
231       if(aType == STANDARD_TYPE(Geom_Line)
232           || aType == STANDARD_TYPE(Geom_Conic)
233           || aType == STANDARD_TYPE(Geom_Circle)
234           || aType == STANDARD_TYPE(Geom_Ellipse)
235           || aType == STANDARD_TYPE(Geom_Hyperbola)
236           || aType == STANDARD_TYPE(Geom_Parabola)) {
237         isFound = true;
238       }
239     }
240
241     return isFound;
242   }
243
244   return false;
245 }
246
247 GeomAPI_Shape::ShapeType GeomAPI_Shape::shapeType() const
248 {
249   const TopoDS_Shape& aShape = impl<TopoDS_Shape>();
250
251   ShapeType aST = GeomAPI_Shape::SHAPE;
252
253   switch(aShape.ShapeType()) {
254   case TopAbs_COMPOUND:
255     aST = GeomAPI_Shape::COMPOUND;
256     break;
257   case TopAbs_COMPSOLID:
258     aST = GeomAPI_Shape::COMPSOLID;
259     break;
260   case TopAbs_SOLID:
261     aST = GeomAPI_Shape::SOLID;
262     break;
263   case TopAbs_SHELL:
264     aST = GeomAPI_Shape::SHELL;
265     break;
266   case TopAbs_FACE:
267     aST = GeomAPI_Shape::FACE;
268     break;
269   case TopAbs_WIRE:
270     aST = GeomAPI_Shape::WIRE;
271     break;
272   case TopAbs_EDGE:
273     aST = GeomAPI_Shape::EDGE;
274     break;
275   case TopAbs_VERTEX:
276     aST = GeomAPI_Shape::VERTEX;
277     break;
278   case TopAbs_SHAPE:
279     aST = GeomAPI_Shape::SHAPE;
280     break;
281   }
282
283   return aST;
284 }
285
286 std::string GeomAPI_Shape::shapeTypeStr() const
287 {
288   ShapeType aShapeType = shapeType();
289   std::string aShapeTypeStr;
290
291   switch(aShapeType) {
292     case COMPOUND: {
293       aShapeTypeStr = "COMPOUND";
294       break;
295     }
296     case COMPSOLID: {
297       aShapeTypeStr = "COMPSOLID";
298       break;
299     }
300     case SOLID: {
301       aShapeTypeStr = "SOLID";
302       break;
303     }
304     case SHELL: {
305       aShapeTypeStr = "SHELL";
306       break;
307     }
308     case FACE: {
309       aShapeTypeStr = "FACE";
310       break;
311     }
312     case WIRE: {
313       aShapeTypeStr = "WIRE";
314       break;
315     }
316     case EDGE: {
317       aShapeTypeStr = "EDGE";
318       break;
319     }
320     case VERTEX: {
321       aShapeTypeStr = "VERTEX";
322       break;
323     }
324     case SHAPE: {
325       aShapeTypeStr = "SHAPE";
326       break;
327     }
328   }
329
330   return aShapeTypeStr;
331 }
332
333 GeomAPI_Shape::Orientation GeomAPI_Shape::orientation() const
334 {
335   TopAbs_Orientation anOrientation = MY_SHAPE->Orientation();
336
337   switch(anOrientation) {
338     case TopAbs_FORWARD:  return FORWARD;
339     case TopAbs_REVERSED: return REVERSED;
340     case TopAbs_INTERNAL: return INTERNAL;
341     case TopAbs_EXTERNAL: return EXTERNAL;
342     default:              return FORWARD;
343   }
344 }
345
346 void GeomAPI_Shape::setOrientation(const GeomAPI_Shape::Orientation theOrientation)
347 {
348   TopAbs_Orientation anOrientation = MY_SHAPE->Orientation();
349
350   switch(theOrientation) {
351     case FORWARD:  MY_SHAPE->Orientation(TopAbs_FORWARD);  break;
352     case REVERSED: MY_SHAPE->Orientation(TopAbs_REVERSED); break;
353     case INTERNAL: MY_SHAPE->Orientation(TopAbs_INTERNAL); break;
354     case EXTERNAL: MY_SHAPE->Orientation(TopAbs_EXTERNAL); break;
355   }
356 }
357
358 bool GeomAPI_Shape::isSubShape(const std::shared_ptr<GeomAPI_Shape> theShape) const
359 {
360   if(!theShape.get()) {
361     return false;
362   }
363
364   const TopoDS_Shape& aShapeToSearch = theShape->impl<TopoDS_Shape>();
365   if(aShapeToSearch.IsNull()) {
366     return false;
367   }
368
369   for(TopExp_Explorer anExp(*MY_SHAPE, aShapeToSearch.ShapeType()); anExp.More(); anExp.Next()) {
370     if(aShapeToSearch.IsEqual(anExp.Current())) {
371       return true;
372     }
373   }
374
375   return false;
376 }
377
378 bool GeomAPI_Shape::computeSize(double& theXmin, double& theYmin, double& theZmin,
379                                 double& theXmax, double& theYmax, double& theZmax) const
380 {
381   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
382   if (aShape.IsNull())
383     return false;
384   Bnd_Box aBndBox;
385   BRepBndLib::Add(aShape, aBndBox);
386   aBndBox.Get(theXmin, theYmin, theZmin, theXmax, theYmax, theZmax);
387   return true;
388 }
389
390 std::string GeomAPI_Shape::getShapeStream() const
391 {
392   std::ostringstream aStream;
393   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
394   BRepTools::Write(aShape, aStream);
395   return aStream.str();
396 }