Salome HOME
Creat\Edit stream operation.
[modules/hydro.git] / src / HYDROData / HYDROData_Polyline3D.cxx
1
2 #include "HYDROData_Polyline3D.h"
3
4 #include "HYDROData_Document.h"
5 #include "HYDROData_PolylineXY.h"
6 #include "HYDROData_ProfileUZ.h"
7
8 #include <gp_Pnt2d.hxx>
9 #include <gp_XY.hxx>
10 #include <gp_XYZ.hxx>
11
12 #include <TopoDS.hxx>
13 #include <TopoDS_Wire.hxx>
14
15 #include <QStringList>
16
17 #define PYTHON_POLYLINE_ID "KIND_POLYLINE"
18
19 IMPLEMENT_STANDARD_HANDLE(HYDROData_Polyline3D,HYDROData_Object)
20 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Polyline3D,HYDROData_Object)
21
22
23 HYDROData_Polyline3D::HYDROData_Polyline3D()
24 : HYDROData_Object()
25 {
26 }
27
28 HYDROData_Polyline3D::~HYDROData_Polyline3D()
29 {
30 }
31
32 QStringList HYDROData_Polyline3D::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
33 {
34   QStringList aResList;
35
36   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
37   if ( aDocument.IsNull() )
38     return aResList;
39
40   QString aDocName = aDocument->GetDocPyName();
41   QString aPolylineName = GetName();
42
43   aResList << QString( "%1 = %2.CreateObject( %3 );" )
44               .arg( aPolylineName ).arg( aDocName ).arg( PYTHON_POLYLINE_ID );
45   aResList << QString( "%1.SetName( \"%1\" );" ).arg( aPolylineName );
46   aResList << QString( "" );
47
48   // TODO
49
50   return aResList;
51 }
52
53 TopoDS_Shape HYDROData_Polyline3D::GetTopShape() const
54 {
55   return getTopShape();
56 }
57
58 TopoDS_Shape HYDROData_Polyline3D::GetShape3D() const
59 {
60   return getShape3D();
61 }
62
63 void HYDROData_Polyline3D::Update()
64 {
65   removeTopShape();
66   removeShape3D();
67
68   Handle(HYDROData_PolylineXY) aPolylineXY = GetPolylineXY();
69   Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
70   if ( aPolylineXY.IsNull() || aProfileUZ.IsNull() )
71     return;
72
73   bool anIsSectionClosed = aPolylineXY->IsClosedSection( 0 );
74   HYDROData_IPolyline::SectionType aSectionType = aPolylineXY->GetSectionType( 0 );
75   HYDROData_IPolyline::PointsList aPolylinePoints = aPolylineXY->GetPoints( 0 );
76   HYDROData_IPolyline::PointsList aProfilePoints = aProfileUZ->GetPoints();
77   if ( aPolylinePoints.IsEmpty() || aProfilePoints.IsEmpty() )
78     return;
79
80   const HYDROData_IPolyline::Point& aFirstPoint = aPolylinePoints.First();
81   const HYDROData_IPolyline::Point& aLastPoint = aPolylinePoints.Last();
82
83   const HYDROData_IPolyline::Point& aFirstParPoint = aProfilePoints.First();
84   const HYDROData_IPolyline::Point& aLastParPoint = aProfilePoints.Last();
85
86   double aPolylineCommonDist = aPolylineXY->GetDistance( 0, aPolylinePoints.Size() - 1 );
87   double aParCommonDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aLastParPoint.X(), 0 ) );
88
89   NCollection_Sequence<Polyline3DPoint> aResPoints;
90   
91   // Add first point as is
92   aResPoints.Append( Polyline3DPoint( aFirstPoint.X(), aFirstPoint.Y(), aFirstParPoint.Y() ) );
93
94   for ( int i = 2, aNbPoints = aPolylinePoints.Size(); i < aNbPoints; ++i )
95   {
96     const HYDROData_IPolyline::Point& aPolylinePoint = aPolylinePoints.Value( i );
97
98     double aDistance = aPolylineXY->GetDistance( 0, i - 1 );
99
100     double aParLen = ( aDistance / aPolylineCommonDist ) * aParCommonDist;
101     double aDepth = aProfileUZ->GetDepthFromDistance( aParLen );
102
103     Polyline3DPoint aCompPoint( aPolylinePoint.X(), aPolylinePoint.Y(), aDepth );
104     aResPoints.Append( aCompPoint );
105   }
106
107   // Add last point as is
108   aResPoints.Append( Polyline3DPoint( aLastPoint.X(), aLastPoint.Y(), aLastParPoint.Y() ) );
109
110   TopoDS_Wire aResWire = HYDROData_PolylineXY::BuildWire( aSectionType, anIsSectionClosed, aResPoints );
111   SetTopShape( aResWire );
112   SetShape3D( aResWire );
113
114
115 bool HYDROData_Polyline3D::SetPolylineXY( const Handle(HYDROData_PolylineXY)& thePolyline )
116 {
117   if ( thePolyline.IsNull() )
118     return false;
119   
120   Handle(HYDROData_PolylineXY) aPrevPolyline = GetPolylineXY();
121   if ( IsEqual( aPrevPolyline, thePolyline ) )
122     return true;
123
124   SetReferenceObject( thePolyline, DataTag_PolylineXY );
125
126   // Indicate model of the need to update the polyline presentation
127   SetToUpdate( true );
128
129   return true;
130 }
131
132 Handle(HYDROData_PolylineXY) HYDROData_Polyline3D::GetPolylineXY() const
133 {
134   return Handle(HYDROData_PolylineXY)::DownCast( 
135            GetReferenceObject( DataTag_PolylineXY ) );
136 }
137
138 void HYDROData_Polyline3D::RemovePolylineXY()
139 {
140   Handle(HYDROData_PolylineXY) aPrevPolyline = GetPolylineXY();
141   if ( aPrevPolyline.IsNull() )
142     return;
143
144   ClearReferenceObjects( DataTag_PolylineXY );
145
146   // Indicate model of the need to update the polyline presentation
147   SetToUpdate( true );
148 }
149
150 bool HYDROData_Polyline3D::SetProfileUZ( const Handle(HYDROData_ProfileUZ)& theProfile )
151 {
152   if ( theProfile.IsNull() )
153     return false;
154   
155   Handle(HYDROData_ProfileUZ) aPrevProfile = GetProfileUZ();
156   if ( IsEqual( aPrevProfile, theProfile ) )
157     return true;
158
159   SetReferenceObject( theProfile, DataTag_ProfileUZ );
160
161   // Indicate model of the need to update the polyline presentation
162   SetToUpdate( true );
163
164   return true;
165 }
166
167 Handle(HYDROData_ProfileUZ) HYDROData_Polyline3D::GetProfileUZ() const
168 {
169   return Handle(HYDROData_ProfileUZ)::DownCast( 
170            GetReferenceObject( DataTag_ProfileUZ ) );
171 }
172
173 void HYDROData_Polyline3D::RemoveProfileUZ()
174 {
175   Handle(HYDROData_ProfileUZ) aPrevProfile = GetProfileUZ();
176   if ( aPrevProfile.IsNull() )
177     return;
178
179   ClearReferenceObjects( DataTag_ProfileUZ );
180
181   // Indicate model of the need to update the polyline presentation
182   SetToUpdate( true );
183 }
184