Salome HOME
Dump to python corrected.
[modules/hydro.git] / src / HYDROData / test_HYDROData_Profile.cxx
1
2 #include <test_HYDROData_Profile.h>
3
4 #include <HYDROData_Document.h>
5 #include <HYDROData_Tool.h>
6 #include <HYDROData_Profile.h>
7 #include <HYDROData_Iterator.h>
8
9 #include <TopoDS_Shape.hxx>
10
11 #include <gp_XY.hxx>
12 #include <gp_XYZ.hxx>
13
14 #include <QDir>
15 #include <QFile>
16 #include <QTextStream>
17
18 bool test_HYDROData_Profile::createTestFile( const QString& theFileName,
19                                              const bool     theIsParametric )
20 {
21   QFile aTmpFile( theFileName );
22   if ( !aTmpFile.open( QIODevice::WriteOnly | QIODevice::Text ) )
23     return false;
24
25   if ( theIsParametric )
26   {
27     QTextStream anOutStream( &aTmpFile );
28     
29     anOutStream << "0      182.15 \n";
30     anOutStream << "4      181.95 \n";
31     anOutStream << "10.18  181.63 \n";
32     anOutStream << "14.75  179.27 \n";
33     anOutStream << "19.75  178.87 \n";
34
35     anOutStream << "\n";
36
37     anOutStream << "-5   50    \n";
38     anOutStream << "0    15    \n";
39     anOutStream << "10.1 10    \n";
40     anOutStream << "20   20    \n";
41     anOutStream << "250  0.005 \n";
42   }
43   else
44   {
45     QTextStream anOutStream( &aTmpFile );
46
47     anOutStream << "1040499.17 6788618.13 182.15 \n";
48     anOutStream << "1040503.12 6788618.79 181.95 \n";
49     anOutStream << "1040509.21 6788619.81 181.63 \n";
50     anOutStream << "1040513.72 6788620.56 179.27 \n";
51     anOutStream << "1040518.65 6788621.38 178.87 \n";
52   }
53
54   aTmpFile.close();
55
56   return true;
57 }
58
59 void test_HYDROData_Profile::testFileImport()
60 {
61   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( 1 );
62
63   QString aParamFileName = QDir::tempPath() + QDir::separator() + "parametric.pa";
64   QString aGeorefFileName = QDir::tempPath() + QDir::separator() + "georef.pa";
65   if ( !createTestFile( aParamFileName, true ) || !createTestFile( aGeorefFileName, false ) )
66     return; // No file has been created
67
68   TCollection_AsciiString aFileName( aParamFileName.toStdString().c_str() );
69
70   CPPUNIT_ASSERT( HYDROData_Profile::ImportFromFile( aDoc, aFileName ) );
71
72   int aProfileCount = 0;
73   HYDROData_Iterator aDocIter( aDoc, KIND_PROFILE );
74   for ( ; aDocIter.More(); aDocIter.Next() )
75   {
76     Handle(HYDROData_Profile) aProfile = 
77       Handle(HYDROData_Profile)::DownCast( aDocIter.Current() );
78     if ( aProfile.IsNull() )
79       continue;
80     
81     CPPUNIT_ASSERT( aProfile->IsValid() == false );
82     CPPUNIT_ASSERT( aProfile->NbPoints() == 5 );
83     
84     aProfileCount++;
85   }
86
87   CPPUNIT_ASSERT( aProfileCount == 2 );
88
89   Handle(HYDROData_Profile) aGeorefProfile = 
90     Handle(HYDROData_Profile)::DownCast( aDoc->CreateObject( KIND_PROFILE ) );
91
92   aFileName = TCollection_AsciiString( aGeorefFileName.toStdString().c_str() );
93   CPPUNIT_ASSERT( aGeorefProfile->ImportFromFile( aFileName ) );
94
95   // Check validity of imported profile
96   CPPUNIT_ASSERT( aGeorefProfile->IsValid() );
97
98   CPPUNIT_ASSERT( aGeorefProfile->GetTopShape().IsNull() == false );
99
100   aGeorefProfile->UpdateShape3D();
101   CPPUNIT_ASSERT( aGeorefProfile->GetShape3D().IsNull() == false );
102
103   HYDROData_Profile::ProfilePoints aProfilePoints = aGeorefProfile->GetProfilePoints();
104   CPPUNIT_ASSERT( aProfilePoints.Length() == 5 );
105
106   HYDROData_Profile::ProfilePoint aProfilePoint = aProfilePoints.Value( 3 );
107   CPPUNIT_ASSERT( ValuesEquals( aProfilePoint.X(), 1040509.21 ) );
108   CPPUNIT_ASSERT( ValuesEquals( aProfilePoint.Y(), 6788619.81 ) );
109   CPPUNIT_ASSERT( ValuesEquals( aProfilePoint.Z(), 181.63 ) );
110
111   aDoc->Close();
112 }
113
114
115 void test_HYDROData_Profile::testCopy()
116 {
117   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
118   
119   Handle(HYDROData_Profile) aProfile1 = 
120     Handle(HYDROData_Profile)::DownCast( aDoc->CreateObject( KIND_PROFILE ) );
121
122   QString aFileName = QDir::tempPath() + QDir::separator() + "georef.pa";
123
124   bool anIsFileCreated = createTestFile( aFileName, false );
125   
126   if ( anIsFileCreated )
127   {
128     TCollection_AsciiString anAsciiFileName( aFileName.toStdString().c_str() );
129     CPPUNIT_ASSERT( aProfile1->ImportFromFile( anAsciiFileName ) );
130
131     CPPUNIT_ASSERT( aProfile1->IsValid() );
132     CPPUNIT_ASSERT( aProfile1->NbPoints() == 5 );
133   }
134
135   Handle(HYDROData_Profile) aProfile2 = 
136     Handle(HYDROData_Profile)::DownCast( aDoc->CreateObject( KIND_PROFILE ) );
137
138   aProfile1->CopyTo( aProfile2 );
139
140   if ( anIsFileCreated )
141   {
142     CPPUNIT_ASSERT( aProfile2->IsValid() );
143     CPPUNIT_ASSERT( aProfile2->NbPoints() == 5 );
144   }
145
146   aDoc->Close();
147 }