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