1 // Copyright (C) 2014-2015 EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 // Lesser General Public License for more details.
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "HYDROData_SinusX.h"
21 #include <HYDROData_PolylineXY.h>
22 #include <HYDROData_Bathymetry.h>
23 #include <HYDROData_Entity.h>
24 #include <HYDROData_Document.h>
25 #include <HYDROData_Profile.h>
26 #include <HYDROData_Iterator.h>
34 #include <QStringList>
35 #include <QTextStream>
38 HYDROData_SinusX::HYDROData_SinusX( )
42 HYDROData_SinusX::~HYDROData_SinusX()
46 void HYDROData_SinusX::CollectExistingNames(Handle_HYDROData_Document theDocument)
48 HYDROData_Iterator anIter( theDocument );
50 myExistingNames.clear();
51 std::vector<int> anAllowedIndexes;
52 for( ; anIter.More(); anIter.Next() )
53 myExistingNames.push_back(anIter.Current()->GetName());
56 QString HYDROData_SinusX::GetName(const QString& theName)
58 if (myExistingNames.contains(theName))
60 QString aName = theName;
61 while (myExistingNames.contains(aName))
63 QStringList aList = aName.split("_");
64 int aLastInd = aName.lastIndexOf("_");
67 anInd = aList.last().toInt(&IsNum);
70 aName = aName.left(aLastInd) + "_";
71 aName+= QString::number(++anInd);
74 aName = theName + "_0";
77 myExistingNames.push_back(aName);
82 myExistingNames.push_back(theName);
89 bool HYDROData_SinusX::Import(const QString& theFilePath, Handle(HYDROData_Document) theDocument, NCollection_Sequence<Handle_HYDROData_Entity>& theEntities)
91 if ( theFilePath.isEmpty() )
96 QString anExt = theFilePath.split('.', QString::SkipEmptyParts).back();
100 QFile aFile (theFilePath);
102 aFile.open(QIODevice::ReadOnly);
106 CollectExistingNames(theDocument);
107 SXToHydro(theDocument, theEntities);
118 void HYDROData_SinusX::SXToHydro(Handle(HYDROData_Document) theDocument, NCollection_Sequence<Handle_HYDROData_Entity>& theEntities)
120 for ( size_t i = 0; i < myCurveBlocks.size(); i++ )
122 if (myCurveBlocks[i].myType == 4) /// scatter plot -> to bathy
124 Handle(HYDROData_Bathymetry) aBath = Handle(HYDROData_Bathymetry)::DownCast( theDocument->CreateObject( KIND_BATHYMETRY ) );
125 HYDROData_Bathymetry::AltitudePoints aAPoints;
126 for (size_t j = 0; j < myCurveBlocks[i].myXYZPoints.size(); j++)
128 gp_XYZ aLocalPoint = gp_XYZ (myCurveBlocks[i].myXYZPoints[j]);
129 theDocument->Transform(aLocalPoint, true);
130 aAPoints.Append(aLocalPoint);
133 aBath->SetAltitudePoints(aAPoints);
134 aBath->SetName(GetName(myCurveBlocks[i].myName));
135 theEntities.Append(aBath);
137 if (myCurveBlocks[i].myType == 1 || myCurveBlocks[i].myType == 3) // XYZ curve or isocontour
139 NCollection_Sequence<gp_XYZ> aPoints;
140 for (size_t j = 0; j < myCurveBlocks[i].myXYZPoints.size(); j++)
142 gp_XYZ aLocalPoint = gp_XYZ (myCurveBlocks[i].myXYZPoints[j]);
143 theDocument->Transform(aLocalPoint, true);
144 aPoints.Append(aLocalPoint);
146 /*if (myCurveBlocks[i].myIsClosed)
147 aPoints.Append(gp_XYZ (myCurveBlocks[i].myXYZPoints[0]));*/
148 Handle(HYDROData_ProfileUZ) aProfileUZ = Handle(HYDROData_ProfileUZ)::DownCast( theDocument->CreateObject( KIND_PROFILEUZ ) );
149 Handle(HYDROData_PolylineXY) aPolyXY = Handle(HYDROData_PolylineXY)::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) );
150 aPolyXY->AddSection( "",
151 myCurveBlocks[i].myIsSpline ? HYDROData_PolylineXY::SECTION_SPLINE : HYDROData_PolylineXY::SECTION_POLYLINE,
152 myCurveBlocks[i].myIsClosed ? true : false);
153 aProfileUZ->CalculateAndAddPoints(aPoints, aPolyXY);
154 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast( theDocument->CreateObject( KIND_PROFILE ) );
155 aProfile->SetParametricPoints(aProfileUZ->GetPoints());
156 aPolyXY->SetName(GetName(myCurveBlocks[i].myName));
157 aProfileUZ->SetName(GetName(myCurveBlocks[i].myName));
158 aProfile->SetName(GetName(myCurveBlocks[i].myName));
159 aPolyXY->SetWireColor(HYDROData_PolylineXY::DefaultWireColor());
160 theEntities.Append(aPolyXY);
161 theEntities.Append(aProfileUZ);
162 theEntities.Append(aProfile);
164 if (myCurveBlocks[i].myType == 2) // XYZ profile
166 if (myCurveBlocks[i].myCurvePlane == 2) // plane XoZ
168 if (myCurveBlocks[i].myAdditionalCurveInfo.size() == 4)
170 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast( theDocument->CreateObject( KIND_PROFILE ) );
171 HYDROData_ProfileUZ::PointsList aPointList;
172 for (size_t j = 0; j < myCurveBlocks[i].myXYZPoints.size(); j++)
173 aPointList.Append(gp_XY (myCurveBlocks[i].myXYZPoints[j].X(), myCurveBlocks[i].myXYZPoints[j].Z()));
174 aProfile->GetProfileUZ()->SetSectionType(0, myCurveBlocks[i].myIsSpline ? HYDROData_PolylineXY::SECTION_SPLINE : HYDROData_PolylineXY::SECTION_POLYLINE);
175 aProfile->GetProfileUZ()->SetSectionClosed(0, myCurveBlocks[i].myIsClosed ? true : false);
176 aProfile->SetParametricPoints(aPointList);
177 if ( ! (myCurveBlocks[i].myAdditionalCurveInfo[0] == 0 && myCurveBlocks[i].myAdditionalCurveInfo[1] == 0 &&
178 myCurveBlocks[i].myAdditionalCurveInfo[2] == 0 && myCurveBlocks[i].myAdditionalCurveInfo[3] == 0) )
179 { // georeferenced profile
180 double xl = myCurveBlocks[i].myAdditionalCurveInfo[0];
181 double yl = myCurveBlocks[i].myAdditionalCurveInfo[1];
182 double xr = myCurveBlocks[i].myAdditionalCurveInfo[2];
183 double yr = myCurveBlocks[i].myAdditionalCurveInfo[3];
184 theDocument->Transform(xl, yl , true);
185 theDocument->Transform(xr, yr , true);
186 aProfile->SetLeftPoint(gp_XY(xl, yl));
187 aProfile->SetRightPoint(gp_XY(xr, yr));
190 aProfile->SetName(GetName(myCurveBlocks[i].myName));
191 theEntities.Append(aProfile);
194 if (myCurveBlocks[i].myCurvePlane == 0) // plane XoY
196 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast( theDocument->CreateObject( KIND_PROFILE ) );
197 HYDROData_Profile::ProfilePoints aPointList;
198 for (size_t j = 0; j < myCurveBlocks[i].myXYZPoints.size(); j++)
200 gp_XYZ aLocalPoint = gp_XYZ (myCurveBlocks[i].myXYZPoints[j]);
201 theDocument->Transform(aLocalPoint, true);
202 aPointList.Append(aLocalPoint);
204 aProfile->GetProfileUZ()->SetSectionType(0, myCurveBlocks[i].myIsSpline ? HYDROData_PolylineXY::SECTION_SPLINE : HYDROData_PolylineXY::SECTION_POLYLINE);
205 aProfile->GetProfileUZ()->SetSectionClosed(0, myCurveBlocks[i].myIsClosed ? true : false);
206 aProfile->SetProfilePoints(aPointList);
207 aProfile->SetName(GetName(myCurveBlocks[i].myName));
208 theEntities.Append(aProfile);
216 bool HYDROData_SinusX::Parse(QFile& theFile)
218 if ( !theFile.isOpen() )
225 myCurveBlocks.clear();
226 bool aTotStat = true;
228 aLine = theFile.readLine().simplified();
229 aList = aLine.split( ' ', QString::SkipEmptyParts );
231 for (;!theFile.atEnd();)
233 if (aList[0] == "B" && (aList[1] == "C" || aList[1] == "P" || aList[1] == "N" || aList[1] == "S" ))
235 HYDROGUI_CurveBlock aCurveBlockInfo;
237 aCurveBlockInfo.myType = 1; // XYZ curve
238 else if (aList[1] == "P")
239 aCurveBlockInfo.myType = 2; // XYZ profile
240 else if (aList[1] == "N")
241 aCurveBlockInfo.myType = 3; // isocontour
242 else if (aList[1] == "S")
243 aCurveBlockInfo.myType = 4; // Scatter plot
245 if (aList.size() == 9)
247 for (int j = 2; j < 8; j++)
248 aCurveBlockInfo.myRefCoords.push_back(aList[j].toDouble());
249 aCurveBlockInfo.myRefRatio = aList[8].toDouble();
255 aBLine = theFile.readLine().simplified();
256 aBList = aBLine.split( ' ', QString::SkipEmptyParts );
258 if (aBList[0] == "CP")
260 if (aBList.size() == 2 && (aBList[1] == "0" || aBList[1] == "1" || aBList[1] == "2"))
261 aCurveBlockInfo.myCurvePlane = aBList[1].toInt();
262 else if (aBList.size() == 3 && (aBList[1] == "0" || aBList[1] == "1") && (aBList[2] == "0" || aBList[2] == "1"))
264 aCurveBlockInfo.myIsClosed = aBList[1].toInt();
265 aCurveBlockInfo.myIsSpline = aBList[2].toInt();
269 for (int j = 1; j < aBList.size(); j++)
270 aCurveBlockInfo.myAdditionalCurveInfo.push_back(aBList[j].toDouble());
273 if (aBList[0] == "CN")
275 for (int i = 1; i < aBList.size(); i++)
276 Name += aBList[i] + "_";
277 if (Name.size() <= 1)
279 Name.remove(Name.size() - 1, 1);
280 aCurveBlockInfo.myName = Name;
282 } while (!theFile.atEnd() && aBLine[0] == 'C' );
288 if (aBList.size() >= 3 && aBLine[0] != 'B' && aBLine[0] != 'C') {
290 anXYZ.SetX (aBList[0].toDouble(&aStat));
291 aTotStat = aTotStat && aStat;
292 anXYZ.SetY (aBList[1].toDouble(&aStat));
293 aTotStat = aTotStat && aStat;
294 anXYZ.SetZ (aBList[2].toDouble(&aStat));
295 aTotStat = aTotStat && aStat;
297 aCurveBlockInfo.myXYZPoints.push_back(anXYZ);
299 aBLine = theFile.readLine().simplified();
300 aBList = aBLine.split( ' ', QString::SkipEmptyParts );
305 } while (!theFile.atEnd() || !aBLine.isEmpty());
307 myCurveBlocks.push_back(aCurveBlockInfo);
315 aLine = theFile.readLine().simplified();
316 aList = aLine.split( ' ', QString::SkipEmptyParts );
325 bool HYDROData_SinusX::Export(const QString& theFilePath, const NCollection_Sequence<Handle_HYDROData_Entity>& theEntities)
327 if ( theFilePath.isEmpty() )
332 QString anExt = theFilePath.split('.', QString::SkipEmptyParts).back();
336 QFile aFile (theFilePath);
338 aFile.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text);
340 HydroToSX(aFile, theEntities);
351 void HYDROData_SinusX::HydroToSX(QFile& theFile, const NCollection_Sequence<Handle_HYDROData_Entity>& theEntities)
353 QTextStream aTextStream(&theFile);
354 aTextStream << "C Generated by HYDRO Module\n";
355 aTextStream << "C\n";
357 for (int i = 1; i <= theEntities.Size(); i++)
359 Handle_HYDROData_Entity anEnt = theEntities.Value(i);
360 if (anEnt->IsKind( STANDARD_TYPE(HYDROData_Bathymetry) ))
362 Handle(HYDROData_Bathymetry) aBathy = Handle(HYDROData_Bathymetry)::DownCast( anEnt );
363 HYDROData_Bathymetry::AltitudePoints anXYZPoints = aBathy->GetAltitudePoints(true);
365 aTextStream << "B S\n";
366 aTextStream << "CN " << aBathy->GetName() << "\n";
367 aTextStream << "CP 0 0\n";
368 aTextStream << "CP 0\n";
369 for (int j = anXYZPoints.Lower(); j <= anXYZPoints.Upper(); j++)
370 aTextStream << " " << QString::number(anXYZPoints(j).X(), 'f', 3)
371 << " " << QString::number(anXYZPoints(j).Y(), 'f', 3)
372 << " " << QString::number(anXYZPoints(j).Z(), 'f', 3) << "\n";
374 else if (anEnt->IsKind( STANDARD_TYPE(HYDROData_PolylineXY) ))
376 Handle(HYDROData_PolylineXY) aPolyXY = Handle(HYDROData_PolylineXY)::DownCast( anEnt );
377 for (int j = 0; j < aPolyXY->NbSections(); j++)
380 bool IsClosed = aPolyXY->IsClosedSection(j);
381 bool IsSpline = false;
382 if (aPolyXY->GetSectionType(j) == HYDROData_PolylineXY::SECTION_SPLINE)
384 HYDROData_PolylineXY::PointsList anXYPoints = aPolyXY->GetPoints(j, true);
386 aTextStream << "B N\n";
387 aTextStream << "CN " << aPolyXY->GetName() << "\n";
388 aTextStream << "CP " << IsClosed << " " << IsSpline << "\n";
389 aTextStream << "CP 0.0\n";
390 aTextStream << "CP 0\n";
391 if (aPolyXY->NbSections() > 1)
392 aTextStream << "C " << aPolyXY->GetName() << "_section_" << QString::number(j) << "\n";
393 for (int k = anXYPoints.Lower(); k <= anXYPoints.Upper(); k++)
394 aTextStream << " " << QString::number(anXYPoints(k).X(), 'f', 3)
395 << " " << QString::number(anXYPoints(k).Y(), 'f', 3)
399 else if (anEnt->IsKind( STANDARD_TYPE(HYDROData_Profile) ))
402 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast( anEnt );
403 HYDROData_ProfileUZ::PointsList aPointList = aProfile->GetParametricPoints();
404 bool IsClosed = aProfile->GetProfileUZ(false)->IsClosedSection(0);;
405 bool IsSpline = false;
406 if (aProfile->GetProfileUZ(false)->GetSectionType(0) == HYDROData_PolylineXY::SECTION_SPLINE)
409 aTextStream << "B P\n";
410 aTextStream << "CN " << aProfile->GetName() << "\n";
411 aTextStream << "CP " << IsClosed << " " << IsSpline << "\n";
412 gp_XY aLeftPoint(0.0, 0.0);
413 gp_XY aRightPoint(0.0, 0.0);
414 if (aProfile->GetLeftPoint(aLeftPoint, true) && aProfile->GetRightPoint(aRightPoint, true))
415 aTextStream << "CP " << QString::number(aLeftPoint.X(), 'f', 3) << " " <<
416 QString::number(aLeftPoint.Y(), 'f', 3) << " " <<
417 QString::number(aRightPoint.X(), 'f', 3) << " " <<
418 QString::number(aRightPoint.Y(), 'f', 3) << "\n";
420 aTextStream << "CP 0.0 0.0 0.0 0.0\n";
421 aTextStream << "CP 2\n";
422 for (int k = aPointList.Lower(); k <= aPointList.Upper(); k++)
423 aTextStream << " " << QString::number(aPointList(k).X(), 'f', 3)
425 << QString::number(aPointList(k).Y(), 'f', 3) << "\n";