Salome HOME
Update the names of Regions and Zones if case name changed (Bug #110).
[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->getNbPoints();
200 }
201
202 void HYDROData_Profile::RemovePoints()
203 {
204   Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
205   if ( !aProfileUZ.IsNull() )
206     aProfileUZ->clear();
207 }
208
209 void HYDROData_Profile::SetParametricPoints( const CurveCreator::Coordinates& theCoords )
210 {
211   RemovePoints();
212
213   Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
214
215   CurveCreator::Coordinates::const_iterator aBegIter = theCoords.begin();
216   CurveCreator::Coordinates::const_iterator anEndIter = theCoords.end();
217   while ( aBegIter != anEndIter )
218   {
219     const CurveCreator::TypeCoord& aCoordX = *aBegIter++;
220     if ( aBegIter == anEndIter )
221       break;
222
223     const CurveCreator::TypeCoord& aCoordY = *aBegIter++;
224
225     aProfileUZ->addPoint( aCoordX, aCoordY, 0 );
226   }
227 }
228
229 CurveCreator::Coordinates HYDROData_Profile::GetParametricPoints() const
230 {
231   Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
232   return aProfileUZ.IsNull() ? CurveCreator::Coordinates() : aProfileUZ->getPoints();
233 }
234
235 void HYDROData_Profile::SetProfilePoints( const ProfilePoints& thePoints )
236 {
237   RemovePoints();
238   if ( thePoints.Length() < 2 )
239     return;
240
241   gp_XY aFirstPoint, aLastPoint;
242
243   Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
244   for ( int i = 1, n = thePoints.Length(); i <= n ; ++i )
245   {
246     const ProfilePoint& aPoint = thePoints.Value( i );
247     gp_XY aPointXY( aPoint.X(), aPoint.Y() );
248
249     if ( i == 1 )
250       aFirstPoint = aPointXY;
251     else if ( i == n )
252       aLastPoint = aPointXY;
253
254     double aDistance = gp_Pnt2d( aFirstPoint ).Distance( aPointXY );
255     
256     aProfileUZ->addPoint( 0, aDistance, aPoint.Z() );
257   }
258
259   SetFirstPoint( aFirstPoint );
260   SetLastPoint( aLastPoint );
261 }
262
263 HYDROData_Profile::ProfilePoints HYDROData_Profile::GetProfilePoints() const
264 {
265   ProfilePoints aResPoints;
266
267   gp_XY aFirstPoint, aLastPoint;
268   if ( !GetFirstPoint( aFirstPoint ) || !GetLastPoint( aLastPoint ) )
269     return aResPoints;
270
271   CurveCreator::Coordinates aParametricPoints = GetParametricPoints();
272   if ( aParametricPoints.size() < 4 )
273     return aResPoints;
274
275   CurveCreator::Coordinates::const_iterator aBegIter = aParametricPoints.begin();
276   CurveCreator::Coordinates::const_iterator anEndIter = aParametricPoints.end()--;
277
278   gp_Pnt2d aFirstParPoint( *aBegIter++, *aBegIter++ );
279   gp_Pnt2d aLastParPoint( *anEndIter--, *anEndIter );
280
281   double aGeoDistance = gp_Pnt2d( aFirstPoint ).Distance( aLastPoint );
282   double aParCommonDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aLastParPoint.X(), 0 ) );
283
284   // Add first point as is
285   aResPoints.Append( ProfilePoint( aFirstPoint.X(), aFirstPoint.Y(), aFirstParPoint.Y() ) );
286
287   // Compute all other points
288   while ( aBegIter != anEndIter )
289   {
290     const CurveCreator::TypeCoord& aCoordU = *aBegIter++;
291     if ( aBegIter == anEndIter )
292       break;
293
294     double aParPointDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aCoordU, 0 ) );
295     
296     double aParLen = ( aParPointDist / aParCommonDist ) * aGeoDistance;
297
298     double aRatio = aParLen / ( aGeoDistance - aParLen );
299
300     double aParX = ( aFirstPoint.X() + aRatio * aLastPoint.X() ) / ( 1 + aRatio );
301     double aParY = ( aFirstPoint.Y() + aRatio * aLastPoint.Y() ) / ( 1 + aRatio );
302
303
304     const CurveCreator::TypeCoord& aCoordZ = *aBegIter++;
305
306     ProfilePoint aCompPoint( aParX, aParY, aCoordZ );
307     aResPoints.Append( aCompPoint );
308   }
309
310   // Add last point as is
311   aResPoints.Append( ProfilePoint( aLastPoint.X(), aLastPoint.Y(), aLastParPoint.Y() ) );
312
313   return aResPoints;
314 }
315
316 void HYDROData_Profile::SetFilePath( const TCollection_AsciiString& theFilePath )
317 {
318   TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), theFilePath );
319 }
320
321 TCollection_AsciiString HYDROData_Profile::GetFilePath() const
322 {
323   TCollection_AsciiString aRes;
324
325   Handle(TDataStd_AsciiString) anAsciiStr;
326   if ( myLab.FindChild( DataTag_FilePath ).FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
327     aRes = anAsciiStr->Get();
328
329   return aRes;
330 }
331
332 bool HYDROData_Profile::ImportFromFile( const Handle(HYDROData_Document)& theDoc,
333                                         const TCollection_AsciiString&    theFileName )
334 {
335   if ( theDoc.IsNull() || theFileName.IsEmpty() )
336     return false;
337
338   OSD_File aFile( theFileName );
339   if ( !aFile.IsReadable() )
340     return false;
341
342   aFile.Open( OSD_ReadOnly, OSD_Protection() );
343   if ( !aFile.IsOpen() )
344     return false;
345
346   bool aRes = true;
347
348   NCollection_Sequence<Handle(HYDROData_Profile)> aCreatedProfiles;
349
350   while ( aRes && !aFile.IsAtEnd() )
351   {
352     Handle(HYDROData_Profile) aNewProfile = 
353       Handle(HYDROData_Profile)::DownCast( theDoc->CreateObject( KIND_PROFILE ) );
354     
355     aRes = aNewProfile->ImportFromFile( aFile );
356
357     aCreatedProfiles.Append( aNewProfile );
358   }
359
360   // Close the file
361   aFile.Close();
362
363   for ( int i = 1, n = aCreatedProfiles.Length(); i < n ; ++i )
364   {
365     Handle(HYDROData_Profile) aProfile = aCreatedProfiles.Value( i );
366     if ( aRes )
367     {
368       QString aProfileName = HYDROData_Tool::GenerateObjectName( theDoc, "Profile" );
369       aProfile->SetName( aProfileName );
370
371       aProfile->SetFilePath( theFileName );
372     }
373     else
374     {
375       aProfile->Remove();
376     }
377   }
378
379   return aRes;
380 }
381
382 bool HYDROData_Profile::ImportFromFile( const TCollection_AsciiString& theFileName )
383 {
384   // Try to open the file
385   OSD_File aFile( theFileName );
386   if ( !aFile.IsReadable() )
387     return false;
388
389   aFile.Open( OSD_ReadOnly, OSD_Protection() );
390   if ( !aFile.IsOpen() )
391     return false;
392
393   bool aRes = ImportFromFile( aFile );
394
395   // Close the file
396   aFile.Close();
397
398   if ( aRes )
399   {
400     // Update file path
401     SetFilePath( theFileName );
402   }
403
404   return aRes;
405 }
406
407 bool HYDROData_Profile::ImportFromFile( OSD_File& theFile )
408 {
409   if ( !theFile.IsOpen() )
410     return false;
411
412   bool aRes = true;
413
414   bool anIsParametric = false;
415   bool anIsGeoref     = false;
416
417   CurveCreator::Coordinates aPointsUZ;
418   ProfilePoints             aPointsXYZ;
419
420   double aPrevVal = -DBL_MAX;
421   while ( !theFile.IsAtEnd() )
422   {
423     Standard_Integer aNbRead = 0;
424     TCollection_AsciiString aLine;
425     theFile.ReadLine( aLine, 1024, aNbRead );
426
427     aLine.LeftAdjust(); aLine.RightAdjust();
428     if ( aLine.IsEmpty() )
429       continue;
430
431     TCollection_AsciiString aValX = aLine.Token( " \t", 1 );
432     TCollection_AsciiString aValY = aLine.Token( " \t", 2 );
433     TCollection_AsciiString aValZ = aLine.Token( " \t", 3 );
434
435     if ( aValX.IsEmpty() || !aValX.IsRealValue() ||
436          aValY.IsEmpty() || !aValY.IsRealValue() )
437     {
438       aRes = false;
439       break;
440     }
441
442     if ( !anIsParametric && !anIsGeoref )
443     {
444       anIsParametric = aValZ.IsEmpty();
445       anIsGeoref = !aValZ.IsEmpty();
446     }
447
448     double aCoordX = aValX.RealValue();
449     double aCoordY = aValY.RealValue();
450
451     if ( anIsParametric )
452     {
453       if ( aCoordX < aPrevVal )
454       {
455         // Move back readed line
456         theFile.Seek( -( aNbRead + 1 ), OSD_FromHere );
457         break;
458       }
459
460       aPointsUZ.push_back( aCoordX );
461       aPointsUZ.push_back( aCoordY );
462
463       aPrevVal = aCoordX;
464     }
465     else
466     {
467       if ( aValZ.IsEmpty() || !aValZ.IsRealValue() )
468       {
469         aRes = false;
470         break;
471       }
472
473       double aCoordZ = aValZ.RealValue();
474
475       ProfilePoint aPoint( aCoordX, aCoordY, aCoordZ );
476       aPointsXYZ.Append( aPoint );
477     }
478   }
479   
480   aRes = aRes && ( anIsParametric && !aPointsUZ.empty() || 
481                    anIsGeoref && !aPointsXYZ.IsEmpty() );
482   if ( aRes )
483   {
484     // Update profile points
485     if ( anIsParametric )
486     {
487       SetParametricPoints( aPointsUZ );
488     }
489     else if ( anIsGeoref )
490     {
491       SetProfilePoints( aPointsXYZ );
492     }
493   }
494
495   return aRes;
496 }
497
498
499