Salome HOME
Merge remote-tracking branch 'remotes/origin/BR_PlaneGCS' into CodeCleanup
[modules/shaper.git] / src / GeomAPI / GeomAPI_ShapeExplorer.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomAPI_ShapeExplorer.cpp
4 // Created:     5 June 2015
5 // Author:      Dmitry Bobylev
6
7 #include <GeomAPI_ShapeExplorer.h>
8
9 #include <Standard_NoMoreObject.hxx>
10 #include <TopExp_Explorer.hxx>
11
12 #define MY_EXPLORER implPtr<TopExp_Explorer>()
13
14 //=================================================================================================
15 GeomAPI_ShapeExplorer::GeomAPI_ShapeExplorer()
16 : GeomAPI_Interface(new TopExp_Explorer())
17 {
18 }
19
20 // returns a type of shape to expolode, but if toFind==SHAPE, it will return the type
21 // of the first sub-element of compoud if theSHape is compound
22 static TopAbs_ShapeEnum ShapeType(const std::shared_ptr<GeomAPI_Shape>& theShape,
23                                   const GeomAPI_Shape::ShapeType toFind)
24 {
25   if (toFind == GeomAPI_Shape::SHAPE) {
26     TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
27     if (!aShape.IsNull() && aShape.ShapeType() == TopAbs_COMPOUND) {
28       TopoDS_Iterator anIter(aShape);
29       if (anIter.More()) {
30         return anIter.Value().ShapeType();
31       }
32     }
33   }
34   return (TopAbs_ShapeEnum)toFind;
35 }
36
37 //=================================================================================================
38 GeomAPI_ShapeExplorer::GeomAPI_ShapeExplorer(const std::shared_ptr<GeomAPI_Shape>& theShape,
39                                              const GeomAPI_Shape::ShapeType toFind,
40                                              const GeomAPI_Shape::ShapeType toAvoid)
41 : GeomAPI_Interface(new TopExp_Explorer(theShape->impl<TopoDS_Shape>(),
42                                        ShapeType(theShape, toFind),
43                                        (TopAbs_ShapeEnum)toAvoid))
44 {
45 }
46
47 //=================================================================================================
48 void GeomAPI_ShapeExplorer::init(const std::shared_ptr<GeomAPI_Shape>& theShape,
49                                  const GeomAPI_Shape::ShapeType toFind,
50                                  const GeomAPI_Shape::ShapeType toAvoid)
51 {
52   MY_EXPLORER->Init(theShape->impl<TopoDS_Shape>(),
53                    ShapeType(theShape, toFind),
54                    (TopAbs_ShapeEnum)toAvoid);
55 }
56
57 //=================================================================================================
58 bool GeomAPI_ShapeExplorer::more() const
59 {
60   return MY_EXPLORER->More() == Standard_True;
61 }
62
63 //=================================================================================================
64 void GeomAPI_ShapeExplorer::next()
65 {
66   try {
67     MY_EXPLORER->Next();
68   } catch (Standard_NoMoreObject) {
69   }
70 }
71
72 //=================================================================================================
73 std::shared_ptr<GeomAPI_Shape> GeomAPI_ShapeExplorer::current()
74 {
75   try {
76     const TopoDS_Shape& aShape = MY_EXPLORER->Current();
77     std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape());
78     aGeomShape->setImpl(new TopoDS_Shape(aShape));
79     return aGeomShape;
80   } catch (Standard_NoMoreObject) {
81     return std::shared_ptr<GeomAPI_Shape>();
82   }
83 }
84
85 //=================================================================================================
86 void GeomAPI_ShapeExplorer::reinit()
87 {
88   MY_EXPLORER->ReInit();
89 }
90
91 //=================================================================================================
92 int GeomAPI_ShapeExplorer::depth() const
93 {
94   return MY_EXPLORER->Depth();
95 }
96
97 //=================================================================================================
98 void GeomAPI_ShapeExplorer::clear()
99 {
100   MY_EXPLORER->Clear();
101 }