1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "HYDROData_SinusX.h"
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>
38 #include <QStringList>
39 #include <QTextStream>
41 HYDROData_SinusX::HYDROData_SinusX( )
45 HYDROData_SinusX::~HYDROData_SinusX()
49 void HYDROData_SinusX::CollectExistingNames(Handle_HYDROData_Document theDocument)
51 HYDROData_Iterator anIter( theDocument );
53 myExistingNames.clear();
54 std::vector<int> anAllowedIndexes;
55 for( ; anIter.More(); anIter.Next() )
56 myExistingNames.push_back(anIter.Current()->GetName());
59 QString HYDROData_SinusX::GetName(const QString& theName)
61 if (myExistingNames.contains(theName))
63 QString aName = theName;
64 while (myExistingNames.contains(aName))
66 QStringList aList = aName.split("_");
67 int aLastInd = aName.lastIndexOf("_");
70 anInd = aList.last().toInt(&IsNum);
73 aName = aName.left(aLastInd) + "_";
74 aName+= QString::number(++anInd);
77 aName = theName + "_0";
80 myExistingNames.push_back(aName);
85 myExistingNames.push_back(theName);
92 bool HYDROData_SinusX::Import(const QString& theFilePath, Handle(HYDROData_Document) theDocument, NCollection_Sequence<Handle_HYDROData_Entity>& theEntities)
94 if ( theFilePath.isEmpty() )
99 QString anExt = theFilePath.split('.', QString::SkipEmptyParts).back();
103 QFile aFile (theFilePath);
105 aFile.open(QIODevice::ReadOnly);
109 CollectExistingNames(theDocument);
110 SXToHydro(theDocument, theEntities);
121 void HYDROData_SinusX::SXToHydro(Handle(HYDROData_Document) theDocument, NCollection_Sequence<Handle_HYDROData_Entity>& theEntities)
123 for ( size_t i = 0; i < myCurveBlocks.size(); i++ )
125 if (myCurveBlocks[i].myType == 4) /// scatter plot -> to bathy
127 Handle(HYDROData_Bathymetry) aBath = Handle(HYDROData_Bathymetry)::DownCast( theDocument->CreateObject( KIND_BATHYMETRY ) );
128 HYDROData_Bathymetry::AltitudePoints aAPoints;
129 for (size_t j = 0; j < myCurveBlocks[i].myXYZPoints.size(); j++)
130 aAPoints.Append(gp_XYZ (myCurveBlocks[i].myXYZPoints[j]));
132 aBath->SetAltitudePoints(aAPoints);
133 aBath->SetName(GetName(myCurveBlocks[i].myName + "_bath"));
134 theEntities.Append(aBath);
136 if (myCurveBlocks[i].myType == 1 || myCurveBlocks[i].myType == 3)
138 NCollection_Sequence<gp_XYZ> aPoints;
139 for (size_t j = 0; j < myCurveBlocks[i].myXYZPoints.size(); j++)
140 aPoints.Append(gp_XYZ (myCurveBlocks[i].myXYZPoints[j]));
141 /*if (myCurveBlocks[i].myIsClosed)
142 aPoints.Append(gp_XYZ (myCurveBlocks[i].myXYZPoints[0]));*/
143 Handle(HYDROData_ProfileUZ) aProfileUZ = Handle(HYDROData_ProfileUZ)::DownCast( theDocument->CreateObject( KIND_PROFILEUZ ) );
144 Handle(HYDROData_PolylineXY) aPolyXY = Handle(HYDROData_PolylineXY)::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) );
145 aPolyXY->AddSection( "",
146 myCurveBlocks[i].myIsSpline ? HYDROData_PolylineXY::SECTION_SPLINE : HYDROData_PolylineXY::SECTION_POLYLINE,
147 myCurveBlocks[i].myIsClosed ? true : false);
148 aProfileUZ->CalculateAndAddPoints(aPoints, aPolyXY);
149 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast( theDocument->CreateObject( KIND_PROFILE ) );
150 aProfile->SetParametricPoints(aProfileUZ->GetPoints());
151 aPolyXY->SetName(GetName(myCurveBlocks[i].myName + "_polyXY"));
152 aProfileUZ->SetName(GetName(myCurveBlocks[i].myName + "_profileUZ"));
153 aProfile->SetName(GetName(myCurveBlocks[i].myName + "_profile"));
154 theEntities.Append(aPolyXY);
155 theEntities.Append(aProfileUZ);
156 theEntities.Append(aProfile);
158 if (myCurveBlocks[i].myType == 2)
160 if (myCurveBlocks[i].myCurvePlane == 2)
162 if (myCurveBlocks[i].myAdditionalCurveInfo.size() == 4)
164 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast( theDocument->CreateObject( KIND_PROFILE ) );
165 HYDROData_ProfileUZ::PointsList aPointList;
166 for (size_t j = 0; j < myCurveBlocks[i].myXYZPoints.size(); j++)
167 aPointList.Append(gp_XY (myCurveBlocks[i].myXYZPoints[j].X(), myCurveBlocks[i].myXYZPoints[j].Z()));
168 aProfile->GetProfileUZ()->SetSectionType(0, myCurveBlocks[i].myIsSpline ? HYDROData_PolylineXY::SECTION_SPLINE : HYDROData_PolylineXY::SECTION_POLYLINE);
169 aProfile->GetProfileUZ()->SetSectionClosed(0, myCurveBlocks[i].myIsClosed ? true : false);
170 aProfile->SetParametricPoints(aPointList);
171 if ( ! (myCurveBlocks[i].myAdditionalCurveInfo[0] == 0 && myCurveBlocks[i].myAdditionalCurveInfo[1] == 0 &&
172 myCurveBlocks[i].myAdditionalCurveInfo[2] == 0 && myCurveBlocks[i].myAdditionalCurveInfo[3] == 0) )
174 aProfile->SetLeftPoint(gp_XY(myCurveBlocks[i].myAdditionalCurveInfo[0], myCurveBlocks[i].myAdditionalCurveInfo[1]));
175 aProfile->SetRightPoint(gp_XY(myCurveBlocks[i].myAdditionalCurveInfo[2], myCurveBlocks[i].myAdditionalCurveInfo[3]));
178 aProfile->SetName(GetName(myCurveBlocks[i].myName + "_profile"));
179 theEntities.Append(aProfile);
182 if (myCurveBlocks[i].myCurvePlane == 0)
184 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast( theDocument->CreateObject( KIND_PROFILE ) );
185 HYDROData_Profile::ProfilePoints aPointList;
186 for (size_t j = 0; j < myCurveBlocks[i].myXYZPoints.size(); j++)
187 aPointList.Append(myCurveBlocks[i].myXYZPoints[j]);
188 aProfile->GetProfileUZ()->SetSectionType(0, myCurveBlocks[i].myIsSpline ? HYDROData_PolylineXY::SECTION_SPLINE : HYDROData_PolylineXY::SECTION_POLYLINE);
189 aProfile->GetProfileUZ()->SetSectionClosed(0, myCurveBlocks[i].myIsClosed ? true : false);
190 aProfile->SetProfilePoints(aPointList);
191 aProfile->SetName(GetName(myCurveBlocks[i].myName + "_profile"));
192 theEntities.Append(aProfile);
200 bool HYDROData_SinusX::Parse(QFile& theFile)
202 if ( !theFile.isOpen() )
209 myCurveBlocks.clear();
210 bool aTotStat = true;
212 aLine = theFile.readLine().simplified();
213 aList = aLine.split( ' ', QString::SkipEmptyParts );
215 for (;!theFile.atEnd();)
217 if (aList[0] == "B" && (aList[1] == "C" || aList[1] == "P" || aList[1] == "N" || aList[1] == "S" ))
219 HYDROGUI_CurveBlock aCurveBlockInfo;
221 aCurveBlockInfo.myType = 1;
222 else if (aList[1] == "P")
223 aCurveBlockInfo.myType = 2;
224 else if (aList[1] == "N")
225 aCurveBlockInfo.myType = 3;
226 else if (aList[1] == "S")
227 aCurveBlockInfo.myType = 4;
229 if (aList.size() == 9)
231 for (int j = 2; j < 8; j++)
232 aCurveBlockInfo.myRefCoords.push_back(aList[j].toDouble());
233 aCurveBlockInfo.myRefRatio = aList[8].toDouble();
239 aBLine = theFile.readLine().simplified();
240 aBList = aBLine.split( ' ', QString::SkipEmptyParts );
242 if (aBList[0] == "CP")
244 if (aBList.size() == 2 && (aBList[1] == "0" || aBList[1] == "1" || aBList[1] == "2"))
245 aCurveBlockInfo.myCurvePlane = aBList[1].toInt();
246 else if (aBList.size() == 3 && (aBList[1] == "0" || aBList[1] == "1") && (aBList[2] == "0" || aBList[2] == "1"))
248 aCurveBlockInfo.myIsClosed = aBList[1].toInt();
249 aCurveBlockInfo.myIsSpline = aBList[2].toInt();
253 for (int j = 1; j < aBList.size(); j++)
254 aCurveBlockInfo.myAdditionalCurveInfo.push_back(aBList[j].toDouble());
257 if (aBList[0] == "CN")
259 for (int i = 1; i < aBList.size(); i++)
260 Name += aBList[i] + "_";
261 Name.remove(Name.size() - 1, 1);
262 aCurveBlockInfo.myName = Name;
264 } while (!theFile.atEnd() && aBLine[0] == 'C' );
270 if (aBList.size() >= 3 && aBLine[0] != 'B' && aBLine[0] != 'C') {
272 anXYZ.SetX (aBList[0].toDouble(&aStat));
273 aTotStat = aTotStat && aStat;
274 anXYZ.SetY (aBList[1].toDouble(&aStat));
275 aTotStat = aTotStat && aStat;
276 anXYZ.SetZ (aBList[2].toDouble(&aStat));
277 aTotStat = aTotStat && aStat;
279 aCurveBlockInfo.myXYZPoints.push_back(anXYZ);
281 aBLine = theFile.readLine().simplified();
282 aBList = aBLine.split( ' ', QString::SkipEmptyParts );
287 } while (!theFile.atEnd() || !aBLine.isEmpty());
289 myCurveBlocks.push_back(aCurveBlockInfo);
297 aLine = theFile.readLine().simplified();
298 aList = aLine.split( ' ', QString::SkipEmptyParts );
307 bool HYDROData_SinusX::Export(const QString& theFilePath, NCollection_Sequence<Handle_HYDROData_Entity>& theEntities)
309 if ( theFilePath.isEmpty() )
314 QString anExt = theFilePath.split('.', QString::SkipEmptyParts).back();
318 QFile aFile (theFilePath);
320 aFile.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text);
322 HydroToSX(aFile, theEntities);
333 void HYDROData_SinusX::HydroToSX(QFile& theFile, NCollection_Sequence<Handle_HYDROData_Entity>& theEntities)
335 QTextStream aTextStream(&theFile);
336 aTextStream << "C Generated by HYDRO Module\n";
337 aTextStream << "C\n";
339 for (int i = 1; i <= theEntities.Size(); i++)
341 Handle_HYDROData_Entity anEnt = theEntities.Value(i);
342 if (anEnt->IsKind( STANDARD_TYPE(HYDROData_Bathymetry) ))
344 Handle(HYDROData_Bathymetry) aBathy = Handle(HYDROData_Bathymetry)::DownCast( anEnt );
345 HYDROData_Bathymetry::AltitudePoints anXYZPoints = aBathy->GetAltitudePoints();
347 aTextStream << "B S\n";
348 aTextStream << "CN " << aBathy->GetName() << "\n";
349 aTextStream << "CP 0 0\n";
350 aTextStream << "CP 0\n";
351 for (int j = anXYZPoints.Lower(); j <= anXYZPoints.Upper(); j++)
352 aTextStream << " " << QString::number(anXYZPoints(j).X(), 'f', 3)
353 << " " << QString::number(anXYZPoints(j).Y(), 'f', 3)
354 << " " << QString::number(anXYZPoints(j).Z(), 'f', 3) << "\n";
356 else if (anEnt->IsKind( STANDARD_TYPE(HYDROData_PolylineXY) ))
358 Handle(HYDROData_PolylineXY) aPolyXY = Handle(HYDROData_PolylineXY)::DownCast( anEnt );
359 for (int j = 0; j < aPolyXY->NbSections(); j++)
362 bool IsClosed = aPolyXY->IsClosedSection(j);
363 bool IsSpline = false;
364 if (aPolyXY->GetSectionType(j) == HYDROData_PolylineXY::SECTION_SPLINE)
366 HYDROData_PolylineXY::PointsList anXYPoints = aPolyXY->GetPoints(j);
368 aTextStream << "B N\n";
369 aTextStream << "CN " << aPolyXY->GetName() << "\n";
370 aTextStream << "CP " << IsClosed << " " << IsSpline << "\n";
371 aTextStream << "CP 0.0\n";
372 aTextStream << "CP 0\n";
373 if (aPolyXY->NbSections() > 1)
374 aTextStream << "C " << aPolyXY->GetName() << "_section_" << QString::number(j) << "\n";
375 for (int k = anXYPoints.Lower(); k <= anXYPoints.Upper(); k++)
376 aTextStream << " " << QString::number(anXYPoints(k).X(), 'f', 3)
377 << " " << QString::number(anXYPoints(k).Y(), 'f', 3)
381 else if (anEnt->IsKind( STANDARD_TYPE(HYDROData_Profile) ))
384 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast( anEnt );
385 HYDROData_ProfileUZ::PointsList aPointList = aProfile->GetParametricPoints();
386 bool IsClosed = aProfile->GetProfileUZ(false)->IsClosedSection(0);;
387 bool IsSpline = false;
388 if (aProfile->GetProfileUZ(false)->GetSectionType(0) == HYDROData_PolylineXY::SECTION_SPLINE)
391 aTextStream << "B P\n";
392 aTextStream << "CN " << aProfile->GetName() << "\n";
393 aTextStream << "CP " << IsClosed << " " << IsSpline << "\n";
394 gp_XY aLeftPoint(0.0, 0.0);
395 gp_XY aRightPoint(0.0, 0.0);
396 if (aProfile->GetLeftPoint(aLeftPoint, true) && aProfile->GetRightPoint(aRightPoint, true))
397 aTextStream << "CP " << QString::number(aLeftPoint.X(), 'f', 3) << " " <<
398 QString::number(aLeftPoint.Y(), 'f', 3) << " " <<
399 QString::number(aRightPoint.X(), 'f', 3) << " " <<
400 QString::number(aRightPoint.Y(), 'f', 3) << "\n";
402 aTextStream << "CP 0.0 0.0 0.0 0.0\n";
403 aTextStream << "CP 2\n";
404 for (int k = aPointList.Lower(); k <= aPointList.Upper(); k++)
405 aTextStream << " " << QString::number(aPointList(k).X(), 'f', 3)
407 << QString::number(aPointList(k).Y(), 'f', 3) << "\n";