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