Salome HOME
9832cadb3a4a47fa95a35795a1d2ec5da90c2af5
[modules/hydro.git] / src / HYDROData / HYDROData_Profile.cxx
1
2 #include "HYDROData_Profile.h"
3
4 #include "HYDROData_Document.h"
5 #include "HYDROData_Iterator.h"
6 #include "HYDROData_Tool.h"
7
8 #include <BRepBuilderAPI_MakeEdge.hxx>
9 #include <BRepBuilderAPI_MakeWire.hxx>
10
11 #include <gp_XY.hxx>
12 #include <gp_XYZ.hxx>
13 #include <gp_Pnt2d.hxx>
14
15 #include <TDataStd_AsciiString.hxx>
16 #include <TDataStd_RealArray.hxx>
17
18 #include <TopoDS_Edge.hxx>
19 #include <TopoDS_Wire.hxx>
20
21 #include <OSD_File.hxx>
22 #include <OSD_Protection.hxx>
23
24 #include <QStringList>
25
26 #define PYTHON_PROFILE_ID "KIND_PROFILE"
27
28 IMPLEMENT_STANDARD_HANDLE(HYDROData_Profile, HYDROData_Object)
29 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Profile, HYDROData_Object)
30
31 HYDROData_Profile::HYDROData_Profile()
32 : HYDROData_Object()
33 {
34 }
35
36 HYDROData_Profile::~HYDROData_Profile()
37 {
38 }
39
40 TopoDS_Shape HYDROData_Profile::GetTopShape() const
41 {
42   TopoDS_Wire aWire;
43
44   gp_XY aFirstPoint, aLastPoint;
45   if ( !GetFirstPoint( aFirstPoint ) || !GetLastPoint( aLastPoint ) )
46     return aWire;
47
48   gp_Pnt aPnt1( aFirstPoint.X(), aFirstPoint.Y(), 0 );
49   gp_Pnt aPnt2( aLastPoint.X(),  aLastPoint.Y(),  0 );
50
51   BRepBuilderAPI_MakeEdge aMakeEdge( aPnt1, aPnt2 );
52   TopoDS_Edge anEdge = aMakeEdge;
53
54   BRepBuilderAPI_MakeWire aMakeWire( anEdge );
55   aWire = aMakeWire;
56
57   return aWire;
58 }
59
60 TopoDS_Shape HYDROData_Profile::GetShape3D() const
61 {
62   return getShape3D();
63 }
64
65 void HYDROData_Profile::UpdateShape3D()
66 {
67   BRepBuilderAPI_MakeWire aMakeWire;
68
69   ProfilePoints aProfilePoints = GetProfilePoints();
70   for ( int i = 1, n = aProfilePoints.Length(); i < n ; ++i )
71   {
72     ProfilePoint aFirstPoint = aProfilePoints.Value( i );
73     ProfilePoint aSecPoint = aProfilePoints.Value( i + 1 );
74
75     gp_Pnt aPnt1( aFirstPoint.X(), aFirstPoint.Y(), aFirstPoint.Z() );
76     gp_Pnt aPnt2( aSecPoint.X(),   aSecPoint.Y(),   aSecPoint.Z()   );
77
78     BRepBuilderAPI_MakeEdge aMakeEdge( aPnt1, aPnt2 );
79     TopoDS_Edge anEdge = aMakeEdge;
80
81     aMakeWire.Add( anEdge );
82   }
83
84   TopoDS_Wire aWire;
85   if ( aMakeWire.IsDone() )
86     aWire = aMakeWire;
87
88   SetShape3D( aWire );
89 }
90
91 /**
92  * Dump object to Python script representation.
93  */
94 QStringList HYDROData_Profile::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
95 {
96   QStringList aResList;
97
98   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
99   if ( aDocument.IsNull() )
100     return aResList;
101                              
102   QString aDocName = aDocument->GetDocPyName();
103   QString aProfileName = GetName();
104
105   aResList << QString( "%1 = %2.CreateObject( %3 );" )
106               .arg( aProfileName ).arg( aDocName ).arg( PYTHON_PROFILE_ID );
107   aResList << QString( "%1.SetName( \"%1\" );" ).arg( aProfileName );
108
109   return aResList;
110 }
111
112 bool HYDROData_Profile::IsValid() const
113 {
114   gp_XY aFirstPoint, aLastPoint;
115   if ( !GetFirstPoint( aFirstPoint ) || !GetLastPoint( aLastPoint ) )
116     return false;
117
118   int aNbPoints = NbPoints();
119   return aNbPoints > 1;
120 }
121
122 void HYDROData_Profile::SetFirstPoint( const gp_XY& thePoint )
123 {
124   TDF_Label aLabel = myLab.FindChild( DataTag_FirstPoint );
125
126   Handle(TDataStd_RealArray) anArray;
127   if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
128     anArray = TDataStd_RealArray::Set( aLabel, 0, 1 );
129
130   anArray->SetValue( 0, thePoint.X() );
131   anArray->SetValue( 1, thePoint.Y() );
132 }
133
134 bool HYDROData_Profile::GetFirstPoint( gp_XY& thePoint ) const
135 {
136   TDF_Label aLabel = myLab.FindChild( DataTag_FirstPoint, false );
137   if ( aLabel.IsNull() )
138     return false;
139
140   Handle(TDataStd_RealArray) anArray;
141   if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
142     return false;
143
144   thePoint.SetX( anArray->Value( 0 ) );
145   thePoint.SetY( anArray->Value( 1 ) );
146
147   return true;
148 }
149
150 void HYDROData_Profile::SetLastPoint( const gp_XY& thePoint )
151 {
152   TDF_Label aLabel = myLab.FindChild( DataTag_LastPoint );
153
154   Handle(TDataStd_RealArray) anArray;
155   if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
156     anArray = TDataStd_RealArray::Set( aLabel, 0, 1 );
157
158   anArray->SetValue( 0, thePoint.X() );
159   anArray->SetValue( 1, thePoint.Y() );
160 }
161
162 bool HYDROData_Profile::GetLastPoint( gp_XY& thePoint ) const
163 {
164   TDF_Label aLabel = myLab.FindChild( DataTag_LastPoint, false );
165   if ( aLabel.IsNull() )
166     return false;
167
168   Handle(TDataStd_RealArray) anArray;
169   if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
170     return false;
171
172   thePoint.SetX( anArray->Value( 0 ) );
173   thePoint.SetY( anArray->Value( 1 ) );
174
175   return true;
176 }
177
178 Handle(HYDROData_ProfileUZ) HYDROData_Profile::GetProfileUZ( const bool theIsCreate ) const
179 {
180   Handle(HYDROData_ProfileUZ) aProfileUZ;
181
182   TDF_Label aLabel = myLab.FindChild( DataTag_ChildProfileUZ, theIsCreate );
183   if ( aLabel.IsNull() )
184     return aProfileUZ;
185
186   aProfileUZ = Handle(HYDROData_ProfileUZ)::DownCast( HYDROData_Iterator::Object( aLabel ) );
187   if ( aProfileUZ.IsNull() && theIsCreate )
188   {
189     aProfileUZ = Handle(HYDROData_ProfileUZ)::DownCast(
190       HYDROData_Iterator::CreateObject( aLabel, KIND_PROFILEUZ ) );
191   }
192
193   return aProfileUZ;
194 }
195
196 int HYDROData_Profile::NbPoints() const
197 {
198   Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
199   return aProfileUZ.IsNull() ? 0 : aProfileUZ->NbPoints();
200 }
201
202 void HYDROData_Profile::RemovePoints()
203 {
204   Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
205   if ( !aProfileUZ.IsNull() )
206     aProfileUZ->RemoveSections();
207 }
208
209 void HYDROData_Profile::SetParametricPoints( const HYDROData_ProfileUZ::PointsList& thePoints )
210 {
211   RemovePoints();
212
213   Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
214   for ( int i = 1, n = thePoints.Length(); i <= n ; ++i )
215   {
216     const HYDROData_ProfileUZ::Point& aPoint = thePoints.Value( i );
217     aProfileUZ->AddPoint( 0, aPoint );
218   }
219 }
220
221 HYDROData_ProfileUZ::PointsList HYDROData_Profile::GetParametricPoints() const
222 {
223   Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
224   return aProfileUZ.IsNull() ? HYDROData_ProfileUZ::PointsList() : aProfileUZ->GetPoints();
225 }
226
227 void HYDROData_Profile::SetProfilePoints( const ProfilePoints& thePoints )
228 {
229   RemovePoints();
230   if ( thePoints.Length() < 2 )
231     return;
232
233   gp_XY aFirstPoint, aLastPoint;
234
235   Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
236   for ( int i = 1, n = thePoints.Length(); i <= n ; ++i )
237   {
238     const ProfilePoint& aPoint = thePoints.Value( i );
239     gp_XY aPointXY( aPoint.X(), aPoint.Y() );
240
241     if ( i == 1 )
242       aFirstPoint = aPointXY;
243     else if ( i == n )
244       aLastPoint = aPointXY;
245
246     double aDistance = gp_Pnt2d( aFirstPoint ).Distance( aPointXY );
247     
248     HYDROData_ProfileUZ::Point aParPoint( aDistance, aPoint.Z() );
249     aProfileUZ->AddPoint( 0, aParPoint );
250   }
251
252   SetFirstPoint( aFirstPoint );
253   SetLastPoint( aLastPoint );
254 }
255
256 HYDROData_Profile::ProfilePoints HYDROData_Profile::GetProfilePoints() const
257 {
258   ProfilePoints aResPoints;
259
260   gp_XY aFirstPoint, aLastPoint;
261   if ( !GetFirstPoint( aFirstPoint ) || !GetLastPoint( aLastPoint ) )
262     return aResPoints;
263
264   HYDROData_ProfileUZ::PointsList aParametricPoints = GetParametricPoints();
265   if ( aParametricPoints.Length() < 2 )
266     return aResPoints;
267
268   const HYDROData_ProfileUZ::Point& aFirstParPoint = aParametricPoints.First();
269   const HYDROData_ProfileUZ::Point& aLastParPoint = aParametricPoints.Last();
270
271   double aGeoDistance = gp_Pnt2d( aFirstPoint ).Distance( aLastPoint );
272   double aParCommonDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aLastParPoint.X(), 0 ) );
273
274   // Add first point as is
275   aResPoints.Append( ProfilePoint( aFirstPoint.X(), aFirstPoint.Y(), aFirstParPoint.Y() ) );
276
277   // Compute all other points
278   for ( int i = 2, n = aParametricPoints.Length(); i < n ; ++i )
279   {
280     const HYDROData_ProfileUZ::Point& aParPoint = aParametricPoints.Value( i );
281
282     double aParPointDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aParPoint.X(), 0 ) );
283     
284     double aParLen = ( aParPointDist / aParCommonDist ) * aGeoDistance;
285
286     double aRatio = aParLen / ( aGeoDistance - aParLen );
287
288     double aParX = ( aFirstPoint.X() + aRatio * aLastPoint.X() ) / ( 1 + aRatio );
289     double aParY = ( aFirstPoint.Y() + aRatio * aLastPoint.Y() ) / ( 1 + aRatio );
290
291     ProfilePoint aCompPoint( aParX, aParY, aParPoint.Y() );
292     aResPoints.Append( aCompPoint );
293   }
294
295   // Add last point as is
296   aResPoints.Append( ProfilePoint( aLastPoint.X(), aLastPoint.Y(), aLastParPoint.Y() ) );
297
298   return aResPoints;
299 }
300
301 void HYDROData_Profile::SetFilePath( const TCollection_AsciiString& theFilePath )
302 {
303   TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), theFilePath );
304 }
305
306 TCollection_AsciiString HYDROData_Profile::GetFilePath() const
307 {
308   TCollection_AsciiString aRes;
309
310   Handle(TDataStd_AsciiString) anAsciiStr;
311   if ( myLab.FindChild( DataTag_FilePath ).FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
312     aRes = anAsciiStr->Get();
313
314   return aRes;
315 }
316
317 bool HYDROData_Profile::ImportFromFile( const Handle(HYDROData_Document)& theDoc,
318                                         const TCollection_AsciiString&    theFileName )
319 {
320   if ( theDoc.IsNull() || theFileName.IsEmpty() )
321     return false;
322
323   OSD_File aFile( theFileName );
324   if ( !aFile.IsReadable() )
325     return false;
326
327   aFile.Open( OSD_ReadOnly, OSD_Protection() );
328   if ( !aFile.IsOpen() )
329     return false;
330
331   bool aRes = true;
332
333   NCollection_Sequence<Handle(HYDROData_Profile)> aCreatedProfiles;
334
335   while ( aRes && !aFile.IsAtEnd() )
336   {
337     Handle(HYDROData_Profile) aNewProfile = 
338       Handle(HYDROData_Profile)::DownCast( theDoc->CreateObject( KIND_PROFILE ) );
339     
340     aRes = aNewProfile->ImportFromFile( aFile );
341
342     aCreatedProfiles.Append( aNewProfile );
343   }
344
345   // Close the file
346   aFile.Close();
347
348   for ( int i = 1, n = aCreatedProfiles.Length(); i < n ; ++i )
349   {
350     Handle(HYDROData_Profile) aProfile = aCreatedProfiles.Value( i );
351     if ( aRes )
352     {
353       QString aProfileName = HYDROData_Tool::GenerateObjectName( theDoc, "Profile" );
354       aProfile->SetName( aProfileName );
355
356       aProfile->SetFilePath( theFileName );
357     }
358     else
359     {
360       aProfile->Remove();
361     }
362   }
363
364   return aRes;
365 }
366
367 bool HYDROData_Profile::ImportFromFile( const TCollection_AsciiString& theFileName )
368 {
369   // Try to open the file
370   OSD_File aFile( theFileName );
371   if ( !aFile.IsReadable() )
372     return false;
373
374   aFile.Open( OSD_ReadOnly, OSD_Protection() );
375   if ( !aFile.IsOpen() )
376     return false;
377
378   bool aRes = ImportFromFile( aFile );
379
380   // Close the file
381   aFile.Close();
382
383   if ( aRes )
384   {
385     // Update file path
386     SetFilePath( theFileName );
387   }
388
389   return aRes;
390 }
391
392 bool HYDROData_Profile::ImportFromFile( OSD_File& theFile )
393 {
394   if ( !theFile.IsOpen() )
395     return false;
396
397   bool aRes = true;
398
399   bool anIsParametric = false;
400   bool anIsGeoref     = false;
401
402   HYDROData_ProfileUZ::PointsList aPointsUZ;
403   ProfilePoints                   aPointsXYZ;
404
405   double aPrevVal = -DBL_MAX;
406   while ( !theFile.IsAtEnd() )
407   {
408     Standard_Integer aNbRead = 0;
409     TCollection_AsciiString aLine;
410     theFile.ReadLine( aLine, 1024, aNbRead );
411
412     aLine.LeftAdjust(); aLine.RightAdjust();
413     if ( aLine.IsEmpty() )
414       continue;
415
416     TCollection_AsciiString aValX = aLine.Token( " \t", 1 );
417     TCollection_AsciiString aValY = aLine.Token( " \t", 2 );
418     TCollection_AsciiString aValZ = aLine.Token( " \t", 3 );
419
420     if ( aValX.IsEmpty() || !aValX.IsRealValue() ||
421          aValY.IsEmpty() || !aValY.IsRealValue() )
422     {
423       aRes = false;
424       break;
425     }
426
427     if ( !anIsParametric && !anIsGeoref )
428     {
429       anIsParametric = aValZ.IsEmpty();
430       anIsGeoref = !aValZ.IsEmpty();
431     }
432
433     double aCoordX = aValX.RealValue();
434     double aCoordY = aValY.RealValue();
435
436     if ( anIsParametric )
437     {
438       if ( aCoordX < aPrevVal )
439       {
440         // Move back readed line
441         theFile.Seek( -( aNbRead + 1 ), OSD_FromHere );
442         break;
443       }
444
445       HYDROData_ProfileUZ::Point aPoint( aCoordX, aCoordY );
446       aPointsUZ.Append( aPoint );
447
448       aPrevVal = aCoordX;
449     }
450     else
451     {
452       if ( aValZ.IsEmpty() || !aValZ.IsRealValue() )
453       {
454         aRes = false;
455         break;
456       }
457
458       double aCoordZ = aValZ.RealValue();
459
460       ProfilePoint aPoint( aCoordX, aCoordY, aCoordZ );
461       aPointsXYZ.Append( aPoint );
462     }
463   }
464   
465   aRes = aRes && ( anIsParametric && !aPointsUZ.IsEmpty() || 
466                    anIsGeoref && !aPointsXYZ.IsEmpty() );
467   if ( aRes )
468   {
469     // Update profile points
470     if ( anIsParametric )
471     {
472       SetParametricPoints( aPointsUZ );
473     }
474     else if ( anIsGeoref )
475     {
476       SetProfilePoints( aPointsXYZ );
477     }
478   }
479
480   return aRes;
481 }
482
483
484