Salome HOME
bca60482b95a9df268d6c5cf60ce1cd079f3db24
[modules/hydro.git] / src / HYDROData / HYDROData_SinusX.cxx
1 // Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROData_SinusX.h"
24
25 #include <HYDROData_PolylineXY.h>
26 #include <HYDROData_Bathymetry.h>
27 #include <HYDROData_Entity.h>
28 #include <HYDROData_Document.h>
29 #include <HYDROData_Profile.h>
30 #include <HYDROData_Iterator.h>
31
32 #include <gp_XYZ.hxx>
33 #include <gp_XY.hxx>
34
35 #include <QFile>
36 #include <QFileInfo>
37 #include <QString>
38 #include <QStringList>
39
40 HYDROData_SinusX::HYDROData_SinusX( ) 
41 {
42 }
43
44 HYDROData_SinusX::~HYDROData_SinusX()
45 {
46 }
47
48 void HYDROData_SinusX::CollectExistingNames(Handle_HYDROData_Document theDocument)
49 {
50   HYDROData_Iterator anIter( theDocument );
51   int anInd = 0;
52   myExistingNames.clear();
53   std::vector<int> anAllowedIndexes;
54   for( ; anIter.More(); anIter.Next() )
55     myExistingNames.push_back(anIter.Current()->GetName());
56 }
57
58 QString HYDROData_SinusX::GetName(const QString& theName)
59 {
60   if (myExistingNames.contains(theName))
61   { 
62     QString aName = theName; 
63     while (myExistingNames.contains(aName)) 
64     {
65       QStringList aList = aName.split("_");
66       int aLastInd = aName.lastIndexOf("_");
67       bool IsNum = false; 
68       int anInd = -1;
69       anInd = aList.last().toInt(&IsNum);
70       if (IsNum)
71       {
72         aName = aName.left(aLastInd) + "_";
73         aName+= QString::number(++anInd);
74       }
75       else
76         aName = theName + "_0";
77     }
78
79     myExistingNames.push_back(aName);
80     return aName;
81   }
82   else
83   {
84     myExistingNames.push_back(theName);
85     return theName;
86   }
87
88 }
89
90
91 bool HYDROData_SinusX::Import(const QString& theFilePath, Handle(HYDROData_Document) theDocument, NCollection_Sequence<Handle_HYDROData_Entity>& theEntities)
92 {
93   if ( theFilePath.isEmpty() )
94   { 
95     return false;
96   }
97
98   QString anExt = theFilePath.split('.', QString::SkipEmptyParts).back();
99
100   if (anExt == "sx")
101   {
102     QFile aFile (theFilePath);
103     
104     aFile.open(QIODevice::ReadOnly);
105
106     Parse(aFile);
107    
108     CollectExistingNames(theDocument);
109     SXToHydro(theDocument, theEntities);
110     
111     aFile.close();
112     
113     return true;
114   }
115   else
116     return false;
117
118 }
119
120 void HYDROData_SinusX::SXToHydro(Handle(HYDROData_Document) theDocument, NCollection_Sequence<Handle_HYDROData_Entity>& theEntities)
121
122   for ( size_t i = 0; i < myCurveBlocks.size(); i++ )
123   {
124     if (myCurveBlocks[i].myType == 4) ///  scatter plot -> to bathy
125     {
126       Handle(HYDROData_Bathymetry) aBath = Handle(HYDROData_Bathymetry)::DownCast( theDocument->CreateObject( KIND_BATHYMETRY ) );
127       HYDROData_Bathymetry::AltitudePoints aAPoints;
128       for (size_t j = 0; j < myCurveBlocks[i].myXYZPoints.size(); j++)
129         aAPoints.Append(gp_XYZ (myCurveBlocks[i].myXYZPoints[j]));
130
131       aBath->SetAltitudePoints(aAPoints);
132       aBath->SetName(GetName(myCurveBlocks[i].myName + "_bath"));
133       theEntities.Append(aBath);
134     }
135     if (myCurveBlocks[i].myType == 1 || myCurveBlocks[i].myType == 3)
136     {
137       NCollection_Sequence<gp_XYZ> aPoints;
138       for (size_t j = 0; j < myCurveBlocks[i].myXYZPoints.size(); j++)
139         aPoints.Append(gp_XYZ (myCurveBlocks[i].myXYZPoints[j]));
140       /*if (myCurveBlocks[i].myIsClosed)
141         aPoints.Append(gp_XYZ (myCurveBlocks[i].myXYZPoints[0]));*/
142       Handle(HYDROData_ProfileUZ) aProfileUZ = Handle(HYDROData_ProfileUZ)::DownCast( theDocument->CreateObject( KIND_PROFILEUZ ) );
143       Handle(HYDROData_PolylineXY) aPolyXY = Handle(HYDROData_PolylineXY)::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) );
144       aPolyXY->AddSection( "",  
145         myCurveBlocks[i].myIsSpline ? HYDROData_PolylineXY::SECTION_SPLINE : HYDROData_PolylineXY::SECTION_POLYLINE, 
146         myCurveBlocks[i].myIsClosed ? true : false); 
147       aProfileUZ->CalculateAndAddPoints(aPoints, aPolyXY);
148       Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast( theDocument->CreateObject( KIND_PROFILE ) );
149       aProfile->SetParametricPoints(aProfileUZ->GetPoints());
150       aPolyXY->SetName(GetName(myCurveBlocks[i].myName + "_polyXY"));
151       aProfileUZ->SetName(GetName(myCurveBlocks[i].myName + "_profileUZ"));
152       aProfile->SetName(GetName(myCurveBlocks[i].myName + "_profile"));
153       theEntities.Append(aPolyXY);
154       theEntities.Append(aProfileUZ);
155       theEntities.Append(aProfile);
156     }
157     if (myCurveBlocks[i].myType == 2)
158     {
159       if (myCurveBlocks[i].myCurvePlane == 2)
160       {
161         if (myCurveBlocks[i].myAdditionalCurveInfo.size() == 4)
162         {
163           Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast( theDocument->CreateObject( KIND_PROFILE ) );
164           HYDROData_ProfileUZ::PointsList aPointList;
165           for (size_t j = 0; j < myCurveBlocks[i].myXYZPoints.size(); j++)
166             aPointList.Append(gp_XY (myCurveBlocks[i].myXYZPoints[j].X(), myCurveBlocks[i].myXYZPoints[j].Z()));
167           aProfile->GetProfileUZ()->SetSectionType(0,  myCurveBlocks[i].myIsSpline ? HYDROData_PolylineXY::SECTION_SPLINE : HYDROData_PolylineXY::SECTION_POLYLINE);
168           aProfile->GetProfileUZ()->SetSectionClosed(0, myCurveBlocks[i].myIsClosed ? true : false);
169           aProfile->SetParametricPoints(aPointList);
170           if ( ! (myCurveBlocks[i].myAdditionalCurveInfo[0] == 0 &&  myCurveBlocks[i].myAdditionalCurveInfo[1] == 0 && 
171             myCurveBlocks[i].myAdditionalCurveInfo[2] == 0 && myCurveBlocks[i].myAdditionalCurveInfo[3] == 0) )
172           {
173             aProfile->SetLeftPoint(gp_XY(myCurveBlocks[i].myAdditionalCurveInfo[0], myCurveBlocks[i].myAdditionalCurveInfo[1]));
174             aProfile->SetRightPoint(gp_XY(myCurveBlocks[i].myAdditionalCurveInfo[2], myCurveBlocks[i].myAdditionalCurveInfo[3]));
175             aProfile->Update();
176           }
177           aProfile->SetName(GetName(myCurveBlocks[i].myName + "_profile"));
178           theEntities.Append(aProfile);
179         }
180       }
181       if (myCurveBlocks[i].myCurvePlane == 0)
182       {
183         Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast( theDocument->CreateObject( KIND_PROFILE ) );
184         HYDROData_Profile::ProfilePoints aPointList;
185         for (size_t j = 0; j < myCurveBlocks[i].myXYZPoints.size(); j++)
186           aPointList.Append(myCurveBlocks[i].myXYZPoints[j]);
187         aProfile->GetProfileUZ()->SetSectionType(0,  myCurveBlocks[i].myIsSpline ? HYDROData_PolylineXY::SECTION_SPLINE : HYDROData_PolylineXY::SECTION_POLYLINE);
188         aProfile->GetProfileUZ()->SetSectionClosed(0, myCurveBlocks[i].myIsClosed ? true : false);
189         aProfile->SetProfilePoints(aPointList);
190         aProfile->SetName(GetName(myCurveBlocks[i].myName + "_profile"));
191         theEntities.Append(aProfile);
192       }
193     }
194   }
195
196 }
197  
198
199 bool HYDROData_SinusX::Parse(QFile& theFile)
200 {
201   if ( !theFile.isOpen() )
202     return false;
203
204   QString aLine;
205   QString aBLine;
206   QStringList aList;
207   QStringList aBList;
208   myCurveBlocks.clear();
209   bool aTotStat = true;
210
211   aLine = theFile.readLine().simplified();
212   aList = aLine.split( ' ', QString::SkipEmptyParts );
213
214   for (;!theFile.atEnd();) 
215   {
216     if (aList[0] == "B" && (aList[1] == "C" || aList[1] == "P" || aList[1] == "N" || aList[1] == "S" ))
217     {  
218       HYDROGUI_CurveBlock aCurveBlockInfo;
219       if (aList[1] == "C")
220         aCurveBlockInfo.myType = 1;
221       else if (aList[1] == "P")
222         aCurveBlockInfo.myType = 2;
223       else if (aList[1] == "N")
224         aCurveBlockInfo.myType = 3;
225       else if (aList[1] == "S")
226         aCurveBlockInfo.myType = 4;
227
228       if (aList.size() == 9)
229       {
230         for (int j = 2; j < 8; j++)
231           aCurveBlockInfo.myRefCoords.push_back(aList[j].toDouble());
232         aCurveBlockInfo.myRefRatio = aList[8].toDouble();
233       }
234
235       QString Name;
236       do
237       {
238         aBLine = theFile.readLine().simplified();
239         aBList = aBLine.split( ' ', QString::SkipEmptyParts );
240          
241         if (aBList[0] == "CP")
242         {
243           if (aBList.size() == 2 && (aBList[1] == "0" || aBList[1] == "1" || aBList[1] == "2"))
244             aCurveBlockInfo.myCurvePlane = aBList[1].toInt();
245           else if (aBList.size() == 3 && (aBList[1] == "0" || aBList[1] == "1") && (aBList[2] == "0" || aBList[2] == "1"))
246           {
247             aCurveBlockInfo.myIsClosed = aBList[1].toInt();
248             aCurveBlockInfo.myIsSpline = aBList[2].toInt();
249           }
250           else
251           {
252             for (int j = 1; j < aBList.size(); j++)
253               aCurveBlockInfo.myAdditionalCurveInfo.push_back(aBList[j].toDouble());
254           }
255         }
256         if (aBList[0] == "CN")
257         {
258            for (int i = 1; i < aBList.size(); i++)
259              Name += aBList[i] + "_"; 
260            Name.remove(Name.size() - 1, 1);
261            aCurveBlockInfo.myName = Name;
262         }
263       } while (!theFile.atEnd() && aBLine[0] == 'C' );
264
265       bool aStat;
266       aTotStat = true;
267       do
268       {
269         if (aBList.size() >= 3 && aBLine[0] != 'B' && aBLine[0] != 'C') {
270           gp_XYZ anXYZ;
271           anXYZ.SetX (aBList[0].toDouble(&aStat));  
272           aTotStat = aTotStat && aStat;
273           anXYZ.SetY (aBList[1].toDouble(&aStat));
274           aTotStat = aTotStat && aStat;
275           anXYZ.SetZ (aBList[2].toDouble(&aStat));
276           aTotStat = aTotStat && aStat;
277
278           aCurveBlockInfo.myXYZPoints.push_back(anXYZ);
279           
280           aBLine = theFile.readLine().simplified();
281           aBList = aBLine.split( ' ', QString::SkipEmptyParts );
282         }
283         else 
284           break;
285     
286       } while (!theFile.atEnd() || !aBLine.isEmpty());
287       if (aTotStat)
288         myCurveBlocks.push_back(aCurveBlockInfo);
289
290       aLine = aBLine;
291       aList = aBList;
292
293     }
294     else
295     {
296       aLine = theFile.readLine().simplified();
297       aList = aLine.split( ' ', QString::SkipEmptyParts );
298     }
299
300   }
301
302   return true;
303
304 }
305