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