Salome HOME
Temporary workaround for python.
[modules/hydro.git] / src / HYDROData / HYDROData_ShapesTool.cxx
1
2 #include "HYDROData_ShapesTool.h"
3
4 #include <BRep_Tool.hxx>
5
6 #include <gp_Pnt.hxx>
7
8 #include <Precision.hxx>
9
10 #include <TopExp.hxx>
11
12 #include <TopoDS.hxx>
13 #include <TopoDS_Edge.hxx>
14 #include <TopoDS_Shape.hxx>
15 #include <TopoDS_Vertex.hxx>
16 #include <TopoDS_Wire.hxx>
17
18 #include <TopTools_SequenceOfShape.hxx>
19 #include <TopTools_ListOfShape.hxx>
20 #include <TopTools_ListIteratorOfListOfShape.hxx>
21
22 #include <TopExp_Explorer.hxx>
23
24 void HYDROData_ShapesTool::ExploreShapeToShapes( const TopoDS_Shape&       theInShape,
25                                                  const TopAbs_ShapeEnum&   theExpType,
26                                                  TopTools_SequenceOfShape& theOutShapes )
27 {
28   theOutShapes.Clear();
29
30   if ( theInShape.IsNull() )
31     return;
32
33   TopExp_Explorer anExp( theInShape, theExpType );
34   for ( ; anExp.More(); anExp.Next() )
35   {
36     TopoDS_Shape anExpShape = anExp.Current();
37     theOutShapes.Append( anExpShape );
38   }
39 }
40
41 bool HYDROData_ShapesTool::Vertices( const TopoDS_Shape& theInShape,
42                                      TopoDS_Vertex&      theFirstVertex,
43                                      TopoDS_Vertex&      theLastVertex,
44                                      const bool          theIsCumOri  )
45 {
46   if ( theInShape.IsNull() )
47     return false;
48
49   if ( theInShape.ShapeType() == TopAbs_EDGE )
50   {
51     TopoDS_Edge anEdge = TopoDS::Edge( theInShape );
52     TopExp::Vertices( anEdge, theFirstVertex, theLastVertex, theIsCumOri );
53   }
54   else if ( theInShape.ShapeType() == TopAbs_WIRE )
55   {
56     TopoDS_Wire aWire = TopoDS::Wire( theInShape );
57     TopExp::Vertices( aWire, theFirstVertex, theLastVertex );
58   }
59   else
60   {
61     return false;
62   }
63
64   return true;
65 }
66
67 bool HYDROData_ShapesTool::IsVerticesEquals( const TopoDS_Vertex& theFirstVert,
68                                              const TopoDS_Vertex& theSecondVert,
69                                              const bool           theIs2D )
70 {
71   gp_Pnt aFirstPoint = BRep_Tool::Pnt( theFirstVert );
72   gp_Pnt aSecondPoint = BRep_Tool::Pnt( theSecondVert );
73
74   // Set the Z coord to zero for 2D equations
75   if ( theIs2D )
76   {
77     aFirstPoint.SetZ( 0.0 );
78     aSecondPoint.SetZ( 0.0 );
79   }
80
81   return aFirstPoint.IsEqual( aSecondPoint, Precision::Confusion() );
82 }
83
84 bool HYDROData_ShapesTool::IsEdgesEquals( const TopoDS_Edge& theFirstEdge,
85                                           const TopoDS_Edge& theSecondEdge,
86                                           const bool         theIs2D )
87 {
88   TopoDS_Vertex aFFirstVert, aFLastVert, aSFirstVert, aSLastVert;
89   TopExp::Vertices( theFirstEdge, aFFirstVert, aFLastVert );
90   TopExp::Vertices( theSecondEdge, aSFirstVert, aSLastVert );
91   return IsVerticesEquals( aFFirstVert, aSFirstVert, theIs2D ) &&
92          IsVerticesEquals( aFLastVert, aSLastVert, theIs2D );
93 }
94
95 void HYDROData_ShapesTool::AddShapes( TopTools_SequenceOfShape&       theShapes,
96                                       const TopTools_SequenceOfShape& theShapesToAdd )
97 {
98   for ( int i = 1, n = theShapesToAdd.Length(); i <= n; ++i )
99   {
100     const TopoDS_Shape& aShape = theShapesToAdd.Value( i );
101     theShapes.Append( aShape );
102   }
103 }
104
105 void HYDROData_ShapesTool::AddShapes( TopTools_SequenceOfShape&   theShapes,
106                                       const TopTools_ListOfShape& theShapesToAdd )
107 {
108   TopTools_ListIteratorOfListOfShape anIter( theShapesToAdd );
109   for ( ; anIter.More(); anIter.Next() )
110   {
111     const TopoDS_Shape& aShape = anIter.Value();
112     theShapes.Append( aShape );
113   }
114 }
115
116 void HYDROData_ShapesTool::AddShapes( TopTools_ListOfShape&           theShapes,
117                                       const TopTools_SequenceOfShape& theShapesToAdd )
118 {
119   for ( int i = 1, n = theShapesToAdd.Length(); i <= n; ++i )
120   {
121     const TopoDS_Shape& aShape = theShapesToAdd.Value( i );
122     theShapes.Append( aShape );
123   }
124 }
125
126 void HYDROData_ShapesTool::AddShapes( TopTools_ListOfShape&       theShapes,
127                                       const TopTools_ListOfShape& theShapesToAdd )
128 {
129   TopTools_ListIteratorOfListOfShape anIter( theShapesToAdd );
130   for ( ; anIter.More(); anIter.Next() )
131   {
132     const TopoDS_Shape& aShape = anIter.Value();
133     theShapes.Append( aShape );
134   }
135 }
136
137 void HYDROData_ShapesTool::DumpShapeSubShapes( std::ostream&           theStream,
138                                                const char*             theTitle,
139                                                const TopoDS_Shape&     theShape,
140                                                const TopAbs_ShapeEnum& theExpType )
141 {
142   TopTools_SequenceOfShape aShapeSubShapes;
143   ExploreShapeToShapes( theShape, theExpType, aShapeSubShapes );
144
145   theStream << theTitle << "\n";
146   DumpSequenceOfShapes( theStream, aShapeSubShapes );
147 }
148
149 void HYDROData_ShapesTool::DumpSequenceOfShapes( std::ostream&                   theStream,
150                                                  const TopTools_SequenceOfShape& theShapes )
151 {
152   for ( int i = 1, n = theShapes.Length(); i <= n; ++i )
153   {
154     const TopoDS_Shape& aShape = theShapes.Value( i );
155     theStream << "Shape " << i << " : " << aShape.TShape() << "\n";
156   }
157 }