Salome HOME
Minor changes.
[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   HYDROData_Object::Update();
66
67   Handle(HYDROData_PolylineXY) aPolylineXY = GetPolylineXY();
68   Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
69   if ( aPolylineXY.IsNull() || aProfileUZ.IsNull() )
70     return;
71
72   bool anIsSectionClosed = aPolylineXY->IsClosedSection( 0 );
73   HYDROData_IPolyline::SectionType aSectionType = aPolylineXY->GetSectionType( 0 );
74   HYDROData_IPolyline::PointsList aPolylinePoints = aPolylineXY->GetPoints( 0 );
75   HYDROData_IPolyline::PointsList aProfilePoints = aProfileUZ->GetPoints();
76   if ( aPolylinePoints.IsEmpty() || aProfilePoints.IsEmpty() )
77     return;
78
79   const HYDROData_IPolyline::Point& aFirstPoint = aPolylinePoints.First();
80   const HYDROData_IPolyline::Point& aLastPoint = aPolylinePoints.Last();
81
82   const HYDROData_IPolyline::Point& aFirstParPoint = aProfilePoints.First();
83   const HYDROData_IPolyline::Point& aLastParPoint = aProfilePoints.Last();
84
85   double aPolylineCommonDist = aPolylineXY->GetDistance( 0, aPolylinePoints.Size() - 1 );
86   double aParCommonDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aLastParPoint.X(), 0 ) );
87
88   NCollection_Sequence<Polyline3DPoint> aResPoints;
89   
90   // Add first point as is
91   aResPoints.Append( Polyline3DPoint( aFirstPoint.X(), aFirstPoint.Y(), aFirstParPoint.Y() ) );
92
93   for ( int i = 2, aNbPoints = aPolylinePoints.Size(); i < aNbPoints; ++i )
94   {
95     const HYDROData_IPolyline::Point& aPolylinePoint = aPolylinePoints.Value( i );
96
97     double aDistance = aPolylineXY->GetDistance( 0, i - 1 );
98
99     double aParLen = ( aDistance / aPolylineCommonDist ) * aParCommonDist;
100     double aDepth = aProfileUZ->GetDepthFromDistance( aParLen );
101
102     Polyline3DPoint aCompPoint( aPolylinePoint.X(), aPolylinePoint.Y(), aDepth );
103     aResPoints.Append( aCompPoint );
104   }
105
106   // Add last point as is
107   aResPoints.Append( Polyline3DPoint( aLastPoint.X(), aLastPoint.Y(), aLastParPoint.Y() ) );
108
109   TopoDS_Wire aResWire = HYDROData_PolylineXY::BuildWire( aSectionType, anIsSectionClosed, aResPoints );
110   SetTopShape( aResWire );
111   SetShape3D( aResWire );
112
113
114 bool HYDROData_Polyline3D::SetPolylineXY( const Handle(HYDROData_PolylineXY)& thePolyline )
115 {
116   if ( thePolyline.IsNull() )
117     return false;
118   
119   Handle(HYDROData_PolylineXY) aPrevPolyline = GetPolylineXY();
120   if ( IsEqual( aPrevPolyline, thePolyline ) )
121     return true;
122
123   SetReferenceObject( thePolyline, DataTag_PolylineXY );
124
125   // Indicate model of the need to update the polyline presentation
126   SetToUpdate( true );
127
128   return true;
129 }
130
131 Handle(HYDROData_PolylineXY) HYDROData_Polyline3D::GetPolylineXY() const
132 {
133   return Handle(HYDROData_PolylineXY)::DownCast( 
134            GetReferenceObject( DataTag_PolylineXY ) );
135 }
136
137 void HYDROData_Polyline3D::RemovePolylineXY()
138 {
139   Handle(HYDROData_PolylineXY) aPrevPolyline = GetPolylineXY();
140   if ( aPrevPolyline.IsNull() )
141     return;
142
143   ClearReferenceObjects( DataTag_PolylineXY );
144
145   // Indicate model of the need to update the polyline presentation
146   SetToUpdate( true );
147 }
148
149 bool HYDROData_Polyline3D::SetProfileUZ( const Handle(HYDROData_ProfileUZ)& theProfile )
150 {
151   if ( theProfile.IsNull() )
152     return false;
153   
154   Handle(HYDROData_ProfileUZ) aPrevProfile = GetProfileUZ();
155   if ( IsEqual( aPrevProfile, theProfile ) )
156     return true;
157
158   SetReferenceObject( theProfile, DataTag_ProfileUZ );
159
160   // Indicate model of the need to update the polyline presentation
161   SetToUpdate( true );
162
163   return true;
164 }
165
166 Handle(HYDROData_ProfileUZ) HYDROData_Polyline3D::GetProfileUZ() const
167 {
168   return Handle(HYDROData_ProfileUZ)::DownCast( 
169            GetReferenceObject( DataTag_ProfileUZ ) );
170 }
171
172 void HYDROData_Polyline3D::RemoveProfileUZ()
173 {
174   Handle(HYDROData_ProfileUZ) aPrevProfile = GetProfileUZ();
175   if ( aPrevProfile.IsNull() )
176     return;
177
178   ClearReferenceObjects( DataTag_ProfileUZ );
179
180   // Indicate model of the need to update the polyline presentation
181   SetToUpdate( true );
182 }
183